@@ -43,7 +43,8 @@ pip install https://github.com/co0lc0der/simple-query-builder-python/archive/mai
4343- ` reset() ` resets state to default values (except PDO property)
4444- ` all() ` executes SQL query and returns all rows of result (` fetchall() ` )
4545- ` 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
4748- ` 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)
4849- ` count() ` prepares a query with SQL ` COUNT(*) ` function and executes it
4950- ` 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`;
6667- Select a row with a condition
6768``` python
6869results = qb.select(' users' ).where([[' id' , ' =' , 10 ]]).one()
70+ # or since 0.3.4
71+ results = qb.select(' users' ).where([[' id' , 10 ]]).one()
6972```
7073``` sql
7174SELECT * FROM ` users` WHERE ` id` = 10 ;
7275```
7376- Select rows with two conditions
7477``` python
7578results = 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()
7681```
7782``` sql
7883SELECT * FROM ` users` WHERE (` id` > 1 ) AND (` group_id` = 2 );
@@ -101,6 +106,12 @@ results = qb.select('posts')\
101106 .offset(14 )\
102107 .limit(7 )\
103108 .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()
104115```
105116``` sql
106117SELECT * FROM ` posts` WHERE (` user_id` = 3 ) OFFSET 14 LIMIT 7 ;
@@ -118,8 +129,13 @@ SELECT COUNT(*) AS `counter` FROM `users`;
1181292 . ` ORDER BY `
119130``` python
120131results = 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' )\
123139 .all()
124140```
125141``` sql
@@ -131,7 +147,7 @@ ORDER BY `b`.`id` DESC;
131147``` python
132148results = qb.select(' posts' , [' id' , ' category' , ' title' ])\
133149 .where([[' views' , ' >=' , 1000 ]])\
134- .groupBy (' category' )\
150+ .group_by (' category' )\
135151 .all()
136152```
137153``` sql
@@ -141,14 +157,20 @@ WHERE (`views` >= 1000) GROUP BY `category`;
141157``` python
142158groups = qb.select(' orders' , {' month_num' : ' MONTH(`created_at`)' , ' total' : ' SUM(`total`)' })\
143159 .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 ]])\
146168 .all()
147169```
148170``` sql
149171SELECT MONTH(` created_at` ) AS ` month_num` , SUM (` total` ) AS ` total`
150172FROM ` orders` WHERE (YEAR(` created_at` ) = 2020 )
151- GROUP BY ` month_num` HAVING (` total` > 20000 );
173+ GROUP BY ` month_num` HAVING (` total` = 20000 );
152174```
1531754 . ` JOIN ` . Supports ` INNER ` , ` LEFT OUTER ` , ` RIGHT OUTER ` , ` FULL OUTER ` and ` CROSS ` joins (` INNER ` is by default)
154176``` python
@@ -191,7 +213,36 @@ FROM `cabs_printers` AS `cp`
191213INNER JOIN ` cabs` AS ` cb` ON ` cp` .` cab_id` = ` cb` .` id`
192214INNER JOIN ` printer_models` AS ` p` ON ` cp` .` printer_id` = ` p` .` id`
193215INNER 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 ;
195246```
196247- Insert a row
197248``` python
@@ -228,6 +279,14 @@ qb.update('users', {
228279 .where([[' id' , ' =' , 7 ]])\
229280 .limit()\
230281 .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()
231290```
232291``` sql
233292UPDATE ` users` SET ` username` = ' John Doe' , ` status` = ' new status'
@@ -249,6 +308,11 @@ qb.delete('users')\
249308 .where([[' name' , ' =' , ' John' ]])\
250309 .limit()\
251310 .go()
311+ # or since 0.3.4
312+ qb.delete(' users' )\
313+ .where([[' name' , ' John' ]])\
314+ .limit()\
315+ .go()
252316```
253317``` sql
254318DELETE FROM ` users` WHERE ` name` = ' John' LIMIT 1 ;
@@ -258,6 +322,10 @@ DELETE FROM `users` WHERE `name` = 'John' LIMIT 1;
258322qb.delete(' comments' )\
259323 .where([[' user_id' , ' =' , 10 ]])\
260324 .go()
325+ # or since 0.3.4
326+ qb.delete(' comments' )\
327+ .where([[' user_id' , 10 ]])\
328+ .go()
261329```
262330``` sql
263331DELETE FROM ` comments` WHERE ` user_id` = 10 ;
0 commit comments