sql-blocks 1.25.528999999999__py3-none-any.whl → 1.25.610999999999__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 +72 -7
- {sql_blocks-1.25.528999999999.dist-info → sql_blocks-1.25.610999999999.dist-info}/METADATA +1 -1
- sql_blocks-1.25.610999999999.dist-info/RECORD +7 -0
- sql_blocks-1.25.528999999999.dist-info/RECORD +0 -7
- {sql_blocks-1.25.528999999999.dist-info → sql_blocks-1.25.610999999999.dist-info}/LICENSE +0 -0
- {sql_blocks-1.25.528999999999.dist-info → sql_blocks-1.25.610999999999.dist-info}/WHEEL +0 -0
- {sql_blocks-1.25.528999999999.dist-info → sql_blocks-1.25.610999999999.dist-info}/top_level.txt +0 -0
sql_blocks/sql_blocks.py
CHANGED
@@ -103,10 +103,16 @@ class SQLObject:
|
|
103
103
|
result += re.split(r'([=()]|<>|\s+ON\s+|\s+on\s+)', fld)
|
104
104
|
return result
|
105
105
|
def cleanup(text: str) -> str:
|
106
|
+
if re.search(r'^CASE\b', text):
|
107
|
+
return text
|
106
108
|
text = re.sub(r'[\n\t]', ' ', text)
|
107
109
|
if exact:
|
108
110
|
text = text.lower()
|
109
111
|
return text.strip()
|
112
|
+
def split_fields(text: str) -> list:
|
113
|
+
if key == SELECT:
|
114
|
+
return Case.parse(text)
|
115
|
+
return re.split(separator, text)
|
110
116
|
def field_set(source: list) -> set:
|
111
117
|
return set(
|
112
118
|
(
|
@@ -116,7 +122,7 @@ class SQLObject:
|
|
116
122
|
re.sub(pattern, '', cleanup(fld))
|
117
123
|
)
|
118
124
|
for string in disassemble(source)
|
119
|
-
for fld in
|
125
|
+
for fld in split_fields(string)
|
120
126
|
)
|
121
127
|
pattern = KEYWORD[key][1]
|
122
128
|
if exact:
|
@@ -642,11 +648,19 @@ class Case:
|
|
642
648
|
self.__conditions = {}
|
643
649
|
self.default = None
|
644
650
|
self.field = field
|
651
|
+
self.current_condition = None
|
652
|
+
self.fields = []
|
645
653
|
|
646
|
-
def when(self, condition: Where, result):
|
654
|
+
def when(self, condition: Where, result=None):
|
655
|
+
self.current_condition = condition
|
656
|
+
if result is None:
|
657
|
+
return self
|
658
|
+
return self.then(result)
|
659
|
+
|
660
|
+
def then(self, result):
|
647
661
|
if isinstance(result, str):
|
648
662
|
result = quoted(result)
|
649
|
-
self.__conditions[result] =
|
663
|
+
self.__conditions[result] = self.current_condition
|
650
664
|
return self
|
651
665
|
|
652
666
|
def else_value(self, default):
|
@@ -655,17 +669,67 @@ class Case:
|
|
655
669
|
self.default = default
|
656
670
|
return self
|
657
671
|
|
658
|
-
def
|
659
|
-
field = Field.format(self.field, main)
|
672
|
+
def format(self, name: str, field: str='') -> str:
|
660
673
|
default = self.default
|
661
|
-
|
674
|
+
if not field:
|
675
|
+
field = self.field
|
676
|
+
return 'CASE \n{}\n\tEND AS {}'.format(
|
662
677
|
'\n'.join(
|
663
678
|
f'\t\tWHEN {field} {cond.content} THEN {res}'
|
664
679
|
for res, cond in self.__conditions.items()
|
665
680
|
) + (f'\n\t\tELSE {default}' if default else ''),
|
666
681
|
name
|
667
682
|
)
|
668
|
-
|
683
|
+
|
684
|
+
def add(self, name: str, main: SQLObject):
|
685
|
+
main.values.setdefault(SELECT, []).append(
|
686
|
+
self.format(
|
687
|
+
name, Field.format(self.field, main)
|
688
|
+
)
|
689
|
+
)
|
690
|
+
|
691
|
+
@classmethod
|
692
|
+
def parse(cls, expr: str) -> list:
|
693
|
+
result = []
|
694
|
+
block: 'Case' = None
|
695
|
+
# ---- functions of keywords: -----------------
|
696
|
+
def _when(word: str):
|
697
|
+
field, condition = word.split(' ', maxsplit=1)
|
698
|
+
condition = Where(condition)
|
699
|
+
if not block:
|
700
|
+
return cls(field).when(condition)
|
701
|
+
return block.when(condition)
|
702
|
+
def _then(word: str):
|
703
|
+
return block.then( eval(word) )
|
704
|
+
def _else(word: str):
|
705
|
+
return block.else_value( eval(word) )
|
706
|
+
def _end(word: str):
|
707
|
+
name, *rest = [t.strip() for t in re.split(r'\s+AS\s+|[,]', word) if t]
|
708
|
+
block.fields.append(
|
709
|
+
block.format(name)
|
710
|
+
)
|
711
|
+
block.fields += rest
|
712
|
+
return block
|
713
|
+
# -------------------------------------------------
|
714
|
+
KEYWORDS = {
|
715
|
+
'WHEN': _when, 'THEN': _then,
|
716
|
+
'ELSE': _else, 'END': _end,
|
717
|
+
}
|
718
|
+
RESERVED_WORDS = ['CASE'] + list(KEYWORDS)
|
719
|
+
REGEX = '|'.join(fr'\b{word}\b' for word in RESERVED_WORDS)
|
720
|
+
expr = re.sub(r'\s+', ' ', expr)
|
721
|
+
tokens = [t for t in re.split(f'({REGEX})', expr) if t.strip()]
|
722
|
+
last_word = ''
|
723
|
+
while tokens:
|
724
|
+
word = tokens.pop(0)
|
725
|
+
if last_word in KEYWORDS:
|
726
|
+
block = KEYWORDS[last_word](word)
|
727
|
+
result += block.fields
|
728
|
+
block.fields = []
|
729
|
+
elif word not in RESERVED_WORDS:
|
730
|
+
result.append(word)
|
731
|
+
last_word = word
|
732
|
+
return result
|
669
733
|
|
670
734
|
|
671
735
|
class Options:
|
@@ -2047,3 +2111,4 @@ def detect(text: str, join_queries: bool = True, format: str='') -> Select | lis
|
|
2047
2111
|
return result
|
2048
2112
|
# ===========================================================================================//
|
2049
2113
|
|
2114
|
+
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sql_blocks
|
3
|
-
Version: 1.25.
|
3
|
+
Version: 1.25.610999999999
|
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=tdfGConHw2iosex_BSXAWxYTGufPyOVMSTyi3g6gqpM,71400
|
3
|
+
sql_blocks-1.25.610999999999.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
4
|
+
sql_blocks-1.25.610999999999.dist-info/METADATA,sha256=09DIeKq_SinVNKt0OyYQQDOZ2_DDR6CmoP_n99t4ZXA,22235
|
5
|
+
sql_blocks-1.25.610999999999.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
6
|
+
sql_blocks-1.25.610999999999.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
|
7
|
+
sql_blocks-1.25.610999999999.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
|
2
|
-
sql_blocks/sql_blocks.py,sha256=qhzO4tlxBE3dSqDZ-ow1b08H90xDytWXrFMrf6Y-5Og,69096
|
3
|
-
sql_blocks-1.25.528999999999.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
4
|
-
sql_blocks-1.25.528999999999.dist-info/METADATA,sha256=GNAFIVbNrBrijE1PbjoHaaPRIwrC_NbSLEd-VF0WWyc,22235
|
5
|
-
sql_blocks-1.25.528999999999.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
6
|
-
sql_blocks-1.25.528999999999.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
|
7
|
-
sql_blocks-1.25.528999999999.dist-info/RECORD,,
|
File without changes
|
File without changes
|
{sql_blocks-1.25.528999999999.dist-info → sql_blocks-1.25.610999999999.dist-info}/top_level.txt
RENAMED
File without changes
|