Rename Query to Statement to match Java driver

This commit is contained in:
Tyler Hobbs
2013-09-18 17:20:04 -05:00
parent 25f8314999
commit b87890d0a3
4 changed files with 35 additions and 32 deletions

View File

@@ -34,7 +34,7 @@ from cassandra.policies import (RoundRobinPolicy, SimpleConvictionPolicy,
ExponentialReconnectionPolicy, HostDistance, ExponentialReconnectionPolicy, HostDistance,
RetryPolicy) RetryPolicy)
from cassandra.query import (SimpleStatement, PreparedStatement, BoundStatement, from cassandra.query import (SimpleStatement, PreparedStatement, BoundStatement,
bind_params, QueryTrace, Query) bind_params, QueryTrace, Statement)
from cassandra.pool import (_ReconnectionHandler, _HostReconnectionHandler, from cassandra.pool import (_ReconnectionHandler, _HostReconnectionHandler,
HostConnectionPool) HostConnectionPool)
@@ -126,7 +126,7 @@ class Cluster(object):
default_retry_policy = RetryPolicy() default_retry_policy = RetryPolicy()
""" """
A default :class:`.policies.RetryPolicy` instance to use for all A default :class:`.policies.RetryPolicy` instance to use for all
:class:`.Query` objects which do not have a :attr:`~.Query.retry_policy` :class:`.Statement` objects which do not have a :attr:`~.Statement.retry_policy`
explicitly set. explicitly set.
""" """
@@ -589,7 +589,7 @@ class Session(object):
If an error is encountered while executing the query, an Exception If an error is encountered while executing the query, an Exception
will be raised. will be raised.
`query` may be a query string or an instance of :class:`cassandra.query.Query`. `query` may be a query string or an instance of :class:`cassandra.query.Statement`.
`parameters` may be a sequence or dict of parameters to bind. If a `parameters` may be a sequence or dict of parameters to bind. If a
sequence is used, ``%s`` should be used the placeholder for each sequence is used, ``%s`` should be used the placeholder for each
@@ -598,16 +598,16 @@ class Session(object):
If `trace` is set to :const:`True`, an attempt will be made to If `trace` is set to :const:`True`, an attempt will be made to
fetch the trace details and attach them to the `query`'s fetch the trace details and attach them to the `query`'s
:attr:`~.Query.trace` attribute in the form of a :class:`.QueryTrace` :attr:`~.Statement.trace` attribute in the form of a :class:`.QueryTrace`
instance. This requires that `query` be a :class:`.Query` subclass instance. This requires that `query` be a :class:`.Statement` subclass
instance and not just a string. If there is an error fetching the instance and not just a string. If there is an error fetching the
trace details, the :attr:`~.Query.trace` attribute will be left as trace details, the :attr:`~.Statement.trace` attribute will be left as
:const:`None`. :const:`None`.
""" """
if trace and not isinstance(query, Query): if trace and not isinstance(query, Statement):
raise TypeError( raise TypeError(
"The query argument must be an instance of a subclass of " "The query argument must be an instance of a subclass of "
"cassandra.query.Query when trace=True") "cassandra.query.Statement when trace=True")
future = self.execute_async(query, parameters, trace) future = self.execute_async(query, parameters, trace)
try: try:

View File

@@ -73,7 +73,7 @@ class LoadBalancingPolicy(object):
def make_query_plan(self, working_keyspace=None, query=None): def make_query_plan(self, working_keyspace=None, query=None):
""" """
Given a :class:`~.query.Query` instance, return a iterable Given a :class:`~.query.Statement` instance, return a iterable
of :class:`.Host` instances which should be queried in that of :class:`.Host` instances which should be queried in that
order. A generator may work well for custom implementations order. A generator may work well for custom implementations
of this method. of this method.
@@ -261,11 +261,11 @@ class TokenAwarePolicy(LoadBalancingPolicy):
This alters the child policy's behavior so that it first attempts to This alters the child policy's behavior so that it first attempts to
send queries to :attr:`~.HostDistance.LOCAL` replicas (as determined send queries to :attr:`~.HostDistance.LOCAL` replicas (as determined
by the child policy) based on the :class:`.Query`'s by the child policy) based on the :class:`.Statement`'s
:attr:`~.Query.routing_key`. Once those hosts are exhausted, the :attr:`~.Statement.routing_key`. Once those hosts are exhausted, the
remaining hosts in the child policy's query plan will be used. remaining hosts in the child policy's query plan will be used.
If no :attr:`~.Query.routing_key` is set on the query, the child If no :attr:`~.Statement.routing_key` is set on the query, the child
policy's query plan will be used as is. policy's query plan will be used as is.
""" """
@@ -482,7 +482,7 @@ class RetryPolicy(object):
:attr:`.Cluster.default_retry_policy` attribute to an instance of this :attr:`.Cluster.default_retry_policy` attribute to an instance of this
class or one of its subclasses. class or one of its subclasses.
To specify a retry policy per query, set the :attr:`.Query.retry_policy` To specify a retry policy per query, set the :attr:`.Statement.retry_policy`
attribute to an instance of this class or one of its subclasses. attribute to an instance of this class or one of its subclasses.
If custom behavior is needed for retrying certain operations, If custom behavior is needed for retrying certain operations,
@@ -516,7 +516,7 @@ class RetryPolicy(object):
as :attr:`.RETRY`) and a :class:`.ConsistencyLevel` to retry the as :attr:`.RETRY`) and a :class:`.ConsistencyLevel` to retry the
operation at or :const:`None` to keep the same consistency level. operation at or :const:`None` to keep the same consistency level.
`query` is the :class:`.Query` that timed out. `query` is the :class:`.Statement` that timed out.
`consistency` is the :class:`.ConsistencyLevel` that the operation was `consistency` is the :class:`.ConsistencyLevel` that the operation was
attempted at. attempted at.
@@ -546,7 +546,7 @@ class RetryPolicy(object):
This is called when a write operation times out from the coordinator's This is called when a write operation times out from the coordinator's
perspective (i.e. a replica did not respond to the coordinator in time). perspective (i.e. a replica did not respond to the coordinator in time).
`query` is the :class:`.Query` that timed out. `query` is the :class:`.Statement` that timed out.
`consistency` is the :class:`.ConsistencyLevel` that the operation was `consistency` is the :class:`.ConsistencyLevel` that the operation was
attempted at. attempted at.
@@ -581,7 +581,7 @@ class RetryPolicy(object):
This means that the read or write operation was never forwared to This means that the read or write operation was never forwared to
any replicas. any replicas.
`query` is the :class:`.Query` that failed. `query` is the :class:`.Statement` that failed.
`consistency` is the :class:`.ConsistencyLevel` that the operation was `consistency` is the :class:`.ConsistencyLevel` that the operation was
attempted at. attempted at.

View File

@@ -13,7 +13,7 @@ from cassandra.cqltypes import unix_time_from_uuid1
from cassandra.decoder import (cql_encoders, cql_encode_object, from cassandra.decoder import (cql_encoders, cql_encode_object,
cql_encode_sequence) cql_encode_sequence)
class Query(object): class Statement(object):
""" """
An abstract class representing a single query. There are two subclasses: An abstract class representing a single query. There are two subclasses:
:class:`.SimpleStatement` and :class:`.BoundStatement`. These can :class:`.SimpleStatement` and :class:`.BoundStatement`. These can
@@ -39,11 +39,6 @@ class Query(object):
to :attr:`.ConsistencyLevel.ONE`. to :attr:`.ConsistencyLevel.ONE`.
""" """
keyspace = None
"""
The string name of the keyspace this query acts on.
"""
_routing_key = None _routing_key = None
def __init__(self, retry_policy=None, tracing_enabled=False, def __init__(self, retry_policy=None, tracing_enabled=False,
@@ -79,9 +74,17 @@ class Query(object):
components should be strings. components should be strings.
""") """)
class SimpleStatement(Query): @property
def keyspace(self):
""" """
A simple, un-prepared query. All attributes of :class:`Query` apply The string name of the keyspace this query acts on.
"""
return None
class SimpleStatement(Statement):
"""
A simple, un-prepared query. All attributes of :class:`Statement` apply
to this class as well. to this class as well.
""" """
@@ -91,7 +94,7 @@ class SimpleStatement(Query):
of parameter placeholders that will be filled through the of parameter placeholders that will be filled through the
`parameters` argument of :meth:`.Session.execute()`. `parameters` argument of :meth:`.Session.execute()`.
""" """
Query.__init__(self, *args, **kwargs) Statement.__init__(self, *args, **kwargs)
self._query_string = query_string self._query_string = query_string
@property @property
@@ -169,12 +172,12 @@ class PreparedStatement(object):
(self.query_string, consistency)) (self.query_string, consistency))
class BoundStatement(Query): class BoundStatement(Statement):
""" """
A prepared statement that has been bound to a particular set of values. A prepared statement that has been bound to a particular set of values.
These may be created directly or through :meth:`.PreparedStatement.bind()`. These may be created directly or through :meth:`.PreparedStatement.bind()`.
All attributes of :class:`Query` apply to this class as well. All attributes of :class:`Statement` apply to this class as well.
""" """
prepared_statement = None prepared_statement = None
@@ -190,13 +193,13 @@ class BoundStatement(Query):
def __init__(self, prepared_statement, *args, **kwargs): def __init__(self, prepared_statement, *args, **kwargs):
""" """
`prepared_statement` should be an instance of :class:`PreparedStatement`. `prepared_statement` should be an instance of :class:`PreparedStatement`.
All other ``*args`` and ``**kwargs`` will be passed to :class:`.Query`. All other ``*args`` and ``**kwargs`` will be passed to :class:`.Statement`.
""" """
self.consistency_level = prepared_statement.consistency_level self.consistency_level = prepared_statement.consistency_level
self.prepared_statement = prepared_statement self.prepared_statement = prepared_statement
self.values = [] self.values = []
Query.__init__(self, *args, **kwargs) Statement.__init__(self, *args, **kwargs)
def bind(self, values): def bind(self, values):
""" """

View File

@@ -17,7 +17,7 @@ from cassandra.policies import (RoundRobinPolicy, DCAwareRoundRobinPolicy,
RetryPolicy, WriteType, RetryPolicy, WriteType,
DowngradingConsistencyRetryPolicy) DowngradingConsistencyRetryPolicy)
from cassandra.pool import Host from cassandra.pool import Host
from cassandra.query import Query from cassandra.query import Statement
class TestRoundRobinPolicy(unittest.TestCase): class TestRoundRobinPolicy(unittest.TestCase):
@@ -191,7 +191,7 @@ class TokenAwarePolicyTest(unittest.TestCase):
policy.populate(cluster, hosts) policy.populate(cluster, hosts)
for i in range(4): for i in range(4):
query = Query(routing_key=struct.pack('>i', i)) query = Statement(routing_key=struct.pack('>i', i))
qplan = list(policy.make_query_plan(None, query)) qplan = list(policy.make_query_plan(None, query))
replicas = get_replicas(None, struct.pack('>i', i)) replicas = get_replicas(None, struct.pack('>i', i))
@@ -222,7 +222,7 @@ class TokenAwarePolicyTest(unittest.TestCase):
policy.populate(cluster, hosts) policy.populate(cluster, hosts)
for i in range(4): for i in range(4):
query = Query(routing_key=struct.pack('>i', i)) query = Statement(routing_key=struct.pack('>i', i))
qplan = list(policy.make_query_plan(None, query)) qplan = list(policy.make_query_plan(None, query))
replicas = get_replicas(None, struct.pack('>i', i)) replicas = get_replicas(None, struct.pack('>i', i))