| 44 | self.connection.enable_constraint_checking() |
| 45 | |
| 46 | def quote_value(self, value): |
| 47 | # The backend "mostly works" without this function and there are use |
| 48 | # cases for compiling Python without the sqlite3 libraries (e.g. |
| 49 | # security hardening). |
| 50 | try: |
| 51 | import sqlite3 |
| 52 | |
| 53 | value = sqlite3.adapt(value) |
| 54 | except ImportError: |
| 55 | pass |
| 56 | except sqlite3.ProgrammingError: |
| 57 | pass |
| 58 | # Manual emulation of SQLite parameter quoting |
| 59 | if isinstance(value, bool): |
| 60 | return str(int(value)) |
| 61 | elif isinstance(value, (Decimal, float, int)): |
| 62 | return str(value) |
| 63 | elif isinstance(value, str): |
| 64 | return "'%s'" % value.replace("'", "''") |
| 65 | elif value is None: |
| 66 | return "NULL" |
| 67 | elif isinstance(value, (bytes, bytearray, memoryview)): |
| 68 | # Bytes are only allowed for BLOB fields, encoded as string |
| 69 | # literals containing hexadecimal data and preceded by a single "X" |
| 70 | # character. |
| 71 | return "X'%s'" % value.hex() |
| 72 | else: |
| 73 | raise ValueError( |
| 74 | "Cannot quote parameter value %r of type %s" % (value, type(value)) |
| 75 | ) |
| 76 | |
| 77 | def prepare_default(self, value): |
| 78 | return self.quote_value(value) |