-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsqlite.py
More file actions
436 lines (353 loc) · 15.8 KB
/
sqlite.py
File metadata and controls
436 lines (353 loc) · 15.8 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
sqlite-electron server executing the sql queries of the nodejs/electron processes
Copyright (C) 2020-2026 Motagamwala Taha Arif Ali
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import array
import sys, json, sqlite3
from sqlite3 import dump # type: ignore
from typing import Any
conn: None | sqlite3.Connection = None
class ConnectionNotSetUpException(Exception):
pass
def decodebytes(data: dict[Any, Any]):
"""
Decodes bytes values in a dictionary by converting them to a list of integers.
Args:
data (dict): The dictionary containing the bytes values.
Returns:
dict: The dictionary with the bytes values replaced by a dictionary with the type "Buffer" and the data as a list of integers.
Example:
>>> data = {'a': b'hello', 'b': b'world'}
>>> decodebytes(data)
{'a': {'type': 'Buffer', 'data': [104, 101, 108, 108, 111]}, 'b': {'type': 'Buffer', 'data': [119, 111, 114, 108, 100]}}
"""
for key, val in data.items():
if type(val) is bytes:
data[key] = {"type": "Buffer", "data": list(val)}
return data
def encodebytes(data: list[Any] | dict[str, Any]):
"""
Encodes a list of data by converting elements to array objects based on specific conditions.
Parameters:
data (list): A list of data to encode.
Returns:
list: The encoded data list.
"""
if type(data) is dict:
for key, val in data.items():
try:
if type(val) is dict:
if "data" in val and "type" in val:
if val["type"] == "Buffer" and type(val["data"]) is list: # type: ignore
data[key] = array.array("B", val["data"]) # type: ignore
except Exception:
pass
elif type(data) is list:
for i, val in enumerate(data):
try:
v: dict[str, Any] = json.loads(val)
if type(v) is dict:
if "data" in v and "type" in v:
if v["type"] == "Buffer" and type(v["data"]) is list:
data[i] = array.array("B", v["data"]) # type: ignore
except Exception:
pass
else:
raise Exception("Invalid data type")
return data
def newConnection(db: str, isuri: bool, autocommit: bool = True):
"""
Function for establishing a new connection to the database.
Parameters:
db (str): The path of the database to connect to.
isuri (bool): Indicates whether the database path is a URI or not.
autocommit (bool | int): The autocommit setting for the connection.
Returns:
bool: True if the connection is successfully established, otherwise returns an error message.
"""
try:
global conn
if conn is None:
conn = sqlite3.connect(db, uri=isuri, autocommit=autocommit)
conn.row_factory = sqlite3.Row
return True
else:
conn.close()
conn = sqlite3.connect(db, uri=isuri, autocommit=autocommit)
conn.row_factory = sqlite3.Row
return True
except Exception as e:
return "Error: " + str(e)
def executeQuery(sql: str, values: list[str] | dict[str, Any]):
"""
Executes an SQL query on the database connection.
Parameters:
sql (str): The SQL query to execute.
values (list): The list of values to bind to the query.
Returns:
bool: True if the query was executed successfully, otherwise an error message.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
global conn
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
if not(not values):
values = encodebytes(values)
conn.execute(sql, values)
return True
else:
conn.execute(sql)
return True
except Exception as e:
return "Error: " + str(e)
def fetchall(sql: str, values: list[str] | dict[str, Any]):
"""
Fetches all rows from the database that match the given SQL query and values.
Args:
sql (str): The SQL query to execute.
values (list): The list of values to bind to the query.
Returns:
list: A list of dictionaries representing the fetched rows. Each dictionary contains the column names as keys and their corresponding values.
str: If an error occurs during the execution of the query, a string with the error message is returned.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
if not(not values):
values = encodebytes(values)
return [decodebytes(dict(i)) for i in conn.execute(sql, values).fetchall()]
else:
return [decodebytes(dict(i)) for i in conn.execute(sql).fetchall()]
except Exception as e:
return "Error: " + str(e)
def fetchone(sql: str, values: list[str] | dict[str, Any]):
"""
Fetches a single row from the database that matches the given SQL query and values.
Args:
sql (str): The SQL query to execute.
values (list): The list of values to bind to the query.
Returns:
dict: A dictionary representing the fetched row. The dictionary contains the column names as keys and their corresponding values.
str: If an error occurs during the execution of the query, a string with the error message is returned.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
if not(not values):
values = encodebytes(values)
return decodebytes(dict(conn.execute(sql, values).fetchone()))
else:
return decodebytes(dict(conn.execute(sql).fetchone()))
except Exception as e:
return "Error: " + str(e)
def fetchmany(sql: str, size: int, values: list[str] | dict[str, Any]):
"""
Fetches multiple rows from the database that match the given SQL query and values.
Args:
sql (str): The SQL query to execute.
size (int): The number of rows to fetch.
values (list): The list of values to bind to the query.
Returns:
list: A list of dictionaries representing the fetched rows. Each dictionary contains the column names as keys and their corresponding values.
str: If an error occurs during the execution of the query, a string with the error message is returned.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
if not(not values):
values = encodebytes(values)
return [decodebytes(dict(i)) for i in conn.execute(sql, values).fetchmany(size)]
else:
return [decodebytes(dict(i)) for i in conn.execute(sql).fetchmany(size)]
except Exception as e:
return "Error: " + str(e)
def executeMany(sql: str, values: list[list[str]] | list[dict[str, Any]]):
"""
Executes a SQL query with multiple sets of values on the database connection.
Args:
sql (str): The SQL query to execute.
values (list): A list of lists containing the values to bind to the query.
Returns:
bool: True if the query was executed successfully, False otherwise.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
Note:
The values parameter should be a list of lists, where each inner list contains the values for a single row.
Example:
executeMany("INSERT INTO table (column1, column2) VALUES (?, ?)", [[value1, value2], [value3, value4]])
"""
try:
global conn
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
for i in range(0, len(values)):
vs = values[i]
vs = encodebytes(vs)
conn.executemany(sql, (values))
return True
except Exception as e:
return "Error: " + str(e)
def executeScript(sqlScript: str):
"""
Executes a SQL script on the database connection.
Args:
sqlScript (str): The path to the SQL script file.
Returns:
bool or str: True if the script was executed successfully, otherwise an error message.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
global conn
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
with open(sqlScript, "r") as sql_file:
sql = sql_file.read()
conn.executescript(sql)
return True
except Exception as e:
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
conn.executescript(sqlScript)
return True
except Exception as e:
return "Error: " + str(e)
def load_extension(name: str):
"""
This function loads an extension into the database.
Args:
name (str): The name of the extension to load.
Returns:
bool or str: True if the extension is loaded successfully, otherwise a string containing the error message.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
conn.enable_load_extension(True)
conn.load_extension(name)
conn.enable_load_extension(False)
return True
except Exception as e:
return "Error: " + str(e)
def backup(target: str, pages: int, name: str, sleep: int):
"""
This function creates a backup of the database.
Args:
target (str): The database connection to save the backup to.
pages (int): The number of pages to copy at a time. If equal to or less than 0, the entire database is copied in a single step. Defaults to -1.
name (str): The name of the database to back up. Either "main" (the default) for the main database, "temp" for the temporary database, or the name of a custom database as attached using the ATTACH DATABASE SQL statement.
sleep (float): The number of seconds to sleep between successive attempts to back up remaining pages.
Returns:
bool or str: True if the backup is created successfully, otherwise a string containing the error message.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
dst = sqlite3.connect(target)
conn.backup(dst, pages=pages, name=name, sleep=sleep)
dst.close()
return True
except Exception as e:
return "Error: " + str(e)
def iterdump(file: str,filter: str | None =None):
"""
Dumps the database contents to a file in SQL format.
Args:
file (str): The path to the file where the SQL dump will be written.
filter (str | None, optional): An optional filter to restrict the dumped tables. Defaults to None.
Returns:
bool or str: True if the dump is successful, otherwise an error message.
Raises:
ConnectionNotSetUpException: If the database connection is not set up.
"""
try:
if conn == None:
raise ConnectionNotSetUpException("Connection not set up")
with open(file, "w") as f:
f.write("\n".join(conn.iterdump(filter=filter)))
return True
except Exception as e:
return "Error: " + str(e)
def main():
"""
The main driver function reading from the input send by nodejs process and executing the sql queries on the database returning the data in JSON format.
This function reads lines of input from the standard input, processes them, and writes the results to the standard output in JSON format. The input lines are expected to be in the form of a JSON array with the first element being the command name and the remaining elements being the arguments for the command.
The supported commands are:
- "newConnection": Creates a new database connection.
- "executeQuery": Executes an SQL query on the database.
- "fetchAll": Fetches all rows from the result of an executed SQL query.
- "fetchMany": Fetches a number of rows from the result of an executed SQL query.
- "fetchOne": Fetches a single row from the result of an executed SQL query.
- "executeMany": Executes an SQL query with multiple sets of parameters.
- "executeScript": Executes an SQL script file.
- "load_extension": Loads an SQL extension into the database.
- "backup": Creates a backup of the database.
- "iterdump": Dumps the database contents to a file in SQL format.
The function reads lines of input from the standard input until it encounters a newline character. It then processes the input line by parsing it as a JSON array and executing the corresponding command
"""
while True:
line: list[str] = []
while True:
lines = sys.stdin.read(1)
line.append(lines)
if lines == "\n":
break
a = "".join([str(item) for item in line])
nodesdtin = json.loads(a)
if nodesdtin[0] == "newConnection":
sys.stdout.write(f"{json.dumps(newConnection(nodesdtin[1], nodesdtin[2], nodesdtin[3]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "executeQuery":
sys.stdout.write(f"{json.dumps(executeQuery(nodesdtin[1], nodesdtin[2]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "fetchall":
sys.stdout.write(f"{json.dumps(fetchall(nodesdtin[1], nodesdtin[2]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "fetchmany":
sys.stdout.write(f"{json.dumps(fetchmany(nodesdtin[1], nodesdtin[2], nodesdtin[3]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "fetchone":
sys.stdout.write(f"{json.dumps(fetchone(nodesdtin[1], nodesdtin[2]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "executeMany":
sys.stdout.write(f"{json.dumps(executeMany(nodesdtin[1], nodesdtin[2]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "executeScript":
sys.stdout.write(f"{json.dumps(executeScript(nodesdtin[1]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "load_extension":
sys.stdout.write(f"{json.dumps(load_extension(nodesdtin[1]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "backup":
sys.stdout.write(f"{json.dumps(backup(nodesdtin[1], nodesdtin[2], nodesdtin[3], nodesdtin[4]))}EOF")
sys.stdout.flush()
elif nodesdtin[0] == "iterdump":
sys.stdout.write(f"{json.dumps(iterdump(nodesdtin[1], nodesdtin[2]))}EOF")
sys.stdout.flush()
else:
sys.stdout.write(f"{json.dumps('Error: Invalid command')}EOF")
sys.stdout.flush()
main()