3 |
3 |
import random
|
4 |
4 |
import re
|
5 |
5 |
import sys
|
|
6 |
import warnings
|
6 |
7 |
|
7 |
8 |
import exc
|
8 |
9 |
import strings
|
... | ... | |
35 |
36 |
|
36 |
37 |
class EmptyRowException(DbException): pass
|
37 |
38 |
|
|
39 |
##### Warnings
|
|
40 |
|
|
41 |
class DbWarning(UserWarning): pass
|
|
42 |
|
38 |
43 |
##### Input validation
|
39 |
44 |
|
40 |
45 |
def check_name(name):
|
... | ... | |
100 |
105 |
|
101 |
106 |
def select(db, table, fields, conds, limit=None, start=None, recover=None):
|
102 |
107 |
assert limit == None or type(limit) == int
|
|
108 |
assert start == None or type(start) == int
|
103 |
109 |
check_name(table)
|
104 |
110 |
map(check_name, fields)
|
105 |
111 |
map(check_name, conds.keys())
|
|
112 |
|
106 |
113 |
def cond(entry):
|
107 |
114 |
col, value = entry
|
108 |
115 |
cond_ = esc_name(db, col)+' '
|
... | ... | |
112 |
119 |
return cond_
|
113 |
120 |
query = ('SELECT ' + ', '.join([esc_name(db, field) for field in fields])
|
114 |
121 |
+ ' FROM '+esc_name(db, table))
|
|
122 |
|
|
123 |
missing = True
|
115 |
124 |
if conds != {}:
|
116 |
125 |
query += ' WHERE '+' AND '.join(map(cond, conds.iteritems()))
|
117 |
|
if limit != None: query += ' LIMIT '+str(limit)
|
118 |
|
if start != None: query += ' OFFSET '+str(start)
|
|
126 |
missing = False
|
|
127 |
if limit != None: query += ' LIMIT '+str(limit); missing = False
|
|
128 |
if start != None:
|
|
129 |
if start != 0: query += ' OFFSET '+str(start)
|
|
130 |
missing = False
|
|
131 |
if missing: warnings.warn(DbWarning(
|
|
132 |
'SELECT statement missing a WHERE, LIMIT, or OFFSET clause: '+query))
|
|
133 |
|
119 |
134 |
return run_query(db, query, conds.values(), recover)
|
120 |
135 |
|
121 |
136 |
def insert(db, table, row, recover=None):
|
sql.py: Print warning if SELECT statement missing a WHERE, LIMIT, or OFFSET clause. Changed bin/map DB input get-all-rows statement to pass start=0 to suppress this warning for that statement.