sql-blocks 1.2025.702__tar.gz → 1.20250709__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.
- {sql_blocks-1.2025.702/sql_blocks.egg-info → sql_blocks-1.20250709}/PKG-INFO +40 -4
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/README.md +39 -3
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/pyproject.toml +1 -1
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/setup.py +1 -1
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/sql_blocks/sql_blocks.py +42 -5
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709/sql_blocks.egg-info}/PKG-INFO +40 -4
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/LICENSE +0 -0
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/setup.cfg +0 -0
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/sql_blocks/__init__.py +0 -0
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/sql_blocks.egg-info/SOURCES.txt +0 -0
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/sql_blocks.egg-info/dependency_links.txt +0 -0
- {sql_blocks-1.2025.702 → sql_blocks-1.20250709}/sql_blocks.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sql_blocks
|
3
|
-
Version: 1.
|
3
|
+
Version: 1.20250709
|
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('
|
451
|
-
|
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
|
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('
|
436
|
-
|
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
|
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_**
|
@@ -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.
|
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,
|
774
|
-
|
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,36 @@ 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
|
+
field = re.split(r'\s+(AS|as)\s+', field)[-1]
|
977
|
+
main.values.setdefault(GROUP_BY, []).append(field)
|
978
|
+
|
941
979
|
@classmethod
|
942
980
|
def add(cls, name: str, main: SQLObject):
|
943
|
-
|
944
|
-
main.values.setdefault(GROUP_BY, []).append(name)
|
981
|
+
cls().__add(name, main)
|
945
982
|
|
946
983
|
|
947
984
|
class Having:
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sql_blocks
|
3
|
-
Version: 1.
|
3
|
+
Version: 1.20250709
|
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('
|
451
|
-
|
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
|
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_**
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|