@@ -52,9 +52,22 @@ class QueryBuilder:
5252 "IN" ,
5353 "NOT IN" ,
5454 ]
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+ ]
5871 _NO_FETCH : int = 0
5972 _FETCH_ONE : int = 1
6073 _FETCH_ALL : int = 2
@@ -124,12 +137,12 @@ def query(self, sql: str = "", params=(), fetch=2, column=0):
124137 return self
125138
126139 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
128141
129- if new_sql != "" :
142+ if new_sql :
130143 new_sql += ";" if new_sql [- 1 ] != ";" else ""
131144
132- if sql == "" :
145+ if not sql :
133146 self ._sql = new_sql
134147
135148 return new_sql
@@ -196,11 +209,11 @@ def pluck(self, key: int = 0, column: int = 1) -> Union[tuple, list, dict, None]
196209 return [(x [key ], x [column ]) for x in self ._result ]
197210
198211 def count (self , table : Union [str , dict ], field : str = "" ):
199- if table == "" or table == {} :
212+ if not table :
200213 self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
201214 return self
202215
203- if field == "" :
216+ if not field :
204217 self .select (table , "COUNT(*) AS `counter`" )
205218 else :
206219 field = field .replace ("." , "`.`" )
@@ -219,10 +232,8 @@ def exists(self) -> bool:
219232 result = self .one ()
220233 return self ._count > 0
221234
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 :
226237 self .set_error (f"Empty items in { inspect .stack ()[0 ][3 ]} method" )
227238 return ""
228239
@@ -233,37 +244,36 @@ def _prepare_aliases(
233244 for item in items :
234245 if isinstance (items , list ):
235246 if isinstance (item , str ):
236- sql .append (f" { item } " )
247+ sql .append (item )
237248 elif isinstance (item , dict ):
238249 first_item = list (item .values ())[0 ]
239250 alias = list (item .keys ())[0 ]
240251 sql .append (
241- f" { first_item } "
252+ first_item
242253 if isinstance (alias , int )
243254 else f"{ first_item } AS { alias } "
244255 )
245256 elif isinstance (items , dict ):
246257 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 } " )
252259 else :
253260 self .set_error (f"Incorrect type of items in { inspect .stack ()[0 ][3 ]} method" )
254261 return ""
255262
256263 return self ._prepare_fieldlist (sql ) if not as_list else sql
257264
258265 def _prepare_conditions (self , where : Union [str , list ]) -> dict :
259- result = {"sql" : "" , "values" : []}
266+ result = {
267+ "sql" : "" ,
268+ "values" : []
269+ }
260270 sql = ""
261271
262272 if not where :
263273 return result
264274
265275 if isinstance (where , str ):
266- sql += f" { where } "
276+ sql += where
267277 elif isinstance (where , list ):
268278 for cond in where :
269279 if isinstance (cond , list ):
@@ -315,42 +325,34 @@ def _prepare_conditions(self, where: Union[str, list]) -> dict:
315325 return result
316326
317327 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 :
319329 self .set_error (f"Empty table or fields in { inspect .stack ()[0 ][3 ]} method" )
320330 return self
321331
322332 self .reset ()
323333
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 ):
329335 self ._sql = f"SELECT { self ._prepare_aliases (fields )} "
330336 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" )
334338 return self
335339
336340 if isinstance (table , dict ) or isinstance (table , str ):
337341 self ._sql += f" FROM { self ._prepare_aliases (table )} "
338342 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" )
342344 return self
343345
344346 return self
345347
346348 def where (self , where : Union [str , list ], addition : str = "" ):
347- if where == "" or where == [] :
349+ if not where :
348350 self .set_error (f"Empty where in { inspect .stack ()[0 ][3 ]} method" )
349351 return self
350352
351353 conditions = self ._prepare_conditions (where )
352354
353- if addition != "" :
355+ if addition :
354356 self ._sql += f" WHERE { conditions ['sql' ]} { addition } "
355357 else :
356358 self ._sql += f" WHERE { conditions ['sql' ]} "
@@ -361,7 +363,7 @@ def where(self, where: Union[str, list], addition: str = ""):
361363 return self
362364
363365 def having (self , having : Union [str , list ]):
364- if having == "" or having == [] :
366+ if not having :
365367 self .set_error (f"Empty having in { inspect .stack ()[0 ][3 ]} method" )
366368 return self
367369
@@ -411,7 +413,7 @@ def _prepare_sorting(self, field: str = "", sort: str = "") -> tuple:
411413 return field , sort
412414
413415 def _prepare_field (self , field : str = "" ) -> str :
414- if field == "" :
416+ if not field :
415417 self .set_error (f"Empty field in { inspect .stack ()[0 ][3 ]} method" )
416418 return ""
417419
@@ -420,15 +422,15 @@ def _prepare_field(self, field: str = "") -> str:
420422 field = field .replace (" AS " , " AS `" )
421423 return f"{ field } `"
422424 else :
423- return f" { field } "
425+ return field
424426 else :
425427 field = field .replace ("." , "`.`" )
426428 field = field .replace (" AS " , "` AS `" )
427429 return f"`{ field } `"
428430
429431 def _prepare_fieldlist (self , fields : Union [str , tuple , list ] = ()) -> str :
430432 result = ""
431- if fields == "" or fields == () or fields == [] :
433+ if not fields :
432434 self .set_error (f"Empty fields in { inspect .stack ()[0 ][3 ]} method" )
433435 return result
434436
@@ -441,7 +443,7 @@ def _prepare_fieldlist(self, fields: Union[str, tuple, list] = ()) -> str:
441443 return result
442444
443445 def order_by (self , field : Union [str , tuple , list ] = (), sort : str = "" ):
444- if field == "" or field == () or field == [] :
446+ if not field :
445447 self .set_error (f"Empty field in { inspect .stack ()[0 ][3 ]} method" )
446448 return self
447449
@@ -462,7 +464,7 @@ def order_by(self, field: Union[str, tuple, list] = (), sort: str = ""):
462464 return self
463465
464466 def group_by (self , field : Union [str , tuple , list ] = ()):
465- if field == "" or field == () or field == [] :
467+ if not field :
466468 self .set_error (f"Empty field in { inspect .stack ()[0 ][3 ]} method" )
467469 return self
468470
@@ -471,16 +473,14 @@ def group_by(self, field: Union[str, tuple, list] = ()):
471473 return self
472474
473475 def delete (self , table : Union [str , dict ]):
474- if table == "" or table == {} :
476+ if not table :
475477 self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
476478 return self
477479
478480 if isinstance (table , dict ) or isinstance (table , str ):
479481 table = self ._prepare_aliases (table )
480482 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" )
484484 return self
485485
486486 self .reset ()
@@ -489,63 +489,49 @@ def delete(self, table: Union[str, dict]):
489489 return self
490490
491491 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 :
493493 self .set_error (f"Empty table or fields in { inspect .stack ()[0 ][3 ]} method" )
494494 return self
495495
496496 if isinstance (table , dict ) or isinstance (table , str ):
497497 table = self ._prepare_aliases (table )
498498 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" )
502500 return self
503501
504502 self .reset ()
505503
506504 if isinstance (fields , dict ):
507505 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 } )"
513507 self ._params = tuple (fields .values ())
514508 elif isinstance (fields , list ):
515509 names = fields .pop (0 )
516510 value = ("?," * len (names )).rstrip ("," )
517511 v = f"({ value } ),"
518512 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 } "
524514 params = []
525515 for item in fields :
526516 if isinstance (item , list ):
527517 for subitem in item :
528518 params .append (subitem )
529519 self ._params = tuple (params )
530520 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" )
534522 return self
535523
536524 return self
537525
538526 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 :
540528 self .set_error (f"Empty table or fields in { inspect .stack ()[0 ][3 ]} method" )
541529 return self
542530
543531 if isinstance (table , dict ) or isinstance (table , str ):
544532 table = self ._prepare_aliases (table )
545533 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" )
549535 return self
550536
551537 if isinstance (fields , list ) or isinstance (fields , dict ):
@@ -554,9 +540,7 @@ def update(self, table: Union[str, dict], fields: Union[list, dict]):
554540 sets += f" { self ._prepare_field (item )} = ?,"
555541 sets = sets .rstrip ("," )
556542 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" )
560544 return self
561545
562546 self .reset ()
@@ -566,29 +550,20 @@ def update(self, table: Union[str, dict], fields: Union[list, dict]):
566550
567551 return self
568552
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" ):
575554 join_type = join_type .upper ()
576555 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" )
580557 return self
581558
582- if table == "" or table == {} :
559+ if not table :
583560 self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
584561 return self
585562
586563 if isinstance (table , dict ) or isinstance (table , str ):
587564 self ._sql += f" { join_type } JOIN { self ._prepare_aliases (table )} "
588565 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" )
592567 return self
593568
594569 if on :
@@ -599,17 +574,15 @@ def join(
599574 elif isinstance (on , str ):
600575 self ._sql += f" ON { on } "
601576 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" )
605578 return self
606579
607580 self .set_error ()
608-
609581 return self
610582
611583 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 :
613586 self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
614587 return self
615588
@@ -621,7 +594,8 @@ def drop(self, table: str, add_exists: bool = True):
621594 return self
622595
623596 def truncate (self , table : str ):
624- if table == "" :
597+ # this method will be moved to another class
598+ if not table :
625599 self .set_error (f"Empty table in { inspect .stack ()[0 ][3 ]} method" )
626600 return self
627601
0 commit comments