sql-blocks 0.1.1__tar.gz → 0.1.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 0.1.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
  )
@@ -30,7 +30,7 @@ You can specify your own alias: `a = Select('Actor a')`
30
30
  'Product',
31
31
  due_date=NamedField(
32
32
  'YEAR_ref',
33
- ExpressionField('extract(year from %)') # <<---
33
+ ExpressionField('extract(year from {f})') # <<---
34
34
  )
35
35
  )
36
36
  ```
@@ -287,7 +287,7 @@ best_movies = SelectIN(
287
287
  rate=[GroupBy, Having.avg(Where.gt(4.5))]
288
288
  )
289
289
  m1 = Select(
290
- Movie=Table('title,release_date),
290
+ Movie=Table('title,release_date'),
291
291
  id=best_movies
292
292
  )
293
293
 
@@ -305,9 +305,9 @@ m2 = Select(
305
305
  Select(
306
306
  'Product',
307
307
  label=Case('price').when(
308
- lt(50), 'cheap'
308
+ Where.lt(50), 'cheap'
309
309
  ).when(
310
- gt(100), 'expensive'
310
+ Where.gt(100), 'expensive'
311
311
  ).else_value(
312
312
  'normal'
313
313
  )
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sql_blocks"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  authors = [
5
5
  { name="Julio Cascalles", email="julio.cascalles@outlook.com" },
6
6
  ]
@@ -3,7 +3,7 @@ from setuptools import setup
3
3
 
4
4
  setup(
5
5
  name = 'sql_blocks',
6
- version = '0.1.1',
6
+ version = '0.1.2',
7
7
  author = 'Júlio Cascalles',
8
8
  author_email = 'julio.cascalles@outlook.com',
9
9
  packages = ['sql_blocks'],
@@ -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
- return self.expr.replace('%', Field.format(name, main))
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 | str=[]):
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.__constructor('<>', value)
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.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
  )
File without changes
File without changes