sql-blocks 1.25.38021329__py3-none-any.whl → 1.25.313022106__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
@@ -244,6 +244,15 @@ class Function:
244
244
  params=self.separator.join(str(p) for p in self.params)
245
245
  )
246
246
 
247
+ @classmethod
248
+ def help(cls) -> str:
249
+ descr = ' '.join(B.__name__ for B in cls.__bases__)
250
+ params = cls.inputs or ''
251
+ return cls().get_pattern().format(
252
+ func_name=f'{descr} {cls.__name__}',
253
+ params=cls.separator.join(str(p) for p in params)
254
+ ) + f' Return {cls.output}'
255
+
247
256
  def set_main_param(self, name: str, main: SQLObject) -> bool:
248
257
  nested_functions = [
249
258
  param for param in self.params if isinstance(param, Function)
@@ -378,7 +387,8 @@ class Frame:
378
387
 
379
388
 
380
389
  class Aggregate(Frame):
381
- ...
390
+ inputs = [FLOAT]
391
+ output = FLOAT
382
392
 
383
393
  class Window(Frame):
384
394
  ...
@@ -397,13 +407,16 @@ class Count(Aggregate, Function):
397
407
 
398
408
  # ---- Window Functions: -----------------------------------
399
409
  class Row_Number(Window, Function):
400
- ...
410
+ output = INT
411
+
401
412
  class Rank(Window, Function):
402
- ...
413
+ output = INT
414
+
403
415
  class Lag(Window, Function):
404
- ...
416
+ output = ANY
417
+
405
418
  class Lead(Window, Function):
406
- ...
419
+ output = ANY
407
420
 
408
421
 
409
422
  # ---- Conversions and other Functions: ---------------------
@@ -620,20 +633,24 @@ class Case:
620
633
  self.default = None
621
634
  self.field = field
622
635
 
623
- def when(self, condition: Where, result: str):
636
+ def when(self, condition: Where, result):
637
+ if isinstance(result, str):
638
+ result = quoted(result)
624
639
  self.__conditions[result] = condition
625
640
  return self
626
641
 
627
- def else_value(self, default: str):
642
+ def else_value(self, default):
643
+ if isinstance(default, str):
644
+ default = quoted(default)
628
645
  self.default = default
629
646
  return self
630
647
 
631
648
  def add(self, name: str, main: SQLObject):
632
649
  field = Field.format(self.field, main)
633
- default = quoted(self.default)
650
+ default = self.default
634
651
  name = 'CASE \n{}\n\tEND AS {}'.format(
635
652
  '\n'.join(
636
- f'\t\tWHEN {field} {cond.expr} THEN {quoted(res)}'
653
+ f'\t\tWHEN {field} {cond.content} THEN {res}'
637
654
  for res, cond in self.__conditions.items()
638
655
  ) + f'\n\t\tELSE {default}' if default else '',
639
656
  name
@@ -1441,15 +1458,18 @@ class Select(SQLObject):
1441
1458
 
1442
1459
  def add(self, name: str, main: SQLObject):
1443
1460
  old_tables = main.values.get(FROM, [])
1444
- new_tables = set([
1445
- '{jt}JOIN {tb} {a2} ON ({a1}.{f1} = {a2}.{f2})'.format(
1461
+ if len(self.values[FROM]) > 1:
1462
+ old_tables += self.values[FROM][1:]
1463
+ new_tables = []
1464
+ row = '{jt}JOIN {tb} {a2} ON ({a1}.{f1} = {a2}.{f2})'.format(
1446
1465
  jt=self.join_type.value,
1447
1466
  tb=self.aka(),
1448
1467
  a1=main.alias, f1=name,
1449
1468
  a2=self.alias, f2=self.key_field
1450
1469
  )
1451
- ] + old_tables[1:])
1452
- main.values[FROM] = old_tables[:1] + list(new_tables)
1470
+ if row not in old_tables[1:]:
1471
+ new_tables.append(row)
1472
+ main.values[FROM] = old_tables[:1] + new_tables + old_tables[1:]
1453
1473
  for key in USUAL_KEYS:
1454
1474
  main.update_values(key, self.values.get(key, []))
1455
1475
 
@@ -1569,7 +1589,7 @@ class CTE(Select):
1569
1589
  result, line = [], ''
1570
1590
  keywords = '|'.join(KEYWORD)
1571
1591
  for word in re.split(fr'({keywords}|AND|OR|,)', str(query)):
1572
- if len(line) >= 60:
1592
+ if len(line) >= 50:
1573
1593
  result.append(line)
1574
1594
  line = ''
1575
1595
  line += word
@@ -1777,3 +1797,22 @@ def detect(text: str, join_queries: bool = True, format: str='') -> Select | lis
1777
1797
  result += query
1778
1798
  return result
1779
1799
  # ===========================================================================================//
1800
+
1801
+ if __name__ == "__main__":
1802
+ print('='*50)
1803
+ movimento = Select(
1804
+ 'Movimento.parquet M',
1805
+ direcao=Case('entrada_saida').when(
1806
+ eq('E'), 1
1807
+ ).else_value( -1 ),
1808
+ pessoa=Field
1809
+ )
1810
+ print(movimento)
1811
+ print('-'*50)
1812
+ cte = CTE('Ciclo C', [movimento])
1813
+ cte(
1814
+ pessoa=[GroupBy, Field],
1815
+ direcao=Having.sum(gt(0))
1816
+ )
1817
+ print(cte)
1818
+ print('='*50)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.25.38021329
3
+ Version: 1.25.313022106
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=rJThLDIWJpyI-plCdfpG5UwZDpp_pEriUbw1GgXRs0U,60595
3
+ sql_blocks-1.25.313022106.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-1.25.313022106.dist-info/METADATA,sha256=M9_xMMjLg9SYWM4m2ocomFow70Xu8eoqlBZ2BQdt3_4,20512
5
+ sql_blocks-1.25.313022106.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-1.25.313022106.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-1.25.313022106.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=5LQPEOkvMylJJxoU2m1P6pAhfcMHVdsnTiYgfLLJ5bQ,59477
3
- sql_blocks-1.25.38021329.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-1.25.38021329.dist-info/METADATA,sha256=un6TFbAfNbyhIrjLzHpvG1Yy77jsyZCw2xy_i3tMw2k,20511
5
- sql_blocks-1.25.38021329.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-1.25.38021329.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-1.25.38021329.dist-info/RECORD,,