sql-blocks 0.1.1__py3-none-any.whl → 0.1.2__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 +27 -4
- {sql_blocks-0.1.1.dist-info → sql_blocks-0.1.2.dist-info}/METADATA +5 -5
- sql_blocks-0.1.2.dist-info/RECORD +7 -0
- sql_blocks-0.1.1.dist-info/RECORD +0 -7
- {sql_blocks-0.1.1.dist-info → sql_blocks-0.1.2.dist-info}/LICENSE +0 -0
- {sql_blocks-0.1.1.dist-info → sql_blocks-0.1.2.dist-info}/WHEEL +0 -0
- {sql_blocks-0.1.1.dist-info → sql_blocks-0.1.2.dist-info}/top_level.txt +0 -0
sql_blocks/sql_blocks.py
CHANGED
@@ -160,11 +160,20 @@ class ExpressionField:
|
|
160
160
|
main.values.setdefault(SELECT, []).append(self.format(name, main))
|
161
161
|
|
162
162
|
def format(self, name: str, main: SQLObject) -> str:
|
163
|
-
|
163
|
+
"""
|
164
|
+
Replace special chars...
|
165
|
+
{af} or {a.f} or % = alias and field
|
166
|
+
{a} = alias
|
167
|
+
{f} = field
|
168
|
+
{t} = table name
|
169
|
+
"""
|
170
|
+
return re.sub('{af}|{a.f}|[%]', '{a}.{f}', self.expr).format(
|
171
|
+
a=main.alias, f=name, t=main.table_name
|
172
|
+
)
|
164
173
|
|
165
174
|
|
166
175
|
class Table:
|
167
|
-
def __init__(self, fields: list
|
176
|
+
def __init__(self, fields: list=[]):
|
168
177
|
if isinstance(fields, str):
|
169
178
|
fields = [f.strip() for f in fields.split(',')]
|
170
179
|
self.fields = fields
|
@@ -263,7 +272,7 @@ class Not(Where):
|
|
263
272
|
|
264
273
|
@classmethod
|
265
274
|
def eq(cls, value):
|
266
|
-
return Where
|
275
|
+
return Where(expr=f'<> {quoted(value)}')
|
267
276
|
|
268
277
|
|
269
278
|
class Case:
|
@@ -396,6 +405,8 @@ class Select(SQLObject):
|
|
396
405
|
self.break_lines = True
|
397
406
|
|
398
407
|
def update_values(self, key: str, new_values: list):
|
408
|
+
if key == SELECT:
|
409
|
+
new_values = [re.split(' as | AS ', val)[-1] for val in new_values]
|
399
410
|
for value in self.diff(key, new_values):
|
400
411
|
self.values.setdefault(key, []).append(value)
|
401
412
|
|
@@ -520,7 +531,7 @@ class Select(SQLObject):
|
|
520
531
|
if '=' in item:
|
521
532
|
a1, f1, a2, f2 = [r.strip() for r in re.split('[().=]', item) if r]
|
522
533
|
obj1: SQLObject = result[a1]
|
523
|
-
obj2:SQLObject = result[a2]
|
534
|
+
obj2: SQLObject = result[a2]
|
524
535
|
PrimaryKey.add(f2, obj2)
|
525
536
|
ForeignKey(obj2.table_name).add(f1, obj1)
|
526
537
|
else:
|
@@ -628,3 +639,15 @@ class RuleDateFuncReplace(Rule):
|
|
628
639
|
temp = Select(f'{target.table_name} {target.alias}')
|
629
640
|
Between(f'{year}-01-01', f'{year}-12-31').add(field, temp)
|
630
641
|
target.values[WHERE][i] = ' AND '.join(temp.values[WHERE])
|
642
|
+
|
643
|
+
|
644
|
+
if __name__ == "__main__":
|
645
|
+
query = Select(
|
646
|
+
'Cast c',
|
647
|
+
actor_id=Select(
|
648
|
+
'Actor a',
|
649
|
+
name=NamedField('actors_name'),
|
650
|
+
id=PrimaryKey
|
651
|
+
)
|
652
|
+
)
|
653
|
+
print(query)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sql_blocks
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.2
|
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
|
@@ -45,7 +45,7 @@ You can specify your own alias: `a = Select('Actor a')`
|
|
45
45
|
'Product',
|
46
46
|
due_date=NamedField(
|
47
47
|
'YEAR_ref',
|
48
|
-
ExpressionField('extract(year from
|
48
|
+
ExpressionField('extract(year from {f})') # <<---
|
49
49
|
)
|
50
50
|
)
|
51
51
|
```
|
@@ -302,7 +302,7 @@ best_movies = SelectIN(
|
|
302
302
|
rate=[GroupBy, Having.avg(Where.gt(4.5))]
|
303
303
|
)
|
304
304
|
m1 = Select(
|
305
|
-
Movie=Table('title,release_date),
|
305
|
+
Movie=Table('title,release_date'),
|
306
306
|
id=best_movies
|
307
307
|
)
|
308
308
|
|
@@ -320,9 +320,9 @@ m2 = Select(
|
|
320
320
|
Select(
|
321
321
|
'Product',
|
322
322
|
label=Case('price').when(
|
323
|
-
lt(50), 'cheap'
|
323
|
+
Where.lt(50), 'cheap'
|
324
324
|
).when(
|
325
|
-
gt(100), 'expensive'
|
325
|
+
Where.gt(100), 'expensive'
|
326
326
|
).else_value(
|
327
327
|
'normal'
|
328
328
|
)
|
@@ -0,0 +1,7 @@
|
|
1
|
+
sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
|
2
|
+
sql_blocks/sql_blocks.py,sha256=IpN6IsdogVWO_K9bgn3WAwgTQS4q8vr5F_8Uml4KqrI,20782
|
3
|
+
sql_blocks-0.1.2.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
4
|
+
sql_blocks-0.1.2.dist-info/METADATA,sha256=yk4lTuXk1AYJHk6C7fyWnmxRCVCSfSLDNJMQoekKP1I,8503
|
5
|
+
sql_blocks-0.1.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
6
|
+
sql_blocks-0.1.2.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
|
7
|
+
sql_blocks-0.1.2.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
|
2
|
-
sql_blocks/sql_blocks.py,sha256=kR6x6bd_E8lox_mrPDnVmad9bp_CF9n0WoIiW7mVulI,20188
|
3
|
-
sql_blocks-0.1.1.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
4
|
-
sql_blocks-0.1.1.dist-info/METADATA,sha256=diP3i83Hp3v7q3w633mJ56yoncAg-WOC5e9gLHRoNO8,8488
|
5
|
-
sql_blocks-0.1.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
6
|
-
sql_blocks-0.1.1.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
|
7
|
-
sql_blocks-0.1.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|