sql-blocks 0.0.8__py3-none-any.whl → 0.1.0__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
@@ -141,19 +141,28 @@ class Distinct(Field):
141
141
  class NamedField:
142
142
  def __init__(self, alias: str, class_type = Field):
143
143
  self.alias = alias
144
- if class_type not in [Field] + Field.__subclasses__():
145
- raise TypeError('class_type must be a Field (sub)class.')
146
144
  self.class_type = class_type
147
145
 
148
146
  def add(self, name: str, main: SQLObject):
149
147
  main.values.setdefault(SELECT, []).append(
150
148
  '{} as {}'.format(
151
149
  self.class_type.format(name, main),
152
- self.alias
150
+ self.alias # --- field alias
153
151
  )
154
152
  )
155
153
 
156
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(name, main))
161
+
162
+ def format(self, name: str, main: SQLObject) -> str:
163
+ return self.expr.replace('%', Field.format(name, main))
164
+
165
+
157
166
  class Table:
158
167
  def __init__(self, fields: list | str=[]):
159
168
  if isinstance(fields, str):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 0.0.8
3
+ Version: 0.1.0
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=kR6x6bd_E8lox_mrPDnVmad9bp_CF9n0WoIiW7mVulI,20188
3
+ sql_blocks-0.1.0.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-0.1.0.dist-info/METADATA,sha256=1GCMvfnuEajoLEiEtEwUZvsqX2SSff3BkEJe2D9P6rk,8488
5
+ sql_blocks-0.1.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-0.1.0.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-0.1.0.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=TodC5q-UEdYEz9v1RRoogVqqRcsKnZRY1WDGinrI2zo,26
2
- sql_blocks/sql_blocks.py,sha256=mC8TTgAXrGL0X_Zn5GPv2HytlaRKm9jpcUyaSnepFOs,19963
3
- sql_blocks-0.0.8.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-0.0.8.dist-info/METADATA,sha256=B7L8E2UI-0GWHsDVPWibQsMrp69wzP4XRfXOOfg5GV4,8190
5
- sql_blocks-0.0.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-0.0.8.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-0.0.8.dist-info/RECORD,,