-
Notifications
You must be signed in to change notification settings - Fork 0
/
c3.py
327 lines (281 loc) · 11.9 KB
/
c3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import logging
import platform
from ctypes import (
CDLL, POINTER, CFUNCTYPE,
pointer, byref, string_at, cast,
c_void_p, c_char_p,
c_int, c_int64, c_uint64, c_double, c_char,
)
from sqlite3_error_map import sqlite_error_code_to_name
logger = logging.getLogger("server")
c_sqlite3_p = c_void_p
c_sqlite3_stmt_p = c_void_p
c_exec_callback_fn = CFUNCTYPE(c_int, c_void_p, c_int, POINTER(c_char_p), POINTER(c_char_p))
c_destructor_fn = CFUNCTYPE(None, c_void_p)
libfile_platform = {
"Linux": "libsqlite3.so",
"Darwin": "libsqlite3.dylib",
}
platform_name = platform.system()
libfile = libfile_platform[platform_name]
lib = CDLL(libfile)
lib.sqlite3_open_v2.argtypes = (c_char_p, POINTER(c_sqlite3_p), c_int, c_char_p,)
lib.sqlite3_open_v2.restype = c_int
lib.sqlite3_close_v2.argtypes = (c_sqlite3_p,)
lib.sqlite3_close_v2.restype = c_int
lib.sqlite3_extended_result_codes.argtypes = (c_sqlite3_p, c_int,)
lib.sqlite3_extended_result_codes.restype = c_int
lib.sqlite3_errmsg.argtypes = (c_sqlite3_p,)
lib.sqlite3_errmsg.restype = c_char_p
lib.sqlite3_errstr.argtypes = (c_int,)
lib.sqlite3_errstr.restype = c_char_p
lib.sqlite3_exec.argtypes = (c_sqlite3_p, c_char_p, c_exec_callback_fn, c_void_p, POINTER(c_char_p),)
lib.sqlite3_exec.restype = c_int
lib.sqlite3_txn_state.argtypes = (c_sqlite3_p, c_char_p,)
lib.sqlite3_txn_state.restype = c_int
lib.sqlite3_changes64.argtypes = (c_sqlite3_p,)
lib.sqlite3_changes64.restype = c_int64
lib.sqlite3_total_changes64.argtypes = (c_sqlite3_p,)
lib.sqlite3_total_changes64.restype = c_int64
lib.sqlite3_last_insert_rowid.argtypes = (c_sqlite3_p,)
lib.sqlite3_last_insert_rowid.restype = c_int64
lib.sqlite3_limit.argtypes = (c_sqlite3_p, c_int, c_int,)
lib.sqlite3_limit.restype = c_int
lib.sqlite3_busy_timeout.argtypes = (c_sqlite3_p, c_int,)
lib.sqlite3_busy_timeout.restype = c_int
lib.sqlite3_get_autocommit.argtypes = (c_sqlite3_p,)
lib.sqlite3_get_autocommit.restype = c_int
lib.sqlite3_prepare_v2.argtypes = (
c_sqlite3_p, c_void_p, c_int, POINTER(c_sqlite3_stmt_p), POINTER(c_void_p),)
lib.sqlite3_prepare_v2.restype = c_int
lib.sqlite3_finalize.argtypes = (c_sqlite3_stmt_p,)
lib.sqlite3_finalize.restype = c_int
lib.sqlite3_step.argtypes = (c_sqlite3_stmt_p,)
lib.sqlite3_step.restype = c_int
lib.sqlite3_bind_parameter_count.argtypes = (c_sqlite3_stmt_p,)
lib.sqlite3_bind_parameter_count.restype = c_int
lib.sqlite3_bind_parameter_index.argtypes = (c_sqlite3_stmt_p, c_char_p,)
lib.sqlite3_bind_parameter_index.restype = c_int
lib.sqlite3_bind_parameter_name.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_bind_parameter_name.restype = c_char_p
lib.sqlite3_bind_blob64.argtypes = (c_sqlite3_stmt_p, c_int, c_void_p, c_uint64, c_destructor_fn,)
lib.sqlite3_bind_blob64.restype = c_int
lib.sqlite3_bind_text.argtypes = (c_sqlite3_stmt_p, c_int, POINTER(c_char), c_int, c_destructor_fn,)
lib.sqlite3_bind_text.restype = c_int
lib.sqlite3_bind_double.argtypes = (c_sqlite3_stmt_p, c_int, c_double,)
lib.sqlite3_bind_double.restype = c_int
lib.sqlite3_bind_int64.argtypes = (c_sqlite3_stmt_p, c_int, c_int64,)
lib.sqlite3_bind_int64.restype = c_int
lib.sqlite3_bind_null.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_bind_null.restype = c_int
lib.sqlite3_column_count.argtypes = (c_sqlite3_stmt_p,)
lib.sqlite3_column_count.restype = c_int
lib.sqlite3_column_name.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_name.restype = c_char_p
lib.sqlite3_column_decltype.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_decltype.restype = c_char_p
lib.sqlite3_column_type.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_type.restype = c_int
lib.sqlite3_column_blob.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_blob.restype = c_void_p
lib.sqlite3_column_text.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_text.restype = c_void_p
lib.sqlite3_column_bytes.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_bytes.restype = c_int
lib.sqlite3_column_double.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_double.restype = c_double
lib.sqlite3_column_int64.argtypes = (c_sqlite3_stmt_p, c_int,)
lib.sqlite3_column_int64.restype = c_int64
lib.sqlite3_stmt_readonly.argtypes = (c_sqlite3_stmt_p,)
lib.sqlite3_stmt_readonly.restype = c_int
lib.sqlite3_stmt_isexplain.argtypes = (c_sqlite3_stmt_p,)
lib.sqlite3_stmt_isexplain.restype = c_int
SQLITE_OPEN_READWRITE = 0x00000002
SQLITE_OPEN_CREATE = 0x00000004
SQLITE_TRANSIENT = c_destructor_fn(-1)
SQLITE_ROW = 100
SQLITE_DONE = 101
SQLITE_INTEGER = 1
SQLITE_FLOAT = 2
SQLITE_BLOB = 4
SQLITE_NULL = 5
SQLITE_TEXT = 3
SQLITE_LIMIT_LENGTH = 0
SQLITE_LIMIT_SQL_LENGTH = 1
SQLITE_LIMIT_COLUMN = 2
SQLITE_LIMIT_EXPR_DEPTH = 3
SQLITE_LIMIT_COMPOUND_SELECT = 4
SQLITE_LIMIT_VDBE_OP = 5
SQLITE_LIMIT_FUNCTION_ARG = 6
SQLITE_LIMIT_ATTACHED = 7
SQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8
SQLITE_LIMIT_VARIABLE_NUMBER = 9
SQLITE_LIMIT_TRIGGER_DEPTH = 10
SQLITE_LIMIT_WORKER_THREADS = 11
class Conn:
def __init__(self, db_ptr):
self.db_ptr = db_ptr
@classmethod
def open(cls, filename):
filename_ptr = c_char_p(filename.encode())
db_ptr = c_sqlite3_p()
flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE
vfs_ptr = c_char_p()
_try(lib.sqlite3_open_v2(filename_ptr, byref(db_ptr), flags, vfs_ptr))
return cls(db_ptr)
def close(self):
if self.db_ptr is not None:
lib.sqlite3_close_v2(self.db_ptr)
self.db_ptr = None
def __del__(self):
self.close()
def extended_result_codes(self, onoff):
assert self.db_ptr is not None
lib.sqlite3_extended_result_codes(self.db_ptr, onoff)
def errmsg(self):
assert self.db_ptr is not None
return str(lib.sqlite3_errmsg(self.db_ptr).decode())
@classmethod
def errstr(cls, code):
return str(lib.sqlite3_errstr(code).decode())
def exec(self, sql):
assert self.db_ptr is not None
sql_ptr = c_char_p(sql.encode())
callback_ptr = c_exec_callback_fn()
arg_ptr = c_void_p()
errmsg_ptr_ptr = pointer(c_char_p())
_try(lib.sqlite3_exec(self.db_ptr, sql_ptr, callback_ptr, arg_ptr, errmsg_ptr_ptr), self)
def txn_state(self):
assert self.db_ptr is not None
schema_ptr = c_char_p()
return lib.sqlite3_txn_state(self.db_ptr, schema_ptr)
def prepare(self, sql):
assert self.db_ptr is not None
sql = sql.encode()
sql_data = c_char_p(sql)
sql_ptr = cast(sql_data, c_void_p)
sql_len = c_int(len(sql) + 1)
stmt_ptr = c_sqlite3_stmt_p()
tail_ptr = c_void_p()
_try(lib.sqlite3_prepare_v2(self.db_ptr, sql_ptr, sql_len, byref(stmt_ptr), byref(tail_ptr)), self)
if stmt_ptr.value is None:
return None, b""
tail = sql[tail_ptr.value - sql_ptr.value:]
return Stmt(self, stmt_ptr), tail.decode()
def changes(self):
assert self.db_ptr is not None
return lib.sqlite3_changes64(self.db_ptr)
def total_changes(self):
assert self.db_ptr is not None
return lib.sqlite3_total_changes64(self.db_ptr)
def last_insert_rowid(self):
assert self.db_ptr is not None
return lib.sqlite3_last_insert_rowid(self.db_ptr)
def limit(self, id, new_val):
assert self.db_ptr is not None
return lib.sqlite3_limit(self.db_ptr, id, new_val)
def busy_timeout(self, ms):
assert self.db_ptr is not None
lib.sqlite3_busy_timeout(self.db_ptr, ms)
def get_autocommit(self):
assert self.db_ptr is not None
return lib.sqlite3_get_autocommit(self.db_ptr) != 0
class Stmt:
def __init__(self, conn, stmt_ptr):
self.conn = conn
self.stmt_ptr = stmt_ptr
def close(self):
if self.stmt_ptr is not None:
lib.sqlite3_finalize(self.stmt_ptr)
self.stmt_ptr = None
def __del__(self):
self.close()
def param_count(self):
assert self.stmt_ptr is not None
return lib.sqlite3_bind_parameter_count(self.stmt_ptr)
def param_index(self, name):
assert self.stmt_ptr is not None
name_ptr = c_char_p(name.encode())
return lib.sqlite3_bind_parameter_index(self.stmt_ptr, name_ptr)
def param_name(self, param_i):
assert self.stmt_ptr is not None
name = lib.sqlite3_bind_parameter_name(self.stmt_ptr, param_i)
return name.decode() if name is not None else None
def bind(self, param_i, value):
assert self.stmt_ptr is not None
if isinstance(value, str):
value = value.encode()
value_ptr, value_len = c_char_p(value), c_int(len(value))
_try(lib.sqlite3_bind_text(self.stmt_ptr, param_i, value_ptr, value_len, SQLITE_TRANSIENT), self.conn)
elif isinstance(value, bytes):
value_ptr, value_len = c_char_p(value), c_uint64(len(value))
_try(lib.sqlite3_bind_blob64(self.stmt_ptr, param_i, value_ptr, value_len, SQLITE_TRANSIENT), self.conn)
elif isinstance(value, int):
_try(lib.sqlite3_bind_int64(self.stmt_ptr, param_i, c_int64(value)), self.conn)
elif isinstance(value, float):
_try(lib.sqlite3_bind_double(self.stmt_ptr, param_i, c_double(value)), self.conn)
elif value is None:
_try(lib.sqlite3_bind_null(self.stmt_ptr, param_i), self.conn)
else:
raise ValueError(f"Cannot bind {type(value)!r}")
def step(self):
assert self.stmt_ptr is not None
res = lib.sqlite3_step(self.stmt_ptr)
if res == SQLITE_DONE:
return False
elif res == SQLITE_ROW:
return True
_try(res, self.conn)
def column_count(self):
assert self.stmt_ptr is not None
return lib.sqlite3_column_count(self.stmt_ptr)
def column_name(self, column_i):
assert self.stmt_ptr is not None
return lib.sqlite3_column_name(self.stmt_ptr, column_i).decode()
def column_decltype(self, column_i):
assert self.stmt_ptr is not None
name = lib.sqlite3_column_decltype(self.stmt_ptr, column_i)
return name.decode() if name is not None else name
def column(self, column_i):
assert self.stmt_ptr is not None
typ = lib.sqlite3_column_type(self.stmt_ptr, column_i)
if typ == SQLITE_INTEGER:
return lib.sqlite3_column_int64(self.stmt_ptr, column_i)
elif typ == SQLITE_FLOAT:
return lib.sqlite3_column_double(self.stmt_ptr, column_i)
elif typ == SQLITE_BLOB:
data_ptr = lib.sqlite3_column_blob(self.stmt_ptr, column_i)
data_len = lib.sqlite3_column_bytes(self.stmt_ptr, column_i)
return bytes(string_at(data_ptr, data_len))
elif typ == SQLITE_TEXT:
data_ptr = lib.sqlite3_column_text(self.stmt_ptr, column_i)
data_len = lib.sqlite3_column_bytes(self.stmt_ptr, column_i)
b = bytes(string_at(data_ptr, data_len))
try:
return b.decode()
except UnicodeDecodeError:
logger.debug("Could not decode column %s, bytes %s", column_i, b, exc_info=True)
raise
elif typ == SQLITE_NULL:
return None
else:
raise ValueError(f"Unknown SQLite type {typ}")
def readonly(self):
assert self.stmt_ptr is not None
return lib.sqlite3_stmt_readonly(self.stmt_ptr) != 0
def isexplain(self):
assert self.stmt_ptr is not None
return lib.sqlite3_stmt_isexplain(self.stmt_ptr)
class SqliteError(RuntimeError):
def __init__(self, message, error_code=None) -> None:
super().__init__(message)
self.error_code = error_code
self.error_name = sqlite_error_code_to_name.get(error_code)
def _try(error_code, conn=None):
if error_code == 0:
return
error_str = Conn.errstr(error_code)
if conn is not None:
details = f": {conn.errmsg()}"
message = f"SQLite function returned error code {error_code} ({error_str}){details}"
raise SqliteError(message, error_code)