sql-blocks 1.2025.702__tar.gz → 1.2025.1002__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.2025.702
3
+ Version: 1.2025.1002
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
@@ -447,15 +447,16 @@ Usefull to conditional Sum, Avg, Count...
447
447
 
448
448
  **Example:**
449
449
 
450
- Select('Emprestimo',
451
- taxa=If('atraso', gt(0), Sum)
450
+ Select('Loan',
451
+ penalty=If('days_late', Sum, gt(0))
452
452
  )
453
+ # ...OR... penalty=If('days_late > 0', Sum)
453
454
 
454
455
  results...
455
456
  ```
456
457
  SELECT
457
458
  Sum(CASE
458
- WHEN atraso > 0 THEN taxa
459
+ WHEN days_late > 0 THEN penalty
459
460
  ELSE 0
460
461
  END)
461
462
  FROM
@@ -794,6 +795,41 @@ SELECT
794
795
  ```
795
796
  ---
796
797
 
798
+ ### 16.1 - _GroupBy as instance_
799
+
800
+ Another way to use GroupBy is to pass functions as parameters:
801
+
802
+ ```
803
+ Function.dialect = Dialect.ORACLE
804
+ query = Select(
805
+ 'Sales s',
806
+ ref_date=GroupBy(
807
+ ref_year=Year, qty_sold=Sum('quantity'),
808
+ vendor=Select(
809
+ 'Vendor v',
810
+ id=[PrimaryKey, Field], name=Field
811
+ )
812
+ )
813
+ )
814
+ print(query)
815
+ ```
816
+ results..
817
+ ```
818
+ SELECT
819
+ Extract(Year FROM s.ref_date) as ref_year,
820
+ Sum(quantity) as qty_sold,
821
+ v.id,
822
+ v.name
823
+ FROM
824
+ Sales s
825
+ JOIN Vendor v ON (s.vendor = v.id)
826
+ GROUP BY
827
+ ref_year,
828
+ v.id,
829
+ v.name
830
+ ```
831
+
832
+ ---
797
833
  ### 17 - CTE and Recursive classes
798
834
 
799
835
  * **17.1 - _CTE class_**
@@ -432,15 +432,16 @@ Usefull to conditional Sum, Avg, Count...
432
432
 
433
433
  **Example:**
434
434
 
435
- Select('Emprestimo',
436
- taxa=If('atraso', gt(0), Sum)
435
+ Select('Loan',
436
+ penalty=If('days_late', Sum, gt(0))
437
437
  )
438
+ # ...OR... penalty=If('days_late > 0', Sum)
438
439
 
439
440
  results...
440
441
  ```
441
442
  SELECT
442
443
  Sum(CASE
443
- WHEN atraso > 0 THEN taxa
444
+ WHEN days_late > 0 THEN penalty
444
445
  ELSE 0
445
446
  END)
446
447
  FROM
@@ -779,6 +780,41 @@ SELECT
779
780
  ```
780
781
  ---
781
782
 
783
+ ### 16.1 - _GroupBy as instance_
784
+
785
+ Another way to use GroupBy is to pass functions as parameters:
786
+
787
+ ```
788
+ Function.dialect = Dialect.ORACLE
789
+ query = Select(
790
+ 'Sales s',
791
+ ref_date=GroupBy(
792
+ ref_year=Year, qty_sold=Sum('quantity'),
793
+ vendor=Select(
794
+ 'Vendor v',
795
+ id=[PrimaryKey, Field], name=Field
796
+ )
797
+ )
798
+ )
799
+ print(query)
800
+ ```
801
+ results..
802
+ ```
803
+ SELECT
804
+ Extract(Year FROM s.ref_date) as ref_year,
805
+ Sum(quantity) as qty_sold,
806
+ v.id,
807
+ v.name
808
+ FROM
809
+ Sales s
810
+ JOIN Vendor v ON (s.vendor = v.id)
811
+ GROUP BY
812
+ ref_year,
813
+ v.id,
814
+ v.name
815
+ ```
816
+
817
+ ---
782
818
  ### 17 - CTE and Recursive classes
783
819
 
784
820
  * **17.1 - _CTE class_**
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sql_blocks"
3
- version = "1.2025.0702"
3
+ version = "1.2025.1002"
4
4
  authors = [
5
5
  { name="Julio Cascalles", email="julio.cascalles@outlook.com" },
6
6
  ]
@@ -3,7 +3,7 @@ from setuptools import setup
3
3
 
4
4
  setup(
5
5
  name = 'sql_blocks',
6
- version = '1.2025.0702',
6
+ version = '1.2025.1002',
7
7
  author = 'Júlio Cascalles',
8
8
  author_email = 'julio.cascalles@outlook.com',
9
9
  packages = ['sql_blocks'],
@@ -272,6 +272,14 @@ class Function(Code):
272
272
  self.params = [set_func_types(p) for p in params]
273
273
  self.pattern = self.get_pattern()
274
274
  super().__init__()
275
+
276
+ @classmethod
277
+ def descendants(cls) -> list:
278
+ result = []
279
+ for sub in cls.__subclasses__():
280
+ result.append(sub)
281
+ result += sub.descendants()
282
+ return result
275
283
 
276
284
  def get_pattern(self) -> str:
277
285
  return '{func_name}({params})'
@@ -459,7 +467,7 @@ class Cast(Function):
459
467
  separator = ' As '
460
468
 
461
469
 
462
- FUNCTION_CLASS = {f.__name__.lower(): f for f in Function.__subclasses__()}
470
+ FUNCTION_CLASS = {f.__name__.lower(): f for f in Function.descendants()}
463
471
 
464
472
 
465
473
  class ExpressionField:
@@ -770,8 +778,11 @@ class If(Code, Frame):
770
778
  """
771
779
  Behaves like an aggregation function
772
780
  """
773
- def __init__(self, field: str, condition: Where, func_class: Function):
774
- self.field = field
781
+ def __init__(self, field: str, func_class: Function, condition: Where=None):
782
+ if not condition:
783
+ field, *elements = re.split(r'([<>=]|\bin\b|\blike\b)', field, re.IGNORECASE)
784
+ condition = Where( ''.join(elements) )
785
+ self.field = field.strip()
775
786
  self.condition = condition
776
787
  self.func_class = func_class
777
788
  self.pattern = ''
@@ -938,10 +949,35 @@ class Partition:
938
949
 
939
950
 
940
951
  class GroupBy(Clause):
952
+ def __init__(self, **args):
953
+ # --- Replace class method by instance method: ------
954
+ self.add = self.__add
955
+ # -----------------------------------------------------
956
+ self.args = args
957
+
958
+ def __add(self, name: str, main: SQLObject):
959
+ func: Function = None
960
+ fields = []
961
+ for alias, obj in self.args.items():
962
+ if isinstance(obj, type) and obj in Function.descendants():
963
+ func: Function = obj
964
+ name = func().format(name, main)
965
+ NamedField(alias).add(name, main)
966
+ fields += [alias]
967
+ elif isinstance(obj, Aggregate):
968
+ obj.As(alias).add('', main)
969
+ elif isinstance(obj, Select):
970
+ query: Select = obj
971
+ fields += query.values.get(SELECT, [])
972
+ query.add(alias, main)
973
+ if not func:
974
+ fields = [self.format(name, main)]
975
+ for field in fields:
976
+ main.values.setdefault(GROUP_BY, []).append(field)
977
+
941
978
  @classmethod
942
979
  def add(cls, name: str, main: SQLObject):
943
- name = cls.format(name, main)
944
- main.values.setdefault(GROUP_BY, []).append(name)
980
+ cls().__add(name, main)
945
981
 
946
982
 
947
983
  class Having:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.2025.702
3
+ Version: 1.2025.1002
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
@@ -447,15 +447,16 @@ Usefull to conditional Sum, Avg, Count...
447
447
 
448
448
  **Example:**
449
449
 
450
- Select('Emprestimo',
451
- taxa=If('atraso', gt(0), Sum)
450
+ Select('Loan',
451
+ penalty=If('days_late', Sum, gt(0))
452
452
  )
453
+ # ...OR... penalty=If('days_late > 0', Sum)
453
454
 
454
455
  results...
455
456
  ```
456
457
  SELECT
457
458
  Sum(CASE
458
- WHEN atraso > 0 THEN taxa
459
+ WHEN days_late > 0 THEN penalty
459
460
  ELSE 0
460
461
  END)
461
462
  FROM
@@ -794,6 +795,41 @@ SELECT
794
795
  ```
795
796
  ---
796
797
 
798
+ ### 16.1 - _GroupBy as instance_
799
+
800
+ Another way to use GroupBy is to pass functions as parameters:
801
+
802
+ ```
803
+ Function.dialect = Dialect.ORACLE
804
+ query = Select(
805
+ 'Sales s',
806
+ ref_date=GroupBy(
807
+ ref_year=Year, qty_sold=Sum('quantity'),
808
+ vendor=Select(
809
+ 'Vendor v',
810
+ id=[PrimaryKey, Field], name=Field
811
+ )
812
+ )
813
+ )
814
+ print(query)
815
+ ```
816
+ results..
817
+ ```
818
+ SELECT
819
+ Extract(Year FROM s.ref_date) as ref_year,
820
+ Sum(quantity) as qty_sold,
821
+ v.id,
822
+ v.name
823
+ FROM
824
+ Sales s
825
+ JOIN Vendor v ON (s.vendor = v.id)
826
+ GROUP BY
827
+ ref_year,
828
+ v.id,
829
+ v.name
830
+ ```
831
+
832
+ ---
797
833
  ### 17 - CTE and Recursive classes
798
834
 
799
835
  * **17.1 - _CTE class_**