delete() and DDL(). The .bind property is now assignable
on those statements as well as on select().
+ - Insert statements can now be compiled with extra "prefix"
+ words between INSERT and INTO, for vendor extensions like
+ MySQL's INSERT IGNORE INTO table.
+
- orm
- any(), has(), contains(), ~contains(), attribute level ==
and != now work properly with self-referential relations -
colparams = self._get_colparams(insert_stmt)
preparer = self.preparer
- return ("INSERT INTO %s (%s) VALUES (%s)" %
+ insert = ' '.join(["INSERT"] +
+ [self.process(x) for x in insert_stmt._prefixes])
+
+ return (insert + " INTO %s (%s) VALUES (%s)" %
(preparer.format_table(insert_stmt.table),
', '.join([preparer.quote(c[0], c[0].name)
for c in colparams]),
column specifications will be generated from the full list of
table columns.
+ prefixes
+ A list of modifier keywords to be inserted between INSERT and INTO,
+ see ``Insert.prefix_with``.
+
inline
if True, SQL defaults will be compiled 'inline' into the statement
and not pre-executed.
bind = property(bind, _set_bind)
class Insert(_UpdateBase):
- def __init__(self, table, values=None, inline=False, bind=None, **kwargs):
+ def __init__(self, table, values=None, inline=False, bind=None, prefixes=None, **kwargs):
self._bind = bind
self.table = table
self.select = None
self.inline=inline
+ if prefixes:
+ self._prefixes = [_literal_as_text(p) for p in prefixes]
+ else:
+ self._prefixes = []
+
self.parameters = self._process_colparams(values)
self.kwargs = kwargs
u.parameters.update(u._process_colparams(v))
return u
+ def prefix_with(self, clause):
+ """Add a word or expression between INSERT and INTO. Generative.
+
+ If multiple prefixes are supplied, they will be separated with
+ spaces.
+ """
+ gen = self._clone()
+ clause = _literal_as_text(clause)
+ gen._prefixes = self._prefixes + [clause]
+ return gen
+
class Update(_UpdateBase):
def __init__(self, table, whereclause, values=None, inline=False, bind=None, **kwargs):
self._bind = bind
self.assert_compile(select_copy, "SELECT FOOBER table1.col1, table1.col2, table1.col3 FROM table1")
self.assert_compile(s, "SELECT table1.col1, table1.col2, table1.col3 FROM table1")
+
+class InsertTest(TestBase, AssertsCompiledSQL):
+ """Tests the generative capability of Insert"""
+
+ # fixme: consolidate converage from elsewhere here and expand
+
+ def setUpAll(self):
+ global t1, t2
+ t1 = table("table1",
+ column("col1"),
+ column("col2"),
+ column("col3"),
+ )
+ t2 = table("table2",
+ column("col1"),
+ column("col2"),
+ column("col3"),
+ )
+
+ def test_prefixes(self):
+ i = t1.insert()
+ self.assert_compile(i,
+ "INSERT INTO table1 (col1, col2, col3) "
+ "VALUES (:col1, :col2, :col3)")
+
+ gen = i.prefix_with("foober")
+ self.assert_compile(gen,
+ "INSERT foober INTO table1 (col1, col2, col3) "
+ "VALUES (:col1, :col2, :col3)")
+
+ self.assert_compile(i,
+ "INSERT INTO table1 (col1, col2, col3) "
+ "VALUES (:col1, :col2, :col3)")
+
+ i2 = t1.insert(prefixes=['squiznart'])
+ self.assert_compile(i2,
+ "INSERT squiznart INTO table1 (col1, col2, col3) "
+ "VALUES (:col1, :col2, :col3)")
+
+ gen2 = i2.prefix_with("quux")
+ self.assert_compile(gen2,
+ "INSERT squiznart quux INTO "
+ "table1 (col1, col2, col3) "
+ "VALUES (:col1, :col2, :col3)")
+
if __name__ == '__main__':
testenv.main()