def test_map_collection(self):
     vals = OrderedDict()
     vals['a'] = 'a'
     vals['b'] = 'b'
     vals['c'] = 'c'
     result = bind_params("%s", (vals, ), Encoder())
     self.assertEqual(result, "{'a': 'a', 'b': 'b', 'c': 'c'}")
Пример #2
0
    def add(self, statement, parameters=None):
        """
        Adds a :class:`.Statement` and optional sequence of parameters
        to be used with the statement to the batch.

        Like with other statements, parameters must be a sequence, even
        if there is only one item.
        """
        if isinstance(statement, six.string_types):
            if parameters:
                encoder = Encoder(
                ) if self._session is None else self._session.encoder
                statement = bind_params(statement, parameters, encoder)
            self._add_statement_and_params(False, statement, ())
        elif isinstance(statement, PreparedStatement):
            query_id = statement.query_id
            bound_statement = statement.bind((
            ) if parameters is None else parameters)
            self._update_state(bound_statement)
            self._add_statement_and_params(True, query_id,
                                           bound_statement.values)
        elif isinstance(statement, BoundStatement):
            if parameters:
                raise ValueError(
                    "Parameters cannot be passed with a BoundStatement "
                    "to BatchStatement.add()")
            self._update_state(statement)
            self._add_statement_and_params(
                True, statement.prepared_statement.query_id, statement.values)
        else:
            # it must be a SimpleStatement
            query_string = statement.query_string
            if parameters:
                encoder = Encoder(
                ) if self._session is None else self._session.encoder
                query_string = bind_params(query_string, parameters, encoder)
            self._update_state(statement)
            self._add_statement_and_params(False, query_string, ())
        return self
 def test_float_precision(self):
     f = 3.4028234663852886e+38
     self.assertEqual(float(bind_params("%s", (f, ), Encoder())), f)
 def test_quote_escaping(self):
     result = bind_params("%s", ("""'ef''ef"ef""ef'""", ), Encoder())
     self.assertEqual(result, """'''ef''''ef"ef""ef'''""")
 def test_list_collection(self):
     result = bind_params("%s", (['a', 'b', 'c'], ), Encoder())
     self.assertEqual(result, "['a', 'b', 'c']")
 def test_set_collection(self):
     result = bind_params("%s", (set(['a', 'b']), ), Encoder())
     self.assertIn(result, ("{'a', 'b'}", "{'b', 'a'}"))
 def test_none_param(self):
     result = bind_params("%s", (None, ), Encoder())
     self.assertEqual(result, "NULL")
 def test_generator_param(self):
     result = bind_params("%s", ((i for i in xrange(3)), ), Encoder())
     self.assertEqual(result, "[0, 1, 2]")
 def test_sequence_param(self):
     result = bind_params("%s", (ValueSequence((1, "a", 2.0)), ), Encoder())
     self.assertEqual(result, "(1, 'a', 2.0)")
 def test_bind_map(self):
     result = bind_params("%(a)s %(b)s %(c)s", dict(a=1, b="a", c=2.0),
                          Encoder())
     self.assertEqual(result, "1 'a' 2.0")
 def test_bind_sequence(self):
     result = bind_params("%s %s %s", (1, "a", 2.0), Encoder())
     self.assertEqual(result, "1 'a' 2.0")