Revision 135
Added by Aaron Marcuse-Kubitza about 13 years ago
sql.py | ||
---|---|---|
7 | 7 |
import ex |
8 | 8 |
import util |
9 | 9 |
|
10 |
def _add_cursor_info(e, cur): ex.add_msg(e, 'query: '+cur.query) |
|
10 |
def get_cur_query(cur): |
|
11 |
if hasattr(cur, 'query'): return cur.query |
|
12 |
elif hasattr(cur, '_last_executed'): return cur._last_executed |
|
13 |
else: return None |
|
11 | 14 |
|
15 |
def _add_cursor_info(e, cur): ex.add_msg(e, 'query: '+get_cur_query(cur)) |
|
16 |
|
|
12 | 17 |
class NameException(Exception): pass |
13 | 18 |
|
14 | 19 |
class DbException(ex.ExceptionWithCause): |
... | ... | |
41 | 46 |
|
42 | 47 |
def col(cur, idx): return cur.description[idx][0] |
43 | 48 |
|
44 |
def row(cur): return iter(lambda: cur.fetchone(), None).next()
|
|
49 |
def rows(cur): return iter(lambda: cur.fetchone(), None)
|
|
45 | 50 |
|
51 |
def row(cur): return rows(cur).next() |
|
52 |
|
|
46 | 53 |
def value(cur): return row(cur)[0] |
47 | 54 |
|
48 | 55 |
def with_savepoint(db, func): |
... | ... | |
57 | 64 |
return return_val |
58 | 65 |
|
59 | 66 |
def select(db, table, fields, conds, limit=None): |
60 |
assert type(limit) == int |
|
67 |
assert limit == None or type(limit) == int
|
|
61 | 68 |
check_name(table) |
62 | 69 |
map(check_name, fields) |
63 | 70 |
map(check_name, conds.keys()) |
... | ... | |
84 | 91 |
else: query += ' DEFAULT VALUES' |
85 | 92 |
return run_query(db, query, row.values()) |
86 | 93 |
|
87 |
def last_insert_id(db): return value(run_query(db, 'SELECT lastval()')) |
|
94 |
def last_insert_id(db): |
|
95 |
module = util.root_module(db) |
|
96 |
if module == 'psycopg2': return value(run_query(db, 'SELECT lastval()')) |
|
97 |
elif module == 'MySQLdb': return db.insert_id() |
|
98 |
else: return None |
|
88 | 99 |
|
89 | 100 |
def try_insert(db, table, row): |
90 | 101 |
try: return with_savepoint(db, lambda: insert(db, table, row)) |
Also available in: Unified diff
map: Implemented DB input support for querying a single table