-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorrorm.py
More file actions
184 lines (135 loc) · 4.55 KB
/
Copy pathhorrorm.py
File metadata and controls
184 lines (135 loc) · 4.55 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from cursors import connect
class F(object):
"""
A wrapper around F(name). Using __getattr__ as a constructor? Horrible.
"""
def __getattr__(self, field_name):
return Field(field_name)
f = F()
class Field(object):
"""
Fields override arithmetic to generate conditions? Horrible.
"""
def __init__(self, field_name):
self.field_name = field_name
def sql(self, param):
return str(self)
def __str__(self):
return self.field_name
def __eq__(self, rhs):
connector = '='
if rhs is None:
rhs = NULL()
connector = 'IS'
return Where(self, connector, rhs)
def __ne__(self, rhs):
connector = '!='
if rhs is None:
rhs = NULL()
connector = 'IS NOT'
return Where(self, connector, rhs)
def __lshift__(self, rhs):
# <<
return Where(self, 'IN', InClause(rhs))
def __gt__(self, rhs):
return Where(self, '>', rhs)
def __ge__(self, rhs):
return Where(self, '>=', rhs)
def __lt__(self, rhs):
return Where(self, '<', rhs)
def __le__(self, rhs):
return Where(self, '<=', rhs)
def __mod__(self, rhs):
# %
return Where(self, 'LIKE', rhs)
def __getattr__(self, attr):
return Field('%s.%s' % (self.field_name, attr))
def params(self):
return []
class InClause(object):
def __init__(self, iterable):
self.iterable = iterable
def sql(self, param):
return '(%s)' % ', '.join([param for i in self.iterable])
def params(self):
return self.iterable
class NULL(object):
def sql(self, param):
return 'NULL'
def params(self):
return []
class Where(object):
def __init__(self, lhs, connector, rhs):
self.lhs = lhs
self.connector = connector
self.rhs = rhs
def __invert__(self):
# ~
return Where(None, 'NOT', self)
def __and__(self, rhs):
return Where(self, 'AND', rhs)
def __or__(self, rhs):
return Where(self, 'OR', rhs)
def sql(self, param):
if hasattr(self.rhs, 'sql'):
rhs_str = self.rhs.sql(param)
else:
rhs_str = param
if self.lhs is None:
return '(%s %s)' % (self.connector, rhs_str)
else:
return '(%s %s %s)' % (self.lhs.sql(param), self.connector, rhs_str)
def params(self):
if hasattr(self.lhs, 'params'):
lhs_params = self.lhs.params()
else:
lhs_params = []
if hasattr(self.rhs, 'params'):
rhs_params = self.rhs.params()
else:
rhs_params = [self.rhs]
return lhs_params + rhs_params
class D(object):
"""
A Database. Attributes are tables.
"""
def __init__(self, *args, **kwargs):
self.con = connect(*args, **kwargs)
for table in self.con.tables():
# Better than overriding __getattr__(), because we get tab completion
setattr(self, table[0], T(self.con, table[0]))
def __getattr__(self, table_name):
return T(self.con, table_name)
class T(object):
"""
A table generator from a given DB
"""
def __init__(self, con, *table_names):
self.con = con
self.param = self.con.param
self.table_names = table_names
def __mul__(self, rhs):
new_names = self.table_names + rhs.table_names
return T(self.con, *new_names)
def _joined_tables(self):
return ', '.join(self.table_names)
def select(self, *args):
fields = []
where = None
# Parse positional args. Maybe we should use kwargs instead.
for elm in args:
if isinstance(elm, Field):
fields.append(elm.sql(self.param))
elif isinstance(elm, Where):
where = elm
joined_fields = ', '.join(fields) or '*'
if where is None:
return self.con('SELECT %s FROM %s' % (joined_fields, self._joined_tables()))
else:
return self.con('SELECT %s FROM %s WHERE %s' % (joined_fields, self._joined_tables(), where.sql(self.param)), *where.params())
# def update(self, mapping, where=None):
# def insert(self, mapping, where=None):
def delete(self, where):
# This will error if you've specified multiple tables, but that's
# probably better than blithely just deleting from the first one.
return self.con('DELETE FROM %s WHERE %s' % (self._joined_tables(), where.sql(self.param)), *where.params())