-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend.py
More file actions
326 lines (260 loc) · 10.7 KB
/
backend.py
File metadata and controls
326 lines (260 loc) · 10.7 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
# -*- coding: utf-8 -*-
'''Response Project - Backend Manager and Adapters'''
# Copyright (C) 2009-2010 John Feuerstein <john@feurix.com>
#
# This file is part of the response project.
#
# Response is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Response is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from globals import __author__, __copyright__, __license__, __version__
import exception
from datetime import datetime
from logger import getModuleLog
log = getModuleLog(__name__)
class DatabaseBackend(object):
'''Base class of database backend adapters with all public functions
used by the backend manager.
'''
# Format of the result set doesn't matter
CURSOR_DEFAULT = None
# If connect() is called with this cursor class the result set must
# return rows as dictionaries, with the column names as keys!
CURSOR_DICT = None
def connect(self, unicode, cursorclass):
raise exception.NotImplemented
def open_cursor(self, connection):
raise exception.NotImplemented
def close_cursor(self, cursor):
raise exception.NotImplemented
def commit(self, connection):
raise exception.NotImplemented
def rollback(self, connection):
raise exception.NotImplemented
def disconnect(self, connection):
raise exception.NotImplemented
def close(self):
raise exception.NotImplemented
def fetchrow(self, cursor):
raise exception.NotImplemented
def query(self, cursor, sql, sql_params):
raise exception.NotImplemented
def validate_recipient(self, cusor, address):
raise exception.NotImplemented
def record_response(self, cursor, sender, recipient):
raise exception.NotImplemented
def get_pending_responses(self, cursor, limit):
raise exception.NotImplemented
def update_sent_timestamp(self, cursor, id, date):
raise exception.NotImplemented
def disable_expired_configs(self, cursor, date):
raise exception.NotImplemented
def delete_old_response_records(self, cursor, date):
raise exception.NotImplemented
def delete_records_of_disabled_configs(self, cursor):
raise exception.NotImplemented
class PostgreSQL(DatabaseBackend):
'''PostgreSQL Backend Adapter'''
# TODO: should be quite easy given the MySQL implementation
pass
class MySQL(DatabaseBackend):
'''MySQL Backend Adapter'''
import MySQLdb
import MySQLdb.cursors
import _mysql_exceptions
import sqlalchemy.pool as pool
LABEL = 'MySQL'
CURSOR_DEFAULT = MySQLdb.cursors.Cursor
CURSOR_DICT = MySQLdb.cursors.DictCursor
# Connection pooling
DatabasePool = pool.manage(MySQLdb, recycle=120)
def __init__(self, config):
self.config = config
if self.config.socket.type == 'UNIX':
self.name = self.config.socket.path
self.connection_params = {
'unix_socket': self.config.socket.path
}
else:
self.name = self.config.socket.host
self.connection_params = {
'host': self.config.socket.host,
'port': self.config.socket.port,
}
self.connection_params.update({
'user': self.config.username,
'passwd': self.config.password
})
def _log(self, _log, message):
_log('%s: %s' % (self.LABEL, message))
def connect(self, unicode=True, cursorclass=CURSOR_DEFAULT):
self.connection_params.update({
'use_unicode': unicode,
'cursorclass': cursorclass,
})
self._log(log.info, 'Asking pool for a connection')
try:
connection = self.DatabasePool.connect(**self.connection_params)
except self.MySQLdb.OperationalError, e:
self._log(log.error, 'Unable to aquire a connection to server %s' \
% self.name)
raise exception.DatabaseError(
'%s: Could not connect to server: %s' % (self.LABEL, e))
self._log(log.debug, 'Successfully aquired connection from pool')
return connection
def open_cursor(self, connection):
self._log(log.debug, 'Opening cursor for connection')
return connection.cursor()
def close_cursor(self, cursor):
self._log(log.debug, 'Closing cursor for connection')
cursor.close()
def commit(self, connection):
self._log(log.info, 'Committing changes')
connection.commit()
def rollback(self, connection):
self._log(log.info, 'Rolling back changes')
connection.rollback()
def disconnect(self, connection):
self._log(log.info, 'Releasing connection')
connection.close()
def close(self):
self._log(log.info, 'Disposing connection pool...')
self.DatabasePool.dispose()
def query(self, cursor, sql, sql_params={}):
if sql_params:
# XXX normalize params? Not really... we quoted strings in the
# query (see backend configuration) and e-mail addresses must not
# contain any dangerous characters. Hint: MySQLdb can do that on
# it's own if we pass it the params. Let's keep it specialized for
# now.
sql = sql % sql_params
self._log(log.debug, 'Executing query: %s' % sql)
try:
return cursor.execute(sql)
except self._mysql_exceptions.ProgrammingError, e:
raise exception.DatabaseQueryError(
'%s: Error executing query: %s' % (self.LABEL, e), sql)
except self._mysql_exceptions.IntegrityError, e:
raise exception.DatabaseQueryError(
'%s: Integrity error: %s' % (self.LABEL, e), sql)
def fetchrow(self, cursor):
return cursor.fetchone()
def validate_recipient(self, cursor, address):
if not self.config.query_validate_recipient_enabled:
return
try:
result = self.query(cursor, self.config.query_validate_recipient,
{'address': address})
if result != 1:
raise exception.DatabaseQueryError(
'%s: Unexpected number of rows returned' % self.LABEL)
elif cursor.fetchall()[0][0] != 1:
raise exception.DatabaseQueryError(
'%s: Unable to find valid recipient entry' % self.LABEL)
except Exception, e:
self._log(log.info, 'Unable to validate recipient %s - %s'
% (address, e))
raise
def record_response(self, cursor, sender, recipient):
try:
result = self.query(cursor, self.config.query_record_response, {
'sender': sender,
'recipient': recipient,
'date': datetime.now(),
})
if result <= 0:
raise exception.DatabaseQueryError(
'%s: Query affected no rows' % self.LABEL)
except Exception, e:
self._log(log.info, 'Unable to record autoresponse: %s -> %s - %s'
% (sender, recipient, e))
raise
def get_pending_responses(self, cursor, limit):
try:
return self.query(cursor, self.config.query_pending_responses,
{
'limit': limit,
})
except Exception, e:
self._log(log.info,
'Unable to query for pending responses: %s' % e)
raise
def update_sent_timestamp(self, cursor, id, date):
try:
return self.query(cursor, self.config.query_update_sent_timestamp,
{
'id': id,
'date': date,
})
except Exception, e:
self._log(log.info,
'Unable to update sent timestamp (record id %s): %s'
% (id, e))
raise
def disable_expired_configs(self, cursor, date):
try:
return self.query(cursor,
self.config.query_disable_expired_configs,
{'date': date})
except Exception, e:
self._log(log.info, 'Unable to disable expired configs: %s' % e)
raise
def delete_old_response_records(self, cursor, date):
try:
return self.query(cursor,
self.config.query_delete_old_response_records,
{'date': date})
except Exception, e:
self._log(log.info, 'Unable to delete old records: %s' % e)
raise
def delete_records_of_disabled_configs(self, cursor):
try:
return self.query(cursor,
self.config.query_delete_records_of_disabled_configs)
except Exception, e:
self._log(log.info,
'Unable to delete records of disabled configs: %s' % e)
raise
class Manager(object):
'''Backend Manager
Each channel has one backend manager instance, resulting in:
- one backend connection (if possible from a pool) per channel
- thread safe query execution :-)
'''
def __init__(self, backend):
self.backend = backend
self.connection = None
self.cursor = None
def __nonzero__(self):
return bool(self.connection)
def connect(self, *args, **kwargs):
if not self.connection:
self.connection = self.backend.connect(*args, **kwargs)
def close(self):
if self.connection:
if self.cursor:
self.backend.close_cursor(self.cursor)
self.backend.commit(self.connection)
self.backend.disconnect(self.connection)
def get_cursor(self):
if not self.cursor:
self.connect()
self.cursor = self.backend.open_cursor(self.connection)
def validate_recipient(self, *args, **kwargs):
self.connect()
self.get_cursor()
self.backend.validate_recipient(self.cursor, *args, **kwargs)
def record_response(self, *args, **kwargs):
self.connect()
self.get_cursor()
self.backend.record_response(self.cursor, *args, **kwargs)