sql-blocks 0.0.7__tar.gz → 0.0.9__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-0.0.7/sql_blocks.egg-info → sql_blocks-0.0.9}/PKG-INFO +15 -1
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/README.md +14 -0
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/pyproject.toml +1 -1
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/setup.py +1 -1
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/sql_blocks/sql_blocks.py +32 -16
- {sql_blocks-0.0.7 → sql_blocks-0.0.9/sql_blocks.egg-info}/PKG-INFO +15 -1
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/LICENSE +0 -0
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/setup.cfg +0 -0
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/sql_blocks/__init__.py +0 -0
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/sql_blocks.egg-info/SOURCES.txt +0 -0
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/sql_blocks.egg-info/dependency_links.txt +0 -0
- {sql_blocks-0.0.7 → sql_blocks-0.0.9}/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: 0.0.
|
3
|
+
Version: 0.0.9
|
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
|
@@ -38,6 +38,20 @@ You can specify your own alias: `a = Select('Actor a')`
|
|
38
38
|
name=NamedField('actors_name', Distinct)
|
39
39
|
)
|
40
40
|
|
41
|
+
|
42
|
+
2.1 -- Using expression as a field:
|
43
|
+
```
|
44
|
+
Select(
|
45
|
+
'Product',
|
46
|
+
due_date=NamedField(
|
47
|
+
'YEAR_ref',
|
48
|
+
ExpressionField('extract(year from %)') # <<---
|
49
|
+
)
|
50
|
+
)
|
51
|
+
```
|
52
|
+
...should return:
|
53
|
+
**SELECT extract(year from due_date) as YEAR_ref...**
|
54
|
+
|
41
55
|
---
|
42
56
|
|
43
57
|
### 3 - To set conditions, use **Where**:
|
@@ -23,6 +23,20 @@ You can specify your own alias: `a = Select('Actor a')`
|
|
23
23
|
name=NamedField('actors_name', Distinct)
|
24
24
|
)
|
25
25
|
|
26
|
+
|
27
|
+
2.1 -- Using expression as a field:
|
28
|
+
```
|
29
|
+
Select(
|
30
|
+
'Product',
|
31
|
+
due_date=NamedField(
|
32
|
+
'YEAR_ref',
|
33
|
+
ExpressionField('extract(year from %)') # <<---
|
34
|
+
)
|
35
|
+
)
|
36
|
+
```
|
37
|
+
...should return:
|
38
|
+
**SELECT extract(year from due_date) as YEAR_ref...**
|
39
|
+
|
26
40
|
---
|
27
41
|
|
28
42
|
### 3 - To set conditions, use **Where**:
|
@@ -10,7 +10,7 @@ DISTINCT_SF_PR = f'(DISTINCT|distinct)|{SUFFIX_AND_PRE}'
|
|
10
10
|
KEYWORD = {
|
11
11
|
'SELECT': (',{}', 'SELECT *', DISTINCT_SF_PR),
|
12
12
|
'FROM': ('{}', '', PATTERN_SUFFIX),
|
13
|
-
'WHERE': ('{}AND ', '',
|
13
|
+
'WHERE': ('{}AND ', '', ''),
|
14
14
|
'GROUP BY': (',{}', '', SUFFIX_AND_PRE),
|
15
15
|
'ORDER BY': (',{}', '', SUFFIX_AND_PRE),
|
16
16
|
'LIMIT': (' ', '', ''),
|
@@ -70,11 +70,17 @@ class SQLObject:
|
|
70
70
|
return KEYWORD[key][0].format(appendix.get(key, ''))
|
71
71
|
|
72
72
|
def diff(self, key: str, search_list: list, symmetrical: bool=False) -> set:
|
73
|
-
|
73
|
+
def cleanup(fld: str) -> str:
|
74
|
+
if symmetrical:
|
75
|
+
fld = fld.lower()
|
76
|
+
return fld.strip()
|
77
|
+
pattern = KEYWORD[key][2]
|
78
|
+
if key == WHERE and symmetrical:
|
79
|
+
pattern = f'{PATTERN_PREFIX}| '
|
74
80
|
separator = self.get_separator(key)
|
75
81
|
def field_set(source: list) -> set:
|
76
82
|
return set(
|
77
|
-
re.sub(pattern, '', fld
|
83
|
+
re.sub(pattern, '', cleanup(fld))
|
78
84
|
for string in source
|
79
85
|
for fld in re.split(separator, string)
|
80
86
|
)
|
@@ -135,19 +141,28 @@ class Distinct(Field):
|
|
135
141
|
class NamedField:
|
136
142
|
def __init__(self, alias: str, class_type = Field):
|
137
143
|
self.alias = alias
|
138
|
-
if class_type not in [Field] + Field.__subclasses__():
|
139
|
-
raise TypeError('class_type must be a Field (sub)class.')
|
140
144
|
self.class_type = class_type
|
141
145
|
|
142
146
|
def add(self, name: str, main: SQLObject):
|
143
147
|
main.values.setdefault(SELECT, []).append(
|
144
148
|
'{} as {}'.format(
|
145
149
|
self.class_type.format(name, main),
|
146
|
-
self.alias
|
150
|
+
self.alias # --- field alias
|
147
151
|
)
|
148
152
|
)
|
149
153
|
|
150
154
|
|
155
|
+
class ExpressionField:
|
156
|
+
def __init__(self, expr: str):
|
157
|
+
self.expr = expr
|
158
|
+
|
159
|
+
def add(self, name: str, main: SQLObject):
|
160
|
+
main.values.setdefault(SELECT, []).append(self.format)
|
161
|
+
|
162
|
+
def format(self, name: str, main: SQLObject) -> str:
|
163
|
+
return self.expr.replace('%', Field.format(name, main))
|
164
|
+
|
165
|
+
|
151
166
|
class Table:
|
152
167
|
def __init__(self, fields: list | str=[]):
|
153
168
|
if isinstance(fields, str):
|
@@ -385,16 +400,17 @@ class Select(SQLObject):
|
|
385
400
|
self.values.setdefault(key, []).append(value)
|
386
401
|
|
387
402
|
def add(self, name: str, main: SQLObject):
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
403
|
+
new_tables = [
|
404
|
+
'{jt}JOIN {tb} {a2} ON ({a1}.{f1} = {a2}.{f2})'.format(
|
405
|
+
jt=self.join_type.value,
|
406
|
+
tb=self.table_name,
|
407
|
+
a1=main.alias, f1=name,
|
408
|
+
a2=self.alias, f2=self.key_field
|
409
|
+
)
|
410
|
+
]
|
411
|
+
if new_tables not in main.values.get(FROM, []):
|
412
|
+
new_tables += self.values[FROM][1:]
|
413
|
+
main.values.setdefault(FROM, []).extend(new_tables)
|
398
414
|
for key in USUAL_KEYS:
|
399
415
|
main.update_values(key, self.values.get(key, []))
|
400
416
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sql_blocks
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.9
|
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
|
@@ -38,6 +38,20 @@ You can specify your own alias: `a = Select('Actor a')`
|
|
38
38
|
name=NamedField('actors_name', Distinct)
|
39
39
|
)
|
40
40
|
|
41
|
+
|
42
|
+
2.1 -- Using expression as a field:
|
43
|
+
```
|
44
|
+
Select(
|
45
|
+
'Product',
|
46
|
+
due_date=NamedField(
|
47
|
+
'YEAR_ref',
|
48
|
+
ExpressionField('extract(year from %)') # <<---
|
49
|
+
)
|
50
|
+
)
|
51
|
+
```
|
52
|
+
...should return:
|
53
|
+
**SELECT extract(year from due_date) as YEAR_ref...**
|
54
|
+
|
41
55
|
---
|
42
56
|
|
43
57
|
### 3 - To set conditions, use **Where**:
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|