-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathsql.py
More file actions
405 lines (317 loc) · 11 KB
/
sql.py
File metadata and controls
405 lines (317 loc) · 11 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
from collections.abc import Iterable, Mapping
import atexit
import datetime
import time
import os
import re
import warnings
import alembic.ddl
import sqlalchemy
DATABASE_NAME = os.environ.get(
"SCRAPERWIKI_DATABASE_NAME", "sqlite:///scraperwiki.sqlite"
)
DATABASE_TIMEOUT = float(os.environ.get("SCRAPERWIKI_DATABASE_TIMEOUT", 300))
SECONDS_BETWEEN_COMMIT = 2
# The scraperwiki.sqlite.SqliteError exception
SqliteError = sqlalchemy.exc.SQLAlchemyError
class Blob(bytes):
"""
Represents a blob as a string.
"""
pass
PYTHON_SQLITE_TYPE_MAP = {
str: sqlalchemy.types.Text,
int: sqlalchemy.types.BigInteger,
bool: sqlalchemy.types.Boolean,
float: sqlalchemy.types.Float,
datetime.date: sqlalchemy.types.Date,
datetime.datetime: sqlalchemy.types.DateTime,
bytes: sqlalchemy.types.LargeBinary,
Blob: sqlalchemy.types.LargeBinary,
}
class _State:
"""
This class maintains global state relating to the database such as
connection. It does not form part of the public interface.
"""
db_path = DATABASE_NAME
engine = None
_connection = None
_transaction = None
metadata = None
table = None
# Whether or not we need to create the table. It's set by
# _set_table(); it's left unassigned here to catch
# accidental uses of it.
# table_pending = None
vars_table_name = "swvariables"
last_commit = None
echo = False
@classmethod
def connection(cls):
if cls._connection is None:
create = sqlalchemy.create_engine
cls.engine = create(
cls.db_path, echo=cls.echo, connect_args={"timeout": DATABASE_TIMEOUT}
)
cls._connection = cls.engine.connect()
cls.new_transaction()
if cls.table is None:
cls.reflect_metadata()
cls.table = sqlalchemy.Table(
"swdata", _State.metadata, extend_existing=True
)
if cls._transaction is None:
cls.new_transaction()
return cls._connection
@classmethod
def new_transaction(cls):
cls.last_commit = time.time()
if cls._transaction is not None:
cls._transaction.commit()
cls._transaction = cls._connection.begin()
@classmethod
def reflect_metadata(cls):
if cls.metadata is None:
cls.metadata = sqlalchemy.MetaData()
cls.metadata.reflect(bind=cls.engine)
@classmethod
def check_last_committed(cls):
if time.time() - cls.last_commit > SECONDS_BETWEEN_COMMIT:
cls.new_transaction()
class Transaction:
"""
This context manager must be used when other services need
to connect to the database.
"""
def __enter__(self):
_State.connection()
_State.new_transaction()
def __exit__(self, *args):
_State._transaction.commit()
_State._transaction = None
@atexit.register
def commit_transactions():
"""
Ensure any outstanding transactions are committed on exit
"""
if _State is not None and _State._transaction is not None:
_State._transaction.commit()
_State._transaction = None
def execute(query, data=None):
"""
Execute an arbitrary SQL query given by query, returning any
results as a list of OrderedDicts. A list of values can be supplied as an,
additional argument, which will be substituted into question marks in the
query.
"""
connection = _State.connection()
_State.new_transaction()
if data is None:
data = []
if isinstance(data, list):
data = tuple(data)
elif not isinstance(data, (tuple, dict)):
data = (data,)
result = connection.exec_driver_sql(query, data)
_State.table = None
_State.metadata = None
try:
del _State.table_pending
except AttributeError:
pass
if not result.returns_rows:
return {"data": [], "keys": []}
return {"data": result.fetchall(), "keys": list(result.keys())}
def select(query, data=None):
"""
Perform a sql select statement with the given query (without 'select') and
return any results as a list of OrderedDicts.
"""
connection = _State.connection()
_State.new_transaction()
if data is None:
data = []
if isinstance(data, list):
data = tuple(data)
elif not isinstance(data, (tuple, dict)):
data = (data,)
result = connection.exec_driver_sql("select " + query, data)
rows = []
for row in result:
rows.append(dict(row._mapping))
return rows
def save(unique_keys, data, table_name="swdata"):
"""
Save the given data to the table specified by `table_name`
(which defaults to 'swdata'). The data must be a mapping
or an iterable of mappings. Unique keys is a list of keys that exist
for all rows and for which a unique index will be created.
"""
_set_table(table_name)
connection = _State.connection()
if isinstance(data, Mapping):
# Is a single datum
data = [data]
elif not isinstance(data, Iterable):
raise TypeError("Data must be a single mapping or an iterable of mappings")
insert = sqlalchemy.insert(_State.table).prefix_with("OR REPLACE")
for row in data:
if not isinstance(row, Mapping):
raise TypeError(
"Elements of data must be mappings, got {}".format(type(row))
)
fit_row(connection, row, unique_keys)
connection.execute(insert.values(row))
_State.check_last_committed()
def _set_table(table_name):
"""
Specify the table to work on.
"""
_State.connection()
_State.reflect_metadata()
_State.table = sqlalchemy.Table(table_name, _State.metadata, extend_existing=True)
if list(_State.table.columns.keys()) == []:
_State.table_pending = True
else:
_State.table_pending = False
def show_tables():
"""
Return the names of the tables currently in the database.
"""
_State.connection()
_State.reflect_metadata()
response = select('name, sql from sqlite_master where type="table"')
return {row["name"]: row["sql"] for row in response}
def save_var(name, value):
"""
Save a variable to the table specified by _State.vars_table_name. Key is
the name of the variable, and value is the value.
"""
connection = _State.connection()
_State.reflect_metadata()
vars_table = sqlalchemy.Table(
_State.vars_table_name,
_State.metadata,
sqlalchemy.Column("name", sqlalchemy.types.Text, primary_key=True),
sqlalchemy.Column("value_blob", sqlalchemy.types.LargeBinary),
sqlalchemy.Column("type", sqlalchemy.types.Text),
keep_existing=True,
)
vars_table.create(connection, checkfirst=True)
column_type = get_column_type(value)
if column_type == sqlalchemy.types.LargeBinary:
value_blob = value
else:
value_blob = str(value).encode("utf-8")
values = dict(
name=name,
value_blob=value_blob,
# value_blob=Blob(value),
type=column_type.__visit_name__.lower(),
)
stmt = sqlalchemy.insert(vars_table).prefix_with("OR REPLACE").values(**values)
connection.execute(stmt)
_State.new_transaction()
def get_var(name, default=None):
"""
Returns the variable with the provided key from the
table specified by _State.vars_table_name.
"""
alchemytypes = {
"text": lambda x: x.decode("utf-8"),
"big_integer": lambda x: int(x),
"date": lambda x: x.decode("utf-8"),
"datetime": lambda x: x.decode("utf-8"),
"float": lambda x: float(x),
"large_binary": lambda x: x,
"boolean": lambda x: x == b"True",
}
connection = _State.connection()
_State.new_transaction()
if _State.vars_table_name not in list(_State.metadata.tables.keys()):
return None
table = sqlalchemy.Table(_State.vars_table_name, _State.metadata)
s = sqlalchemy.select(table.c.value_blob, table.c.type)
s = s.where(table.c.name == name)
result = connection.execute(s).fetchone()
if not result:
return None
return alchemytypes[result[1]](result[0])
# This is to do the variable type conversion through the SQL engine
execute = connection.execute
execute(f"CREATE TEMPORARY TABLE _sw_tmp ('value' {result.type})")
execute("INSERT INTO _sw_tmp VALUES (:value)", value=result.value_blob)
var = execute("SELECT value FROM _sw_tmp").fetchone().value
execute("DROP TABLE _sw_tmp")
return var.decode("utf-8")
def create_index(column_names, unique=False):
"""
Create a new index of the columns in column_names, where column_names is
a list of strings. If unique is True, it will be a
unique index.
"""
_State.reflect_metadata()
table_name = _State.table.name
table = _State.table
index_name = re.sub(r"[^a-zA-Z0-9]", "", table_name) + "_"
index_name += "_".join(re.sub(r"[^a-zA-Z0-9]", "", x) for x in column_names)
if unique:
index_name += "_unique"
columns = []
for column_name in column_names:
columns.append(table.columns[column_name])
current_indices = [x.name for x in table.indexes]
index = sqlalchemy.schema.Index(index_name, *columns, unique=unique)
if index.name not in current_indices:
index.create(bind=_State.engine)
def fit_row(connection, row, unique_keys):
"""
Takes a row and checks to make sure it fits in the columns of the
current table. If it does not fit, adds the required columns.
"""
new_columns = []
for column_name, column_value in list(row.items()):
new_column = sqlalchemy.Column(column_name, get_column_type(column_value))
if column_name not in list(_State.table.columns.keys()):
new_columns.append(new_column)
_State.table.append_column(new_column)
if _State.table_pending:
create_table(unique_keys)
return
for new_column in new_columns:
add_column(connection, new_column)
def create_table(unique_keys):
"""
Save the table currently waiting to be created.
"""
_State.new_transaction()
_State.table.create(bind=_State.engine, checkfirst=True)
if unique_keys != []:
create_index(unique_keys, unique=True)
_State.table_pending = False
_State.reflect_metadata()
def add_column(connection, column):
"""
Add a column to the current table.
"""
stmt = alembic.ddl.base.AddColumn(_State.table.name, column)
connection.execute(stmt)
_State.reflect_metadata()
def get_column_type(column_value):
"""
Return the appropriate SQL column type for the given value.
"""
return PYTHON_SQLITE_TYPE_MAP.get(type(column_value), sqlalchemy.types.Text)
def commit():
warnings.warn("scraperwiki.sql.commit is now a no-op")
def drop():
"""
Drop the current table if it exists
"""
# Ensure the connection is up
_State.connection()
_State.table.drop(checkfirst=True)
_State.metadata.remove(_State.table)
_State.table = None
_State.new_transaction()