本文整理匯總了Python中psycopg2.extensions.adapt方法的典型用法代碼示例。如果您正苦於以下問題:Python extensions.adapt方法的具體用法?Python extensions.adapt怎麽用?Python extensions.adapt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類psycopg2.extensions
的用法示例。
在下文中一共展示了extensions.adapt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: as_string
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def as_string(self, context):
# is it a connection or cursor?
if isinstance(context, ext.connection):
conn = context
elif isinstance(context, ext.cursor):
conn = context.connection
else:
raise TypeError("context must be a connection or a cursor")
a = ext.adapt(self._wrapped)
if hasattr(a, 'prepare'):
a.prepare(conn)
rv = a.getquoted()
if sys.version_info[0] >= 3 and isinstance(rv, bytes):
rv = rv.decode(ext.encodings[conn.encoding])
return rv
示例2: getquoted
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def getquoted(self):
r = self.adapted
if r.isempty:
return b"'empty'"
if not r.lower_inf:
# not exactly: we are relying that none of these object is really
# quoted (they are numbers). Also, I'm lazy and not preparing the
# adapter because I assume encoding doesn't matter for these
# objects.
lower = adapt(r.lower).getquoted().decode('ascii')
else:
lower = ''
if not r.upper_inf:
upper = adapt(r.upper).getquoted().decode('ascii')
else:
upper = ''
return ("'%s%s,%s%s'" % (
r._bounds[0], lower, upper, r._bounds[1])).encode('ascii')
# TODO: probably won't work with infs, nans and other tricky cases.
示例3: test_inet_conform
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_inet_conform(self):
from psycopg2.extras import Inet
i = Inet("192.168.1.0/24")
a = psycopg2.extensions.adapt(i)
a.prepare(self.conn)
self.assertEqual(
filter_scs(self.conn, b("E'192.168.1.0/24'::inet")),
a.getquoted())
# adapts ok with unicode too
i = Inet(u"192.168.1.0/24")
a = psycopg2.extensions.adapt(i)
a.prepare(self.conn)
self.assertEqual(
filter_scs(self.conn, b("E'192.168.1.0/24'::inet")),
a.getquoted())
示例4: test_none_fast_path
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_none_fast_path(self):
# the None adapter is not actually invoked in regular adaptation
class WonkyAdapter(object):
def __init__(self, obj): pass
def getquoted(self): return "NOPE!"
curs = self.conn.cursor()
orig_adapter = ext.adapters[type(None), ext.ISQLQuote]
try:
ext.register_adapter(type(None), WonkyAdapter)
self.assertEqual(ext.adapt(None).getquoted(), "NOPE!")
s = curs.mogrify("SELECT %s;", (None,))
self.assertEqual(b("SELECT NULL;"), s)
finally:
ext.register_adapter(type(None), orig_adapter)
示例5: getquoted
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def getquoted(self):
r = self.adapted
if r.isempty:
return b("'empty'")
if not r.lower_inf:
# not exactly: we are relying that none of these object is really
# quoted (they are numbers). Also, I'm lazy and not preparing the
# adapter because I assume encoding doesn't matter for these
# objects.
lower = adapt(r.lower).getquoted().decode('ascii')
else:
lower = ''
if not r.upper_inf:
upper = adapt(r.upper).getquoted().decode('ascii')
else:
upper = ''
return ("'%s%s,%s%s'" % (
r._bounds[0], lower, upper, r._bounds[1])).encode('ascii')
# TODO: probably won't work with infs, nans and other tricky cases.
示例6: adapt_array
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def adapt_array(arr):
conn = arr.field.model_class._meta.database.get_conn()
items = adapt(arr.items)
items.prepare(conn)
return AsIs('%s::%s%s' % (
items,
arr.field.get_column_type(),
'[]' * arr.field.dimensions))
示例7: getquoted
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def getquoted(self):
obj = _A(self.addr)
if hasattr(obj, 'prepare'):
obj.prepare(self._conn)
return obj.getquoted() + b"::inet"
示例8: sanitize_for_db
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def sanitize_for_db(field):
if field == None:
return 'NULL'
elif type(field) == int:
return repr(int(field))
elif type(field) == str and "'" in field:
from psycopg2.extensions import adapt
return str(adapt(field))
else:
return repr(field)
示例9: quote_value
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def quote_value(self, value):
if value ==None:
return SQL_NULL
if is_list(value):
json = value2json(value)
return self.quote_value(json)
if is_text(value) and len(value) > 256:
value = value[:256]
return SQL(adapt(value))
示例10: test_adapt_subtype
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_adapt_subtype(self):
from psycopg2.extensions import adapt
class Sub(str): pass
s1 = "hel'lo"
s2 = Sub(s1)
self.assertEqual(adapt(s1).getquoted(), adapt(s2).getquoted())
示例11: test_adapt_most_specific
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_adapt_most_specific(self):
from psycopg2.extensions import adapt, register_adapter, AsIs
class A(object): pass
class B(A): pass
class C(B): pass
register_adapter(A, lambda a: AsIs("a"))
register_adapter(B, lambda b: AsIs("b"))
try:
self.assertEqual(b('b'), adapt(C()).getquoted())
finally:
del psycopg2.extensions.adapters[A, psycopg2.extensions.ISQLQuote]
del psycopg2.extensions.adapters[B, psycopg2.extensions.ISQLQuote]
示例12: test_no_mro_no_joy
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_no_mro_no_joy(self):
from psycopg2.extensions import adapt, register_adapter, AsIs
class A: pass
class B(A): pass
register_adapter(A, lambda a: AsIs("a"))
try:
self.assertRaises(psycopg2.ProgrammingError, adapt, B())
finally:
del psycopg2.extensions.adapters[A, psycopg2.extensions.ISQLQuote]
示例13: test_adapt_subtype_3
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_adapt_subtype_3(self):
from psycopg2.extensions import adapt, register_adapter, AsIs
class A: pass
class B(A): pass
register_adapter(A, lambda a: AsIs("a"))
try:
self.assertEqual(b("a"), adapt(B()).getquoted())
finally:
del psycopg2.extensions.adapters[A, psycopg2.extensions.ISQLQuote]
示例14: test_str
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def test_str(self):
snowman = u"\u2603"
obj = {'a': [1, 2, snowman]}
j = psycopg2.extensions.adapt(psycopg2.extras.Json(obj))
s = str(j)
self.assert_(isinstance(s, str))
# no pesky b's
self.assert_(s.startswith("'"))
self.assert_(s.endswith("'"))
示例15: getquoted
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import adapt [as 別名]
def getquoted(self):
obj = _A(self.addr)
if hasattr(obj, 'prepare'):
obj.prepare(self._conn)
return obj.getquoted() + b("::inet")