Example #1
0
    def execute(self, query, args, consistency):
        """
        Execute a CQL query against the server.

        :param query: The CQL query to execute
        :type query: str.

        :param args: The arguments to substitute
        :type args: dict.

        :param consistency: The consistency level
        :type consistency: ConsistencyLevel

        In order to avoid unpleasant issues of CQL injection
        (Hey, just because there's no SQL doesn't mean that Little
        Bobby Tables won't mess things up for you like in XKCD #327)
        you probably want to use argument substitution instead of
        concatting strings together to build a query.

        Thus, like the official CQL driver for non-Twisted python
        that comes with the Cassandra distro, we do variable substitution.

        Example::

            d = client.execute("UPDATE :table SET 'fff' = :val WHERE "
            "KEY = :key",{"val":1234, "key": "fff", "table": "blah"})

        :returns: A Deferred that fires with either None, an int, or an
                  iterable of `{'column': value, ...}` dictionaries, depending
                  on the CQL query.  e.g. a UPDATE would return None,
                  whereas a SELECT would return an int or some rows

        Example output::

            [{"fff": 1222}]
        """
        prep_query = prepare(query, args)

        def _execute(client):
            return client.execute_cql3_query(prep_query,
                                             ttypes.Compression.NONE, consistency)

        def _proc_results(result):
            if result.type == ttypes.CqlResultType.ROWS:
                return self._unmarshal_result(result.schema, result.rows,
                                              unmarshallers)
            elif result.type == ttypes.CqlResultType.INT:
                return result.num
            else:
                return None

        d = self._connection()
        d.addCallback(_execute)
        d.addCallback(_proc_results)
        return d
Example #2
0
    def test_prepare(self):
        result = prepare("string :with a colon :with", {'with': 'value'})
        self.assertEqual(result, "string 'value' a colon 'value'")

        result = prepare("string :with a colon :with", {})
        self.assertEqual(result, "string :with a colon :with")
Example #3
0
 def test_prepare_create_table(self):
     result = prepare("create table :named", {'named': '`spam_eggs`'})
     self.assertEqual(result, "create table spam_eggs")
Example #4
0
    def test_prepare(self):
        result = prepare("string :with a colon :with", {'with': 'value'})
        self.assertEqual(result, "string 'value' a colon 'value'")

        result = prepare("string :with a colon :with", {})
        self.assertEqual(result, "string :with a colon :with")
Example #5
0
 def test_prepare_create_table(self):
     result = prepare("create table :named", {'named': '`spam_eggs`'})
     self.assertEqual(result, "create table spam_eggs")