[SQL-CVS] r1672 - in SQLObject/trunk/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2006-03-28 12:17:45
|
Author: phd
Date: 2006-03-28 05:17:30 -0700 (Tue, 28 Mar 2006)
New Revision: 1672
Modified:
SQLObject/trunk/sqlobject/sqlbuilder.py
SQLObject/trunk/sqlobject/tests/test_converters.py
Log:
Applied the patch 1450568: faster sqlbuilder.Insert.
Modified: SQLObject/trunk/sqlobject/sqlbuilder.py
===================================================================
--- SQLObject/trunk/sqlobject/sqlbuilder.py 2006-03-21 18:34:42 UTC (rev 1671)
+++ SQLObject/trunk/sqlobject/sqlbuilder.py 2006-03-28 12:17:30 UTC (rev 1672)
@@ -486,20 +486,18 @@
allowNonDict = False
if template is not NoDefault:
insert += " (%s)" % ", ".join(template)
- first = True
insert += " VALUES "
+ listToJoin = []
+ listToJoin_app = listToJoin.append
for value in self.valueList:
- if first:
- first = False
- else:
- insert += ", "
if type(value) is type({}):
if template is NoDefault:
raise TypeError, "You can't mix non-dictionaries with dictionaries in an INSERT if you don't provide a template (%s)" % repr(value)
value = dictToList(template, value)
elif not allowNonDict:
raise TypeError, "You can't mix non-dictionaries with dictionaries in an INSERT if you don't provide a template (%s)" % repr(value)
- insert += "(%s)" % ", ".join([sqlrepr(v, db) for v in value])
+ listToJoin_app("(%s)" % ", ".join([sqlrepr(v, db) for v in value]))
+ insert = "%s%s" % (insert, ", ".join(listToJoin))
return insert
registerConverter(Insert, SQLExprConverter)
Modified: SQLObject/trunk/sqlobject/tests/test_converters.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_converters.py 2006-03-21 18:34:42 UTC (rev 1671)
+++ SQLObject/trunk/sqlobject/tests/test_converters.py 2006-03-28 12:17:30 UTC (rev 1672)
@@ -112,9 +112,39 @@
assert sqlrepr(instance, 'mysql') == "SELECT 'test'"
def test_insert():
+ # Single column, no keyword arguments.
instance = Insert('test', [('test',)])
assert sqlrepr(instance, 'mysql') == "INSERT INTO test VALUES ('test')"
+ # Multiple columns, no keyword arguments.
+ instance2 = Insert('test', [('1st', '2nd', '3th', '4th')])
+ assert sqlrepr(instance2, 'postgres') == "INSERT INTO test VALUES ('1st', '2nd', '3th', '4th')"
+
+ # Multiple rows, multiple columns, "valueList" keyword argument.
+ instance3 = Insert('test', valueList=[('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')])
+ assert sqlrepr(instance3, 'sqlite') == "INSERT INTO test VALUES ('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')"
+
+ # Multiple columns, "values" keyword argument.
+ instance4 = Insert('test', values=('v1', 'v2', 'v3'))
+ assert sqlrepr(instance4, 'mysql') == "INSERT INTO test VALUES ('v1', 'v2', 'v3')"
+
+ # Single column, "valueList" keyword argument.
+ instance5 = Insert('test', valueList=[('v1',)])
+ assert sqlrepr(instance5, 'mysql') == "INSERT INTO test VALUES ('v1')"
+
+ # Multiple rows, Multiple columns, template.
+ instance6 = Insert('test', valueList=[('a1', 'b1'), ('a2', 'b2')], template=['col1', 'col2'])
+ assert sqlrepr(instance6, 'mysql') == "INSERT INTO test (col1, col2) VALUES ('a1', 'b1'), ('a2', 'b2')"
+
+ # Multiple columns, implicit template (dictionary value).
+ instance7 = Insert('test', valueList=[{'col1': 'a1', 'col2': 'b1'}])
+ assert sqlrepr(instance7, 'mysql') == "INSERT INTO test (col2, col1) VALUES ('b1', 'a1')"
+
+ # Multiple rows, Multiple columns, implicit template.
+ instance8 = Insert('test', valueList=[{'col1': 'a1', 'col2': 'b1'},
+ {'col1': 'a2', 'col2': 'b2'}])
+ assert sqlrepr(instance8, 'mysql') == "INSERT INTO test (col2, col1) VALUES ('b1', 'a1'), ('b2', 'a2')"
+
def test_update():
instance = Update('test', {'test':'test'})
assert sqlrepr(instance, 'mysql') == "UPDATE test SET test='test'"
|