sql-blocks 0.0.7__py3-none-any.whl → 0.0.9__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 CHANGED
@@ -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 ', '', f'{PATTERN_PREFIX}| '),
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
- pattern = KEYWORD[key][2]
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.strip()).lower()
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
- main.update_values(
389
- FROM, [
390
- '{jt}JOIN {tb} {a2} ON ({a1}.{f1} = {a2}.{f2})'.format(
391
- jt=self.join_type.value,
392
- tb=self.table_name,
393
- a1=main.alias, f1=name,
394
- a2=self.alias, f2=self.key_field
395
- )
396
- ] + self.values[FROM][1:]
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.7
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**:
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=TodC5q-UEdYEz9v1RRoogVqqRcsKnZRY1WDGinrI2zo,26
2
+ sql_blocks/sql_blocks.py,sha256=6_QkwFGtWkMtBKp2z8xc7qSb-809mfNU4q0msq-wkJE,20176
3
+ sql_blocks-0.0.9.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-0.0.9.dist-info/METADATA,sha256=UH8cxa2oHsT2lL8Nkmcg0PrMlewfQZ0pxo6ROMuPDeY,8488
5
+ sql_blocks-0.0.9.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-0.0.9.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-0.0.9.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=TodC5q-UEdYEz9v1RRoogVqqRcsKnZRY1WDGinrI2zo,26
2
- sql_blocks/sql_blocks.py,sha256=w8vI-UxyBXj_YD09zKF3AKzJlmGN1wIRJgzbcCh6rmU,19684
3
- sql_blocks-0.0.7.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-0.0.7.dist-info/METADATA,sha256=2WFDh50ro70p_8f850VxyXQx82MY7BIq1nZEE-EY8Jk,8190
5
- sql_blocks-0.0.7.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-0.0.7.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-0.0.7.dist-info/RECORD,,