Skip to content

Commit 2e37bce

Browse files
committed
add _prepare_tables() method
1 parent 3b0305f commit 2e37bce

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

simple_query_builder/querybuilder.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,7 @@ def _prepare_aliases(self, items: Union[str, list, dict], as_list: bool = False)
257257
elif isinstance(item, dict):
258258
first_item = list(item.values())[0]
259259
alias = list(item.keys())[0]
260-
sql.append(
261-
first_item
262-
if isinstance(alias, int)
263-
else f"{first_item} AS {alias}"
264-
)
260+
sql.append(first_item if isinstance(alias, int) else f"{first_item} AS {alias}")
265261
elif isinstance(items, dict):
266262
new_item = items[item]
267263
sql.append(new_item if isinstance(item, int) else f"{new_item} AS {item}")
@@ -333,7 +329,17 @@ def _prepare_conditions(self, where: Union[str, list]) -> dict:
333329

334330
return result
335331

336-
def select(self, table: Union[str, dict], fields: Union[str, list, dict] = "*"):
332+
def _prepare_tables(self, table: Union[str, list, dict]) -> str:
333+
if isinstance(table, str) and any(x in table for x in self._FIELD_SPEC_CHARS):
334+
self._sql = f"SELECT {table}"
335+
self._fields = table
336+
elif isinstance(table, str) and 'select' in table.lower():
337+
self._sql += f" FROM ({table})"
338+
else:
339+
self._sql += f" FROM {self._prepare_aliases(table)}"
340+
return self._sql
341+
342+
def select(self, table: Union[str, list, dict], fields: Union[str, list, dict] = "*"):
337343
if not table or not fields:
338344
self.set_error(f"Empty table or fields in {inspect.stack()[0][3]} method")
339345
return self
@@ -349,12 +355,8 @@ def select(self, table: Union[str, dict], fields: Union[str, list, dict] = "*"):
349355
self.set_error(f"Incorrect type of fields in {inspect.stack()[0][3]} method. Fields must be String, List or Dictionary")
350356
return self
351357

352-
if isinstance(table, dict) or isinstance(table, str):
353-
if isinstance(table, str) and any(x in table for x in self._FIELD_SPEC_CHARS) and fields == '*':
354-
self._sql = f"SELECT {table}"
355-
self._fields = table
356-
else:
357-
self._sql += f" FROM {self._prepare_aliases(table)}"
358+
if isinstance(table, dict) or isinstance(table, list) or isinstance(table, str):
359+
self._prepare_tables(table)
358360
else:
359361
self.set_error(f"Incorrect type of table in {inspect.stack()[0][3]} method. Table must be String or Dictionary")
360362
return self
@@ -518,30 +520,29 @@ def group_by(self, field: Union[str, tuple, list] = ()):
518520

519521
return self
520522

521-
def delete(self, table: Union[str, dict]):
523+
def delete(self, table: Union[str, list, dict]):
522524
if not table:
523525
self.set_error(f"Empty table in {inspect.stack()[0][3]} method")
524526
return self
525527

526-
if isinstance(table, dict) or isinstance(table, str):
527-
table = self._prepare_aliases(table)
528+
if isinstance(table, dict) or isinstance(table, list) or isinstance(table, str):
529+
self.reset()
530+
self._sql = f"DELETE"
531+
self._prepare_tables(table)
528532
else:
529533
self.set_error(f"Incorrect type of table in {inspect.stack()[0][3]} method. Table must be String or Dictionary")
530534
return self
531535

532-
self.reset()
533-
534-
self._sql = f"DELETE FROM {table}"
535536
return self
536537

537-
def insert(self, table: Union[str, dict], fields: Union[list, dict]):
538+
def insert(self, table: Union[str, list, dict], fields: Union[list, dict]):
538539
if not table or not fields:
539540
self.set_error(f"Empty table or fields in {inspect.stack()[0][3]} method")
540541
return self
541542

542543
self._fields = fields
543544

544-
if isinstance(table, dict) or isinstance(table, str):
545+
if isinstance(table, dict) or isinstance(table, list) or isinstance(table, str):
545546
table = self._prepare_aliases(table)
546547
else:
547548
self.set_error(f"Incorrect type of table in {inspect.stack()[0][3]} method. Table must be String or Dictionary")

0 commit comments

Comments
 (0)