-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
384 lines (279 loc) · 11.6 KB
/
__init__.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
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
### CREDITS ##########################################################################################
# Copyright (c) 2007 Tom De Smedt.
# See LICENSE.txt for details.
__author__ = "Tom De Smedt"
__version__ = "1.9.2"
__copyright__ = "Copyright (c) 2007 Tom De Smedt"
__license__ = "MIT"
import sys, os
sys.path.append(os.path.dirname(__file__))
from .pysqlite2 import dbapi2 as sqlite
### DATABASE #########################################################################################
class Database:
def __init__(self):
self._name = None
self._tables = [] # All table names.
self._indices = []
self._commit = 0
self._i = 0
def connect(self, name):
"""Generic database.
Opens the SQLite database with the given name.
The .db extension is automatically appended to the name.
For each table in the database an attribute is created,
and assigned a Table object.
You can do: database.table or database[table].
"""
self._name = name.rstrip(".db")
self._con = sqlite.connect(self._name + ".db")
self._cur = self._con.cursor()
self._tables = []
self._cur.execute("select name from sqlite_master where type='table'")
for r in self._cur: self._tables.append(r[0])
self._indices = []
self._cur.execute("select name from sqlite_master where type='index'")
for r in self._cur: self._indices.append(r[0])
for t in self._tables:
self._cur.execute("pragma table_info("+t+")")
fields = []
key = ""
for r in self._cur:
fields.append(r[1])
if r[2] == "integer": key = r[1]
setattr(self, t, Table(self, t, key, fields))
def create(self, name, overwrite=True):
"""Creates an SQLite database file.
Creates an SQLite database with the given name.
The .box file extension is added automatically.
Overwrites any existing database by default.
"""
self._name = name.rstrip(".db")
from os import unlink
if overwrite:
try: unlink(self._name + ".db")
except: pass
self._con = sqlite.connect(self._name + ".db")
self._cur = self._con.cursor()
def __len__(self):
return len(self._tables)
def __getitem__(self, name):
if isinstance(name, int): name = self._tables[name]
return getattr(self, name)
# Deprecated, use for-loop and Database[table_name] instead.
def tables(self): return self._tables
table = __getitem__
def create_table(self, name, fields=[], key="id"):
"""Creates a new table.
Creates a table with the given name,
containing the list of given fields.
Since SQLite uses manifest typing, no data type need be supplied.
The primary key is "id" by default,
an integer that can be set or otherwise autoincrements.
"""
for f in fields:
if f == key: fields.remove(key)
sql = "create table "+name+" "
sql += "("+key+" integer primary key"
for f in fields: sql += ", "+f+" varchar(255)"
sql += ")"
self._cur.execute(sql)
self._con.commit()
self.index(name, key, unique=True)
self.connect(self._name)
# Deprecated, use create_table() instead.
append = create_table
def create_index(self, table, field, unique=False, ascending=True):
"""Creates a table index.
Creates an index on the given table,
on the given field with unique values enforced or not,
in ascending or descending order.
"""
if unique: u = "unique "
else: u = ""
if ascending: a = "asc"
else: a = "desc"
sql = "create "+u+"index index_"+table+"_"+field+" "
sql += "on "+table+"("+field+" "+a+")"
self._cur.execute(sql)
self._con.commit()
# Deprecated, use create_index() instead.
index = create_index
def commit(self, each=0):
"""Sets the commit frequency.
Modifications to the database,
e.g. row insertions are commited in batch,
specified by the given number.
A number that is reasonably high allows for faster transactions.
Commits anything still pending.
"""
self._commit = each
self._con.commit()
def close(self):
"""Commits any pending transactions and closes the database.
"""
self._con.commit()
self._cur.close()
self._con.close()
def sql(self, sql):
""" Executes a raw SQL statement on the database.
"""
self._cur.execute(sql)
if sql.lower().find("select") >= 0:
matches = []
for r in self._cur: matches.append(r)
return matches
def dump(self, ext=".xml"):
""" Dumps the data in the tables into another format (like XML).
"""
self._con.commit()
if ext == ".xml":
return self._dump_xml()
def _dump_xml(self):
data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
data += "<database name=\""+self._name+"\">\n"
for name in self._indices:
s = name.split("_")
data += "<index name=\""+name+"\" table=\""+s[0]+"\" field=\""+s[1]+"\">\n"
for table in self:
data += "<table name=\""+table._name+"\" key=\""+table._key+"\">\n"
for row in table.all():
data += "\t<row id=\""+str(row[table._fields.index(table._key)])+"\">\n"
i = 0
for field in table._fields:
r = str(row[i])
if row[i] == None: r = ""
data += "\t\t<"+field+">"+r+"</"+field+">\n"
i += 1
data += "\t</row>\n"
data += "</table>\n"
data += "</database>"
f = open(self._name+".xml", "w")
f.write(data)
f.close()
return data
# Deprecated, use dump() instead.
xml = dump
### TABLE ############################################################################################
class Table:
def __init__(self, db, name, key, fields):
"""Generic table.
Constructs a table with the given name, primary key and columns.
Each of the column names becomes the name of a table method
that fetches rows from the table.
For example, a table with an id field has the following method:
table.id(query, operator="=")
"""
self._db = db
self._name = name
self._key = key
self._fields = fields
for f in self._fields:
import new
im = lambda t, q, operator="=", fields="*", _field=f: t.find(q, operator, fields, _field)
setattr(self, f, new.instancemethod(im, self, None))
def _get_name(self): return self._name
name = property(_get_name)
def __len__(self):
""" The row count of the table. This should be optimized.
"""
sql = "select "+self._key+" from "+self._name
self._db._cur.execute(sql)
i = 0
for r in self._db._cur: i += 1
return i
def find(self, q, operator="=", fields="*", key=None):
"""A simple SQL SELECT query.
Retrieves all rows from the table
where the given query value is found in the given column (primary key if None).
A different comparison operator (e.g. >, <, like) can be set.
The wildcard character is * and automatically sets the operator to "like".
Optionally, the fields argument can be a list of column names to select.
Returns a list of row tuples containing fields.
"""
if key == None: key = self._key
if fields != "*": fields = ", ".join(fields)
try: q = str(q)
except: pass
if q != "*" and (q[0] == "*" or q[-1] == "*"):
if q[0] == "*": q = "%"+q.lstrip("*")
if q[-1] == "*": q = q.rstrip("*")+"%"
operator = "like"
if q != "*":
sql = "select "+fields+" from "+self._name+" where "+key+" "+operator+" ?"
self._db._cur.execute(sql, (q,))
else:
sql = "select "+fields+" from "+self._name
self._db._cur.execute(sql)
# You need to watch out here when bad unicode data
# has entered the database: pysqlite will throw an OperationalError.
# http://lists.initd.org/pipermail/pysqlite/2006-April/000488.html
matches = []
for r in self._db._cur: matches.append(r)
return matches
def all(self):
"""Returns all the rows in the table.
"""
return self.find("*")
def fields(self):
"""Returns the column names.
Returns the name of each column in the database,
in the same order row fields are returned from find().
"""
return self._fields
def append(self, *args, **kw):
"""Adds a new row to a table.
Adds a row to the given table.
The column names and their corresponding values
must either be supplied as a dictionary of {fields:values},
or a series of keyword arguments of field=value style.
"""
if args and kw:
return
if args and type(args[0]) == dict:
fields = [k for k in args[0]]
v = [args[0][k] for k in args[0]]
if kw:
fields = [k for k in kw]
v = [kw[k] for k in kw]
q = ", ".join(["?" for x in fields])
sql = "insert into "+self._name+" ("+", ".join(fields)+") "
sql += "values ("+q+")"
self._db._cur.execute(sql, v)
self._db._i += 1
if self._db._i >= self._db._commit:
self._db._i = 0
self._db._con.commit()
def edit(self, id, *args, **kw):
""" Edits the row with given id.
"""
if args and kw:
return
if args and type(args[0]) == dict:
fields = [k for k in args[0]]
v = [args[0][k] for k in args[0]]
if kw:
fields = [k for k in kw]
v = [kw[k] for k in kw]
sql = "update "+self._name+" set "+"=?, ".join(fields)+"=? where "+self._key+"="+str(id)
self._db._cur.execute(sql, v)
self._db._i += 1
if self._db._i >= self._db._commit:
self._db._i = 0
self._db._con.commit()
def remove(self, id, operator="=", key=None):
""" Deletes the row with given id.
"""
if key == None: key = self._key
try: id = str(id)
except: pass
sql = "delete from "+self._name+" where "+key+" "+operator+" ?"
self._db._cur.execute(sql, (id,))
######################################################################################################
def create(name, overwrite=True):
db = Database()
db.create(name, overwrite)
return db
def connect(name):
db = Database()
db.connect(name)
return db