Ok, I done the tests over the new structure of SQLRelatedJoin, it
seems to be working even if I use a non standard table name or column
name, I sending two diffs:
joins.py-0.7.1dev_r1728-py2.3.diff
joins.py-0.8dev_r1727-py2.3.diff
The first one have the SQLRelatedJoin fixed, the second one have the
fix of ManyToMany and the fix of SQLRelatedJoin. I remove the old code
of SQLRelatedJoin because it still returning a select result, in true,
there is no difference on the object returned, except that now you can
use filter or orderBy without problems...
Thanks for attention
2006/4/25, Oleg Broytmann <[EMAIL PROTECTED]>:
> On Tue, Apr 25, 2006 at 11:47:17AM -0300, michelts wrote:
> > I done the SQLRelatedJoin using SQLBuilder but I saw that there is a
> > OneToMany and ManyToMany joins now (I saw this in svn). Will *Join
> > deprecated?
>
> Not in the nearest future. ManyToMany wasn't even backported to 0.7.1.
>
> Oleg.
> --
> Oleg Broytmann http://phd.pp.ru/ [EMAIL PROTECTED]
> Programmers don't die, they just GOSUB without RETURN.
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> sqlobject-discuss mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
>
--
Michel Thadeu Sabchuk
Curitiba - Brasil
Index: joins.py
===================================================================
--- joins.py (revision 1729)
+++ joins.py (working copy)
@@ -225,36 +225,52 @@
class RelatedJoin(MultipleJoin):
baseClass = SORelatedJoin
+# helper classes to SQLRelatedJoin
+class OtherTableToJoin(sqlbuilder.SQLExpression):
+ def __init__(self, otherTable, otherIdName, interTable, joinColumn):
+ self.otherTable = otherTable
+ self.otherIdName = otherIdName
+ self.interTable = interTable
+ self.joinColumn = joinColumn
+
+ def __sqlrepr__(self, db):
+ return '%s.%s = %s.%s' % (self.otherTable, self.otherIdName, self.interTable, self.joinColumn)
+
+class JoinToTable(sqlbuilder.SQLExpression):
+ def __init__(self, table, idName, interTable, joinColumn):
+ self.table = table
+ self.idName = idName
+ self.interTable = interTable
+ self.joinColumn = joinColumn
+
+ def __sqlrepr__(self, db):
+ return '%s.%s = %s.%s' % (self.interTable, self.joinColumn, self.table, self.idName)
+
+class TableToId(sqlbuilder.SQLExpression):
+ def __init__(self, table, idName, idValue):
+ self.table = table
+ self.idName = idName
+ self.idValue = idValue
+
+ def __sqlrepr__(self, db):
+ return '%s.%s = %s' % (self.table, self.idName, self.idValue)
+
class SOSQLRelatedJoin(SORelatedJoin):
def performJoin(self, inst):
- options={
- 'otherTable' : self.otherClass.sqlmeta.table,
- 'otherID' : self.otherClass.sqlmeta.idName,
- 'interTable' : self.intermediateTable,
- 'table' : self.soClass.sqlmeta.table,
- 'ID' : self.soClass.sqlmeta.idName,
- 'joinCol' : self.joinColumn,
- 'otherCol' : self.otherColumn,
- 'idValue' : inst.id,
- }
- clause = '''\
-%(otherTable)s.%(otherID)s = %(interTable)s.%(otherCol)s and
-%(interTable)s.%(joinCol)s = %(table)s.%(ID)s and
-%(table)s.%(ID)s = %(idValue)s''' % options
- if inst.sqlmeta._perConnection:
- conn = inst._connection
- else:
- conn = None
- results = self.otherClass.select(sqlbuilder.SQLConstant(clause),
- clauseTables=(
- options['table'],
- options['otherTable'],
- options['interTable'],
+ results = self.otherClass.select(sqlbuilder.AND(
+ OtherTableToJoin(
+ self.otherClass.sqlmeta.table, self.otherClass.sqlmeta.idName,
+ self.intermediateTable, self.otherColumn
),
- connection=conn
- )
- # TODO (michelts): apply orderBy on the selection
- return results
+ JoinToTable(
+ self.soClass.sqlmeta.table, self.soClass.sqlmeta.idName,
+ self.intermediateTable, self.joinColumn
+ ),
+ TableToId(self.soClass.sqlmeta.table, self.soClass.sqlmeta.idName, inst.id),
+ ), clauseTables=(self.soClass.sqlmeta.table, self.otherClass.sqlmeta.table, self.intermediateTable))
+ if self.orderBy is NoDefault:
+ self.orderBy = self.otherClass.sqlmeta.defaultOrder
+ return results.orderBy(self.orderBy)
class SQLRelatedJoin(RelatedJoin):
baseClass = SOSQLRelatedJoin
Index: joins.py
===================================================================
--- joins.py (revision 1729)
+++ joins.py (working copy)
@@ -227,36 +227,52 @@
class RelatedJoin(MultipleJoin):
baseClass = SORelatedJoin
+# helper classes to SQLRelatedJoin
+class OtherTableToJoin(sqlbuilder.SQLExpression):
+ def __init__(self, otherTable, otherIdName, interTable, joinColumn):
+ self.otherTable = otherTable
+ self.otherIdName = otherIdName
+ self.interTable = interTable
+ self.joinColumn = joinColumn
+
+ def __sqlrepr__(self, db):
+ return '%s.%s = %s.%s' % (self.otherTable, self.otherIdName, self.interTable, self.joinColumn)
+
+class JoinToTable(sqlbuilder.SQLExpression):
+ def __init__(self, table, idName, interTable, joinColumn):
+ self.table = table
+ self.idName = idName
+ self.interTable = interTable
+ self.joinColumn = joinColumn
+
+ def __sqlrepr__(self, db):
+ return '%s.%s = %s.%s' % (self.interTable, self.joinColumn, self.table, self.idName)
+
+class TableToId(sqlbuilder.SQLExpression):
+ def __init__(self, table, idName, idValue):
+ self.table = table
+ self.idName = idName
+ self.idValue = idValue
+
+ def __sqlrepr__(self, db):
+ return '%s.%s = %s' % (self.table, self.idName, self.idValue)
+
class SOSQLRelatedJoin(SORelatedJoin):
def performJoin(self, inst):
- options={
- 'otherTable' : self.otherClass.sqlmeta.table,
- 'otherID' : self.otherClass.sqlmeta.idName,
- 'interTable' : self.intermediateTable,
- 'table' : self.soClass.sqlmeta.table,
- 'ID' : self.soClass.sqlmeta.idName,
- 'joinCol' : self.joinColumn,
- 'otherCol' : self.otherColumn,
- 'idValue' : inst.id,
- }
- clause = '''\
-%(otherTable)s.%(otherID)s = %(interTable)s.%(otherCol)s and
-%(interTable)s.%(joinCol)s = %(table)s.%(ID)s and
-%(table)s.%(ID)s = %(idValue)s''' % options
- if inst.sqlmeta._perConnection:
- conn = inst._connection
- else:
- conn = None
- results = self.otherClass.select(sqlbuilder.SQLConstant(clause),
- clauseTables=(
- options['table'],
- options['otherTable'],
- options['interTable'],
+ results = self.otherClass.select(sqlbuilder.AND(
+ OtherTableToJoin(
+ self.otherClass.sqlmeta.table, self.otherClass.sqlmeta.idName,
+ self.intermediateTable, self.otherColumn
),
- connection=conn
- )
- # TODO (michelts): apply orderBy on the selection
- return results
+ JoinToTable(
+ self.soClass.sqlmeta.table, self.soClass.sqlmeta.idName,
+ self.intermediateTable, self.joinColumn
+ ),
+ TableToId(self.soClass.sqlmeta.table, self.soClass.sqlmeta.idName, inst.id),
+ ), clauseTables=(self.soClass.sqlmeta.table, self.otherClass.sqlmeta.table, self.intermediateTable))
+ if self.orderBy is NoDefault:
+ self.orderBy = self.otherClass.sqlmeta.defaultOrder
+ return results.orderBy(self.orderBy)
class SQLRelatedJoin(RelatedJoin):
baseClass = SOSQLRelatedJoin
@@ -392,7 +408,7 @@
def __getattr__(self, attr):
# @@: This passes through private variable access too... should it?
# Also magic methods, like __str__
- return getattr(self, select, attr)
+ return getattr(self.select, attr)
def __repr__(self):
return '<%s for: %s>' % (self.__class__.__name__, repr(self.select))