sql-blocks 1.2025.701__py3-none-any.whl → 1.2025.1002__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 +42 -17
- {sql_blocks-1.2025.701.dist-info → sql_blocks-1.2025.1002.dist-info}/METADATA +40 -4
- sql_blocks-1.2025.1002.dist-info/RECORD +7 -0
- sql_blocks-1.2025.701.dist-info/RECORD +0 -7
- {sql_blocks-1.2025.701.dist-info → sql_blocks-1.2025.1002.dist-info}/LICENSE +0 -0
- {sql_blocks-1.2025.701.dist-info → sql_blocks-1.2025.1002.dist-info}/WHEEL +0 -0
- {sql_blocks-1.2025.701.dist-info → sql_blocks-1.2025.1002.dist-info}/top_level.txt +0 -0
sql_blocks/sql_blocks.py
CHANGED
@@ -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,10 +778,14 @@ 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
|
788
|
+
self.pattern = ''
|
777
789
|
super().__init__()
|
778
790
|
|
779
791
|
def format(self, name: str, main: SQLObject) -> str:
|
@@ -937,10 +949,35 @@ class Partition:
|
|
937
949
|
|
938
950
|
|
939
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
|
+
|
940
978
|
@classmethod
|
941
979
|
def add(cls, name: str, main: SQLObject):
|
942
|
-
|
943
|
-
main.values.setdefault(GROUP_BY, []).append(name)
|
980
|
+
cls().__add(name, main)
|
944
981
|
|
945
982
|
|
946
983
|
class Having:
|
@@ -2330,15 +2367,3 @@ def detect(text: str, join_queries: bool = True, format: str='') -> Select | lis
|
|
2330
2367
|
result += query
|
2331
2368
|
return result
|
2332
2369
|
# ===========================================================================================//
|
2333
|
-
|
2334
|
-
if __name__ == "__main__":
|
2335
|
-
query = Select(
|
2336
|
-
'Emprestimos e',
|
2337
|
-
taxa=If('atraso', gt(0), Sum).over(
|
2338
|
-
mes_ano=OrderBy.DESC,
|
2339
|
-
_=Rows(Current(), Following(5)),
|
2340
|
-
# _=Rows(Preceding(3), Following()),
|
2341
|
-
# _=Rows( Preceding(3) ),
|
2342
|
-
).As('multa')
|
2343
|
-
)
|
2344
|
-
print(query)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sql_blocks
|
3
|
-
Version: 1.2025.
|
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('
|
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_**
|
@@ -0,0 +1,7 @@
|
|
1
|
+
sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
|
2
|
+
sql_blocks/sql_blocks.py,sha256=QStl2invYAuvpSKkAqeutMlDgzOgNQvLF2pA8XK90aY,80473
|
3
|
+
sql_blocks-1.2025.1002.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
4
|
+
sql_blocks-1.2025.1002.dist-info/METADATA,sha256=SOD6TNWQF46azhVlf7N1PHQX-Xgx0-CbqbAC7-1-cfc,24417
|
5
|
+
sql_blocks-1.2025.1002.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
6
|
+
sql_blocks-1.2025.1002.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
|
7
|
+
sql_blocks-1.2025.1002.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
|
2
|
-
sql_blocks/sql_blocks.py,sha256=dv4KLJe2LfxTuJZ8q5ZxJOy4I60LJX5Cl-k7NjmxKHk,79413
|
3
|
-
sql_blocks-1.2025.701.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
4
|
-
sql_blocks-1.2025.701.dist-info/METADATA,sha256=d4KXNLmtcIqwyQL2eLM15Zknm01M98DX5FoMgaWBrHA,23646
|
5
|
-
sql_blocks-1.2025.701.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
6
|
-
sql_blocks-1.2025.701.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
|
7
|
-
sql_blocks-1.2025.701.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|