Classdesc

A class encapsulating a prepared query statement. It includes state that can be sent to a server and executed without re-parsing the query. It includes bind variables which may be set for each successive use of the query. PreparedStatement object is returned as a result of prepare method. It can be passed to query and queryIterable methods for execution and be reused for multiple queries, potentially with different values of bind variables.

You may share an instance of PreparedStatement by queries running async-concurrently, e.g. queries invoked concurrently by different async functions. This is referred to as async-safety:
An instance of PreparedStatement is async-safe if bind variables are not used. If bind variables are used, it is not async-safe. In this case, you can construct additional instances of PreparedStatement using copyStatement method in order to share it among async-concurrent queries.

Hierarchy

  • PreparedStatement

Implements

Properties

bindings: {
    [name: string]: FieldValue;
}

Sets and gets the bindings object explicitly. Bindings object is an object which properties contain the bind variables for this prepared statement. For each variable, binding object has property which name is the variable name and the value is the variable value. Note that "$" in the variable name is included in its property name. For positional variables, the names are determined by the query engine.

Example

Setting bindings.

prepStmt.bindings = {
$id: 100,
$name: 'John'
};
// This is equivalent to:
prepStmt.set('$id', 100);
prepStmt.set('$name', 'John');

Type declaration

consumedCapacity?: ConsumedCapacity

Capacity consumed by this operation, see ConsumedCapacity. Undefined if using on-premises service or if this is a result of a put or a delete sub-operation as part of WriteMultipleResult.

queryPlan: string

Query execution plan printout if was requested by prepare (see getQueryPlan), otherwise undefined.

resultSchema: string

JSON representation of the query result schema if was requested by prepare (see getResultSchema), otherwise undefined.

sql: string

SQL text of this prepared statement.

Methods

  • Returns a copy of this prepared statement without its variables.

    This method returns a new instance of PreparedStatement that shares this object's prepared query, which is immutable, but does not share its variables. Use this method when you need to execute the same prepared query async-concurrently (call this method to create a new copy for each additional concurrent query).

    Returns

    A copy of this prepared statement without its variables

    Returns PreparedStatement

  • Binds a variable to use for the query. The variable can be identified either by its name or its position.

    To bind by name, pass a name of the variable as it was declared in DECLARE statement of the query.

    You can also bind a variable by its position within the query string. The positions start at 1. The variable that appears first in the query text has position 1, the variable that appears second has position 2 and so on. Binding by position is useful for queries where bind variables identified by "?" are used instead of named variables (but it can be used for both types of variables).

    Existing variables with the same name or position are silently overwritten. The names, positions and types are validated when the query is executed.

    Example

    Binding variables by name.

    let client = new NoSQLClient(//.....
    let prepStmt = await client.prepare(
    'DECLARE $id INTEGER; $sal DOUBLE; SELECT id, firstName, lastName ' +
    'FROM Emp WHERE id <= $id AND salary <= $sal');
    ps.set('$id', 1100);
    .set('$sal', 100500);
    for await(const res of client.queryIterable(stmt)) {
    //.....
    }
    ps.set('$id', 2000);
    for await(const res of client.queryIterable(stmt)) {
    //.....
    }
    //.....

    Example

    Binding variables by position.

    let prepStmt = await client.prepare(
    'SELECT id, firstName, lastName FROM Emp WHERE ' +
    'id <= ? AND salary <= ?');
    ps.set(1, 1100)
    .set(2, 100500);
    //.....

    Returns

    This instance for chaining

    Throws

    If binding by position and the position is invalid.

    Parameters

    • nameOrPosition: string | number

      Name or position of the variable

    • val: FieldValue

      Value of the variable of the appropriate type

    Returns PreparedStatement

Generated using TypeDoc