@@ -52,9 +52,22 @@ class QueryBuilder:
52
52
"IN" ,
53
53
"NOT IN" ,
54
54
]
55
- _LOGICS : list = ["AND" , "OR" , "NOT" ]
56
- _SORT_TYPES : list = ["ASC" , "DESC" ]
57
- _JOIN_TYPES : list = ["INNER" , "LEFT OUTER" , "RIGHT OUTER" , "FULL OUTER" , "CROSS" ]
55
+ _LOGICS : list = [
56
+ "AND" ,
57
+ "OR" ,
58
+ "NOT"
59
+ ]
60
+ _SORT_TYPES : list = [
61
+ "ASC" ,
62
+ "DESC"
63
+ ]
64
+ _JOIN_TYPES : list = [
65
+ "INNER" ,
66
+ "LEFT OUTER" ,
67
+ "RIGHT OUTER" ,
68
+ "FULL OUTER" ,
69
+ "CROSS"
70
+ ]
58
71
_NO_FETCH : int = 0
59
72
_FETCH_ONE : int = 1
60
73
_FETCH_ALL : int = 2
@@ -124,12 +137,12 @@ def query(self, sql: str = "", params=(), fetch=2, column=0):
124
137
return self
125
138
126
139
def add_semicolon (self , sql : str = "" ) -> str :
127
- new_sql = self ._sql if sql == "" else sql
140
+ new_sql = self ._sql if not sql else sql
128
141
129
- if new_sql != "" :
142
+ if new_sql :
130
143
new_sql += ";" if new_sql [- 1 ] != ";" else ""
131
144
132
- if sql == "" :
145
+ if not sql :
133
146
self ._sql = new_sql
134
147
135
148
return new_sql
@@ -196,11 +209,11 @@ def pluck(self, key: int = 0, column: int = 1) -> Union[tuple, list, dict, None]
196
209
return [(x [key ], x [column ]) for x in self ._result ]
197
210
198
211
def count (self , table : Union [str , dict ], field : str = "" ):
199
- if table == "" or table == {} :
212
+ if not table :
200
213
self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
201
214
return self
202
215
203
- if field == "" :
216
+ if not field :
204
217
self .select (table , "COUNT(*) AS `counter`" )
205
218
else :
206
219
field = field .replace ("." , "`.`" )
@@ -219,10 +232,8 @@ def exists(self) -> bool:
219
232
result = self .one ()
220
233
return self ._count > 0
221
234
222
- def _prepare_aliases (
223
- self , items : Union [str , list , dict ], as_list : bool = False
224
- ) -> Union [str , list ]:
225
- if items == "" or items == {} or items == []:
235
+ def _prepare_aliases (self , items : Union [str , list , dict ], as_list : bool = False ) -> Union [str , list ]:
236
+ if not items :
226
237
self .set_error (f"Empty items in { inspect .stack ()[0 ][3 ]} method" )
227
238
return ""
228
239
@@ -233,37 +244,36 @@ def _prepare_aliases(
233
244
for item in items :
234
245
if isinstance (items , list ):
235
246
if isinstance (item , str ):
236
- sql .append (f" { item } " )
247
+ sql .append (item )
237
248
elif isinstance (item , dict ):
238
249
first_item = list (item .values ())[0 ]
239
250
alias = list (item .keys ())[0 ]
240
251
sql .append (
241
- f" { first_item } "
252
+ first_item
242
253
if isinstance (alias , int )
243
254
else f"{ first_item } AS { alias } "
244
255
)
245
256
elif isinstance (items , dict ):
246
257
new_item = items [item ]
247
- sql .append (
248
- f"{ new_item } "
249
- if isinstance (item , int )
250
- else f"{ new_item } AS { item } "
251
- )
258
+ sql .append (new_item if isinstance (item , int ) else f"{ new_item } AS { item } " )
252
259
else :
253
260
self .set_error (f"Incorrect type of items in { inspect .stack ()[0 ][3 ]} method" )
254
261
return ""
255
262
256
263
return self ._prepare_fieldlist (sql ) if not as_list else sql
257
264
258
265
def _prepare_conditions (self , where : Union [str , list ]) -> dict :
259
- result = {"sql" : "" , "values" : []}
266
+ result = {
267
+ "sql" : "" ,
268
+ "values" : []
269
+ }
260
270
sql = ""
261
271
262
272
if not where :
263
273
return result
264
274
265
275
if isinstance (where , str ):
266
- sql += f" { where } "
276
+ sql += where
267
277
elif isinstance (where , list ):
268
278
for cond in where :
269
279
if isinstance (cond , list ):
@@ -315,42 +325,34 @@ def _prepare_conditions(self, where: Union[str, list]) -> dict:
315
325
return result
316
326
317
327
def select (self , table : Union [str , dict ], fields : Union [str , list , dict ] = "*" ):
318
- if table == "" or table == {} or fields == "" or fields == [] or fields == {} :
328
+ if not table or not fields :
319
329
self .set_error (f"Empty table or fields in { inspect .stack ()[0 ][3 ]} method" )
320
330
return self
321
331
322
332
self .reset ()
323
333
324
- if (
325
- isinstance (fields , dict )
326
- or isinstance (fields , list )
327
- or isinstance (fields , str )
328
- ):
334
+ if isinstance (fields , dict ) or isinstance (fields , list ) or isinstance (fields , str ):
329
335
self ._sql = f"SELECT { self ._prepare_aliases (fields )} "
330
336
else :
331
- self .set_error (
332
- f"Incorrect type of fields in { inspect .stack ()[0 ][3 ]} method. Fields must be String, List or Dictionary"
333
- )
337
+ self .set_error (f"Incorrect type of fields in { inspect .stack ()[0 ][3 ]} method. Fields must be String, List or Dictionary" )
334
338
return self
335
339
336
340
if isinstance (table , dict ) or isinstance (table , str ):
337
341
self ._sql += f" FROM { self ._prepare_aliases (table )} "
338
342
else :
339
- self .set_error (
340
- f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary"
341
- )
343
+ self .set_error (f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary" )
342
344
return self
343
345
344
346
return self
345
347
346
348
def where (self , where : Union [str , list ], addition : str = "" ):
347
- if where == "" or where == [] :
349
+ if not where :
348
350
self .set_error (f"Empty where in { inspect .stack ()[0 ][3 ]} method" )
349
351
return self
350
352
351
353
conditions = self ._prepare_conditions (where )
352
354
353
- if addition != "" :
355
+ if addition :
354
356
self ._sql += f" WHERE { conditions ['sql' ]} { addition } "
355
357
else :
356
358
self ._sql += f" WHERE { conditions ['sql' ]} "
@@ -361,7 +363,7 @@ def where(self, where: Union[str, list], addition: str = ""):
361
363
return self
362
364
363
365
def having (self , having : Union [str , list ]):
364
- if having == "" or having == [] :
366
+ if not having :
365
367
self .set_error (f"Empty having in { inspect .stack ()[0 ][3 ]} method" )
366
368
return self
367
369
@@ -411,7 +413,7 @@ def _prepare_sorting(self, field: str = "", sort: str = "") -> tuple:
411
413
return field , sort
412
414
413
415
def _prepare_field (self , field : str = "" ) -> str :
414
- if field == "" :
416
+ if not field :
415
417
self .set_error (f"Empty field in { inspect .stack ()[0 ][3 ]} method" )
416
418
return ""
417
419
@@ -420,15 +422,15 @@ def _prepare_field(self, field: str = "") -> str:
420
422
field = field .replace (" AS " , " AS `" )
421
423
return f"{ field } `"
422
424
else :
423
- return f" { field } "
425
+ return field
424
426
else :
425
427
field = field .replace ("." , "`.`" )
426
428
field = field .replace (" AS " , "` AS `" )
427
429
return f"`{ field } `"
428
430
429
431
def _prepare_fieldlist (self , fields : Union [str , tuple , list ] = ()) -> str :
430
432
result = ""
431
- if fields == "" or fields == () or fields == [] :
433
+ if not fields :
432
434
self .set_error (f"Empty fields in { inspect .stack ()[0 ][3 ]} method" )
433
435
return result
434
436
@@ -441,7 +443,7 @@ def _prepare_fieldlist(self, fields: Union[str, tuple, list] = ()) -> str:
441
443
return result
442
444
443
445
def order_by (self , field : Union [str , tuple , list ] = (), sort : str = "" ):
444
- if field == "" or field == () or field == [] :
446
+ if not field :
445
447
self .set_error (f"Empty field in { inspect .stack ()[0 ][3 ]} method" )
446
448
return self
447
449
@@ -462,7 +464,7 @@ def order_by(self, field: Union[str, tuple, list] = (), sort: str = ""):
462
464
return self
463
465
464
466
def group_by (self , field : Union [str , tuple , list ] = ()):
465
- if field == "" or field == () or field == [] :
467
+ if not field :
466
468
self .set_error (f"Empty field in { inspect .stack ()[0 ][3 ]} method" )
467
469
return self
468
470
@@ -471,16 +473,14 @@ def group_by(self, field: Union[str, tuple, list] = ()):
471
473
return self
472
474
473
475
def delete (self , table : Union [str , dict ]):
474
- if table == "" or table == {} :
476
+ if not table :
475
477
self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
476
478
return self
477
479
478
480
if isinstance (table , dict ) or isinstance (table , str ):
479
481
table = self ._prepare_aliases (table )
480
482
else :
481
- self .set_error (
482
- f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary"
483
- )
483
+ self .set_error (f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary" )
484
484
return self
485
485
486
486
self .reset ()
@@ -489,63 +489,49 @@ def delete(self, table: Union[str, dict]):
489
489
return self
490
490
491
491
def insert (self , table : Union [str , dict ], fields : Union [list , dict ]):
492
- if table == "" or table == {} or fields == [] or fields == {} :
492
+ if not table or not fields :
493
493
self .set_error (f"Empty table or fields in { inspect .stack ()[0 ][3 ]} method" )
494
494
return self
495
495
496
496
if isinstance (table , dict ) or isinstance (table , str ):
497
497
table = self ._prepare_aliases (table )
498
498
else :
499
- self .set_error (
500
- f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary"
501
- )
499
+ self .set_error (f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary" )
502
500
return self
503
501
504
502
self .reset ()
505
503
506
504
if isinstance (fields , dict ):
507
505
values = ("?," * len (fields )).rstrip ("," )
508
- self ._sql = (
509
- f"INSERT INTO { table } ("
510
- + self ._prepare_fieldlist (list (fields .keys ()))
511
- + f") VALUES ({ values } )"
512
- )
506
+ self ._sql = f"INSERT INTO { table } (" + self ._prepare_fieldlist (list (fields .keys ())) + f") VALUES ({ values } )"
513
507
self ._params = tuple (fields .values ())
514
508
elif isinstance (fields , list ):
515
509
names = fields .pop (0 )
516
510
value = ("?," * len (names )).rstrip ("," )
517
511
v = f"({ value } ),"
518
512
values = (v * len (fields )).rstrip ("," )
519
- self ._sql = (
520
- f"INSERT INTO { table } ("
521
- + self ._prepare_fieldlist (names )
522
- + f") VALUES { values } "
523
- )
513
+ self ._sql = f"INSERT INTO { table } (" + self ._prepare_fieldlist (names ) + f") VALUES { values } "
524
514
params = []
525
515
for item in fields :
526
516
if isinstance (item , list ):
527
517
for subitem in item :
528
518
params .append (subitem )
529
519
self ._params = tuple (params )
530
520
else :
531
- self .set_error (
532
- f"Incorrect type of fields in { inspect .stack ()[0 ][3 ]} method. Fields must be String, List or Dictionary"
533
- )
521
+ self .set_error (f"Incorrect type of fields in { inspect .stack ()[0 ][3 ]} method. Fields must be String, List or Dictionary" )
534
522
return self
535
523
536
524
return self
537
525
538
526
def update (self , table : Union [str , dict ], fields : Union [list , dict ]):
539
- if table == "" or table == {} or fields == [] or fields == {} :
527
+ if not table or not fields :
540
528
self .set_error (f"Empty table or fields in { inspect .stack ()[0 ][3 ]} method" )
541
529
return self
542
530
543
531
if isinstance (table , dict ) or isinstance (table , str ):
544
532
table = self ._prepare_aliases (table )
545
533
else :
546
- self .set_error (
547
- f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary"
548
- )
534
+ self .set_error (f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary" )
549
535
return self
550
536
551
537
if isinstance (fields , list ) or isinstance (fields , dict ):
@@ -554,9 +540,7 @@ def update(self, table: Union[str, dict], fields: Union[list, dict]):
554
540
sets += f" { self ._prepare_field (item )} = ?,"
555
541
sets = sets .rstrip ("," )
556
542
else :
557
- self .set_error (
558
- f"Incorrect type of fields in { inspect .stack ()[0 ][3 ]} method. Fields must be String, List or Dictionary"
559
- )
543
+ self .set_error (f"Incorrect type of fields in { inspect .stack ()[0 ][3 ]} method. Fields must be String, List or Dictionary" )
560
544
return self
561
545
562
546
self .reset ()
@@ -566,29 +550,20 @@ def update(self, table: Union[str, dict], fields: Union[list, dict]):
566
550
567
551
return self
568
552
569
- def join (
570
- self ,
571
- table : Union [str , dict ] = "" ,
572
- on : Union [str , tuple , list ] = (),
573
- join_type : str = "INNER" ,
574
- ):
553
+ def join (self , table : Union [str , dict ] = "" , on : Union [str , tuple , list ] = (), join_type : str = "INNER" ):
575
554
join_type = join_type .upper ()
576
555
if join_type == "" or join_type not in self ._JOIN_TYPES :
577
- self .set_error (
578
- f"Empty join_type or is not allowed in { inspect .stack ()[0 ][3 ]} method"
579
- )
556
+ self .set_error (f"Empty join_type or is not allowed in { inspect .stack ()[0 ][3 ]} method" )
580
557
return self
581
558
582
- if table == "" or table == {} :
559
+ if not table :
583
560
self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
584
561
return self
585
562
586
563
if isinstance (table , dict ) or isinstance (table , str ):
587
564
self ._sql += f" { join_type } JOIN { self ._prepare_aliases (table )} "
588
565
else :
589
- self .set_error (
590
- f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary"
591
- )
566
+ self .set_error (f"Incorrect type of table in { inspect .stack ()[0 ][3 ]} method. Table must be String or Dictionary" )
592
567
return self
593
568
594
569
if on :
@@ -599,17 +574,15 @@ def join(
599
574
elif isinstance (on , str ):
600
575
self ._sql += f" ON { on } "
601
576
else :
602
- self .set_error (
603
- f"Incorrect type of on in { inspect .stack ()[0 ][3 ]} method. On must be String, Tuple or List"
604
- )
577
+ self .set_error (f"Incorrect type of on in { inspect .stack ()[0 ][3 ]} method. On must be String, Tuple or List" )
605
578
return self
606
579
607
580
self .set_error ()
608
-
609
581
return self
610
582
611
583
def drop (self , table : str , add_exists : bool = True ):
612
- if table == "" :
584
+ # this method will be moved to another class
585
+ if not table :
613
586
self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
614
587
return self
615
588
@@ -621,7 +594,8 @@ def drop(self, table: str, add_exists: bool = True):
621
594
return self
622
595
623
596
def truncate (self , table : str ):
624
- if table == "" :
597
+ # this method will be moved to another class
598
+ if not table :
625
599
self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
626
600
return self
627
601
0 commit comments