sql-blocks 0.0.2__py3-none-any.whl → 0.0.3__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
@@ -124,14 +124,19 @@ class ForeignKey:
124
124
  def __init__(self, table_name: str):
125
125
  self.table_name = table_name
126
126
 
127
+ @staticmethod
128
+ def get_key(obj1: SQLObject, obj2: SQLObject) -> tuple:
129
+ return obj1.table_name, obj2.table_name
130
+
127
131
  def add(self, name: str, main: SQLObject):
128
- key = f'{main.table_name}_{self.table_name}'
129
- ForeignKey.references[key] = name
132
+ key = self.get_key(main, self)
133
+ ForeignKey.references[key] = (name, '')
130
134
 
131
135
  @classmethod
132
- def find(cls, obj1: SQLObject, obj2: SQLObject) -> list:
133
- key = f'{obj1.table_name}_{obj2.table_name}'
134
- return cls.references.get(key, '').split('_')
136
+ def find(cls, obj1: SQLObject, obj2: SQLObject) -> tuple:
137
+ key = cls.get_key(obj1, obj2)
138
+ a, b = cls.references.get(key, ('', ''))
139
+ return a, (b or obj2.key_field)
135
140
 
136
141
 
137
142
  def quoted(value) -> str:
@@ -341,17 +346,17 @@ class Select(SQLObject):
341
346
  update_values(key, self.values.get(key, []))
342
347
 
343
348
  def __add__(self, other: SQLObject):
344
- foreign_field, *primary_key = ForeignKey.find(self, other)
349
+ foreign_field, primary_key = ForeignKey.find(self, other)
345
350
  if not foreign_field:
346
- foreign_field, *primary_key = ForeignKey.find(other, self)
351
+ foreign_field, primary_key = ForeignKey.find(other, self)
347
352
  if foreign_field:
348
- if primary_key:
349
- PrimaryKey.add(primary_key[0], self)
353
+ if primary_key and not self.key_field:
354
+ PrimaryKey.add(primary_key, self)
350
355
  self.add(foreign_field, other)
351
356
  return other
352
357
  raise ValueError(f'No relationship found between {self.table_name} and {other.table_name}.')
353
358
  elif primary_key:
354
- PrimaryKey.add(primary_key[0], other)
359
+ PrimaryKey.add(primary_key, other)
355
360
  other.add(foreign_field, self)
356
361
  return self
357
362
 
@@ -419,7 +424,7 @@ class Select(SQLObject):
419
424
  fld.strip() for fld in re.split(
420
425
  separator, values[key]
421
426
  ) if len(tables) == 1
422
- or re.findall(f'^[( ]*{obj.alias}\.', fld)
427
+ or re.findall(f'^[( ]*{obj.alias}.', fld)
423
428
  ]
424
429
  obj.values[key] = [
425
430
  f'{obj.alias}.{f}' if not '.' in f else f for f in fields
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 0.0.2
3
+ Version: 0.0.3
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
@@ -53,7 +53,6 @@ You can specify your own alias: `a = Select('Actor a')`
53
53
  `a = Select( 'Actor a', age=Between(45, 69) )`
54
54
 
55
55
  3.2 -- Sub-queries:
56
-
57
56
  ```
58
57
  query = Select('Movie m', title=Field,
59
58
  id=SubSelect(
@@ -85,6 +84,15 @@ query = Select('Movie m', title=Field,
85
84
  ```
86
85
  > Could be AND=Options(...)
87
86
 
87
+ 3.4 -- Negative conditions use the _Not_ class instead of _Where_
88
+ ```
89
+ franchise
90
+ based_on_book=Not.is_null()
91
+ ```
92
+
93
+ 3.5 -- List of values
94
+ hash_tag=Where.list(['space', 'monster', 'gore'])
95
+
88
96
  ---
89
97
  ### 4 - A field can be two things at the same time:
90
98
 
@@ -292,5 +300,15 @@ m2 = Select(
292
300
  **m1 == m2 # --- True!**
293
301
 
294
302
  ---
295
- ---
296
303
 
304
+ ### 10 - CASE...WHEN...THEN
305
+ Select(
306
+ 'Product',
307
+ label=Case('price').when(
308
+ lt(50), 'cheap'
309
+ ).when(
310
+ gt(100), 'expensive'
311
+ ).else_value(
312
+ 'normal'
313
+ )
314
+ )
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=TodC5q-UEdYEz9v1RRoogVqqRcsKnZRY1WDGinrI2zo,26
2
+ sql_blocks/sql_blocks.py,sha256=r_PIxS-VqaY2zuBGwBgz6Z1Z2zBrx_CrfZ35yTMb7Sw,13352
3
+ sql_blocks-0.0.3.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-0.0.3.dist-info/METADATA,sha256=MHZb5SM_2RCZim4wESLsLaiCYReDOkn7tYJwGNsGWBE,6992
5
+ sql_blocks-0.0.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-0.0.3.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-0.0.3.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=TodC5q-UEdYEz9v1RRoogVqqRcsKnZRY1WDGinrI2zo,26
2
- sql_blocks/sql_blocks.py,sha256=V5WKBZg90emr0-jPpFEyX9rtU4VR3JlQKKu1qhlaiH0,13193
3
- sql_blocks-0.0.2.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-0.0.2.dist-info/METADATA,sha256=stJ7wQ6iRmHNvf4nJBVr_r5W13Z-ey1ZAllXd8kV9bw,6604
5
- sql_blocks-0.0.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-0.0.2.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-0.0.2.dist-info/RECORD,,