Author: phd
Date: 2005-10-28 12:44:48 +0000 (Fri, 28 Oct 2005)
New Revision: 1163
Modified:
SQLObject/trunk/sqlobject/dbconnection.py
SQLObject/trunk/sqlobject/tests/test_joins_conditional.py
Log:
Fixed a number of bugs with JOIN syntax. Added a test.
Modified: SQLObject/trunk/sqlobject/dbconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/dbconnection.py 2005-10-28 11:41:40 UTC (rev 1162)
+++ SQLObject/trunk/sqlobject/dbconnection.py 2005-10-28 12:44:48 UTC (rev 1163)
@@ -373,8 +373,7 @@
q = "SELECT %s" % ", ".join([str(expression) for expression in expressions])
q += " FROM %s" % ", ".join(tables)
if join:
- if tables: q += ','
- q += self._addJoins(select)
+ q += self._addJoins(select, tables)
q += " WHERE"
q = self._addWhereClause(select, q, limit=0, order=0)
val = self.queryOne(q)
@@ -411,8 +410,7 @@
", ".join(tables))
if join:
- if tables: q += ','
- q += self._addJoins(select)
+ q += self._addJoins(select, tables)
q += " WHERE"
return self._addWhereClause(select, q)
@@ -433,16 +431,24 @@
if j.table2 in tables: tables.remove(j.table2)
return tables
- def _addJoins(self, select):
+ def _addJoins(self, select, tables):
ops = select.ops
join = ops.get('join')
if type(join) is str:
- pass
+ join_str = ' ' + join
elif isinstance(join, sqlbuilder.SQLJoin):
- join_str = self.sqlrepr(join)
+ if tables and join.table1:
+ join_str = ", "
+ else:
+ join_str = ' '
+ join_str += self.sqlrepr(join)
else:
- join_str = ", ".join([self.sqlrepr(j) for j in join])
- return ' ' + join_str
+ if tables and join[0].table1:
+ join_str = ", "
+ else:
+ join_str = ' '
+ join_str += " ".join([self.sqlrepr(j) for j in join])
+ return join_str
def _addWhereClause(self, select, startSelect, limit=1, order=1):
Modified: SQLObject/trunk/sqlobject/tests/test_joins_conditional.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_joins_conditional.py 2005-10-28 11:41:40 UTC (rev 1162)
+++ SQLObject/trunk/sqlobject/tests/test_joins_conditional.py 2005-10-28 12:44:48 UTC (rev 1163)
@@ -59,3 +59,13 @@
)
assert str(select) == \
"SELECT test_join1.id, test_join1.col1 FROM test_join1, test_join2 LEFT JOIN test_join3 WHERE 1 = 1"
+
+def test_5join_3tables_syntax2():
+ setup()
+ setupClass(TestJoin3)
+
+ select = TestJoin1.select(
+ join=(LEFTJOIN(None, TestJoin2), LEFTJOIN(None, TestJoin3))
+ )
+ assert str(select) == \
+ "SELECT test_join1.id, test_join1.col1 FROM test_join1 LEFT JOIN test_join2 LEFT JOIN test_join3 WHERE 1 = 1"
|