Project

General

Profile

1
# Database access
2

    
3
import copy
4
import re
5
import warnings
6

    
7
import exc
8
import dicts
9
import iters
10
import lists
11
from Proxy import Proxy
12
import rand
13
import strings
14
import util
15

    
16
##### Exceptions
17

    
18
def get_cur_query(cur):
19
    if hasattr(cur, 'query'): return cur.query
20
    elif hasattr(cur, '_last_executed'): return cur._last_executed
21
    else: return None
22

    
23
def _add_cursor_info(e, cur): exc.add_msg(e, 'query: '+get_cur_query(cur))
24

    
25
class DbException(exc.ExceptionWithCause):
26
    def __init__(self, msg, cause=None, cur=None):
27
        exc.ExceptionWithCause.__init__(self, msg, cause)
28
        if cur != None: _add_cursor_info(self, cur)
29

    
30
class NameException(DbException): pass
31

    
32
class ExceptionWithColumns(DbException):
33
    def __init__(self, cols, cause=None):
34
        DbException.__init__(self, 'columns: ' + ', '.join(cols), cause)
35
        self.cols = cols
36

    
37
class DuplicateKeyException(ExceptionWithColumns): pass
38

    
39
class NullValueException(ExceptionWithColumns): pass
40

    
41
class EmptyRowException(DbException): pass
42

    
43
##### Warnings
44

    
45
class DbWarning(UserWarning): pass
46

    
47
##### Result retrieval
48

    
49
def col_names(cur): return (col[0] for col in cur.description)
50

    
51
def rows(cur): return iter(lambda: cur.fetchone(), None)
52

    
53
def consume_rows(cur):
54
    '''Used to fetch all rows so result will be cached'''
55
    iters.consume_iter(rows(cur))
56

    
57
def next_row(cur): return rows(cur).next()
58

    
59
def row(cur):
60
    row_ = next_row(cur)
61
    consume_rows(cur)
62
    return row_
63

    
64
def next_value(cur): return next_row(cur)[0]
65

    
66
def value(cur): return row(cur)[0]
67

    
68
def values(cur): return iters.func_iter(lambda: next_value(cur))
69

    
70
def value_or_none(cur):
71
    try: return value(cur)
72
    except StopIteration: return None
73

    
74
##### Database connections
75

    
76
db_config_names = ['engine', 'host', 'user', 'password', 'database']
77

    
78
db_engines = {
79
    'MySQL': ('MySQLdb', {'password': 'passwd', 'database': 'db'}),
80
    'PostgreSQL': ('psycopg2', {}),
81
}
82

    
83
DatabaseErrors_set = set([DbException])
84
DatabaseErrors = tuple(DatabaseErrors_set)
85

    
86
def _add_module(module):
87
    DatabaseErrors_set.add(module.DatabaseError)
88
    global DatabaseErrors
89
    DatabaseErrors = tuple(DatabaseErrors_set)
90

    
91
def db_config_str(db_config):
92
    return db_config['engine']+' database '+db_config['database']
93

    
94
def _query_lookup(query, params): return (query, dicts.make_hashable(params))
95

    
96
log_debug_none = lambda msg: None
97

    
98
class DbConn:
99
    def __init__(self, db_config, serializable=True, log_debug=log_debug_none):
100
        self.db_config = db_config
101
        self.serializable = serializable
102
        self.log_debug = log_debug
103
        
104
        self.__db = None
105
        self.query_results = {}
106
    
107
    def __getattr__(self, name):
108
        if name == '__dict__': raise Exception('getting __dict__')
109
        if name == 'db': return self._db()
110
        else: raise AttributeError()
111
    
112
    def __getstate__(self):
113
        state = copy.copy(self.__dict__) # shallow copy
114
        state['log_debug'] = None # don't pickle the debug callback
115
        state['_DbConn__db'] = None # don't pickle the connection
116
        return state
117
    
118
    def _db(self):
119
        if self.__db == None:
120
            # Process db_config
121
            db_config = self.db_config.copy() # don't modify input!
122
            module_name, mappings = db_engines[db_config.pop('engine')]
123
            module = __import__(module_name)
124
            _add_module(module)
125
            for orig, new in mappings.iteritems():
126
                try: util.rename_key(db_config, orig, new)
127
                except KeyError: pass
128
            
129
            # Connect
130
            self.__db = module.connect(**db_config)
131
            
132
            # Configure connection
133
            if self.serializable: run_raw_query(self,
134
                'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
135
        
136
        return self.__db
137
    
138
    class DbCursor(Proxy):
139
        def __init__(self, outer):
140
            Proxy.__init__(self, outer.db.cursor())
141
            self.query_results = outer.query_results
142
            self.query_lookup = None
143
            self.result = []
144
        
145
        def execute(self, query, params=None):
146
            self._is_insert = query.upper().find('INSERT') >= 0
147
            self.query_lookup = _query_lookup(query, params)
148
            try: return_value = self.inner.execute(query, params)
149
            except Exception, e:
150
                self.result = e # cache the exception as the result
151
                self._cache_result()
152
                raise
153
            finally: self.query = get_cur_query(self.inner)
154
            # Fetch all rows so result will be cached
155
            if self.rowcount == 0 and not self._is_insert: consume_rows(self)
156
            return return_value
157
        
158
        def fetchone(self):
159
            row = self.inner.fetchone()
160
            if row != None: self.result.append(row)
161
            # otherwise, fetched all rows
162
            else: self._cache_result()
163
            return row
164
        
165
        def _cache_result(self):
166
            # For inserts, only cache exceptions since inserts are not
167
            # idempotent, but an invalid insert will always be invalid
168
            if self.query_results != None and (not self._is_insert
169
                or isinstance(self.result, Exception)):
170
                
171
                assert self.query_lookup != None
172
                self.query_results[self.query_lookup] = self.CacheCursor(
173
                    util.dict_subset(dicts.AttrsDictView(self),
174
                    ['query', 'result', 'rowcount', 'description']))
175
        
176
        class CacheCursor:
177
            def __init__(self, cached_result): self.__dict__ = cached_result
178
            
179
            def execute(self, *args, **kw_args):
180
                if isinstance(self.result, Exception): raise self.result
181
                # otherwise, result is a rows list
182
                self.iter = iter(self.result)
183
            
184
            def fetchone(self):
185
                try: return self.iter.next()
186
                except StopIteration: return None
187
    
188
    def run_query(self, query, params=None, cacheable=False):
189
        used_cache = False
190
        try:
191
            # Get cursor
192
            if cacheable:
193
                query_lookup = _query_lookup(query, params)
194
                try:
195
                    cur = self.query_results[query_lookup]
196
                    used_cache = True
197
                except KeyError: cur = self.DbCursor(self)
198
            else: cur = self.db.cursor()
199
            
200
            # Run query
201
            try: cur.execute(query, params)
202
            except Exception, e:
203
                _add_cursor_info(e, cur)
204
                raise
205
        finally:
206
            if self.log_debug != log_debug_none: # only compute msg if needed
207
                if used_cache: cache_status = 'Cache hit'
208
                elif cacheable: cache_status = 'Cache miss'
209
                else: cache_status = 'Non-cacheable'
210
                self.log_debug(cache_status+': '
211
                    +strings.one_line(get_cur_query(cur)))
212
        
213
        return cur
214
    
215
    def is_cached(self, query, params=None):
216
        return _query_lookup(query, params) in self.query_results
217

    
218
connect = DbConn
219

    
220
##### Input validation
221

    
222
def check_name(name):
223
    if re.search(r'\W', name) != None: raise NameException('Name "'+name
224
        +'" may contain only alphanumeric characters and _')
225

    
226
def esc_name_by_module(module, name, preserve_case=False):
227
    if module == 'psycopg2':
228
        if preserve_case: quote = '"'
229
        # Don't enclose in quotes because this disables case-insensitivity
230
        else: return name
231
    elif module == 'MySQLdb': quote = '`'
232
    else: raise NotImplementedError("Can't escape name for "+module+' database')
233
    return quote + name.replace(quote, '') + quote
234

    
235
def esc_name_by_engine(engine, name, **kw_args):
236
    return esc_name_by_module(db_engines[engine][0], name, **kw_args)
237

    
238
def esc_name(db, name, **kw_args):
239
    return esc_name_by_module(util.root_module(db.db), name, **kw_args)
240

    
241
def qual_name(db, schema, table):
242
    def esc_name(name): return sql.esc_name(db, name, preserve_case=True)
243
    return esc_name(schema)+'.'+esc_name(table)
244

    
245
##### Querying
246

    
247
def run_raw_query(db, *args, **kw_args):
248
    '''For args, see DbConn.run_query()'''
249
    return db.run_query(*args, **kw_args)
250

    
251
##### Recoverable querying
252

    
253
def with_savepoint(db, func):
254
    savepoint = 'savepoint_'+str(rand.rand_int()) # must be unique
255
    run_raw_query(db, 'SAVEPOINT '+savepoint)
256
    try: return_val = func()
257
    except:
258
        run_raw_query(db, 'ROLLBACK TO SAVEPOINT '+savepoint)
259
        raise
260
    else:
261
        run_raw_query(db, 'RELEASE SAVEPOINT '+savepoint)
262
        return return_val
263

    
264
def run_query(db, query, params=None, recover=None, cacheable=False):
265
    if recover == None: recover = False
266
    
267
    def run(): return run_raw_query(db, query, params, cacheable)
268
    if recover and not db.is_cached(query, params):
269
        return with_savepoint(db, run)
270
    else: return run() # don't need savepoint if cached
271

    
272
##### Basic queries
273

    
274
def select(db, table, fields=None, conds=None, limit=None, start=None,
275
    recover=None, cacheable=True):
276
    '''@param fields Use None to select all fields in the table'''
277
    if conds == None: conds = {}
278
    assert limit == None or type(limit) == int
279
    assert start == None or type(start) == int
280
    check_name(table)
281
    if fields != None: map(check_name, fields)
282
    map(check_name, conds.keys())
283
    
284
    def cond(entry):
285
        col, value = entry
286
        cond_ = esc_name(db, col)+' '
287
        if value == None: cond_ += 'IS'
288
        else: cond_ += '='
289
        cond_ += ' %s'
290
        return cond_
291
    query = 'SELECT '
292
    if fields == None: query += '*'
293
    else: query += ', '.join([esc_name(db, field) for field in fields])
294
    query += ' FROM '+esc_name(db, table)
295
    
296
    missing = True
297
    if conds != {}:
298
        query += ' WHERE '+' AND '.join(map(cond, conds.iteritems()))
299
        missing = False
300
    if limit != None: query += ' LIMIT '+str(limit); missing = False
301
    if start != None:
302
        if start != 0: query += ' OFFSET '+str(start)
303
        missing = False
304
    if missing: warnings.warn(DbWarning(
305
        'SELECT statement missing a WHERE, LIMIT, or OFFSET clause: '+query))
306
    
307
    return run_query(db, query, conds.values(), recover, cacheable)
308

    
309
default = object() # tells insert() to use the default value for a column
310

    
311
def insert(db, table, row, returning=None, recover=None, cacheable=True,
312
    table_is_esc=False):
313
    '''
314
    @param returning str|None An inserted column (such as pkey) to return
315
    @param table_is_esc Whether the table name has already been escaped
316
    '''
317
    if not table_is_esc: check_name(table)
318
    if lists.is_seq(row): cols = None
319
    else:
320
        cols = row.keys()
321
        row = row.values()
322
        map(check_name, cols)
323
    row = list(row) # ensure that "!= []" works
324
    
325
    # Check for special values
326
    labels = []
327
    values = []
328
    for value in row:
329
        if value == default: labels.append('DEFAULT')
330
        else:
331
            labels.append('%s')
332
            values.append(value)
333
    
334
    # Build query
335
    query = 'INSERT INTO '+table
336
    if values != []:
337
        if cols != None: query += ' ('+', '.join(cols)+')'
338
        query += ' VALUES ('+(', '.join(labels))+')'
339
    else: query += ' DEFAULT VALUES'
340
    
341
    if returning != None:
342
        check_name(returning)
343
        query += ' RETURNING '+returning
344
    
345
    return run_query(db, query, values, recover, cacheable)
346

    
347
def last_insert_id(db):
348
    module = util.root_module(db.db)
349
    if module == 'psycopg2': return value(run_query(db, 'SELECT lastval()'))
350
    elif module == 'MySQLdb': return db.insert_id()
351
    else: return None
352

    
353
def truncate(db, table, schema='public'):
354
    return run_query(db, 'TRUNCATE '+qual_name(db, schema, table)+' CASCADE')
355

    
356
##### Database structure queries
357

    
358
def pkey(db, table, recover=None):
359
    '''Assumed to be first column in table'''
360
    check_name(table)
361
    return col_names(select(db, table, limit=0, recover=recover)).next()
362

    
363
def index_cols(db, table, index):
364
    '''Can also use this for UNIQUE constraints, because a UNIQUE index is
365
    automatically created. When you don't know whether something is a UNIQUE
366
    constraint or a UNIQUE index, use this function.'''
367
    check_name(table)
368
    check_name(index)
369
    module = util.root_module(db.db)
370
    if module == 'psycopg2':
371
        return list(values(run_query(db, '''\
372
SELECT attname
373
FROM
374
(
375
        SELECT attnum, attname
376
        FROM pg_index
377
        JOIN pg_class index ON index.oid = indexrelid
378
        JOIN pg_class table_ ON table_.oid = indrelid
379
        JOIN pg_attribute ON attrelid = indrelid AND attnum = ANY (indkey)
380
        WHERE
381
            table_.relname = %(table)s
382
            AND index.relname = %(index)s
383
    UNION
384
        SELECT attnum, attname
385
        FROM
386
        (
387
            SELECT
388
                indrelid
389
                , (regexp_matches(indexprs, E':varattno (\\\\d+)', 'g'))[1]::int
390
                    AS indkey
391
            FROM pg_index
392
            JOIN pg_class index ON index.oid = indexrelid
393
            JOIN pg_class table_ ON table_.oid = indrelid
394
            WHERE
395
                table_.relname = %(table)s
396
                AND index.relname = %(index)s
397
        ) s
398
        JOIN pg_attribute ON attrelid = indrelid AND attnum = indkey
399
) s
400
ORDER BY attnum
401
''',
402
            {'table': table, 'index': index}, cacheable=True)))
403
    else: raise NotImplementedError("Can't list index columns for "+module+
404
        ' database')
405

    
406
def constraint_cols(db, table, constraint):
407
    check_name(table)
408
    check_name(constraint)
409
    module = util.root_module(db.db)
410
    if module == 'psycopg2':
411
        return list(values(run_query(db, '''\
412
SELECT attname
413
FROM pg_constraint
414
JOIN pg_class ON pg_class.oid = conrelid
415
JOIN pg_attribute ON attrelid = conrelid AND attnum = ANY (conkey)
416
WHERE
417
    relname = %(table)s
418
    AND conname = %(constraint)s
419
ORDER BY attnum
420
''',
421
            {'table': table, 'constraint': constraint})))
422
    else: raise NotImplementedError("Can't list constraint columns for "+module+
423
        ' database')
424

    
425
def tables(db, schema='public', table_like='%'):
426
    module = util.root_module(db.db)
427
    params = {'schema': schema, 'table_like': table_like}
428
    if module == 'psycopg2':
429
        return values(run_query(db, '''\
430
SELECT tablename
431
FROM pg_tables
432
WHERE
433
    schemaname = %(schema)s
434
    AND tablename LIKE %(table_like)s
435
ORDER BY tablename
436
''',
437
            params, cacheable=True))
438
    elif module == 'MySQLdb':
439
        return values(run_query(db, 'SHOW TABLES LIKE %(table_like)s', params,
440
            cacheable=True))
441
    else: raise NotImplementedError("Can't list tables for "+module+' database')
442

    
443
##### Database management
444

    
445
def empty_db(db, schema='public', **kw_args):
446
    '''For kw_args, see tables()'''
447
    for table in tables(db, schema, **kw_args): truncate(db, table, schema)
448

    
449
##### Heuristic queries
450

    
451
def try_insert(db, table, row, returning=None):
452
    '''Recovers from errors'''
453
    try: return insert(db, table, row, returning, recover=True)
454
    except Exception, e:
455
        msg = str(e)
456
        match = re.search(r'duplicate key value violates unique constraint '
457
            r'"(([^\W_]+)_[^"]+)"', msg)
458
        if match:
459
            constraint, table = match.groups()
460
            try: cols = index_cols(db, table, constraint)
461
            except NotImplementedError: raise e
462
            else: raise DuplicateKeyException(cols, e)
463
        match = re.search(r'null value in column "(\w+)" violates not-null '
464
            'constraint', msg)
465
        if match: raise NullValueException([match.group(1)], e)
466
        raise # no specific exception raised
467

    
468
def put(db, table, row, pkey, row_ct_ref=None):
469
    '''Recovers from errors.
470
    Only works under PostgreSQL (uses `INSERT ... RETURNING`)'''
471
    try:
472
        cur = try_insert(db, table, row, pkey)
473
        if row_ct_ref != None and cur.rowcount >= 0:
474
            row_ct_ref[0] += cur.rowcount
475
        return value(cur)
476
    except DuplicateKeyException, e:
477
        return value(select(db, table, [pkey],
478
            util.dict_subset_right_join(row, e.cols), recover=True))
479

    
480
def get(db, table, row, pkey, row_ct_ref=None, create=False):
481
    '''Recovers from errors'''
482
    try: return value(select(db, table, [pkey], row, 1, recover=True))
483
    except StopIteration:
484
        if not create: raise
485
        return put(db, table, row, pkey, row_ct_ref) # insert new row
(22-22/33)