Skip to content

Commit ee566d4

Browse files
committed
refactor union_select() method
1 parent 8b11f56 commit ee566d4

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

simple_query_builder/querybuilder.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -634,18 +634,18 @@ def union(self, union_all: bool = False):
634634
self._sql += " UNION ALL " if union_all else " UNION "
635635
return self
636636

637-
def union_select(self, table: Union[str, dict], union_all: bool = False):
637+
def union_select(self, table: Union[str, list, dict], union_all: bool = False):
638638
if not table:
639639
self.set_error(f"Empty table in {inspect.stack()[0][3]} method")
640640
return self
641641

642-
if 'UNION' in self._sql:
643-
self.set_error(f"SQL has already UNION in {inspect.stack()[0][3]} method")
644-
return self
645-
646642
self._concat = True
647-
fields = self._fields
648-
self._sql += " UNION ALL " if union_all else " UNION "
643+
fields = self._fields if self._fields else '*'
644+
sql = self._sql
645+
sql += " UNION ALL " if union_all else " UNION "
646+
self._sql = sql + f"SELECT {self._prepare_aliases(fields)} FROM {self._prepare_aliases(table)}"
647+
648+
return self
649649

650650
if isinstance(fields, dict) or isinstance(fields, list) or isinstance(fields, str):
651651
self._sql += f"SELECT {self._prepare_aliases(fields)}"

0 commit comments

Comments
 (0)