sql-blocks 1.25.30011511__py3-none-any.whl → 1.25.30012339__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
@@ -441,11 +441,11 @@ class Where:
441
441
  return cls.__constructor('=', value)
442
442
 
443
443
  @classmethod
444
- def contains(cls, content: str, pos: Position = Position.Middle):
444
+ def contains(cls, text: str, pos: Position = Position.Middle):
445
445
  return cls(
446
446
  "LIKE '{}{}{}'".format(
447
447
  '%' if pos != Position.StartsWith else '',
448
- content,
448
+ text,
449
449
  '%' if pos != Position.EndsWith else ''
450
450
  )
451
451
  )
@@ -524,7 +524,10 @@ eq, contains, gt, gte, lt, lte, is_null, inside = (
524
524
  getattr(Where, method) for method in
525
525
  ('eq', 'contains', 'gt', 'gte', 'lt', 'lte', 'is_null', 'inside')
526
526
  )
527
- startswith, endswith = [lambda x: contains(x, pos) for pos in Position if pos.value]
527
+ startswith, endswith = [
528
+ lambda x: contains(x, Position.StartsWith),
529
+ lambda x: contains(x, Position.EndsWith)
530
+ ]
528
531
 
529
532
 
530
533
  class Not(Where):
@@ -1454,33 +1457,34 @@ class NotSelectIN(SelectIN):
1454
1457
  condition_class = Not
1455
1458
 
1456
1459
 
1457
- class CTE:
1460
+ class CTE(Select):
1458
1461
  prefix = ''
1459
1462
 
1460
- def __init__(self, name: str, query_list: list[Select]):
1461
- self.name = name
1463
+ def __init__(self, table_name: str, query_list: list[Select]):
1464
+ super().__init__(table_name)
1462
1465
  for query in query_list:
1463
1466
  query.break_lines = False
1464
1467
  self.query_list = query_list
1465
1468
 
1466
- def format(self, query: Select) -> str:
1467
- LINE_MAX_SIZE = 50
1468
- result, line = [], ''
1469
- for word in str(query).split(' '):
1470
- if len(line) >= LINE_MAX_SIZE and word in KEYWORD:
1471
- result.append(line)
1472
- line = ''
1473
- line += word + ' '
1474
- if line:
1475
- result.append(line)
1476
- return '\n\t'.join(result)
1477
-
1478
1469
  def __str__(self) -> str:
1479
- return 'WITH {prefix}{name} AS (\n\t{queries}\n)SELECT * FROM {name}'.format(
1480
- prefix=self.prefix, name=self.name,
1481
- queries='\nUNION ALL\n\t'.join(
1482
- self.format(q) for q in self.query_list
1483
- )
1470
+ # ---------------------------------------------------------
1471
+ def justify(query: Select) -> str:
1472
+ LINE_MAX_SIZE = 50
1473
+ result, line = [], ''
1474
+ for word in str(query).split(' '):
1475
+ if len(line) >= LINE_MAX_SIZE and word in KEYWORD:
1476
+ result.append(line)
1477
+ line = ''
1478
+ line += word + ' '
1479
+ if line:
1480
+ result.append(line)
1481
+ return '\n\t'.join(result)
1482
+ # ---------------------------------------------------------
1483
+ return 'WITH {}{} AS (\n\t{}\n){}'.format(
1484
+ self.prefix, self.table_name,
1485
+ '\nUNION ALL\n\t'.join(
1486
+ justify(q) for q in self.query_list
1487
+ ), super().__str__()
1484
1488
  )
1485
1489
 
1486
1490
  class Recursive(CTE):
@@ -1488,8 +1492,8 @@ class Recursive(CTE):
1488
1492
 
1489
1493
  def __str__(self) -> str:
1490
1494
  if len(self.query_list) > 1:
1491
- alias = self.name[0].lower()
1492
- self.query_list[-1].values[FROM].append(f', {self.name} {alias}')
1495
+ self.query_list[-1].values[FROM].append(
1496
+ f', {self.table_name} {self.alias}')
1493
1497
  return super().__str__()
1494
1498
 
1495
1499
 
@@ -1620,7 +1624,7 @@ def detect(text: str, join_queries: bool = True) -> Select | list[Select]:
1620
1624
  continue
1621
1625
  pos = [ f.span() for f in re.finditer(fr'({table})[(]', text) ]
1622
1626
  for begin, end in pos[::-1]:
1623
- new_name = f'{table}_{count}' # See set_table (line 45)
1627
+ new_name = f'{table}_{count}' # See set_table (line 55)
1624
1628
  Select.EQUIVALENT_NAMES[new_name] = table
1625
1629
  text = text[:begin] + new_name + '(' + text[end:]
1626
1630
  count -= 1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.25.30011511
3
+ Version: 1.25.30012339
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
@@ -98,10 +98,7 @@ query = Select('Movie m', title=Field,
98
98
  awards=contains("Oscar")
99
99
  )
100
100
  AND=Options(
101
- ..., name=contains(
102
- 'Chris',
103
- Position.StartsWith
104
- )
101
+ ..., name=startswith('Chris')
105
102
  )
106
103
  ```
107
104
 
@@ -153,6 +150,35 @@ FROM
153
150
  JOIN Cast c ON (a.cast = c.id)
154
151
  ```
155
152
 
153
+ ---
154
+ **5.1 Multiple tables without JOIN**
155
+ > Warning: This is **NOT** recommended! ⛔
156
+
157
+
158
+ #### Example:
159
+ singer = Select(
160
+ "Singer artist", id=PrimaryKey,
161
+ name=NamedField('artist_name')
162
+ )
163
+ album = Select (
164
+ "Album album",
165
+ name=NamedField('album_name'),
166
+ artist_id=Where.join(singer), # <===== 👀
167
+ )
168
+ **>> print(query)**
169
+
170
+ SELECT
171
+ album.name as album_name,
172
+ artist.name as artist_name,
173
+ album.year_recorded
174
+ FROM
175
+ Album album
176
+ ,Singer artist
177
+ WHERE
178
+ (album.artist_id = artist.id)
179
+
180
+
181
+
156
182
  ---
157
183
  ### 6 - The reverse process (parse):
158
184
  ```
@@ -383,7 +409,7 @@ m2 = Select(
383
409
  query = Select(
384
410
  'Installments i', due_date=Field, customer=Select(
385
411
  'Customer c', id=PrimaryKey,
386
- name=contains('Smith', Position.EndsWith)
412
+ name=endswith('Smith')
387
413
  )
388
414
  )
389
415
  print(query)
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
+ sql_blocks/sql_blocks.py,sha256=TSrqjpP41uyocxLpTePI2JzSJorxbFB_jjLfWkHmnJ0,54584
3
+ sql_blocks-1.25.30012339.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-1.25.30012339.dist-info/METADATA,sha256=0nqWca0uwfqNns6xVIJAB_uR1KMjPnUUgrJHvBPX7bg,16753
5
+ sql_blocks-1.25.30012339.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-1.25.30012339.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-1.25.30012339.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=fTEX1TEkjr-uUg_aLVmkwVM5Ul3UQyvsW6Vw7z_TCQQ,54398
3
- sql_blocks-1.25.30011511.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-1.25.30011511.dist-info/METADATA,sha256=Hqo-6uZ0zLVyhvw7H9E1Hc7vGT5Ny6Kjxy2nzTqIEn0,16187
5
- sql_blocks-1.25.30011511.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-1.25.30011511.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-1.25.30011511.dist-info/RECORD,,