Commits

Kirill Simonov committed 10f950d

`tweak.schema`: allow to use the `<schema>_` prefix in link names.

  • Participants
  • Parent commits 4dc22bb

Comments (0)

Files changed (2)

File Makefile.common

 PGSQL_ADDRESS?=${PGSQL_ADDRESS1}${PGSQL_ADDRESS2}
 PGSQL_URI?=pgsql://${PGSQL_ADDRESS}/${PGSQL_DATABASE}
 
-PGSQL_CLIENT?=${PSQL_NATIVE} \
+PGSQL_CLIENT?=${PGSQL_NATIVE} \
 	$(if ${PGSQL_HOST},-h ${PGSQL_HOST}) \
 	$(if ${PGSQL_PORT}, -p ${PGSQL_PORT}) \
 	$(if ${PGSQL_USERNAME},-U ${PGSQL_USERNAME}) \

File src/htsql_tweak/schema/lookup.py

 #
 
 
-from htsql.tr.lookup import LookupRoot, normalize
-from htsql.tr.recipe import FreeTableRecipe, AmbiguousRecipe
+from htsql.entity import DirectJoin, ReverseJoin
+from htsql.tr.lookup import LookupRoot, LookupTable, normalize
+from htsql.tr.recipe import (FreeTableRecipe, AttachedTableRecipe,
+                             AmbiguousRecipe)
 
 
 class SchemaLookupRoot(LookupRoot):
             return AmbiguousRecipe()
 
 
+class SchemaLookupTable(LookupTable):
+
+    def lookup_direct_join(self):
+        recipe = super(SchemaLookupTable, self).lookup_direct_join()
+        if recipe is not None:
+            return recipe
+        origin = self.binding.table
+        candidates = []
+        for foreign_key in origin.foreign_keys:
+            name = foreign_key.target_schema_name+'_'+foreign_key.target_name
+            if normalize(name) == self.key:
+                candidates.append(foreign_key)
+        if len(candidates) == 1:
+            foreign_key = candidates[0]
+            target_schema = self.catalog.schemas[foreign_key.target_schema_name]
+            target = target_schema.tables[foreign_key.target_name]
+            join = DirectJoin(origin, target, foreign_key)
+            return AttachedTableRecipe([join])
+        if len(candidates) > 1:
+            return AmbiguousRecipe()
+
+    def lookup_reverse_join(self):
+        recipe = super(SchemaLookupTable, self).lookup_reverse_join()
+        if recipe is not None:
+            return recipe
+        origin = self.binding.table
+        candidates = []
+        for target_schema in self.catalog.schemas:
+            for target in target_schema.tables:
+                name = target.schema_name+'_'+target.name
+                if normalize(name) != self.key:
+                    continue
+                for foreign_key in target.foreign_keys:
+                    if (foreign_key.target_schema_name == origin.schema_name
+                            and foreign_key.target_name == origin.name):
+                        candidates.append(foreign_key)
+        if len(candidates) == 1:
+            foreign_key = candidates[0]
+            target_schema = self.catalog.schemas[foreign_key.origin_schema_name]
+            target = target_schema.tables[foreign_key.origin_name]
+            join = ReverseJoin(origin, target, foreign_key)
+            return AttachedTableRecipe([join])
+        if len(candidates) > 1:
+            return AmbiguousRecipe()
+
+