コード例 #1
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
    def test_defect_self_adapt(self):
        class DefectSelfAdapt(float):
            def __conform__(self, _):
                return None

        with self.assertRaises(sqlite.ProgrammingError):
            sqlite.adapt(DefectSelfAdapt(1.))
コード例 #2
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
    def test_defect_proto(self):
        class DefectProto():
            def __adapt__(self):
                return None

        with self.assertRaises(sqlite.ProgrammingError):
            sqlite.adapt(1., DefectProto)
コード例 #3
0
 def quote_value(self, value):
     # The backend "mostly works" without this function and there are use
     # cases for compiling Python without the sqlite3 libraries (e.g.
     # security hardening).
     try:
         import sqlite3
         value = sqlite3.adapt(value)
     except ImportError:
         pass
     except sqlite3.ProgrammingError:
         pass
     # Manual emulation of SQLite parameter quoting
     if isinstance(value, bool):
         return str(int(value))
     elif isinstance(value, (Decimal, float, int)):
         return str(value)
     elif isinstance(value, str):
         return "'%s'" % value.replace("\'", "\'\'")
     elif value is None:
         return "NULL"
     elif isinstance(value, (bytes, bytearray, memoryview)):
         # Bytes are only allowed for BLOB fields, encoded as string
         # literals containing hexadecimal data and preceded by a single "X"
         # character.
         return "X'%s'" % value.hex()
     else:
         raise ValueError("Cannot quote parameter value %r of type %s" %
                          (value, type(value)))
コード例 #4
0
ファイル: schema.py プロジェクト: KonradMarzec1991/my_MDB
 def quote_value(self, value):
     # The backend "mostly works" without this function and there are use
     # cases for compiling Python without the sqlite3 libraries (e.g.
     # security hardening).
     try:
         import sqlite3
         value = sqlite3.adapt(value)
     except ImportError:
         pass
     except sqlite3.ProgrammingError:
         pass
     # Manual emulation of SQLite parameter quoting
     if isinstance(value, type(True)):
         return str(int(value))
     elif isinstance(value, (Decimal, float, int)):
         return str(value)
     elif isinstance(value, str):
         return "'%s'" % value.replace("\'", "\'\'")
     elif value is None:
         return "NULL"
     elif isinstance(value, (bytes, bytearray, memoryview)):
         # Bytes are only allowed for BLOB fields, encoded as string
         # literals containing hexadecimal data and preceded by a single "X"
         # character:
         # value = b'\x01\x02' => value_hex = b'0102' => return X'0102'
         value = bytes(value)
         hex_encoder = codecs.getencoder('hex_codec')
         value_hex, _length = hex_encoder(value)
         # Use 'ascii' encoding for b'01' => '01', no need to use force_text here.
         return "X'%s'" % value_hex.decode('ascii')
     else:
         raise ValueError("Cannot quote parameter value %r of type %s" %
                          (value, type(value)))
コード例 #5
0
ファイル: schema.py プロジェクト: 0038lana/Test-Task
 def quote_value(self, value):
     # The backend "mostly works" without this function and there are use
     # cases for compiling Python without the sqlite3 libraries (e.g.
     # security hardening).
     import sqlite3
     try:
         value = sqlite3.adapt(value)
     except sqlite3.ProgrammingError:
         pass
     # Manual emulation of SQLite parameter quoting
     if isinstance(value, type(True)):
         return str(int(value))
     elif isinstance(value, (Decimal, float)):
         return str(value)
     elif isinstance(value, six.integer_types):
         return str(value)
     elif isinstance(value, six.string_types):
         return "'%s'" % six.text_type(value).replace("\'", "\'\'")
     elif value is None:
         return "NULL"
     elif isinstance(value, (bytes, bytearray, six.memoryview)):
         # Bytes are only allowed for BLOB fields, encoded as string
         # literals containing hexadecimal data and preceded by a single "X"
         # character:
         # value = b'\x01\x02' => value_hex = b'0102' => return X'0102'
         value = bytes(value)
         hex_encoder = codecs.getencoder('hex_codec')
         value_hex, _length = hex_encoder(value)
         # Use 'ascii' encoding for b'01' => '01', no need to use force_text here.
         return "X'%s'" % value_hex.decode('ascii')
     else:
         raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))
コード例 #6
0
ファイル: schema.py プロジェクト: GravyHands/django
 def quote_value(self, value):
     # The backend "mostly works" without this function and there are use
     # cases for compiling Python without the sqlite3 libraries (e.g.
     # security hardening).
     try:
         import sqlite3
         value = sqlite3.adapt(value)
     except ImportError:
         pass
     except sqlite3.ProgrammingError:
         pass
     # Manual emulation of SQLite parameter quoting
     if isinstance(value, bool):
         return str(int(value))
     elif isinstance(value, (Decimal, float, int)):
         return str(value)
     elif isinstance(value, str):
         return "'%s'" % value.replace("\'", "\'\'")
     elif value is None:
         return "NULL"
     elif isinstance(value, (bytes, bytearray, memoryview)):
         # Bytes are only allowed for BLOB fields, encoded as string
         # literals containing hexadecimal data and preceded by a single "X"
         # character.
         return "X'%s'" % value.hex()
     else:
         raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))
コード例 #7
0
 def quote_value(self, value):
     # The backend "mostly works" without this function and there are use
     # cases for compiling Python without the sqlite3 libraries (e.g.
     # security hardening).
     try:
         import sqlite3
         value = sqlite3.adapt(value)
     except ImportError:
         pass
     except sqlite3.ProgrammingError:
         pass
     # Manual emulation of SQLite parameter quoting
     if isinstance(value, type(True)):
         return str(int(value))
コード例 #8
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
 def test_adapt_alt(self):
     alt = "other"
     self.assertEqual(alt, sqlite.adapt(1., None, alt))
コード例 #9
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
 def test_adapt(self):
     val = 42
     self.assertEqual(float(val), sqlite.adapt(val))
コード例 #10
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
    def test_custom_proto(self):
        class CustomProto():
            def __adapt__(self):
                return "adapted"

        self.assertEqual(sqlite.adapt(1., CustomProto), "adapted")
コード例 #11
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
 def test_missing_protocol(self):
     with self.assertRaises(sqlite.ProgrammingError):
         sqlite.adapt(1, None)
コード例 #12
0
ファイル: types.py プロジェクト: paxtonfitzpatrick/cpython
 def test_missing_adapter(self):
     with self.assertRaises(sqlite.ProgrammingError):
         sqlite.adapt(1.)  # No float adapter registered
コード例 #13
0
ファイル: sqlite.py プロジェクト: dpep/py_arrow_adapters
from arrow import Arrow
from sqlite3 import adapt, register_adapter

register_adapter(Arrow, lambda x: adapt(x.to('UTC').naive))

# https://docs.python.org/3.7/library/sqlite3.html#sqlite3.register_adapter
コード例 #14
0
import codecs