sql-blocks 1.2025.627__py3-none-any.whl → 1.2025.629__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
@@ -330,16 +330,20 @@ class DateDiff(Function):
330
330
  append_param = True
331
331
 
332
332
  def __str__(self) -> str:
333
- def is_field_or_func(name: str) -> bool:
333
+ def is_field_or_func(obj) -> bool:
334
+ if not isinstance(obj, str):
335
+ return True
334
336
  candidate = re.sub(
335
- '[()]', '', name.split('.')[-1]
337
+ '[()]', '', obj.split('.')[-1]
336
338
  )
337
339
  return candidate.isidentifier()
340
+ self.params = [
341
+ p if is_field_or_func(p) else f"'{p}'"
342
+ for p in self.params
343
+ ]
338
344
  if self.dialect != Dialect.SQL_SERVER:
339
- params = [str(p) for p in self.params]
340
345
  return ' - '.join(
341
- p if is_field_or_func(p) else f"'{p}'"
342
- for p in params
346
+ str(p) for p in self.params
343
347
  ) # <==== Date subtract
344
348
  return super().__str__()
345
349
 
@@ -654,6 +658,8 @@ class Not(Where):
654
658
 
655
659
 
656
660
  class Case:
661
+ break_lines = True
662
+
657
663
  def __init__(self, field: str):
658
664
  self.__conditions = {}
659
665
  self.default = None
@@ -669,7 +675,10 @@ class Case:
669
675
 
670
676
  def then(self, result):
671
677
  if isinstance(result, str):
672
- result = quoted(result)
678
+ if result.startswith('='):
679
+ result = result[1:]
680
+ else:
681
+ result = quoted(result)
673
682
  self.__conditions[result] = self.current_condition
674
683
  return self
675
684
 
@@ -680,16 +689,24 @@ class Case:
680
689
  return self
681
690
 
682
691
  def format(self, name: str, field: str='') -> str:
692
+ TABULATION = '\t\t' if self.break_lines else ' '
693
+ LINE_BREAK = '\n' if self.break_lines else ' '
683
694
  default = self.default
684
695
  if not field:
685
696
  field = self.field
686
- return 'CASE \n{}\n\tEND AS {}'.format(
687
- '\n'.join(
688
- f'\t\tWHEN {field} {cond.content} THEN {res}'
697
+ return 'CASE{brk}{cond}{df}{tab}END{alias}'.format(
698
+ brk=LINE_BREAK,
699
+ cond=LINE_BREAK.join(
700
+ f'{TABULATION}WHEN {field} {cond.content} THEN {res}'
689
701
  for res, cond in self.__conditions.items()
690
- ) + (f'\n\t\tELSE {default}' if default else ''),
691
- name
702
+ ),
703
+ df=f'{LINE_BREAK}{TABULATION}ELSE {default}' if not default is None else '',
704
+ tab='\n\t' if self.break_lines else ' ',
705
+ alias=f' AS {name}' if name else ''
692
706
  )
707
+
708
+ def __str__(self):
709
+ return self.format('', self.field)
693
710
 
694
711
  def add(self, name: str, main: SQLObject):
695
712
  main.values.setdefault(SELECT, []).append(
@@ -2089,7 +2106,9 @@ class CTEFactory:
2089
2106
  def __str__(self):
2090
2107
  CTE.show_query = False
2091
2108
  lines = [str(cte) for cte in self.cte_list]
2092
- return ',\n'.join(lines) + '\n' + str(self.main)
2109
+ result = ',\n'.join(lines) + '\n' + str(self.main)
2110
+ CTE.show_query = True
2111
+ return result
2093
2112
 
2094
2113
  @staticmethod
2095
2114
  def extract_subqueries(txt: str) -> dict:
@@ -2278,4 +2297,4 @@ def detect(text: str, join_queries: bool = True, format: str='') -> Select | lis
2278
2297
  for query in query_list[1:]:
2279
2298
  result += query
2280
2299
  return result
2281
- # ===========================================================================================//
2300
+ # ===========================================================================================//
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.2025.627
3
+ Version: 1.2025.629
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=ODdGib5t0Jdk1-t9CKCEISWRWzVFJR4tCpfZt5Qw4GA,78119
3
+ sql_blocks-1.2025.629.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-1.2025.629.dist-info/METADATA,sha256=tk1PPS4ojtXU5oYyqXBwwr_QjzAlIjI7rqzmJhMa4bI,23328
5
+ sql_blocks-1.2025.629.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-1.2025.629.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-1.2025.629.dist-info/RECORD,,
@@ -1,7 +0,0 @@
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,,