-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlite.py
More file actions
290 lines (236 loc) · 8.24 KB
/
lite.py
File metadata and controls
290 lines (236 loc) · 8.24 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
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
)
import re
import threading
from lite_oil import getConnection
from lite_oil import shutdown as shutdown
from schema import ColumnInfo
import util
if TYPE_CHECKING:
from psycopg2 import connection # type: ignore[attr-defined]
JSONable = Dict[str, Any]
__threadData = threading.local()
__columnInfo: Dict[str, List[ColumnInfo]] = {}
__tableNames: Dict[str, str] = {
"TagBase": "tag",
"Genre": "tag",
"Tag": "tag",
"Fandom": "tag",
"Character": "tag",
"FicTagBase": "fic_tag",
"FicGenre": "fic_tag",
"FicTag": "fic_tag",
"FicFandom": "fic_tag",
"FicCharacter": "fic_tag",
}
autocommit: bool = True
logQueries: bool = False
def getTableName(clsName: str) -> str:
global __tableNames
if clsName in __tableNames:
return __tableNames[clsName]
name = clsName
if name.startswith("FFN"):
name = "ffn" + name[3:]
name = name.replace("FFN", "_ffn")
name = name[0].lower() + re.sub("([A-Z])", "_\\1", name[1:]).lower()
__tableNames[clsName] = name
return name
def transformQueryData(data: Sequence[Any]) -> Sequence[Any]:
ld = list(data)
nd = []
for d in ld:
if isinstance(d, bytes):
nd.append(f"{{bytes: len:{len(d)}}}")
else:
nd.append(d)
return tuple(nd)
def logQuery(kind: str, table: str, sql: str, data: Sequence[Any]) -> None:
if not logQueries:
return
sql = sql.replace("\t", " ").replace("\n", " ")
while sql.find(" ") >= 0:
sql = sql.replace(" ", " ")
data = transformQueryData(data)
util.logMessage(f"{kind}: sql={sql} data={data}")
T = TypeVar("T", bound="StoreType")
class StoreType:
subDB: str = "meta"
columns: List[ColumnInfo]
pkColumns: List[ColumnInfo]
regColumns: List[ColumnInfo]
@classmethod
def getConnection(cls) -> "connection":
return getConnection(cls.subDB)
@classmethod
def getNonGeneratedColumns(cls) -> List[ColumnInfo]:
return [col for col in cls.columns if col.type.lower().find("serial") < 0]
@classmethod
def getTableName(cls) -> str:
return getTableName(cls.__name__)
@classmethod
def fromRow(cls: Type[T], row: Sequence[Any]) -> T:
raise NotImplementedError()
def toJSONable(self) -> JSONable:
raise NotImplementedError()
@classmethod
def get(cls: Type[T], pkValues: Sequence[Any]) -> Optional[T]:
table = cls.getTableName()
conn = cls.getConnection()
sql = f"SELECT * FROM {table} WHERE "
whereParts = [f"{pk.name} = %s" for pk in cls.pkColumns]
if len(whereParts) == 0:
raise Exception(f"table {table} has no primary key")
sql += " AND ".join(whereParts)
with conn.cursor() as curs:
logQuery("get", table, sql, pkValues)
curs.execute(sql, pkValues)
r = curs.fetchone()
if r is None:
return None
return cls.fromRow(r)
@classmethod
def lookup(cls: Type[T], pkValues: Sequence[Any]) -> T:
obj = cls.get(pkValues)
if obj is not None:
return obj
raise Exception(f"unable to lookup {cls.__name__}: {pkValues}")
@staticmethod
def buildWhere(
whereData: Optional[Dict[str, Any]] = None
) -> Tuple[Sequence[Any], str]:
operators = {">", "<", ">=", "<=", "!=", "==", "is"}
data: List[Any] = []
whereSql = ""
if whereData is not None and len(whereData) > 0:
whereParts: List[str] = []
for col in whereData:
bit = whereData[col]
if (
isinstance(bit, tuple)
and len(bit) == 2
and isinstance(bit[0], str)
and bit[0] in operators
):
whereParts += [f"{col} {bit[0]} %s"]
data += [bit[1]]
else:
whereParts += [f"{col} = %s"]
data += [whereData[col]]
whereSql = " WHERE " + " AND ".join(whereParts)
return (tuple(data), whereSql)
@classmethod
def select(
cls: Type[T],
whereData: Optional[Dict[str, Any]] = None,
orderBy: Optional[str] = None,
) -> List[T]:
table = cls.getTableName()
conn = cls.getConnection()
data, whereSql = StoreType.buildWhere(whereData)
sql = f"SELECT * FROM {table} {whereSql}"
if orderBy is not None:
sql += " ORDER BY " + orderBy
with conn.cursor() as curs:
logQuery("select", table, sql, data)
curs.execute(sql, data)
res = [cls.fromRow(r) for r in curs.fetchall()]
return res
@classmethod
def count(cls, whereData: Optional[Dict[str, Any]] = None) -> int:
table = cls.getTableName()
conn = cls.getConnection()
data, whereSql = StoreType.buildWhere(whereData)
sql = f"SELECT COUNT(1) FROM {table} {whereSql}"
print(sql)
with conn.cursor() as curs:
logQuery("count", table, sql, data)
curs.execute(sql, data)
r = curs.fetchone()
assert r is not None
return int(r[0])
def __getParts(self, which: List[str]) -> Sequence[Any]:
return tuple([self.__dict__[piece] for piece in which])
def toTuple(self) -> Sequence[Any]:
cols = type(self).columns
return self.__getParts([col.name for col in cols])
def toInsertTuple(self) -> Sequence[Any]:
cols = type(self).getNonGeneratedColumns()
return self.__getParts([col.name for col in cols])
def getPKTuple(self) -> Sequence[Any]:
return self.__getParts([col.name for col in type(self).pkColumns])
def getNonPKTuple(self) -> Sequence[Any]:
return self.__getParts([col.name for col in type(self).regColumns])
def insert(self) -> None:
table = type(self).getTableName()
cols = type(self).getNonGeneratedColumns()
sql = "INSERT INTO {}({}) VALUES({})".format(
table, ", ".join([c.name for c in cols]), ", ".join(["%s"] * len(cols))
)
data = self.toInsertTuple()
conn = type(self).getConnection()
with conn.cursor() as curs:
try:
logQuery("insert", table, sql, data)
curs.execute(sql, data)
except:
util.logMessage(f"failed to insert: {sql}: {data}", "lite.log")
raise
global autocommit
if autocommit:
conn.commit()
def update(self) -> None:
table = type(self).getTableName()
pkCols = type(self).pkColumns
nkCols = type(self).regColumns
sql = f"UPDATE {table} "
sql += " SET " + (", ".join([col.name + " = %s" for col in nkCols]))
sql += " WHERE " + (" AND ".join([col.name + " = %s" for col in pkCols]))
data = tuple(list(self.getNonPKTuple()) + list(self.getPKTuple()))
conn = type(self).getConnection()
with conn.cursor() as curs:
logQuery("update", table, sql, data)
curs.execute(sql, data)
global autocommit
if autocommit:
conn.commit()
def upsert(self) -> None:
me = type(self).get(self.getPKTuple())
if me is None:
self.insert()
else:
self.update()
@classmethod
def new(cls: Type[T]) -> T:
obj = cls()
for col in cls.columns:
# don't overwrite values set by the constructor
if col.name in obj.__dict__:
continue
obj.__setattr__(col.name, col.dflt_value)
return obj
@classmethod
def create(cls: Type[T], pkValues: Sequence[Any]) -> T:
obj = cls.new()
for i in range(len(cls.pkColumns)):
obj.__setattr__(cls.pkColumns[i].name, pkValues[i])
obj.insert()
res = cls.get(pkValues)
if res is None:
raise Exception("unable to create")
return res
@classmethod
def getOrCreate(cls: Type[T], pkValues: Sequence[Any]) -> T:
obj = cls.get(pkValues)
if obj is not None:
return obj
return cls.create(pkValues)