@@ -43,7 +43,8 @@ pip install https://github.com/co0lc0der/simple-query-builder-python/archive/mai
43
43
- ` reset() ` resets state to default values (except PDO property)
44
44
- ` all() ` executes SQL query and returns all rows of result (` fetchall() ` )
45
45
- ` one() ` executes SQL query and returns the first row of result (` fetchone() ` )
46
- - ` column(col_index) ` executes SQL query and returns the first column of result, ` col_index ` is ` 0 ` by default
46
+ - ` column(col_index) ` executes SQL query and returns the needed column of result, ` col_index ` is ` 0 ` by default
47
+ - ` pluck(key_index, col_index) ` executes SQL query and returns a list of tuples (the key (usually ID) and the needed column of result), ` key_index ` is ` 0 ` and ` col_index ` is ` 1 ` by default
47
48
- ` go() ` this method is for non ` SELECT ` queries. it executes SQL query and returns nothing (but returns the last inserted row ID for ` INSERT ` method)
48
49
- ` count() ` prepares a query with SQL ` COUNT(*) ` function and executes it
49
50
- ` query(sql, params, fetch_type, col_index) ` executes prepared ` sql ` with ` params ` , it can be used for custom queries
@@ -66,13 +67,17 @@ SELECT * FROM `users`;
66
67
- Select a row with a condition
67
68
``` python
68
69
results = qb.select(' users' ).where([[' id' , ' =' , 10 ]]).one()
70
+ # or since 0.3.4
71
+ results = qb.select(' users' ).where([[' id' , 10 ]]).one()
69
72
```
70
73
``` sql
71
74
SELECT * FROM ` users` WHERE ` id` = 10 ;
72
75
```
73
76
- Select rows with two conditions
74
77
``` python
75
78
results = qb.select(' users' ).where([[' id' , ' >' , 1 ], ' and' , [' group_id' , ' =' , 2 ]]).all()
79
+ # or since 0.3.4
80
+ results = qb.select(' users' ).where([[' id' , ' >' , 1 ], ' and' , [' group_id' , 2 ]]).all()
76
81
```
77
82
``` sql
78
83
SELECT * FROM ` users` WHERE (` id` > 1 ) AND (` group_id` = 2 );
@@ -101,6 +106,12 @@ results = qb.select('posts')\
101
106
.offset(14 )\
102
107
.limit(7 )\
103
108
.all()
109
+ # or since 0.3.4
110
+ results = qb.select(' posts' )\
111
+ .where([[' user_id' , 3 ]])\
112
+ .offset(14 )\
113
+ .limit(7 )\
114
+ .all()
104
115
```
105
116
``` sql
106
117
SELECT * FROM ` posts` WHERE (` user_id` = 3 ) OFFSET 14 LIMIT 7 ;
@@ -118,8 +129,13 @@ SELECT COUNT(*) AS `counter` FROM `users`;
118
129
2 . ` ORDER BY `
119
130
``` python
120
131
results = qb.select({' b' : ' branches' }, [' b.id' , ' b.name' ])\
121
- .where([[' b.id' , ' >' , 1 ], ' and' , [' b.parent_id' , ' =' , 1 ]])\
122
- .orderBy(' b.id' , ' desc' )\
132
+ .where([[' b.id' , ' >' , 1 ], ' and' , [' b.parent_id' , 1 ]])\
133
+ .order_by(' b.id' , ' desc' )\
134
+ .all()
135
+ # or since 0.3.4
136
+ results = qb.select({' b' : ' branches' }, [' b.id' , ' b.name' ])\
137
+ .where([[' b.id' , ' >' , 1 ], ' and' , [' b.parent_id' , 1 ]])\
138
+ .order_by(' b.id desc' )\
123
139
.all()
124
140
```
125
141
``` sql
@@ -131,7 +147,7 @@ ORDER BY `b`.`id` DESC;
131
147
``` python
132
148
results = qb.select(' posts' , [' id' , ' category' , ' title' ])\
133
149
.where([[' views' , ' >=' , 1000 ]])\
134
- .groupBy (' category' )\
150
+ .group_by (' category' )\
135
151
.all()
136
152
```
137
153
``` sql
@@ -141,14 +157,20 @@ WHERE (`views` >= 1000) GROUP BY `category`;
141
157
``` python
142
158
groups = qb.select(' orders' , {' month_num' : ' MONTH(`created_at`)' , ' total' : ' SUM(`total`)' })\
143
159
.where([[' YEAR(`created_at`)' , ' =' , 2020 ]])\
144
- .groupBy(' month_num' )\
145
- .having([[' total' , ' >' , 20000 ]])\
160
+ .group_by(' month_num' )\
161
+ .having([[' total' , ' =' , 20000 ]])\
162
+ .all()
163
+ # or since 0.3.4
164
+ groups = qb.select(' orders' , {' month_num' : ' MONTH(`created_at`)' , ' total' : ' SUM(`total`)' })\
165
+ .where([[' YEAR(`created_at`)' , 2020 ]])\
166
+ .group_by(' month_num' )\
167
+ .having([[' total' , 20000 ]])\
146
168
.all()
147
169
```
148
170
``` sql
149
171
SELECT MONTH(` created_at` ) AS ` month_num` , SUM (` total` ) AS ` total`
150
172
FROM ` orders` WHERE (YEAR(` created_at` ) = 2020 )
151
- GROUP BY ` month_num` HAVING (` total` > 20000 );
173
+ GROUP BY ` month_num` HAVING (` total` = 20000 );
152
174
```
153
175
4 . ` JOIN ` . Supports ` INNER ` , ` LEFT OUTER ` , ` RIGHT OUTER ` , ` FULL OUTER ` and ` CROSS ` joins (` INNER ` is by default)
154
176
``` python
@@ -191,7 +213,36 @@ FROM `cabs_printers` AS `cp`
191
213
INNER JOIN ` cabs` AS ` cb` ON ` cp` .` cab_id` = ` cb` .` id`
192
214
INNER JOIN ` printer_models` AS ` p` ON ` cp` .` printer_id` = ` p` .` id`
193
215
INNER JOIN ` cartridge_types` AS ` c` ON p .cartridge_id = c .id
194
- WHERE (` cp` .` cab_id` IN (11 ,12 ,13 )) OR (` cp` .` cab_id` = 5 ) AND (` p` .` id` > ` c` .` id` );
216
+ WHERE (` cp` .` cab_id` IN (11 , 12 , 13 )) OR (` cp` .` cab_id` = 5 ) AND (` p` .` id` > ` c` .` id` );
217
+ ```
218
+ ``` python
219
+ # since 0.3.4
220
+ results = qb.select({' cp' : ' cabs_printers' }, [
221
+ ' cp.id' ,
222
+ ' cp.cab_id' ,
223
+ {' cab_name' : ' cb.name' },
224
+ ' cp.printer_id' ,
225
+ {' cartridge_id' : ' c.id' },
226
+ {' printer_name' : ' p.name' },
227
+ {' cartridge_type' : ' c.name' },
228
+ ' cp.comment'
229
+ ])\
230
+ .join({' cb' : ' cabs' }, [' cp.cab_id' , ' cb.id' ])\
231
+ .join({' p' : ' printer_models' }, [' cp.printer_id' , ' p.id' ])\
232
+ .join({' c' : ' cartridge_types' }, [' p.cartridge_id' , ' c.id' ])\
233
+ .group_by([' cp.printer_id' , ' cartridge_id' ])\
234
+ .order_by([' cp.cab_id' , ' cp.printer_id desc' ])\
235
+ .all()
236
+ ```
237
+ ``` sql
238
+ SELECT ` cp` .` id` , ` cp` .` cab_id` , ` cb` .` name` AS ` cab_name` , ` cp` .` printer_id` , ` c` .` id` AS ` cartridge_id` ,
239
+ ` p` .` name` AS ` printer_name` , ` c` .` name` AS ` cartridge_type` , ` cp` .` comment`
240
+ FROM ` cabs_printers` AS ` cp`
241
+ INNER JOIN ` cabs` AS ` cb` ON ` cp` .` cab_id` = ` cb` .` id`
242
+ INNER JOIN ` printer_models` AS ` p` ON ` cp` .` printer_id` = ` p` .` id`
243
+ INNER JOIN ` cartridge_types` AS ` c` ON ` p` .` cartridge_id` = ` c` .` id`
244
+ GROUP BY ` cp` .` printer_id` , ` cartridge_id`
245
+ ORDER BY ` cp` .` cab_id` ASC , ` cp` .` printer_id` DESC ;
195
246
```
196
247
- Insert a row
197
248
``` python
@@ -228,6 +279,14 @@ qb.update('users', {
228
279
.where([[' id' , ' =' , 7 ]])\
229
280
.limit()\
230
281
.go()
282
+ # or since 0.3.4
283
+ qb.update(' users' , {
284
+ ' username' : ' John Doe' ,
285
+ ' status' : ' new status'
286
+ })\
287
+ .where([[' id' , 7 ]])\
288
+ .limit()\
289
+ .go()
231
290
```
232
291
``` sql
233
292
UPDATE ` users` SET ` username` = ' John Doe' , ` status` = ' new status'
@@ -249,6 +308,11 @@ qb.delete('users')\
249
308
.where([[' name' , ' =' , ' John' ]])\
250
309
.limit()\
251
310
.go()
311
+ # or since 0.3.4
312
+ qb.delete(' users' )\
313
+ .where([[' name' , ' John' ]])\
314
+ .limit()\
315
+ .go()
252
316
```
253
317
``` sql
254
318
DELETE FROM ` users` WHERE ` name` = ' John' LIMIT 1 ;
@@ -258,6 +322,10 @@ DELETE FROM `users` WHERE `name` = 'John' LIMIT 1;
258
322
qb.delete(' comments' )\
259
323
.where([[' user_id' , ' =' , 10 ]])\
260
324
.go()
325
+ # or since 0.3.4
326
+ qb.delete(' comments' )\
327
+ .where([[' user_id' , 10 ]])\
328
+ .go()
261
329
```
262
330
``` sql
263
331
DELETE FROM ` comments` WHERE ` user_id` = 10 ;
0 commit comments