Commits

Kirill Simonov committed 9f62cca

Added addon `tweak.schema`.

The addon allows addressing tables with ambiguous names as `<schema>_<table>`.

Comments (0)

Files changed (4)

         'tweak.pgsql.timeout'
             ' = htsql_tweak.pgsql_timeout.export:TWEAK_PGSQL_TIMEOUT',
         'tweak.autolimit = htsql_tweak.autolimit.export:TWEAK_AUTOLIMIT',
+        'tweak.schema = htsql_tweak.schema.export:TWEAK_SCHEMA',
     ],
 }
 INSTALL_REQUIRES = [

src/htsql_tweak/schema/__init__.py

+#
+# Copyright (c) 2006-2011, Prometheus Research, LLC
+# Authors: Clark C. Evans <cce@clarkevans.com>,
+#          Kirill Simonov <xi@resolvent.net>
+#
+
+

src/htsql_tweak/schema/export.py

+#
+# Copyright (c) 2006-2011, Prometheus Research, LLC
+# Authors: Clark C. Evans <cce@clarkevans.com>,
+#          Kirill Simonov <xi@resolvent.net>
+#
+
+
+from htsql.util import autoimport
+from htsql.addon import Addon
+
+
+autoimport('htsql_tweak.schema')
+
+
+class TWEAK_SCHEMA(Addon):
+    pass
+
+

src/htsql_tweak/schema/lookup.py

+#
+# Copyright (c) 2006-2011, Prometheus Research, LLC
+# Authors: Clark C. Evans <cce@clarkevans.com>,
+#          Kirill Simonov <xi@resolvent.net>
+#
+
+
+from htsql.tr.lookup import LookupRoot, normalize
+from htsql.tr.recipe import FreeTableRecipe, AmbiguousRecipe
+
+
+class SchemaLookupRoot(LookupRoot):
+
+    def __call__(self):
+        recipe = super(SchemaLookupRoot, self).__call__()
+        if recipe is not None:
+            return recipe
+        recipe = self.lookup_schema_table()
+        if recipe is not None:
+            return recipe
+        return None
+
+    def lookup_schema_table(self):
+        candidates = []
+        for schema in self.catalog.schemas:
+            for table in schema.tables:
+                if normalize(schema.name+'_'+table.name) == self.key:
+                    candidates.append(table)
+        if len(candidates) == 1:
+            table = candidates[0]
+            return FreeTableRecipe(table)
+        if len(candidates) > 1:
+            return AmbiguousRecipe()
+
+