Oleg Broytmann <phd <at> phd.pp.ru> writes:

>    IWB interesting to see the code.


Ok, put a slightly updated version on SF Patch #1653898

The description there is pretty complete, the relevant changes for the
Alias/Join part are below, but note the big caveat from the SF description:
the db argument to sqlrepr is currently (old & new code) lost when wrapping
Selects in Joins or Aliases.

I've only tested this on postgresql for the time being.

- Luke

 class AliasField(Field):
-    as_string = '' # set it to "AS" if your database requires it
-
-    def __init__(self, tableName, fieldName, alias):
+    def __init__(self, tableName, fieldName, alias, aliasTable):
         Field.__init__(self, tableName, fieldName)
         self.alias = alias
+        self.aliasTable = aliasTable

     def __sqlrepr__(self, db):
         return self.alias + "." + self.fieldName
-
+
     def tablesUsedImmediate(self):
-        return ["%s %s %s" % (self.tableName, self.as_string, self.alias)]
+        return [self.aliasTable]

 class AliasTable(Table):
+    as_string = '' # set it to "AS" if your database requires it
     FieldClass = AliasField

     _alias_lock = threading.Lock()
@@ -437,55 +439,137 @@
             finally:
                 self._alias_lock.release()
         self.alias = alias
+        self.fullAlias = "%s %s %s" % (self.tableName, self.as_string, \
self.alias)

     def __getattr__(self, attr):
         if attr.startswith('__'):
             raise AttributeError
         if self.table:
             attr = getattr(self.table.q, attr).fieldName
-        return self.FieldClass(self.tableName, attr, self.alias)
+        return self.FieldClass(self.tableName, attr, self.alias, 
self.fullAlias)
+
+    def __sqlrepr__(self, db):
+        return self.fullAlias

-class Alias:
+class Alias(SQLExpression):
     def __init__(self, table, alias=None):
         self.q = AliasTable(table, alias)

+    def __sqlrepr__(self, db):
+        return sqlrepr(self.q, db)

+    def components(self):
+        return [self.q]
+


 class SQLJoin(SQLExpression):
     def __init__(self, table1, table2, op=','):
-        if table1 and type(table1) <> str:
-            if isinstance(table1, Alias):
-                table1 = "%s AS %s" % (table1.q.tableName, table1.q.alias)
-            else:
-                table1 = table1.sqlmeta.table
-        if type(table2) <> str:
-            if isinstance(table2, Alias):
-                table2 = "%s AS %s" % (table2.q.tableName, table2.q.alias)
-            else:
-                table2 = table2.sqlmeta.table
+        if hasattr(table1, 'sqlmeta'):
+            table1 = table1.sqlmeta.table
+        if hasattr(table2, 'sqlmeta'):
+            table2 = table2.sqlmeta.table
+        if isinstance(table1, str):
+            table1 = SQLConstant(table1)
+        if isinstance(table2, str): 
+            table2 = SQLConstant(table2)
         self.table1 = table1
         self.table2 = table2
         self.op = op
 
     def __sqlrepr__(self, db):
         if self.table1:
-            return "%s%s %s" % (self.table1, self.op, self.table2)
+            return "%s%s %s" % (sqlrepr(self.table1, db), self.op,
sqlrepr(self.table2, db))
         else:
-            return "%s %s" % (self.op, self.table2)
+            return "%s %s" % (self.op, sqlrepr(self.table2, db))



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to