Skip to content

Commit 348cc12

Browse files
committed
Change escape from \ to '
1 parent d2a0056 commit 348cc12

File tree

4 files changed

+80
-91
lines changed

4 files changed

+80
-91
lines changed

pycovenantsql/converters.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,10 @@ def escape_float(value, mapping=None):
5050
return ('%.15g' % value)
5151

5252
_escape_table = [unichr(x) for x in range(128)]
53-
_escape_table[0] = u'\\0'
54-
_escape_table[ord('\\')] = u'\\\\'
55-
_escape_table[ord('\n')] = u'\\n'
56-
_escape_table[ord('\r')] = u'\\r'
57-
_escape_table[ord('\032')] = u'\\Z'
58-
_escape_table[ord('"')] = u'\\"'
59-
_escape_table[ord("'")] = u"\\'"
53+
_escape_table[ord("'")] = u"''"
6054

6155
def _escape_unicode(value, mapping=None):
62-
"""escapes *value* without adding quote.
56+
"""escapes *value* with adding single quote.
6357
6458
Value should be unicode
6559
"""

pycovenantsql/tests/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ def safe_create_table(self, connection, tablename, ddl, cleanup=True):
6262
"""
6363
cursor = connection.cursor()
6464

65-
with warnings.catch_warnings():
66-
warnings.simplefilter("ignore")
67-
cursor.execute("drop table if exists `%s`" % (tablename,))
65+
self.drop_table(connection, tablename)
6866
cursor.execute(ddl)
6967
cursor.close()
7068
if cleanup:

pycovenantsql/tests/test_basic.py

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,81 @@
1212

1313
__all__ = ["TestConversion", "TestCursor", "TestBulkInserts"]
1414

15-
1615
class TestConversion(base.PyCovenantSQLTestCase):
1716
def test_datatypes(self):
1817
""" test every data type """
1918
conn = self.connections[0]
2019
c = conn.cursor()
21-
c.execute("create table test_datatypes (b bit, i int, l bigint, f real, s varchar(32), u varchar(32), bb blob, d date, dt datetime, ts timestamp, td time, t time, st datetime)")
22-
try:
23-
# insert values
24-
25-
v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol", "binary\x00data".encode(conn.encoding), datetime.date(1988,2,2), datetime.datetime(2014, 5, 15, 7, 45, 57), datetime.timedelta(5,6), datetime.time(16,32), time.localtime())
26-
c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", v)
27-
c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes")
28-
r = c.fetchone()
29-
self.assertEqual(0, r[0])
30-
self.assertEqual(v[1:10], r[1:10])
31-
self.assertEqual(datetime.timedelta(0, 60 * (v[10].hour * 60 + v[10].minute)), r[10])
32-
self.assertEqual(datetime.datetime(*v[-1][:6]), r[-1])
33-
34-
c.execute("delete from test_datatypes")
35-
36-
# check nulls
37-
c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", [None] * 12)
38-
c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes")
39-
r = c.fetchone()
40-
self.assertEqual(tuple([None] * 12), r)
4120

21+
self.safe_create_table(
22+
conn, "test_datatypes","create table test_datatypes (b bit, i int, l bigint, f real, s varchar(32), u varchar(32), bb blob, d date, dt datetime, ts timestamp, td time, t time, st datetime)")
23+
24+
# insert values
25+
v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol", "binary\x00data".encode(conn.encoding), datetime.date(1988,2,2), datetime.datetime(2014, 5, 15, 7, 45, 57), datetime.timedelta(5,6), datetime.time(16,32), time.localtime())
26+
c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", v)
27+
c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes")
28+
r = c.fetchone()
29+
self.assertEqual(0, r[0])
30+
self.assertEqual(v[1:10], r[1:10])
31+
self.assertEqual(datetime.timedelta(0, 60 * (v[10].hour * 60 + v[10].minute)), r[10])
32+
self.assertEqual(datetime.datetime(*v[-1][:6]), r[-1])
33+
34+
c.execute("delete from test_datatypes")
35+
36+
# check nulls
37+
c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", [None] * 12)
38+
c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes")
39+
r = c.fetchone()
40+
self.assertEqual(tuple([None] * 12), r)
41+
42+
c.execute("delete from test_datatypes")
43+
44+
# check sequences type
45+
for seq_type in (tuple, list, set, frozenset):
46+
c.execute("insert into test_datatypes (i, l) values (2,4), (6,8), (10,12)")
47+
seq = seq_type([2,6])
48+
c.execute("select l from test_datatypes where i in %s order by i", (seq,))
49+
r = c.fetchall()
50+
self.assertEqual(((4,),(8,)), r)
4251
c.execute("delete from test_datatypes")
4352

44-
# check sequences type
45-
for seq_type in (tuple, list, set, frozenset):
46-
c.execute("insert into test_datatypes (i, l) values (2,4), (6,8), (10,12)")
47-
seq = seq_type([2,6])
48-
c.execute("select l from test_datatypes where i in %s order by i", (seq,))
49-
r = c.fetchall()
50-
self.assertEqual(((4,),(8,)), r)
51-
c.execute("delete from test_datatypes")
52-
53-
finally:
54-
c.execute("drop table test_datatypes")
53+
c.close()
5554

5655
def test_dict(self):
5756
""" test dict escaping """
5857
conn = self.connections[0]
5958
c = conn.cursor()
60-
c.execute("create table test_dict (a integer, b integer, c integer)")
61-
try:
62-
c.execute("insert into test_dict (a,b,c) values (%(a)s, %(b)s, %(c)s)", {"a":1,"b":2,"c":3})
63-
c.execute("select a,b,c from test_dict")
64-
self.assertEqual((1,2,3), c.fetchone())
65-
finally:
66-
c.execute("drop table test_dict")
59+
self.safe_create_table(
60+
conn, "test_dict", "create table test_dict (a integer, b integer, c integer)")
61+
c.execute("insert into test_dict (a,b,c) values (%(a)s, %(b)s, %(c)s)", {"a":1,"b":2,"c":3})
62+
c.execute("select a,b,c from test_dict")
63+
self.assertEqual((1,2,3), c.fetchone())
64+
c.close()
6765

6866
def test_string(self):
6967
conn = self.connections[0]
7068
c = conn.cursor()
71-
c.execute("create table test_dict (a text)")
69+
self.safe_create_table(
70+
conn, "test_string", "create table test_string (a text)")
7271
test_value = "I am a test string"
73-
try:
74-
c.execute("insert into test_dict (a) values (%s)", test_value)
75-
c.execute("select a from test_dict")
76-
self.assertEqual((test_value,), c.fetchone())
77-
finally:
78-
c.execute("drop table test_dict")
72+
73+
c.execute("insert into test_string (a) values (%s)", test_value)
74+
c.execute("select a from test_string")
75+
self.assertEqual((test_value,), c.fetchone())
76+
77+
c.close()
7978

8079
def test_integer(self):
8180
conn = self.connections[0]
8281
c = conn.cursor()
83-
c.execute("create table test_dict (a integer)")
82+
83+
self.safe_create_table(
84+
conn, "test_integer", "create table test_integer (a integer)")
8485
test_value = 12345
85-
try:
86-
c.execute("insert into test_dict (a) values (%s)", test_value)
87-
c.execute("select a from test_dict")
88-
self.assertEqual((test_value,), c.fetchone())
89-
finally:
90-
c.execute("drop table test_dict")
86+
c.execute("insert into test_integer (a) values (%s)", test_value)
87+
c.execute("select a from test_integer")
88+
self.assertEqual((test_value,), c.fetchone())
89+
c.close()
9190

9291
# TODO support binary
9392
# def test_binary(self):
@@ -143,16 +142,16 @@ def test_datetime_microseconds(self):
143142
conn = self.connections[0]
144143
c = conn.cursor()
145144
dt = datetime.datetime(2013, 11, 12, 9, 9, 9, 123450)
146-
c.execute("create table test_datetime (id int, ts datetime(6))")
147-
try:
148-
c.execute(
149-
"insert into test_datetime values (%s, %s)",
150-
(1, dt)
151-
)
152-
c.execute("select ts from test_datetime")
153-
self.assertEqual((dt,), c.fetchone())
154-
finally:
155-
c.execute("drop table test_datetime")
145+
self.safe_create_table(
146+
conn, "test_datetime", "create table test_datetime (id int, ts datetime(6))")
147+
c.execute(
148+
"insert into test_datetime values (%s, %s)",
149+
(1, dt)
150+
)
151+
c.execute("select ts from test_datetime")
152+
self.assertEqual((dt,), c.fetchone())
153+
154+
c.close()
156155

157156

158157
class TestCursor(base.PyCovenantSQLTestCase):
@@ -213,27 +212,25 @@ def test_fetch_no_result(self):
213212
""" test a fetchone() with no rows """
214213
conn = self.connections[0]
215214
c = conn.cursor()
216-
c.execute("create table test_nr (b varchar(32))")
217-
try:
218-
data = "pycovenantsql"
219-
c.execute("insert into test_nr (b) values (%s)", (data,))
220-
self.assertEqual(None, c.fetchone())
221-
finally:
222-
c.execute("drop table test_nr")
215+
self.safe_create_table(
216+
conn, "test_nr", "create table test_nr (b varchar(32))")
217+
data = "pycovenantsql"
218+
c.execute("insert into test_nr (b) values (%s)", (data,))
219+
self.assertEqual(None, c.fetchone())
220+
c.close()
223221

224222
def test_aggregates(self):
225223
""" test aggregate functions """
226224
conn = self.connections[0]
227225
c = conn.cursor()
228-
try:
229-
c.execute('create table test_aggregates (i integer)')
230-
for i in range(0, 10):
231-
c.execute('insert into test_aggregates (i) values (%s)', (i,))
232-
c.execute('select sum(i) from test_aggregates')
233-
r, = c.fetchone()
234-
self.assertEqual(sum(range(0,10)), r)
235-
finally:
236-
c.execute('drop table test_aggregates')
226+
self.safe_create_table(
227+
conn, "test_aggregates", "create table test_aggregates (i integer)")
228+
for i in range(0, 10):
229+
c.execute('insert into test_aggregates (i) values (%s)', (i,))
230+
c.execute('select sum(i) from test_aggregates')
231+
r, = c.fetchone()
232+
self.assertEqual(sum(range(0,10)), r)
233+
c.close()
237234

238235
def test_single_tuple(self):
239236
""" test a single tuple """

pycovenantsql/tests/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_escape_string(self):
8484
con = self.connections[0]
8585
cur = con.cursor()
8686

87-
self.assertEqual(con.escape("foo'bar"), "'foo\\'bar'")
87+
self.assertEqual(con.escape("foo'bar"), "'foo''bar'")
8888

8989
def test_escape_builtin_encoders(self):
9090
con = self.connections[0]

0 commit comments

Comments
 (0)