sql-blocks 1.2025.1002__py3-none-any.whl → 1.20250710__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
@@ -624,8 +624,9 @@ class Where:
624
624
  ))
625
625
 
626
626
  @classmethod
627
- def join(cls, query: SQLObject):
627
+ def join(cls, query: SQLObject, pairs: dict=None):
628
628
  where = cls(query)
629
+ where.pairs = pairs
629
630
  where.add = where.add_join
630
631
  return where
631
632
 
@@ -634,11 +635,18 @@ class Where:
634
635
  main.values[FROM].append(f',{query.table_name} {query.alias}')
635
636
  for key in USUAL_KEYS:
636
637
  main.update_values(key, query.values.get(key, []))
637
- if query.key_field:
638
- main.values.setdefault(WHERE, []).append('({a1}.{f1} = {a2}.{f2})'.format(
639
- a1=main.alias, f1=name,
640
- a2=query.alias, f2=query.key_field
641
- ))
638
+ if not self.pairs:
639
+ if not query.key_field:
640
+ return
641
+ self.pairs = {name: query.key_field}
642
+ a1, a2 = main.alias, query.alias
643
+ main.has_named_field
644
+ for f1, f2 in self.pairs.items():
645
+ if main.has_named_field(f1):
646
+ expr = f'({a2}.{f2} = {f1})'
647
+ else:
648
+ expr = f'({a2}.{f2} = {a1}.{f1})'
649
+ main.values.setdefault(WHERE, []).append(expr)
642
650
 
643
651
  def add(self, name: str, main: SQLObject):
644
652
  func_type = FUNCTION_CLASS.get(name.lower())
@@ -970,9 +978,12 @@ class GroupBy(Clause):
970
978
  query: Select = obj
971
979
  fields += query.values.get(SELECT, [])
972
980
  query.add(alias, main)
981
+ elif obj == Field:
982
+ fields += [alias]
973
983
  if not func:
974
- fields = [self.format(name, main)]
984
+ fields += [self.format(name, main)]
975
985
  for field in fields:
986
+ field = re.split(r'\s+(AS|as)\s+', field)[-1]
976
987
  main.values.setdefault(GROUP_BY, []).append(field)
977
988
 
978
989
  @classmethod
@@ -2367,3 +2378,23 @@ def detect(text: str, join_queries: bool = True, format: str='') -> Select | lis
2367
2378
  result += query
2368
2379
  return result
2369
2380
  # ===========================================================================================//
2381
+
2382
+ if __name__ == "__main__":
2383
+ query = Select(
2384
+ 'Sales s', #quantity=Sum().As('qty_sold'),
2385
+ ref_date=GroupBy(
2386
+ ref_year=Year, qty_sold=Sum('quantity'),
2387
+ vendor=Select(
2388
+ 'Vendor v', id=[PrimaryKey, NamedField('vendor_id')], name=Field
2389
+ ),
2390
+ prod_id=Field
2391
+ )
2392
+ )
2393
+ cte = CTE('Sales_by_year', [query])(
2394
+ _=Where.join(
2395
+ Select('Goal G'), dict(
2396
+ prod_id='product', ref_year='year', vendor_id='vendor'
2397
+ )
2398
+ )
2399
+ )
2400
+ print(cte)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sql_blocks
3
- Version: 1.2025.1002
3
+ Version: 1.20250710
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
@@ -197,6 +197,7 @@ FROM
197
197
  WHERE
198
198
  (album.artist_id = artist.id)
199
199
 
200
+ (*) --> For more than one relationship, use the **pairs** parameter.
200
201
 
201
202
 
202
203
  ---
@@ -0,0 +1,7 @@
1
+ sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
+ sql_blocks/sql_blocks.py,sha256=FLjT_V60QyfqUvbdu2hKUcFh3KTJNP091p0q_pg43v4,81469
3
+ sql_blocks-1.20250710.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
+ sql_blocks-1.20250710.dist-info/METADATA,sha256=zuWxpPD-1LoJHSD7D4EywcpQaYi5buEjfuypOmwNdOE,24486
5
+ sql_blocks-1.20250710.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ sql_blocks-1.20250710.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
+ sql_blocks-1.20250710.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sql_blocks/__init__.py,sha256=5ItzGCyqqa6kwY8wvF9kapyHsAiWJ7KEXCcC-OtdXKg,37
2
- sql_blocks/sql_blocks.py,sha256=QStl2invYAuvpSKkAqeutMlDgzOgNQvLF2pA8XK90aY,80473
3
- sql_blocks-1.2025.1002.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
4
- sql_blocks-1.2025.1002.dist-info/METADATA,sha256=SOD6TNWQF46azhVlf7N1PHQX-Xgx0-CbqbAC7-1-cfc,24417
5
- sql_blocks-1.2025.1002.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- sql_blocks-1.2025.1002.dist-info/top_level.txt,sha256=57AbUvUjYNy4m1EqDaU3WHeP-uyIAfV0n8GAUp1a1YQ,11
7
- sql_blocks-1.2025.1002.dist-info/RECORD,,