sql-blocks 1.2025.625__py3-none-any.whl → 1.2025.627__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sql_blocks/sql_blocks.py CHANGED
@@ -2009,7 +2009,7 @@ class CTE(Select):
2009
2009
  # ---------------------------------------------------------
2010
2010
  return 'WITH {}{} AS (\n {}\n){}'.format(
2011
2011
  self.prefix, self.table_name,
2012
- '\nUNION ALL\n '.join(
2012
+ '\n\tUNION ALL\n '.join(
2013
2013
  justify(q) for q in self.query_list
2014
2014
  ), super().__str__() if self.show_query else ''
2015
2015
  )
@@ -2081,8 +2081,9 @@ class CTEFactory:
2081
2081
  self.cte_list = [
2082
2082
  CTE(alias, [
2083
2083
  Select.parse(query)[0]
2084
+ for query in elements
2084
2085
  ])
2085
- for alias, query in summary.items()
2086
+ for alias, elements in summary.items()
2086
2087
  ]
2087
2088
 
2088
2089
  def __str__(self):
@@ -2091,8 +2092,20 @@ class CTEFactory:
2091
2092
  return ',\n'.join(lines) + '\n' + str(self.main)
2092
2093
 
2093
2094
  @staticmethod
2094
- def extract_subqueries(txt: str) -> dict:
2095
+ def extract_subqueries(txt: str) -> dict:
2095
2096
  result = {}
2097
+ # ---------------------------------------------------
2098
+ def clean_subquery(source: list) -> str:
2099
+ while source:
2100
+ if source[0].upper() == 'SELECT':
2101
+ break
2102
+ word = source.pop(0)
2103
+ if word.upper() in ('FROM', 'JOIN'):
2104
+ result[MAIN_TAG] += f' {word}'
2105
+ return ' '.join(source)
2106
+ def balanced_parentheses(expr: str) -> bool:
2107
+ return expr.count('(') == expr.count(')')
2108
+ # ---------------------------------------------------
2096
2109
  for found in re.finditer(r'(FROM|JOIN)\s*[(]\s*SELECT', txt, re.IGNORECASE):
2097
2110
  start = found.start()
2098
2111
  alias = ''
@@ -2102,16 +2115,23 @@ class CTEFactory:
2102
2115
  if not found:
2103
2116
  break
2104
2117
  end = found.end() + pos
2105
- elements = txt[start: end].split()
2106
- if '(' not in elements[-3]:
2107
- _, alias = elements[-2:]
2118
+ last = end
2119
+ if balanced_parentheses(txt[start: end]):
2120
+ pos += found.start()
2121
+ alias = re.findall(r'\s*(\w+)$', txt[pos: end])[0]
2122
+ end = pos
2108
2123
  pos = end
2109
- first_word = elements.pop(0)
2110
2124
  if not result:
2111
2125
  result[MAIN_TAG] = txt[:start]
2112
- result[MAIN_TAG] += f' {first_word} {alias} {alias}'
2113
- result[alias] = ' '.join(elements[1: -3])
2114
- result[MAIN_TAG] += txt[end:]
2126
+ query_list = [
2127
+ clean_subquery( expr.split() )
2128
+ for expr in re.split(
2129
+ r'\bUNION\b', txt[start: end], re.IGNORECASE
2130
+ )
2131
+ ]
2132
+ result[MAIN_TAG] += f' {alias} {alias}'
2133
+ result[alias] = query_list
2134
+ result[MAIN_TAG] += txt[last:]
2115
2135
  return result
2116
2136
 
2117
2137
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.2025.625
3
+ Version: 1.2025.627
4
4
  Summary: Allows you to create objects for parts of SQL query commands. Also to combine these objects by joining them, adding or removing parts...
5
5
  Home-page: https://github.com/julio-cascalles/sql_blocks
6
6
  Author: Júlio Cascalles
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
+ sql_blocks/sql_blocks.py,sha256=Eq2EMaVALy1IBGcjyjh7SaE1WZBPNvXCi748iCi6AtM,77469
3
+ sql_blocks-1.2025.627.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-1.2025.627.dist-info/METADATA,sha256=n1Ju94eliblEldBRpj-VzmXg4_09K_vgT5hszgqqVS0,23328
5
+ sql_blocks-1.2025.627.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-1.2025.627.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-1.2025.627.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=J7zEJ5JNRxI3F-7TIypb0myb5OgdE5Stv4boZCTVBLM,76610
3
- sql_blocks-1.2025.625.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-1.2025.625.dist-info/METADATA,sha256=I6KTi5S_usvCjJEQl6lm8LG4DaxHhX0NlGRwhHpKBT8,23328
5
- sql_blocks-1.2025.625.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-1.2025.625.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-1.2025.625.dist-info/RECORD,,