def test_update(): x = {'a': 1, 'b': '2', 'c': 1223.4} sql, params = query_builder.update('foo', **x) assert sql == 'UPDATE `foo` SET `a`=%(_QB_a)s, `b`=%(_QB_b)s, `c`=%(_QB_c)s' assert params == {'_QB_a': 1, '_QB_b': '2', '_QB_c': 1223.4} assert database.escape_query( sql, params) == r"UPDATE `foo` SET `a`=1, `b`='2', `c`=1223.4"
def test_replace(): rows = [{'a': 1, 'b': '2', 'c': 1223.4}, {'a': 2, 'b': '5', 'c': 1}] sql, params = query_builder.multi_replace('foo', *rows) assert sql == 'REPLACE INTO `foo` (`a`, `b`, `c`) VALUES (%(_QB_ROW_0)s), (%(_QB_ROW_1)s)' assert params == {'_QB_ROW_0': [1, '2', 1223.4], '_QB_ROW_1': [2, '5', 1]} assert database.escape_query( sql, params ) == r"REPLACE INTO `foo` (`a`, `b`, `c`) VALUES (1,'2',1223.4), (2,'5',1)"
def _build_extra_predicate(self, extra_predicate): """ This method is a good one to extend if you want to create a queue which always applies an extra predicate. """ if extra_predicate is None: return '' # if they don't have a supported format seq, wrap it for them if not isinstance(extra_predicate[1], (list, dict, tuple)): extra_predicate = [extra_predicate[0], (extra_predicate[1], )] extra_predicate = database.escape_query(*extra_predicate) return 'AND (' + extra_predicate + ')'
def test_simple_expression(): x = {'a': 1, 'b': '2', 'c': 1223.4} sql, params = query_builder.simple_expression(', ', **x) assert sql == '`a`=%(_QB_a)s, `b`=%(_QB_b)s, `c`=%(_QB_c)s' assert params == {'_QB_a': 1, '_QB_b': '2', '_QB_c': 1223.4} assert database.escape_query(sql, params) == r"`a`=1, `b`='2', `c`=1223.4"
def build_example_query(job, file_id): load_data = LoadDataStmt(job, file_id, '<source file>') return database.escape_query(*load_data.build())
def test_update(): x = { 'a': 1, 'b': '2', 'c': 1223.4 } sql, params = query_builder.update('foo', **x) assert sql == 'UPDATE `foo` SET `a`=%(_QB_a)s, `b`=%(_QB_b)s, `c`=%(_QB_c)s' assert params == { '_QB_a': 1, '_QB_b': '2', '_QB_c': 1223.4 } assert database.escape_query(sql, params) == r"UPDATE `foo` SET `a`=1, `b`='2', `c`=1223.4"
def test_replace(): rows = [{ 'a': 1, 'b': '2', 'c': 1223.4 }, { 'a': 2, 'b': '5', 'c': 1 }] sql, params = query_builder.multi_replace('foo', *rows) assert sql == 'REPLACE INTO `foo` (`a`, `b`, `c`) VALUES (%(_QB_ROW_0)s), (%(_QB_ROW_1)s)' assert params == { '_QB_ROW_0': [1, '2', 1223.4], '_QB_ROW_1': [2, '5', 1] } assert database.escape_query(sql, params) == r"REPLACE INTO `foo` (`a`, `b`, `c`) VALUES (1,'2',1223.4), (2,'5',1)"
def test_simple_expression(): x = { 'a': 1, 'b': '2', 'c': 1223.4 } sql, params = query_builder.simple_expression(', ', **x) assert sql == '`a`=%(_QB_a)s, `b`=%(_QB_b)s, `c`=%(_QB_c)s' assert params == { '_QB_a': 1, '_QB_b': '2', '_QB_c': 1223.4 } assert database.escape_query(sql, params) == r"`a`=1, `b`='2', `c`=1223.4"