Project

General

Profile

1
# Database access
2

    
3
import random
4
import re
5
import sys
6

    
7
import ex
8

    
9
def _add_cursor_info(e, cur): ex.add_msg(e, 'query: '+cur.query)
10

    
11
class NameException(Exception): pass
12

    
13
class DbException(ex.ExceptionWithCause):
14
    def __init__(self, msg, cause=None, cur=None):
15
        ex.ExceptionWithCause.__init__(self, msg, cause)
16
        if cur != None: _add_cursor_info(self, cur)
17

    
18
class ExceptionWithColumn(DbException):
19
    def __init__(self, col, cause=None):
20
        DbException.__init__(self, 'column: '+col, cause)
21
        self.col = col
22

    
23
class DuplicateKeyException(ExceptionWithColumn): pass
24

    
25
class NullValueException(ExceptionWithColumn): pass
26

    
27
class EmptyRowException(DbException): pass
28

    
29
def check_name(name):
30
    if re.search(r'\W', name) != None: raise NameException('Name "'+name
31
        +'" may contain only alphanumeric characters and _')
32

    
33
def run_query(db, query, params=None):
34
    cur = db.cursor()
35
    try: cur.execute(query, params)
36
    except Exception, e:
37
        _add_cursor_info(e, cur)
38
        raise
39
    return cur
40

    
41
def col(cur, idx): return cur.description[idx][0]
42

    
43
def row(cur): return iter(lambda: cur.fetchone(), None).next()
44

    
45
def value(cur): return row(cur)[0]
46

    
47
def with_savepoint(db, func):
48
    savepoint = 'savepoint_'+str(random.randint(0, sys.maxint)) # must be unique
49
    run_query(db, 'SAVEPOINT '+savepoint)
50
    try: return_val = func()
51
    except:
52
        run_query(db, 'ROLLBACK TO SAVEPOINT '+savepoint)
53
        raise
54
    else:
55
        run_query(db, 'RELEASE SAVEPOINT '+savepoint)
56
        return return_val
57

    
58
def select(db, table, fields, conds, limit=None):
59
    assert type(limit) == int
60
    check_name(table)
61
    map(check_name, fields)
62
    map(check_name, conds.keys())
63
    def cond(entry):
64
        col, value = entry
65
        cond_ = col+' '
66
        if value == None: cond_ += 'IS'
67
        else: cond_ += '='
68
        cond_ += ' %s'
69
        return cond_
70
    query = 'SELECT '+', '.join(fields)+' FROM '+table
71
    if conds != {}:
72
        query += ' WHERE '+' AND '.join(map(cond, conds.iteritems()))
73
    if limit != None: query += ' LIMIT '+str(limit)
74
    return run_query(db, query, conds.values())
75

    
76
def insert(db, table, row):
77
    check_name(table)
78
    cols = row.keys()
79
    map(check_name, cols)
80
    query = 'INSERT INTO '+table
81
    if row != {}: query += ' ('+', '.join(cols)+') VALUES ('\
82
        +', '.join(['%s']*len(cols))+')'
83
    else: query += ' DEFAULT VALUES'
84
    return run_query(db, query, row.values())
85

    
86
def last_insert_id(db): return value(run_query(db, 'SELECT lastval()'))
87

    
88
def try_insert(db, table, row):
89
    try: return with_savepoint(db, lambda: insert(db, table, row))
90
    except Exception, e:
91
        msg = str(e)
92
        match = re.search(r'duplicate key value violates unique constraint "'
93
            +table+'_(\w+)_index"', msg)
94
        if match: raise DuplicateKeyException(match.group(1), e)
95
        match = re.search(r'null value in column "(\w+)" violates not-null '
96
            'constraint', msg)
97
        if match: raise NullValueException(match.group(1), e)
98
        raise # no specific exception raised
99

    
100
def pkey(db, table): # Assumed to be first column in table
101
    check_name(table)
102
    return col(run_query(db, 'SELECT * FROM '+table+' LIMIT 0'), 0)
103

    
104
def get(db, table, row, pkey, create=False, row_ct_ref=None):
105
    try: return value(select(db, table, [pkey], row, 1))
106
    except StopIteration:
107
        if not create: raise
108
        # Insert new row
109
        try:
110
            row_ct = try_insert(db, table, row).rowcount
111
            if row_ct_ref != None and row_ct >= 0: row_ct_ref[0] += row_ct
112
            return last_insert_id(db)
113
        except DuplicateKeyException, e:
114
            return value(select(db, table, [pkey], {e.col: row[e.col]}))
(5-5/10)