[Zope3-checkins] CVS: Zope3/src/zope/app/rdb - __init__.py:1.21
Stuart Bishop
zen@shangri-la.dropbear.id.au
Tue, 22 Jul 2003 06:06:25 -0400
Update of /cvs-repository/Zope3/src/zope/app/rdb
In directory cvs.zope.org:/tmp/cvs-serv28352
Modified Files:
__init__.py
Log Message:
Common sqlquote method
=== Zope3/src/zope/app/rdb/__init__.py 1.20 => 1.21 ===
--- Zope3/src/zope/app/rdb/__init__.py:1.20 Mon Jul 7 13:14:55 2003
+++ Zope3/src/zope/app/rdb/__init__.py Tue Jul 22 06:05:51 2003
@@ -21,6 +21,7 @@
"""
__metaclass__ = type
+import types, string
from types import StringTypes
from persistence import Persistent
@@ -40,6 +41,31 @@
from zope.app.component.nextservice import getNextService
+def sqlquote(x):
+ """
+ Escape data suitable for inclusion in generated ANSI SQL92 code for
+ cases where bound variables are not suitable.
+
+ >>> sqlquote('''Hi''')
+ "'Hi'"
+ >>> sqlquote('''It's mine''')
+ "'It''s mine'"
+ >>> sqlquote(32)
+ 32
+ >>> sqlquote(None)
+ 'NULL'
+ """
+ if type(x) == types.StringType:
+ x = "'" + string.replace(
+ string.replace(str(x), '\\', '\\\\'), "'", "''") + "'"
+ elif type(x) in (types.IntType, types.LongType, types.FloatType):
+ pass
+ elif x is None:
+ x = 'NULL'
+ else:
+ raise TypeError, 'do not know how to handle type %s' % type(x)
+ return x
+
class ResultSet(list):
"""Database Result Set.
@@ -456,3 +482,5 @@
from zope.testing.cleanup import addCleanUp
addCleanUp(_clear)
del addCleanUp
+
+