forked from digitalocean/sample-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlutil.py
More file actions
295 lines (236 loc) · 8.97 KB
/
sqlutil.py
File metadata and controls
295 lines (236 loc) · 8.97 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
import os
import sqlite3
import tempfile
from xml.etree import ElementTree
from langchain_community.utilities import SQLDatabase
from util import (
get_node_name, get_tag_name, prefix_field_name,
VALUE, CHILDREN, LABEL
)
class SQLStmt:
columns: list;
values: list;
def __init__(self):
self.columns = []
self.values = []
class SQLRef:
statements: list
ref: str
ref_id: int
def __init__(self, ref: str, ref_id: int):
self.statements = []
self.ref = ref
self.ref_id = ref_id
class SQLGlobals:
db: object
checked: list
ids: dict
ignore_ns: bool
full_name: bool
def __init__(self,
db: object,
checked: list,
ignore_ns: bool,
full_name: bool,
ids: dict = dict()):
if db and db.startswith('file:///'):
self.db = open(db[8:], "w")
elif db.startswith('sqlite:///'):
self.db = sqlite3.connect(db[10:])
else:
self.db = SQLDatabase.from_uri(db)
print("DB dialect: " + self.db.dialect)
print('Use Database: ' + str(type(self.db)))
self.checked = checked
self.ignore_ns = ignore_ns
self.full_name = full_name
self.ids = ids
def run(self,
stmt: str):
if isinstance(self.db, sqlite3.Connection):
try:
self.db.execute(stmt)
except:
print(f'Last SQL: {stmt}')
raise
elif isinstance(self.db, SQLDatabase):
try:
self.db.run(stmt)
except:
print(f'Last SQL: {stmt}')
raise
else:
# print(stmt)
self.db.write(stmt)
self.db.write('\n')
def begin(self):
if isinstance(self.db, sqlite3.Connection):
try:
self.db.execute('PRAGMA synchronous = OFF;')
self.db.execute('BEGIN TRANSACTION;')
except:
print(f'Last SQL: BEGIN TRANSACTION')
raise
elif isinstance(self.db, SQLDatabase):
pass
else:
self.db.write('BEGIN TRANSACTION;\n')
def end(self):
if isinstance(self.db, sqlite3.Connection):
try:
self.db.execute('END TRANSACTION;')
except:
print(f'Last SQL: END TRANSACTION')
raise
elif isinstance(self.db, SQLDatabase):
pass
# try:
# self.db.run('END TRANSACTION;')
# except:
# print(f'Last SQL: commit')
# raise
else:
self.db.write('END TRANSACTION;\n')
self.db.flush()
class CreateSQL:
@staticmethod
def get_clean_name(node: dict) -> str:
return node[LABEL].split(': ')[0]
@staticmethod
def get_table_name(node: dict,
full_name: bool) -> {str, str}:
tablename_id = CreateSQL.get_clean_name(node)
tablename = node[VALUE] if full_name else tablename_id
return tablename, tablename_id
@staticmethod
def create_sql_fields(node: dict,
columns: list,
checked: list,
statements: list,
prefix: str,
full_name: bool,
foreign_key: str):
if CHILDREN in node:
for child in node[CHILDREN]:
field = prefix_field_name(CreateSQL.get_clean_name(child), prefix)
columns.append(field)
if child[VALUE] in checked:
CreateSQL.create_sql_table(child, checked, statements, full_name, foreign_key)
else:
CreateSQL.create_sql_fields(child, columns, checked, statements, field + '.',
full_name, foreign_key)
@staticmethod
def create_sql_table(node: dict,
checked: list,
statements: list,
full_name: bool,
foreign_key: str = None):
tablename, tablename_id = CreateSQL.get_table_name(node, full_name)
statements.append(f'DROP TABLE IF EXISTS "{tablename}";')
index = len(statements)
sql = f'CREATE TABLE "{tablename}" ('
sql += '\n\t"' + tablename_id + '_ID" INTEGER PRIMARY KEY' # AUTOINCREMENT
my_foreign_key = f'\t"REFERENCE_ID" INTEGER,\n\tFOREIGN KEY ("REFERENCE_ID") REFERENCES "{tablename}"("{tablename_id}_ID")'
columns = []
CreateSQL.create_sql_fields(node, columns, checked, statements, "", full_name,
my_foreign_key)
if len(columns) > 0:
for col in columns:
sql += f',\n\t"{col}" TEXT'
else:
sql += ',\n\t"VALUE" TEXT'
if foreign_key:
sql += ',\n' + foreign_key
sql += "\n);\n"
statements.insert(index, sql)
@staticmethod
def search_checked_nodes(node: dict,
checked: list,
statements: list,
full_name: bool):
value = node[VALUE]
if value in checked:
CreateSQL.create_sql_table(node, checked, statements, full_name)
elif CHILDREN in node:
for child in node[CHILDREN]:
CreateSQL.search_checked_nodes(child, checked, statements, full_name)
class InsertSQL:
@staticmethod
def insert_sql_fields(node: ElementTree,
parent_path: str,
prefix: str,
db: SQLGlobals,
stmt: SQLStmt,
ref: SQLRef):
for attrib, text in node.items():
value = None if text is None else text.strip()
if value:
name, subpath = get_tag_name(attrib, parent_path, db.ignore_ns)
field = prefix_field_name(name, prefix)
stmt.columns.append(field)
stmt.values.append(value)
for child in node:
name, subpath = get_node_name(child, parent_path, db.ignore_ns)
field = prefix_field_name(name, prefix)
value = None if child.text is None else child.text.strip()
if value:
if len(value) > 0:
stmt.columns.append(field)
stmt.values.append(value)
if subpath in db.checked:
InsertSQL.insert_sql_table(child, name, subpath, db, ref)
else:
InsertSQL.insert_sql_fields(child, subpath, field + '.', db, stmt, ref)
@staticmethod
def insert_sql_table(node: ElementTree,
tablename: str,
tablepath: str,
db: SQLGlobals,
parent_ref: SQLRef = None):
# print(f'Check {parent_path}')
tablename_id = tablename
if db.full_name:
tablename = tablepath
ref_name = f'"{tablename_id}_ID"'
ref_id = db.ids.get(ref_name, 0) + 1
db.ids[ref_name] = ref_id
ref = SQLRef(ref_name, ref_id)
stmt = SQLStmt()
InsertSQL.insert_sql_fields(node, tablepath, "", db, stmt, ref)
sql = f'INSERT INTO "{tablename}" ({ref_name}'
if parent_ref:
sql += ',REFERENCE_ID'# + parent_ref.ref
for col in stmt.columns:
sql += f',"{col}"'
value = None if node.text is None else node.text.strip()
if len(stmt.columns) < 1 and value:
sql += ',"VALUE"'
sql += f') VALUES ({ref_id}'
if parent_ref:
sql += f',{parent_ref.ref_id}'
for v in stmt.values:
val = v.replace('"', '"')
sql += f',"{val}"'
if len(stmt.values) < 1 and value:
v = value.replace('"', '"')
sql += f',"{v}"'
sql += ');'
# We must insert the main table before the tables that reference to it
if parent_ref:
parent_ref.statements.append(sql)
parent_ref.statements.extend(ref.statements)
else:
db.run(sql)
for stmt in ref.statements:
db.run(stmt)
@staticmethod
def search_checked_nodes(node: ElementTree,
parent_path: str,
db: SQLGlobals,
parent_ref: SQLRef = None):
name, subpath = get_node_name(node, parent_path, db.ignore_ns)
if subpath in db.checked:
InsertSQL.insert_sql_table(node, name, subpath, db, parent_ref)
else:
for child in node:
InsertSQL.search_checked_nodes(child, subpath, db, parent_ref)