@@ -21,8 +21,7 @@ def __init__(self, db_path):
21
21
"""
22
22
self ._logger = logging .getLogger (self .__class__ .__name__ )
23
23
24
- # Open or create the database
25
- self ._database = create_database (db_path )
24
+ self .db_path = db_path
26
25
27
26
# The transactor is required when you have methods decorated with the @transact decorator
28
27
# This field name must NOT be changed.
@@ -31,9 +30,16 @@ def __init__(self, db_path):
31
30
# Create a DeferredLock that should be used by callers to schedule their call.
32
31
self .db_lock = DeferredLock ()
33
32
34
- self ._version = 1
33
+ def initialize (self ):
34
+ """
35
+ Opens/creates the database and initializes the version.
36
+ """
37
+ # Open or create the database
38
+ self ._database = create_database (self .db_path )
39
+ self ._version = 0
35
40
self ._retrieve_version ()
36
41
42
+
37
43
def _retrieve_version (self ):
38
44
"""
39
45
Attempts to retrieve the current datbase version from the MyInfo table.
@@ -49,25 +55,18 @@ def on_error(failure):
49
55
self ._logger .exception (u"Failed to load database version: %s" , failure .getTraceback ())
50
56
51
57
# Schedule the query and add a callback and errback to the deferred.
52
- return self .fetch_one ( u"SELECT value FROM MyInfo WHERE entry == 'version'" ).addCallbacks (on_result , on_error )
58
+ return self .schedule_query ( self . fetch_one , u"SELECT value FROM MyInfo WHERE entry == 'version'" ).addCallbacks (on_result , on_error )
53
59
54
- def schedule_query (* args , ** kwargs ):
60
+ def schedule_query (self , callable , * args , ** kwargs ):
55
61
"""
56
62
Utility function to schedule a query to be executed using the db_lock.
57
- :param args : The arguments of which the first is self and the second the function to be run .
58
- Any additional arguments will be passed as the function arguments.
59
- :param kwargs: Keyword arguments that are passed to the function
63
+ :param callable : The database function that is to be executed .
64
+ :param args: Any additional arguments that will be passed as the callable's arguments.
65
+ :param kwargs: Keyword arguments that are passed to the callable function.
60
66
:return: A deferred that fires with the result of the query.
61
67
"""
62
- if len (args ) < 2 :
63
- if not args :
64
- raise TypeError ("run() takes at least 2 arguments, none given." )
65
- raise TypeError ("%s.run() takes at least 2 arguments, 1 given" % (
66
- args [0 ].__class__ .__name__ ,))
67
- self , f = args [:2 ]
68
- args = args [2 :]
69
68
70
- return self .db_lock .run (f , * args , ** kwargs )
69
+ return self .db_lock .run (callable , * args , ** kwargs )
71
70
72
71
@transact
73
72
def execute_query (self , query , arguments = None ):
@@ -109,18 +108,18 @@ def fetch_all(self, query, arguments=None):
109
108
return connection .execute (query , arguments ).get_all ()
110
109
111
110
@transact
112
- def insert (self , table_name , ** argv ):
111
+ def insert (self , table_name , ** kwargs ):
113
112
"""
114
113
Inserts data provided as keyword arguments into the table provided as an argument.
115
114
:param table_name: The name of the table the data has to be inserted into.
116
115
:param argv: A dictionary where the key represents the column and the value the value to be inserted.
117
116
:return: A deferred that fires when the data has been inserted.
118
117
"""
119
118
connection = Connection (self ._database )
120
- self ._insert (connection , table_name , ** argv )
119
+ self ._insert (connection , table_name , ** kwargs )
121
120
connection .close ()
122
121
123
- def _insert (self , connection , table_name , ** argv ):
122
+ def _insert (self , connection , table_name , ** kwargs ):
124
123
"""
125
124
Utility function to insert data which is not decorated by the @transact to prevent
126
125
a loop calling this function to create many threads.
@@ -130,14 +129,14 @@ def _insert(self, connection, table_name, **argv):
130
129
:param argv: A dictionary where the key represents the column and the value the value to be inserted.
131
130
:return: A deferred that fires when the data has been inserted.
132
131
"""
133
- if len (argv ) == 0 : return
134
- if len (argv ) == 1 :
135
- sql = u'INSERT INTO %s (%s) VALUES (?);' % (table_name , argv .keys ()[0 ])
132
+ if len (kwargs ) == 0 : raise ValueError ( "No keyword arguments supplied." )
133
+ if len (kwargs ) == 1 :
134
+ sql = u'INSERT INTO %s (%s) VALUES (?);' % (table_name , kwargs .keys ()[0 ])
136
135
else :
137
- questions = '?,' * len (argv )
138
- sql = u'INSERT INTO %s %s VALUES (%s);' % (table_name , tuple (argv .keys ()), questions [: - 1 ] )
136
+ questions = ',' . join (( '?' ,) * len (kwargs ) )
137
+ sql = u'INSERT INTO %s %s VALUES (%s);' % (table_name , tuple (kwargs .keys ()), questions )
139
138
140
- connection .execute (sql , argv .values (), noresult = True )
139
+ connection .execute (sql , kwargs .values (), noresult = True )
141
140
142
141
@transact
143
142
def insert_many (self , table_name , arg_list ):
@@ -155,7 +154,7 @@ def insert_many(self, table_name, arg_list):
155
154
156
155
connection .close ()
157
156
158
- def delete (self , table_name , ** argv ):
157
+ def delete (self , table_name , ** kwargs ):
159
158
"""
160
159
Utility function to delete from the database.
161
160
:param table_name: the table name to delete from
@@ -167,7 +166,7 @@ def delete(self, table_name, **argv):
167
166
"""
168
167
sql = u'DELETE FROM %s WHERE ' % table_name
169
168
arg = []
170
- for k , v in argv .iteritems ():
169
+ for k , v in kwargs .iteritems ():
171
170
if isinstance (v , tuple ):
172
171
sql += u'%s %s ? AND ' % (k , v [0 ])
173
172
arg .append (v [1 ])
@@ -177,7 +176,7 @@ def delete(self, table_name, **argv):
177
176
sql = sql [:- 5 ] # Remove the last AND
178
177
return self .execute_query (sql , arg )
179
178
180
- def num_rows (self , table_name ):
179
+ def count (self , table_name ):
181
180
"""
182
181
Utility function to get the number of rows of a table.
183
182
:param table_name: The table name
0 commit comments