def test_execute_statement_with_params(self):
        entry_count = 20
        self._populate_map(entry_count, lambda v: Student(v, v))
        statement = SqlStatement(
            "SELECT age FROM %s WHERE height = CAST(? AS REAL)" %
            self.map_name)
        statement.add_parameter(13.0)
        result = self.client.sql.execute_statement(statement)

        six.assertCountEqual(self, [13],
                             [row.get_object("age") for row in result])
Exemplo n.º 2
0
    def execute_statement(self, query, *args, **kwargs):
        if self.is_v5_or_newer_client:
            return self.client.sql.execute(query, *args, **kwargs).result()

        # Compatibility with 4.x clients
        statement = SqlStatement(query)
        for arg in args:
            statement.add_parameter(arg)

        for key, value in kwargs.items():
            setattr(statement, key, value)

        return self.client.sql.execute_statement(statement)
        # Get the index of the is_active column
        is_active_index = row_metadata.find_column("is_active")

        # Get the object with the column index
        is_active = row.get_object_with_index(is_active_index)

        print(name, age, is_active)

# Construct a statement object to control the properties of the query
# Special keywords __key and this can be used to refer to key and value.
# Also, a placeholder parameters can be specified
statement = SqlStatement("SELECT __key, age FROM customers WHERE name LIKE ?")

# Parameters will replace the placeholders on the server side
statement.add_parameter("Jo%")
statement.timeout = 5

with client.sql.execute_statement(statement) as result:
    # Row metadata can also be retrieved from the result
    row_metadata = result.get_row_metadata().result()

    for row in result:
        key = row.get_object("__key")
        age = row.get_object("age")

        print(key, age)

# Parameters can be passed directly in the basic execution syntax
result = client.sql.execute(
    "SELECT this FROM customers WHERE age > ? AND age < ?", 30, 40)