-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryapi.py
More file actions
181 lines (133 loc) · 4.36 KB
/
Copy pathqueryapi.py
File metadata and controls
181 lines (133 loc) · 4.36 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
"""
"""
from functools import partial
from sqlquery import _querybuilder
from sqlquery._querybuilder import QueryBuilder
SQLFunction = _querybuilder.SQLFunction
InvalidQueryException = _querybuilder.InvalidQueryException
def AND(*conditions):
"""
Similar to the :py:func:`.XOR` function except the boolean operator is `AND`
"""
assert conditions
return _querybuilder.logical_and(conditions)
def OR(*conditions):
"""
Similar to the :py:func:`.XOR` function except the boolean operator is `OR`
"""
assert conditions
return _querybuilder.logical_or(conditions)
def XOR(*conditions):
"""
Creates an XOR clause between all conditions, e.g.
::
x <> 1 XOR y <> 2
*conditions* should be a list of column names.
"""
assert conditions
return _querybuilder.logical_xor(conditions)
def ASC(field):
"""
Similar to the :py:func:`.DESC` function except creates an ascending order
by clause.
"""
assert field
return _querybuilder.order_ascending(field)
def DESC(field):
"""
Creates a descending order by clause on a field which can be used with the
:py:meth:`.QueryBuilder.order_by` function.
*field* is be a column name.
"""
assert field
return _querybuilder.order_descending(field)
def COUNT(field=None):
"""
The count function for a select/where/having clause,
e.g.
::
`SELECT COUNT(x) ...`
*field* is an optional argument which should be column name. If it is not
present then `COUNT(1)` is generated
"""
return _querybuilder.count(field)
def MAX(field):
"""
The max function for a select/where/having clause, e.g. `SELECT MAX(x) ...`
*field* should be a column name.
"""
return _querybuilder.max(field)
def MIN(field):
"""
The same as :py:func:`~.MAX` but instead generating a `MIN` function.
"""
return _querybuilder.min(field)
def SUM(field):
"""
The same as :py:func:`~.MAX` but instead generating a `SUM` function.
"""
return _querybuilder.sum(field)
def UTCNOW():
"""
Generates the scalar `UTC_NOW()` function.
"""
return _querybuilder.utcnow()
def UNIX_TIMESTAMP():
"""
Generates the scalar `UTC_NOW()` function.
"""
return _querybuilder.unix_timestamp()
def create_function_builder(sql_func):
"""
Create a custom SQL function builder. This allows you to extend the current
functionality to apply a custom function.
For example, say you had a function named `MOVING_AVG`, then you could do:
::
>>> moving_avg = create_function_builder("MOVING_AVG")
>>> select(moving_avg("temperature")).on_table("weather").sql()
(u'SELECT MOVING_AVG(`a`.`temperature`) FROM `weather` AS `a`', ())
"""
return partial(_querybuilder.SQLFunction, sql_func)
def select(*names):
"""
Create a select clause, e.g. `SELECT ...`
Each value in *names* should be a string identifying a column.
"""
return QueryBuilder().select(*names)
def update(**data):
"""
Create an update clause, e.g. `UPDATE ...`
Each key/value in *data* should represent a column/value that will be
updated. The key should be a string, and value can/should be any type
that is consumed by your DB client library.
Note that no conversion is done on these values, so, for example, if you
pass a `datetime` instance, then that will remain a `datetime` instance
when you get back the list of arguments along with the query string in
:py:meth:`.QueryBuilder.sql`.
"""
return QueryBuilder().update(**data)
def insert(*data):
"""
Create an insert clause, e.g. `INSERT INTO ...`
Each value in *data* should be dictionary with key/values representing
columns/values. The format of this should be the same as in
:py:func:`.update`.
"""
return QueryBuilder().insert(*data)
def insert_ignore(*data):
"""
The same interface as :py:func:`.insert`, however a `INSERT IGNORE`
statement is generated rather than an `INSERT`.
"""
return QueryBuilder().insert_ignore(*data)
def replace(*data):
"""
The same interface as :py:func:`.insert`, however a `REPLACE` statement
is generated rather than an `INSERT`.
"""
return QueryBuilder().replace(*data)
def delete():
"""
Create a delete clause, e.g. `DELETE ...`
"""
return QueryBuilder().delete()