Ejemplo n.º 1
0
func (this *Unset) ExecCommand(args []string) (int, string) {
	/* Command to Unset the value of the given parameter.
	 */

	if len(args) > this.MaxArgs() {
		return errors.TOO_MANY_ARGS, ""

	} else if len(args) < this.MinArgs() {
		return errors.TOO_FEW_ARGS, ""

	} else {
		//Check what kind of parameter needs to be Unset.
		// For query parameters
		if strings.HasPrefix(args[0], "-$") {
			// For Named Parameters
			vble := args[0]
			vble = vble[2:]

			err_code, err_str := PopValue_Helper(true, NamedParam, vble)
			if err_code != 0 {
				return err_code, err_str
			}
			go_n1ql.UnsetQueryParams(vble)

		} else if strings.HasPrefix(args[0], "-") {
			// For query parameters
			vble := args[0]
			vble = vble[1:]

			err_code, err_str := PopValue_Helper(true, QueryParam, vble)
			if err_code != 0 {
				return err_code, err_str
			}
			go_n1ql.UnsetQueryParams(vble)

		} else if strings.HasPrefix(args[0], "$") {
			// For User defined session variables
			vble := args[0]
			vble = vble[1:]

			err_code, err_str := PopValue_Helper(true, UserDefSV, vble)
			if err_code != 0 {
				return err_code, err_str
			}

		} else {
			// For Predefined session variables
			vble := args[0]

			err_code, err_str := PopValue_Helper(true, PreDefSV, vble)
			if err_code != 0 {
				return err_code, err_str
			}
		}
	}
	return 0, ""
}
Ejemplo n.º 2
0
/* Pop the top value of the parameter stack.
   This is used by the \POP command with no arguments.
*/
func Popparam_Helper(param map[string]*Stack, isrestp bool, isnamep bool) (int, string) {
	for name, val := range param {
		_, err_code, err_str := val.Pop()

		if isrestp == true && val.Len() == 0 {
			delete(param, name)
			go_n1ql.UnsetQueryParams(name)
		}

		if err_code != 0 {
			return err_code, err_str
		}

		if isrestp == true && val.Len() != 0 {
			if isnamep == true {
				name = "$" + name
			}
			err_code, err_str = setNewParamPop(name, val)
			if err_code != 0 {
				return err_code, err_str
			}
		}
	}
	return 0, ""
}
Ejemplo n.º 3
0
func (this *Pop) ExecCommand(args []string) (int, string) {

	if len(args) > this.MaxArgs() {
		return errors.TOO_MANY_ARGS, ""

	} else if len(args) < this.MinArgs() {
		return errors.TOO_FEW_ARGS, ""

	} else if len(args) == 0 {
		/* For \Pop with no input arguments, Pop the top value
		on the stack for every variable. Dont return errors in
		this case as any one of these stacks can be empty.
		*/

		//Named Parameters
		Popparam_Helper(NamedParam, true, true)

		//Query Parameters
		Popparam_Helper(QueryParam, true, false)

		//User Defined Session Variables
		Popparam_Helper(UserDefSV, false, false)

		//Should not pop predefined variables unless
		//they are expicitely specified as an arg to POP.
		//Popparam_Helper(PreDefSV, false)

	} else {
		//Check what kind of parameter needs to be popped

		if strings.HasPrefix(args[0], "-$") {
			// For Named Parameters
			vble := args[0]
			vble = vble[2:]

			err_code, err_string := PopValue_Helper(false, NamedParam, vble)
			if err_code != 0 {
				return err_code, err_string
			}

			st_val, ok := NamedParam[vble]

			if ok {
				if NamedParam[vble].Len() == 0 {
					go_n1ql.UnsetQueryParams(vble)
				} else {
					name := "$" + vble
					err_code, err_str := setNewParamPop(name, st_val)
					if err_code != 0 {
						return err_code, err_str
					}
				}

			} else {
				go_n1ql.UnsetQueryParams(vble)
			}

		} else if strings.HasPrefix(args[0], "-") {
			// For query parameters
			vble := args[0]
			vble = vble[1:]

			err_code, err_string := PopValue_Helper(false, QueryParam, vble)
			if err_code != 0 {
				return err_code, err_string
			}

			st_val, ok := QueryParam[vble]

			if ok {
				if QueryParam[vble].Len() == 0 {
					go_n1ql.UnsetQueryParams(vble)
				} else {
					err_code, err_str := setNewParamPop(vble, st_val)
					if err_code != 0 {
						return err_code, err_str
					}
				}

			} else {
				go_n1ql.UnsetQueryParams(vble)
			}

		} else if strings.HasPrefix(args[0], "$") {
			// For User defined session variables
			vble := args[0]
			vble = vble[1:]

			err_code, err_string := PopValue_Helper(false, UserDefSV, vble)
			if err_code != 0 {
				return err_code, err_string
			}

		} else {
			// For Predefined session variables
			vble := args[0]

			err_code, err_string := PopValue_Helper(false, PreDefSV, vble)
			if err_code != 0 {
				return err_code, err_string
			}
		}
	}
	return 0, ""
}
Ejemplo n.º 4
0
func main() {

	n1ql, err := sql.Open("n1ql", "http://localhost:8093")
	if err != nil {
		log.Fatal(err)
	}

	err = n1ql.Ping()
	if err != nil {
		log.Fatal(err)
	}

	// Set query parameters
	ac := []byte(`[{"user": "******", "pass": "******"}]`)
	go_n1ql.SetQueryParams("creds", string(ac))
	go_n1ql.SetQueryParams("timeout", "10s")
	go_n1ql.SetQueryParams("scan_consistency", "request_plus")

	rows, err := n1ql.Query("select element (select * from contacts)")

	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		var contacts string
		if err := rows.Scan(&contacts); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s : \n", contacts)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	rows.Close()

	var name string
	rows, err = n1ql.Query("select name,type from contacts")

	if err != nil {
		log.Fatal(err)
	}
	for rows.Next() {
		var contacts string
		if err := rows.Scan(&contacts, &name); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s %s: \n", contacts, name)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	rows.Close()

	// if the following option is set, go_n1ql will return metrics as the last row
	go_n1ql.SetPassthroughMode(true)
	name = "dave"
	rows, err = n1ql.Query("select * from contacts unnest contacts.children where contacts.name = ? and children.age > ?", name, 10)

	if err != nil {
		log.Fatal(err)
	}
	for rows.Next() {
		var contacts string
		if err := rows.Scan(&contacts); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s : \n", contacts)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	rows.Close()

	rows, err = n1ql.Query("select name,type from contacts")

	if err != nil {
		log.Fatal(err)
	}
	for rows.Next() {
		var contacts string
		if err := rows.Scan(&contacts, &name); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s %s: \n", contacts, name)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	rows.Close()

	// explain statement
	rows, err = n1ql.Query("explain select * from contacts")

	if err != nil {
		log.Fatal(err)
	}
	for rows.Next() {
		var contacts string
		if err := rows.Scan(&contacts); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s : \n", contacts)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	rows.Close()

	go_n1ql.SetPassthroughMode(false)

	// prepared statements with positional args

	stmt, err := n1ql.Prepare("select personal_details, shipped_order_history from users_with_orders where doc_type=? and personal_details.age = ?")

	if err != nil {
		log.Fatal(err)
	}

	rows, err = stmt.Query("user_profile", 60)
	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		var personal, shipped string
		if err := rows.Scan(&personal, &shipped); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned personal_details: %s shipped_order_history %s : \n", personal, shipped)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	rows.Close()

	// Exec examples
	result, err := n1ql.Exec("Upsert INTO contacts values (\"irish\",{\"name\":\"irish\", \"type\":\"contact\"})")
	if err != nil {
		log.Fatal(err)
	}

	rowsAffected, err := result.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Rows affected %d", rowsAffected)

	stmt, err = n1ql.Prepare("Upsert INTO contacts values (?,?)")
	if err != nil {
		log.Fatal(err)
	}

	// Map Values need to be marshaled
	value, _ := json.Marshal(map[string]interface{}{"name": "irish", "type": "contact"})
	result, err = stmt.Exec("irish4", value)
	if err != nil {
		log.Fatal(err)
	}

	rowsAffected, err = result.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Rows affected %d", rowsAffected)

	for i := 0; i < 20; i++ {
		key := fmt.Sprintf("irish%d", i)
		result, err = stmt.Exec(key, value)
		if err != nil {
			log.Fatal(err)
		}

		ra, err := result.RowsAffected()
		if err != nil {
			log.Fatal(err)
		}
		rowsAffected += ra
	}

	log.Printf("Total Rows Affected %d", rowsAffected)

	stmt.Close()
	result, err = stmt.Exec("test", "this shouldn't work")
	if err == nil {
		log.Fatal("Statement not closed")
	}

	result, err = n1ql.Exec("delete from contacts use keys ? ", "irish")
	if err != nil {
		log.Fatal(err)
	}

	keys := make([]string, 0)
	for i := 0; i < 20; i++ {
		keys = append(keys, fmt.Sprintf("irish%d", i))
	}

	value, _ = json.Marshal(keys)
	result, err = n1ql.Exec("delete from contacts use keys ?", value)
	if err != nil {
		log.Fatal(err)
	}

	go_n1ql.UnsetQueryParams("scan_consistency")
	result, err = n1ql.Exec("delete from contacts use keys ?", value)
	if err != nil {
		log.Fatal(err)
	}

	go_n1ql.UnsetQueryParams("scan_consistency")

	// error expected
	go_n1ql.SetQueryParams("scan_consistency", "rubbish_plus")
	result, err = n1ql.Exec("delete from contacts use keys ?", value)
	if err == nil {
		log.Fatal("Error expected")
	}

}