sql-blocks 0.31.13__py3-none-any.whl → 0.31.89__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
@@ -63,7 +63,7 @@ class SQLObject:
63
63
 
64
64
  @staticmethod
65
65
  def get_separator(key: str) -> str:
66
- appendix = {WHERE: 'and|', FROM: 'join|JOIN'}
66
+ appendix = {WHERE: r'\s+and\s+|', FROM: r'\s+join\s+|\s+JOIN\s+'}
67
67
  return KEYWORD[key][0].format(appendix.get(key, ''))
68
68
 
69
69
  def diff(self, key: str, search_list: list, exact: bool=False) -> set:
@@ -79,7 +79,7 @@ class SQLObject:
79
79
  fld = fld.lower()
80
80
  return fld.strip()
81
81
  def is_named_field(fld: str) -> bool:
82
- return key == SELECT and re.search(' as | AS ', fld)
82
+ return key == SELECT and re.search(r'\s+as\s+|\s+AS\s+', fld)
83
83
  def field_set(source: list) -> set:
84
84
  return set(
85
85
  (
@@ -369,10 +369,14 @@ class OrderBy:
369
369
  sort: SortType = SortType.ASC
370
370
  @classmethod
371
371
  def add(cls, name: str, main: SQLObject):
372
+ def is_function() -> bool:
373
+ diff = main.diff(SELECT, [name.lower()], True)
374
+ FUNCTION_CLASS = {f.__name__.lower(): f for f in Function.__subclasses__()}
375
+ return diff.intersection(FUNCTION_CLASS)
372
376
  found = re.findall(r'^_\d', name)
373
377
  if found:
374
378
  name = found[0].replace('_', '')
375
- elif main.alias:
379
+ elif main.alias and not is_function():
376
380
  name = f'{main.alias}.{name}'
377
381
  main.values.setdefault(ORDER_BY, []).append(name+cls.sort.value)
378
382
 
@@ -476,6 +480,10 @@ class QueryLanguage:
476
480
  if not method or (not values and not self.has_default[key]):
477
481
  self.result[ref] = ''
478
482
  continue
483
+ if key == FROM:
484
+ values[0] = '{} {}'.format(
485
+ self.target.aka(), self.target.alias
486
+ ).strip()
479
487
  text = method(values)
480
488
  self.result[ref] = self.prefix(key) + text
481
489
  return self.pattern.format(**self.result).strip()
@@ -767,7 +775,7 @@ class CypherParser(Parser):
767
775
  REGEX = {}
768
776
  CHAR_SET = r'[(,?)^{}[\]]'
769
777
  KEYWORDS = '|'.join(
770
- fr'\s+{word}\s+'
778
+ fr'\b{word}\b'
771
779
  for word in "where return WHERE RETURN and AND".split()
772
780
  )
773
781
 
@@ -854,7 +862,11 @@ class CypherParser(Parser):
854
862
  else:
855
863
  if not curr.values.get(SELECT):
856
864
  raise IndexError(f'Foreign Key not found for {curr.table_name}.')
857
- foreign_fld = curr.values[SELECT][0].split('.')[-1]
865
+ fields = [
866
+ fld for fld in curr.values[SELECT]
867
+ if fld not in curr.values.get(GROUP_BY, [])
868
+ ]
869
+ foreign_fld = fields[0].split('.')[-1]
858
870
  curr.delete(foreign_fld, [SELECT])
859
871
  if curr.join_type == JoinType.RIGHT:
860
872
  pk_field, foreign_fld = foreign_fld, pk_field
@@ -1040,12 +1052,16 @@ class Select(SQLObject):
1040
1052
  for value in self.diff(key, new_values):
1041
1053
  self.values.setdefault(key, []).append(value)
1042
1054
 
1055
+ def aka(self) -> str:
1056
+ result = self.table_name
1057
+ return self.EQUIVALENT_NAMES.get(result, result)
1058
+
1043
1059
  def add(self, name: str, main: SQLObject):
1044
1060
  old_tables = main.values.get(FROM, [])
1045
1061
  new_tables = set([
1046
1062
  '{jt}JOIN {tb} {a2} ON ({a1}.{f1} = {a2}.{f2})'.format(
1047
1063
  jt=self.join_type.value,
1048
- tb=self.EQUIVALENT_NAMES.get(self.table_name, self.table_name),
1064
+ tb=self.aka(),
1049
1065
  a1=main.alias, f1=name,
1050
1066
  a2=self.alias, f2=self.key_field
1051
1067
  )
@@ -1247,9 +1263,19 @@ def detect(text: str) -> Select:
1247
1263
 
1248
1264
 
1249
1265
  if __name__ == "__main__":
1250
- print('@'*100)
1251
- print( detect(
1252
- # 'User(^name?role="Manager",id)<-Contact(requester, guest)->User(id,name)'
1253
- 'User(^name,id) <-Contact(requester,guest)-> User(id,name)'
1254
- ) )
1255
- print('@'*100)
1266
+ print('░▒▓▒░'*20)
1267
+ p, c, a = Select.parse(
1268
+ '''
1269
+ Professor(?nome="Júlio Cascalles", id)
1270
+ <- Curso@disciplina(professor, aluno) ->
1271
+ Aluno(id ^count$qtd_alunos)
1272
+ ''', CypherParser
1273
+ )
1274
+ print(p)
1275
+ print('-'*50)
1276
+ print(c)
1277
+ print('-'*50)
1278
+ print(a)
1279
+ print('='*50)
1280
+ print(a + c + p)
1281
+ print('░▒▓▒░'*20)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 0.31.13
3
+ Version: 0.31.89
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
@@ -390,7 +390,7 @@ a, c, m = Select.parse(
390
390
  <- Cast(actor_id, movie_id) ->
391
391
  Movie(id ^title)
392
392
  """,
393
- Cypher
393
+ CypherParser
394
394
  # ^^^ recognizes syntax like Neo4J queries
395
395
  )
396
396
  ```
@@ -466,7 +466,7 @@ print(query)
466
466
  db.people.find({
467
467
  {
468
468
  $or: [
469
- status:{$eq:"B"},
469
+ {status:{$eq:"B"}},
470
470
  age:{$lt:50}
471
471
  ]
472
472
  },
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
+ sql_blocks/sql_blocks.py,sha256=nsIT8JEeYHw3KIkzAUHUdzUAR_i2asJmdytsvW0hYKc,43342
3
+ sql_blocks-0.31.89.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-0.31.89.dist-info/METADATA,sha256=xKHVfM76nzvZBr-TYC5_PN4bAvYW9oWoi5ChPIPdbDE,12202
5
+ sql_blocks-0.31.89.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-0.31.89.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-0.31.89.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=AcA-AOZ4XSXD5srMNsnv43ljPF06_DInTu9ljrzQQMQ,42496
3
- sql_blocks-0.31.13.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-0.31.13.dist-info/METADATA,sha256=6Ckz7aC4I8tQ1JU-hX_AIMsyw1rw2MzwXRQvOAZ7KII,12195
5
- sql_blocks-0.31.13.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-0.31.13.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-0.31.13.dist-info/RECORD,,