-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
69 lines (54 loc) · 2.14 KB
/
query.py
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
# User.select(User.name, User.age).where(or_(User.name == 'Ivan', User.age > 10)).get(10)
def and_(*expressions):
fields = [expr[0] for expr in expressions]
result = ' AND '.join(fields)
return '('+result+')', [expr[1] for expr in expressions]
def or_(*expressions):
fields = [expr[0] for expr in expressions]
result = ' OR '.join(fields)
return '('+result+')', [expr[1] for expr in expressions]
class Query(object):
def __init__(self, table_class=None, fields=None):
if table_class.database.db_type == 'postgres':
self.table = table_class.schema_and_table
else:
self.table = table_class.table
self.cursor = table_class.cursor
self.placeholder = table_class.placeholder
if not fields:
self.selected_fields = 'SELECT * FROM {} '.format(self.table)
else:
temp = ', '.join([f.name for f in fields])
self.selected_fields = 'SELECT {} FROM {} '.format(temp, self.table)
self.lim = 1
self.query = self.selected_fields
self.values = []
def __repr__(self):
return "Query object: Table: {} | Fields: {}".format(self.table, self.query)
def where(self, expr):
if 'WHERE' not in self.query:
self.query += 'WHERE {} '.format(expr[0])
else:
self.query += 'AND {} '.format(expr[0])
if hasattr(expr[1], '__iter__'):
self.values.extend(expr[1])
else:
self.values.append(expr[1])
self.query = self.query.replace('?', self.placeholder)
return self
# how many rows to fetch
def limit(self, count):
self.lim = count
return self
def get(self):
self.cursor.execute(self.query, self.values)
return self.cursor.fetchmany(self.lim)
def first(self):
self.cursor.execute(self.query, self.values)
return self.cursor.fetchone()
def one(self):
self.cursor.execute(self.query, self.values)
temp = self.cursor.fetchmany(2)
if temp is None or len(temp) > 1:
raise Exception("No such unique row in the table!")
return temp[0]