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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
"""Helpers for DBMS specific (advanced or non standard) functionalities.
Helpers are provided for postgresql, mysql and sqlite.
:copyright:
2000-2008 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE),
all rights reserved.
:contact:
http://www.logilab.org/project/logilab-common --
mailto:python-projects@logilab.org
:license:
`General Public License version 2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>`_
"""
__docformat__ = "restructuredtext en"
from logilab.common.deprecation import obsolete
class BadQuery(Exception): pass
class UnsupportedFunction(BadQuery): pass
class metafunc(type):
def __new__(mcs, name, bases, dict):
dict['name'] = name.upper()
return type.__new__(mcs, name, bases, dict)
class FunctionDescr(object):
__metaclass__ = metafunc
supported_backends = ()
rtype = None # None <-> returned type should be the same as the first argument
aggregat = False
minargs = 1
maxargs = 1
name_mapping = {}
def __init__(self, name=None, rtype=rtype, aggregat=aggregat):
if name is not None:
name = name.upper()
self.name = name
self.rtype = rtype
self.aggregat = aggregat
def backend_name(self, backend):
try:
return self.name_mapping[backend]
except KeyError:
return self.name
backend_name = classmethod(backend_name)
#@classmethod
def check_nbargs(cls, nbargs):
if cls.minargs is not None and \
nbargs < cls.minargs:
raise BadQuery('not enough argument for function %s' % cls.name)
if cls.maxargs is not None and \
nbargs < cls.maxargs:
raise BadQuery('too many arguments for function %s' % cls.name)
check_nbargs = classmethod(check_nbargs)
class AggrFunctionDescr(FunctionDescr):
aggregat = True
rtype = None
class MAX(AggrFunctionDescr): pass
class MIN(AggrFunctionDescr): pass
class SUM(AggrFunctionDescr): pass
class COUNT(AggrFunctionDescr):
rtype = 'Int'
class AVG(AggrFunctionDescr):
rtype = 'Float'
class UPPER(FunctionDescr):
rtype = 'String'
class LOWER(FunctionDescr):
rtype = 'String'
class IN(FunctionDescr):
"""this is actually a 'keyword' function..."""
maxargs = None
class LENGTH(FunctionDescr):
rtype = 'Int'
class DATE(FunctionDescr):
rtype = 'Date'
class RANDOM(FunctionDescr):
supported_backends = ('postgres', 'mysql',)
rtype = 'Float'
minargs = maxargs = 0
name_mapping = {'postgres': 'RANDOM',
'mysql': 'RAND',
}
class _GenericAdvFuncHelper:
"""Generic helper, trying to provide generic way to implement
specific functionnalities from others DBMS
An exception is raised when the functionality is not emulatable
"""
# DBMS resources descriptors and accessors
backend_name = None # overriden in subclasses ('postgres', 'sqlite', etc.)
needs_from_clause = False
union_parentheses_support = True
users_support = True
groups_support = True
ilike_support = True
FUNCTIONS = {
# aggregat functions
'MIN': MIN, 'MAX': MAX,
'SUM': SUM,
'COUNT': COUNT,
'AVG': AVG,
# transformation functions
'UPPER': UPPER, 'LOWER': LOWER,
'LENGTH': LENGTH,
'DATE': DATE,
'RANDOM': RANDOM,
# keyword function
'IN': IN
}
TYPE_MAPPING = {
'String' : 'text',
'Int' : 'integer',
'Float' : 'float',
'Decimal' : 'decimal',
'Boolean' : 'boolean',
'Date' : 'date',
'Time' : 'time',
'Datetime' : 'timestamp',
'Interval' : 'interval',
'Password' : 'bytea',
'Bytes' : 'bytea',
}
#@classmethod
def register_function(cls, funcdef):
if isinstance(funcdef, basestring) :
funcdef = FunctionDescr(funcdef.upper())
assert not funcdef.name in cls.FUNCTIONS, \
'%s is already registered' % funcdef.name
cls.FUNCTIONS[funcdef.name] = funcdef
register_function = classmethod(register_function)
#@classmethod
def function_description(cls, funcname):
"""return the description (`FunctionDescription`) for a RQL function"""
try:
return cls.FUNCTIONS[funcname.upper()]
except KeyError:
raise UnsupportedFunction(funcname)
function_description = classmethod(function_description)
#@obsolete('use users_support attribute')
def support_users(self):
"""return True if the DBMS support users (this is usually
not true for in memory DBMS)
"""
return self.users_support
support_user = obsolete('use users_support attribute')(support_users)
#@obsolete('use groups_support attribute')
def support_groups(self):
"""return True if the DBMS support groups"""
return self.groups_support
support_user = obsolete('use groups_support attribute')(support_groups)
def func_sqlname(self, funcname):
funcdef = self.function_description(funcname)
return funcdef.backend_name(self.backend_name)
def system_database(self):
"""return the system database for the given driver"""
raise NotImplementedError('not supported by this DBMS')
def backup_command(self, dbname, dbhost, dbuser, dbpassword, backupfile,
keepownership=True):
"""return a command to backup the given database"""
raise NotImplementedError('not supported by this DBMS')
def restore_commands(self, dbname, dbhost, dbuser, backupfile,
encoding='utf-8', keepownership=True, drop=True):
"""return a list of commands to restore a backup the given database"""
raise NotImplementedError('not supported by this DBMS')
# helpers to standardize SQL according to the database
def sql_current_date(self):
return 'CURRENT_DATE'
def sql_current_time(self):
return 'CURRENT_TIME'
def sql_current_timestamp(self):
return 'CURRENT_TIMESTAMP'
def sql_create_sequence(self, seq_name):
return '''CREATE TABLE %s (last INTEGER);
INSERT INTO %s VALUES (0);''' % (seq_name, seq_name)
def sql_create_index(self, table, column, unique=False):
idx = self._index_name(table, column, unique)
if unique:
return 'ALTER TABLE %s ADD UNIQUE(%s)' % (table, column)
else:
return 'CREATE INDEX %s ON %s(%s);' % (idx, table, column)
def sql_drop_index(self, table, column, unique=False):
idx = self._index_name(table, column, unique)
if unique:
return 'ALTER TABLE %s DROP CONSTRAINT %s' % (table, idx)
else:
return 'DROP INDEX %s' % idx
def sql_drop_sequence(self, seq_name):
return 'DROP TABLE %s;' % seq_name
def sqls_increment_sequence(self, seq_name):
return ('UPDATE %s SET last=last+1;' % seq_name,
'SELECT last FROM %s;' % seq_name)
def sql_temporary_table(self, table_name, table_schema,
drop_on_commit=True):
return "CREATE TEMPORARY TABLE %s (%s);" % (table_name, table_schema)
def boolean_value(self, value):
if value:
return 'TRUE'
else:
return 'FALSE'
def increment_sequence(self, cursor, seq_name):
for sql in self.sqls_increment_sequence(seq_name):
cursor.execute(sql)
return cursor.fetchone()[0]
def create_user(self, cursor, user, password):
"""create a new database user"""
if not self.users_support:
raise NotImplementedError('not supported by this DBMS')
cursor.execute("CREATE USER %(user)s "
"WITH PASSWORD '%(password)s'" % locals())
def _index_name(self, table, column, unique=False):
if unique:
# note: this naming is consistent with indices automatically
# created by postgres when UNIQUE appears in a table schema
return '%s_%s_key' % (table.lower(), column.lower())
else:
return '%s_%s_idx' % (table.lower(), column.lower())
def create_index(self, cursor, table, column, unique=False):
if not self.index_exists(cursor, table, column, unique):
cursor.execute(self.sql_create_index(table, column, unique))
def drop_index(self, cursor, table, column, unique=False):
if self.index_exists(cursor, table, column, unique):
cursor.execute(self.sql_drop_index(table, column, unique))
def index_exists(self, cursor, table, column, unique=False):
idx = self._index_name(table, column, unique)
return idx in self.list_indices(cursor, table)
def user_exists(self, cursor, username):
"""return True if a user with the given username exists"""
return username in self.list_users(cursor)
def list_users(self, cursor):
"""return the list of existing database users"""
raise NotImplementedError('not supported by this DBMS')
def create_database(self, cursor, dbname, owner=None, encoding='utf-8'):
"""create a new database"""
raise NotImplementedError('not supported by this DBMS')
def list_databases(self):
"""return the list of existing databases"""
raise NotImplementedError('not supported by this DBMS')
def list_tables(self, cursor):
"""return the list of tables of a database"""
raise NotImplementedError('not supported by this DBMS')
def list_indices(self, cursor, table=None):
"""return the list of indices of a database, only for the given table if specified"""
raise NotImplementedError('not supported by this DBMS')
def pgdbcmd(cmd, dbhost, dbuser):
cmd = [cmd]
if dbhost:
cmd.append('--host=%s' % dbhost)
if dbuser:
cmd.append('--username=%s' % dbuser)
return cmd
class _PGAdvFuncHelper(_GenericAdvFuncHelper):
"""Postgres helper, taking advantage of postgres SEQUENCE support
"""
backend_name = 'postgres'
# modifiable but should not be shared
FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy()
def system_database(self):
"""return the system database for the given driver"""
return 'template1'
def backup_command(self, dbname, dbhost, dbuser, backupfile,
keepownership=True):
"""return a command to backup the given database"""
cmd = ['pg_dump -Fc']
if dbhost:
cmd.append('--host=%s' % dbhost)
if dbuser:
cmd.append('--username=%s' % dbuser)
if not keepownership:
cmd.append('--no-owner')
cmd.append('--file=%s' % backupfile)
cmd.append(dbname)
return ' '.join(cmd)
def restore_commands(self, dbname, dbhost, dbuser, backupfile,
encoding='utf-8', keepownership=True, drop=True):
"""return a list of commands to restore a backup the given database"""
cmds = []
if drop:
cmd = pgdbcmd('dropdb', dbhost, dbuser)
cmd.append(dbname)
cmds.append(' '.join(cmd))
cmd = pgdbcmd('createdb -T template0 -E %s' % encoding, dbhost, dbuser)
cmd.append(dbname)
cmds.append(' '.join(cmd))
cmd = pgdbcmd('pg_restore -Fc', dbhost, dbuser)
cmd.append('--dbname %s' % dbname)
if not keepownership:
cmd.append('--no-owner')
cmd.append(backupfile)
cmds.append(' '.join(cmd))
return cmds
def sql_create_sequence(self, seq_name):
return 'CREATE SEQUENCE %s;' % seq_name
def sql_drop_sequence(self, seq_name):
return 'DROP SEQUENCE %s;' % seq_name
def sqls_increment_sequence(self, seq_name):
return ("SELECT nextval('%s');" % seq_name,)
def sql_temporary_table(self, table_name, table_schema,
drop_on_commit=True):
if not drop_on_commit:
return "CREATE TEMPORARY TABLE %s (%s);" % (table_name,
table_schema)
return "CREATE TEMPORARY TABLE %s (%s) ON COMMIT DROP;" % (table_name,
table_schema)
def create_database(self, cursor, dbname, owner=None, encoding='utf-8'):
"""create a new database"""
sql = "CREATE DATABASE %(dbname)s"
if owner:
sql += " WITH OWNER=%(owner)s"
if encoding:
sql += " ENCODING='%(encoding)s'"
cursor.execute(sql % locals())
def create_language(self, cursor, extlang):
"""postgres specific method to install a procedural language on a database"""
# make sure plpythonu is not directly in template1
cursor.execute("SELECT * FROM pg_language WHERE lanname='%s';" % extlang)
if cursor.fetchall():
print '%s language already installed' % extlang
else:
cursor.execute('CREATE LANGUAGE %s' % extlang)
print '%s language installed' % extlang
def list_users(self, cursor, username=None):
"""return the list of existing database users"""
if username:
warn('username argument is deprecated, use user_exists method',
DeprecationWarning, stacklevel=2)
return self.user_exists(cursor, username)
cursor.execute("SELECT usename FROM pg_user")
return [r[0] for r in cursor.fetchall()]
def list_databases(self, cursor):
"""return the list of existing databases"""
cursor.execute('SELECT datname FROM pg_database')
return [r[0] for r in cursor.fetchall()]
def list_tables(self, cursor):
"""return the list of tables of a database"""
cursor.execute("SELECT tablename FROM pg_tables")
return [r[0] for r in cursor.fetchall()]
def list_indices(self, cursor, table=None):
"""return the list of indices of a database, only for the given table if specified"""
sql = "SELECT indexname FROM pg_indexes"
if table:
sql += " WHERE LOWER(tablename)='%s'" % table.lower()
cursor.execute(sql)
return [r[0] for r in cursor.fetchall()]
class _SqliteAdvFuncHelper(_GenericAdvFuncHelper):
"""Generic helper, trying to provide generic way to implement
specific functionnalities from others DBMS
An exception is raised when the functionality is not emulatable
"""
backend_name = 'sqlite'
# modifiable but should not be shared
FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy()
FUNCTIONS.pop('RANDOM') # not defined in sqlite
users_support = groups_support = False
ilike_support = False
union_parentheses_support = False
def sql_create_index(self, table, column, unique=False):
idx = self._index_name(table, column, unique)
if unique:
return 'CREATE UNIQUE INDEX %s ON %s(%s);' % (idx, table, column)
else:
return 'CREATE INDEX %s ON %s(%s);' % (idx, table, column)
def sql_drop_index(self, table, column, unique=False):
return 'DROP INDEX %s' % self._index_name(table, column, unique)
def list_tables(self, cursor):
"""return the list of tables of a database"""
# filter type='table' else we get indices as well
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
return [r[0] for r in cursor.fetchall()]
def list_indices(self, cursor, table=None):
"""return the list of indices of a database, only for the given table if specified"""
sql = "SELECT name FROM sqlite_master WHERE type='index'"
if table:
sql += " AND LOWER(tbl_name)='%s'" % table.lower()
cursor.execute(sql)
return [r[0] for r in cursor.fetchall()]
class _MyAdvFuncHelper(_GenericAdvFuncHelper):
"""MySQL helper, taking advantage of postgres SEQUENCE support
"""
backend_name = 'mysql'
needs_from_clause = True
ilike_support = False # insensitive search by default
# modifiable but should not be shared
FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy()
TYPE_MAPPING = _GenericAdvFuncHelper.TYPE_MAPPING.copy()
TYPE_MAPPING['Password'] = 'tinyblob'
TYPE_MAPPING['String'] = 'mediumtext'
TYPE_MAPPING['Bytes'] = 'longblob'
def system_database(self):
"""return the system database for the given driver"""
return ''
def backup_command(self, dbname, dbhost, dbuser, backupfile,
keepownership=True):
"""return a command to backup the given database"""
# XXX compress
return 'mysqldump -h %s -u %s -p -r %s %s' % (dbhost, dbuser, backupfile, dbname)
def restore_commands(self, dbname, dbhost, dbuser, backupfile,
encoding='utf-8', keepownership=True, drop=True):
"""return a list of commands to restore a backup the given database"""
cmds = []
if drop:
cmd = 'echo "DROP DATABASE %s;" | mysql -h %s -u %s -p' % (
dbname, dbhost, dbuser)
cmds.append(cmd)
cmd = 'echo "%s;" | mysql -h %s -u %s -p' % (
self.sql_create_database(dbname, encoding), dbhost, dbuser)
cmds.append(cmd)
cmd = 'mysql -h %s -u %s -p %s < %s' % (dbhost, dbuser, dbname, backupfile)
cmds.append(cmd)
return cmds
def sql_temporary_table(self, table_name, table_schema,
drop_on_commit=True):
if not drop_on_commit:
return "CREATE TEMPORARY TABLE %s (%s);" % (table_name,
table_schema)
return "CREATE TEMPORARY TABLE %s (%s) ON COMMIT DROP;" % (table_name,
table_schema)
def sql_create_database(self, dbname, encoding='utf-8'):
sql = "CREATE DATABASE %(dbname)s"
if encoding:
sql += " CHARACTER SET %(encoding)s"
return sql % locals()
def create_database(self, cursor, dbname, owner=None, encoding='utf-8'):
"""create a new database"""
cursor.execute(self.sql_create_database(dbname, encoding))
if owner:
cursor.execute('GRANT ALL ON `%s`.* to %s' % (dbname, owner))
def boolean_value(self, value):
if value:
return True
else:
return False
def list_users(self, cursor):
"""return the list of existing database users"""
# Host, Password
cursor.execute("SELECT User FROM mysql.user")
return [r[0] for r in cursor.fetchall()]
def list_databases(self, cursor):
"""return the list of existing databases"""
cursor.execute('SHOW DATABASES')
return [r[0] for r in cursor.fetchall()]
def list_tables(self, cursor):
"""return the list of tables of a database"""
cursor.execute("SHOW TABLES")
return [r[0] for r in cursor.fetchall()]
def list_indices(self, cursor, table=None):
"""return the list of indices of a database, only for the given table if specified"""
if table:
cursor.execute("SHOW INDEX FROM %s" % table)
return [r[2] for r in cursor.fetchall()]
allindices = []
for table in self.list_tables(cursor):
allindices += self.list_indices(cursor, table)
return allindices
ADV_FUNC_HELPER_DIRECTORY = {'postgres': _PGAdvFuncHelper(),
'sqlite': _SqliteAdvFuncHelper(),
'mysql': _MyAdvFuncHelper(),
}
def get_adv_func_helper(driver):
"""returns an advanced function helper for the given driver"""
return ADV_FUNC_HELPER_DIRECTORY[driver]
def register_function(driver, funcdef):
ADV_FUNC_HELPER_DIRECTORY[driver].register_function(funcdef)
# this function should be called `register_function` but the other
# definition was defined prior to this one
def auto_register_function(funcdef):
"""register the function `funcdef` on supported backends"""
for driver in funcdef.supported_backends:
register_function(driver, funcdef)
|