Esempio n. 1
0
async def _execute_statement(model,
                             statement,
                             consistency_level,
                             timeout,
                             connection=None):
    """执行 cql 语句
    a copy of cassandra.cqlengine.query._execute_statement
    """
    params = statement.get_context()

    simple_statement = SimpleStatement(str(statement),
                                       consistency_level=consistency_level,
                                       fetch_size=statement.fetch_size)

    if model._partition_key_index:
        key_values = statement.partition_key_values(model._partition_key_index)
        if not any(v is None for v in key_values):
            parts = model._routing_key_from_values(
                key_values,
                conn.get_cluster(connection).protocol_version)
            simple_statement.routing_key = parts
            simple_statement.keyspace = model._get_keyspace()
    connection = connection or model._get_connection()
    try:
        return await execute(simple_statement,
                             params,
                             timeout=timeout,
                             connection=connection)
    except Unavailable:
        # 当无法用 LOCAL_QUORUM 获取数据的时候,用 ONE 来获取
        simple_statement.consistency_level = ConsistencyLevel.ONE
        return await execute(simple_statement,
                             params,
                             timeout=timeout,
                             connection=connection)
Esempio n. 2
0
async def _execute_statement(model,
                             statement,
                             consistency_level,
                             timeout,
                             connection=None):
    """
    Based on cassandra.cqlengine.query._execute_statement
    """
    params = statement.get_context()
    s = SimpleStatement(str(statement),
                        consistency_level=consistency_level,
                        fetch_size=statement.fetch_size)
    if model._partition_key_index:
        key_values = statement.partition_key_values(model._partition_key_index)
        if not any(v is None for v in key_values):
            parts = model._routing_key_from_values(
                key_values,
                conn.get_cluster(connection).protocol_version)
            s.routing_key = parts
            s.keyspace = model._get_keyspace()
    connection = connection or model._get_connection()
    return await execute(s, params, timeout=timeout, connection=connection)