sql-blocks 1.25.420021312__py3-none-any.whl → 1.25.47999999999__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
@@ -1473,9 +1473,15 @@ class Select(SQLObject):
1473
1473
  for key in USUAL_KEYS:
1474
1474
  main.update_values(key, self.values.get(key, []))
1475
1475
 
1476
- def __add__(self, other: SQLObject):
1476
+ def copy(self) -> SQLObject:
1477
1477
  from copy import deepcopy
1478
- query = deepcopy(self)
1478
+ return deepcopy(self)
1479
+
1480
+ def no_relation_error(self, other: SQLObject):
1481
+ raise ValueError(f'No relationship found between {self.table_name} and {other.table_name}.')
1482
+
1483
+ def __add__(self, other: SQLObject):
1484
+ query = self.copy()
1479
1485
  if query.table_name.lower() == other.table_name.lower():
1480
1486
  for key in USUAL_KEYS:
1481
1487
  query.update_values(key, other.values.get(key, []))
@@ -1488,7 +1494,7 @@ class Select(SQLObject):
1488
1494
  PrimaryKey.add(primary_key, query)
1489
1495
  query.add(foreign_field, other)
1490
1496
  return other
1491
- raise ValueError(f'No relationship found between {query.table_name} and {other.table_name}.')
1497
+ self.no_relation_error(other) # === raise ERROR ... ===
1492
1498
  elif primary_key:
1493
1499
  PrimaryKey.add(primary_key, other)
1494
1500
  other.add(foreign_field, query)
@@ -1508,6 +1514,22 @@ class Select(SQLObject):
1508
1514
  if self.diff(key, other.values.get(key, []), True):
1509
1515
  return False
1510
1516
  return True
1517
+
1518
+ def __sub__(self, other: SQLObject) -> SQLObject:
1519
+ fk_field, primary_k = ForeignKey.find(self, other)
1520
+ if fk_field:
1521
+ query = self.copy()
1522
+ other = other.copy()
1523
+ else:
1524
+ fk_field, primary_k = ForeignKey.find(other, self)
1525
+ if not fk_field:
1526
+ self.no_relation_error(other) # === raise ERROR ... ===
1527
+ query = other.copy()
1528
+ other = self.copy()
1529
+ query.__class__ = NotSelectIN
1530
+ Field.add(fk_field, query)
1531
+ query.add(primary_k, other)
1532
+ return other
1511
1533
 
1512
1534
  def limit(self, row_count: int=100, offset: int=0):
1513
1535
  if Function.dialect == Dialect.SQL_SERVER:
@@ -1800,29 +1822,3 @@ def detect(text: str, join_queries: bool = True, format: str='') -> Select | lis
1800
1822
  return result
1801
1823
  # ===========================================================================================//
1802
1824
 
1803
-
1804
- if __name__ == "__main__":
1805
- query = Select(
1806
- 'Matricula M',
1807
- aluno_id=Select(
1808
- 'Aluno A',
1809
- nome=NamedField('nome_aluno'),
1810
- id=PrimaryKey
1811
- ),
1812
- curso=Select(
1813
- 'Curso C',
1814
- nome=NamedField('nome_curso'),
1815
- id=PrimaryKey
1816
- )
1817
- )
1818
- print('='*50)
1819
- print(query)
1820
- print('-'*50)
1821
- m, c, a = Select.parse( str(query) )
1822
- m(inicio=[Not.gt('2024-11-15'), Field])
1823
- query = m + a
1824
- print(query)
1825
- print('-'*50)
1826
- query.optimize()
1827
- print(query)
1828
- print('='*50)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.25.420021312
3
+ Version: 1.25.47999999999
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
@@ -315,6 +315,30 @@ m = Select...
315
315
  >> **m + c => Ok!**
316
316
 
317
317
 
318
+ ---
319
+ **8.3 Difference between queries**
320
+ ```
321
+ STATUS_DELIVERED_OK = 93
322
+ orders = Select('orders',
323
+ customer_id=ForeignKey('customers'),
324
+ status=eq(STATUS_DELIVERED_OK)
325
+ )
326
+ customers = Select('customers'
327
+ id=PrimaryKey, name=Field
328
+ )
329
+ gap = orders - customers
330
+ ```
331
+ return _customers without orders_:
332
+
333
+ SELECT
334
+ c.name
335
+ FROM
336
+ customers c
337
+ WHERE
338
+ NOT c.id IN (
339
+ SELECT o.customer_id FROM orders o
340
+ WHERE o.status = 93
341
+ )
318
342
  ---
319
343
 
320
344
  ### 9 - Comparing objects
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
+ sql_blocks/sql_blocks.py,sha256=Ho1Q7yej4MoDsXC9nvtnb2nsHjAk2MNi0zkWj65uYTk,61017
3
+ sql_blocks-1.25.47999999999.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-1.25.47999999999.dist-info/METADATA,sha256=Oh1WCd2FRe1OToEyxH4rvf7l5dREZ8JDbESunVoIFxg,21270
5
+ sql_blocks-1.25.47999999999.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-1.25.47999999999.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-1.25.47999999999.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=WkFmxphTnqT74MgGX6JCKLMRmxwDPGXdDmZ8DU-qObs,60834
3
- sql_blocks-1.25.420021312.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-1.25.420021312.dist-info/METADATA,sha256=qCiTXUBNpwkwBGWXLROxIVo2tz5XgLr5yLJeoYQKAmQ,20708
5
- sql_blocks-1.25.420021312.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-1.25.420021312.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-1.25.420021312.dist-info/RECORD,,