sql-blocks 1.25.26011923__py3-none-any.whl → 1.25.30011511__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
@@ -42,17 +42,23 @@ class SQLObject:
42
42
  if not table_name:
43
43
  return
44
44
  cls = SQLObject
45
+ is_file_name = any([
46
+ '/' in table_name, '.' in table_name
47
+ ])
48
+ ref = table_name
49
+ if is_file_name:
50
+ ref = table_name.split('/')[-1].split('.')[0]
45
51
  if cls.ALIAS_FUNC:
46
- self.__alias = cls.ALIAS_FUNC(table_name)
52
+ self.__alias = cls.ALIAS_FUNC(ref)
47
53
  elif ' ' in table_name.strip():
48
54
  table_name, self.__alias = table_name.split()
49
- elif '_' in table_name:
55
+ elif '_' in ref:
50
56
  self.__alias = ''.join(
51
57
  word[0].lower()
52
- for word in table_name.split('_')
58
+ for word in ref.split('_')
53
59
  )
54
60
  else:
55
- self.__alias = table_name.lower()[:3]
61
+ self.__alias = ref.lower()[:3]
56
62
  self.values.setdefault(FROM, []).append(f'{table_name} {self.alias}')
57
63
 
58
64
  @property
@@ -423,12 +429,12 @@ class Position(Enum):
423
429
  class Where:
424
430
  prefix = ''
425
431
 
426
- def __init__(self, expr: str):
427
- self.expr = expr
432
+ def __init__(self, content: str):
433
+ self.content = content
428
434
 
429
435
  @classmethod
430
436
  def __constructor(cls, operator: str, value):
431
- return cls(expr=f'{operator} {quoted(value)}')
437
+ return cls(f'{operator} {quoted(value)}')
432
438
 
433
439
  @classmethod
434
440
  def eq(cls, value):
@@ -472,15 +478,33 @@ class Where:
472
478
 
473
479
  @classmethod
474
480
  def formula(cls, formula: str):
475
- return cls( ExpressionField(formula) )
481
+ where = cls( ExpressionField(formula) )
482
+ where.add = where.add_expression
483
+ return where
484
+
485
+ def add_expression(self, name: str, main: SQLObject):
486
+ self.content = self.content.format(name, main)
487
+ main.values.setdefault(WHERE, []).append('{} {}'.format(
488
+ self.prefix, self.content
489
+ ))
490
+
491
+ @classmethod
492
+ def join(cls, query: SQLObject):
493
+ where = cls(query)
494
+ where.add = where.add_join
495
+ return where
496
+
497
+ def add_join(self, name: str, main: SQLObject):
498
+ query = self.content
499
+ main.values[FROM].append(f',{query.table_name} {query.alias}')
500
+ for key in USUAL_KEYS:
501
+ main.update_values(key, query.values.get(key, []))
502
+ main.values.setdefault(WHERE, []).append('({a1}.{f1} = {a2}.{f2})'.format(
503
+ a1=main.alias, f1=name,
504
+ a2=query.alias, f2=query.key_field
505
+ ))
476
506
 
477
507
  def add(self, name: str, main: SQLObject):
478
- if isinstance(self.expr, ExpressionField):
479
- self.expr = self.expr.format(name, main)
480
- main.values.setdefault(WHERE, []).append('{} {}'.format(
481
- self.prefix, self.expr
482
- ))
483
- return
484
508
  func_type = FUNCTION_CLASS.get(name.lower())
485
509
  exists = any(
486
510
  main.is_named_field(fld, SELECT)
@@ -492,7 +516,7 @@ class Where:
492
516
  elif not exists:
493
517
  name = Field.format(name, main)
494
518
  main.values.setdefault(WHERE, []).append('{}{} {}'.format(
495
- self.prefix, name, self.expr
519
+ self.prefix, name, self.content
496
520
  ))
497
521
 
498
522
 
@@ -500,6 +524,7 @@ eq, contains, gt, gte, lt, lte, is_null, inside = (
500
524
  getattr(Where, method) for method in
501
525
  ('eq', 'contains', 'gt', 'gte', 'lt', 'lte', 'is_null', 'inside')
502
526
  )
527
+ startswith, endswith = [lambda x: contains(x, pos) for pos in Position if pos.value]
503
528
 
504
529
 
505
530
  class Not(Where):
@@ -507,7 +532,7 @@ class Not(Where):
507
532
 
508
533
  @classmethod
509
534
  def eq(cls, value):
510
- return Where(expr=f'<> {quoted(value)}')
535
+ return Where(f'<> {quoted(value)}')
511
536
 
512
537
 
513
538
  class Case:
@@ -548,7 +573,7 @@ class Options:
548
573
  child: Where
549
574
  for field, child in self.__children.items():
550
575
  conditions.append(' {} {} '.format(
551
- Field.format(field, main), child.expr
576
+ Field.format(field, main), child.content
552
577
  ))
553
578
  main.values.setdefault(WHERE, []).append(
554
579
  '(' + logical_separator.join(conditions) + ')'
@@ -647,7 +672,7 @@ class Having:
647
672
 
648
673
  def add(self, name: str, main:SQLObject):
649
674
  main.values[GROUP_BY][-1] += ' HAVING {} {}'.format(
650
- self.function.format(name, main), self.condition.expr
675
+ self.function.format(name, main), self.condition.content
651
676
  )
652
677
 
653
678
  @classmethod
@@ -1451,11 +1476,11 @@ class CTE:
1451
1476
  return '\n\t'.join(result)
1452
1477
 
1453
1478
  def __str__(self) -> str:
1454
- return 'WITH {}{} AS (\n\t{}\n)SELECT * FROM {}'.format(
1455
- self.prefix, self.name,
1456
- '\nUNION ALL\n\t'.join(
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(
1457
1482
  self.format(q) for q in self.query_list
1458
- ), self.name
1483
+ )
1459
1484
  )
1460
1485
 
1461
1486
  class Recursive(CTE):
@@ -1584,7 +1609,7 @@ def parser_class(text: str) -> Parser:
1584
1609
  return None
1585
1610
 
1586
1611
 
1587
- def detect(text: str, join_queries: bool = True) -> Select:
1612
+ def detect(text: str, join_queries: bool = True) -> Select | list[Select]:
1588
1613
  from collections import Counter
1589
1614
  parser = parser_class(text)
1590
1615
  if not parser:
@@ -1607,15 +1632,3 @@ def detect(text: str, join_queries: bool = True) -> Select:
1607
1632
  result += query
1608
1633
  return result
1609
1634
 
1610
-
1611
- if __name__ == "__main__":
1612
- MY_NAME = 'Júlio Cascalles'
1613
- # query = Select('SocialMedia s', post=Count, reaction=Sum, user=GroupBy)
1614
- # print( CTE('Metrics', [query]) )
1615
- q1 = Select(
1616
- 'SocialMedia me', name=[ eq(MY_NAME), Field ]
1617
- )
1618
- q2 = Select(
1619
- 'SocialMedia you', name=Field, id=Where.formula('{af} = n.friend')
1620
- )
1621
- print( Recursive('Network', [q1, q2]) )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.25.26011923
3
+ Version: 1.25.30011511
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
@@ -0,0 +1,7 @@
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,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=lVV1Z09JVmwvV0vzKqZVBHpGWpZefD1RsMZy-aIJZSk,53868
3
- sql_blocks-1.25.26011923.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-1.25.26011923.dist-info/METADATA,sha256=qvFr99uB0b95uv7MqpvpZaMrH7VrE557bmW4YWFQgnA,16187
5
- sql_blocks-1.25.26011923.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-1.25.26011923.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-1.25.26011923.dist-info/RECORD,,