summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-03-17 15:15:44 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-03-17 15:15:44 -0400
commit318f47dc80c58dee8c798afcc8c19a5dbb21eef7 (patch)
treeefd951b139017acb14302b33b2cad9ecab88fb3b /test
parentb81e9741ba26f2740725c9d403d116284af7d7a4 (diff)
parent55367ac4a26dbc3c0e57783c3964cd5c42647a35 (diff)
downloadsqlalchemy-318f47dc80c58dee8c798afcc8c19a5dbb21eef7.tar.gz
- added pyodbc for sybase driver.
- generalized the "freetds" / "unicode statements" behavior of MS-SQL/pyodbc into the base Pyodbc connector, as this seems to apply to Sybase as well. - generalized the python-sybase "use autocommit for DDL" into the pyodbc connector. With pyodbc, the "autocommit" flag on connection is used, as Pyodbc seems to have more database conversation than python-sybase that can't otherwise be suppressed. - Some platforms will now interpret certain literal values as non-bind parameters, rendered literally into the SQL statement. This to support strict SQL-92 rules that are enforced by some platforms including MS-SQL and Sybase. In this model, bind parameters aren't allowed in the columns clause of a SELECT, nor are certain ambiguous expressions like "?=?". When this mode is enabled, the base compiler will render the binds as inline literals, but only across strings and numeric values. Other types such as dates will raise an error, unless the dialect subclass defines a literal rendering function for those. The bind parameter must have an embedded literal value already or an error is raised (i.e. won't work with straight bindparam('x')). Dialects can also expand upon the areas where binds are not accepted, such as within argument lists of functions (which don't work on MS-SQL when native SQL binding is used).
Diffstat (limited to 'test')
-rw-r--r--test/dialect/test_mssql.py55
-rw-r--r--test/sql/test_query.py50
-rw-r--r--test/sql/test_select.py211
3 files changed, 244 insertions, 72 deletions
diff --git a/test/dialect/test_mssql.py b/test/dialect/test_mssql.py
index 89a3af5fb..8092d8cdc 100644
--- a/test/dialect/test_mssql.py
+++ b/test/dialect/test_mssql.py
@@ -6,7 +6,7 @@ from sqlalchemy import types, exc, schema
from sqlalchemy.orm import *
from sqlalchemy.sql import table, column
from sqlalchemy.databases import mssql
-from sqlalchemy.dialects.mssql import pyodbc
+from sqlalchemy.dialects.mssql import pyodbc, mxodbc
from sqlalchemy.engine import url
from sqlalchemy.test import *
from sqlalchemy.test.testing import eq_, emits_warning_on
@@ -22,7 +22,35 @@ class CompileTest(TestBase, AssertsCompiledSQL):
def test_update(self):
t = table('sometable', column('somecolumn'))
self.assert_compile(t.update(t.c.somecolumn==7), "UPDATE sometable SET somecolumn=:somecolumn WHERE sometable.somecolumn = :somecolumn_1", dict(somecolumn=10))
-
+
+ # TODO: should this be for *all* MS-SQL dialects ?
+ def test_mxodbc_binds(self):
+ """mxodbc uses MS-SQL native binds, which aren't allowed in various places."""
+
+ mxodbc_dialect = mxodbc.dialect()
+ t = table('sometable', column('foo'))
+
+ for expr, compile in [
+ (
+ select([literal("x"), literal("y")]),
+ "SELECT 'x', 'y'",
+ ),
+ (
+ select([t]).where(t.c.foo.in_(['x', 'y', 'z'])),
+ "SELECT sometable.foo FROM sometable WHERE sometable.foo IN ('x', 'y', 'z')",
+ ),
+ (
+ func.foobar("x", "y", 4, 5),
+ "foobar('x', 'y', 4, 5)",
+ ),
+ (
+ select([t]).where(func.len('xyz') > func.len(t.c.foo)),
+ "SELECT sometable.foo FROM sometable WHERE len('xyz') > len(sometable.foo)",
+ )
+ ]:
+ self.assert_compile(expr, compile, dialect=mxodbc_dialect)
+
+
def test_in_with_subqueries(self):
"""Test that when using subqueries in a binary expression
the == and != are changed to IN and NOT IN respectively.
@@ -127,15 +155,24 @@ class CompileTest(TestBase, AssertsCompiledSQL):
column('col4'))
(s1, s2) = (
- select([t1.c.col3.label('col3'), t1.c.col4.label('col4')], t1.c.col2.in_(["t1col2r1", "t1col2r2"])),
- select([t2.c.col3.label('col3'), t2.c.col4.label('col4')], t2.c.col2.in_(["t2col2r2", "t2col2r3"]))
+ select([t1.c.col3.label('col3'), t1.c.col4.label('col4')],
+ t1.c.col2.in_(["t1col2r1", "t1col2r2"])),
+ select([t2.c.col3.label('col3'), t2.c.col4.label('col4')],
+ t2.c.col2.in_(["t2col2r2", "t2col2r3"]))
)
u = union(s1, s2, order_by=['col3', 'col4'])
- self.assert_compile(u, "SELECT t1.col3 AS col3, t1.col4 AS col4 FROM t1 WHERE t1.col2 IN (:col2_1, :col2_2) "\
- "UNION SELECT t2.col3 AS col3, t2.col4 AS col4 FROM t2 WHERE t2.col2 IN (:col2_3, :col2_4) ORDER BY col3, col4")
-
- self.assert_compile(u.alias('bar').select(), "SELECT bar.col3, bar.col4 FROM (SELECT t1.col3 AS col3, t1.col4 AS col4 FROM t1 WHERE "\
- "t1.col2 IN (:col2_1, :col2_2) UNION SELECT t2.col3 AS col3, t2.col4 AS col4 FROM t2 WHERE t2.col2 IN (:col2_3, :col2_4)) AS bar")
+ self.assert_compile(u,
+ "SELECT t1.col3 AS col3, t1.col4 AS col4 FROM t1 WHERE t1.col2 IN "
+ "(:col2_1, :col2_2) "\
+ "UNION SELECT t2.col3 AS col3, t2.col4 AS col4 FROM t2 WHERE t2.col2 "
+ "IN (:col2_3, :col2_4) ORDER BY col3, col4")
+
+ self.assert_compile(u.alias('bar').select(),
+ "SELECT bar.col3, bar.col4 FROM (SELECT t1.col3 AS col3, "
+ "t1.col4 AS col4 FROM t1 WHERE "\
+ "t1.col2 IN (:col2_1, :col2_2) UNION SELECT t2.col3 AS col3, "
+ "t2.col4 AS col4 FROM t2 WHERE t2.col2 IN (:col2_3, :col2_4)) "
+ "AS bar")
def test_function(self):
self.assert_compile(func.foo(1, 2), "foo(:foo_1, :foo_2)")
diff --git a/test/sql/test_query.py b/test/sql/test_query.py
index a189594b7..8664ba6dc 100644
--- a/test/sql/test_query.py
+++ b/test/sql/test_query.py
@@ -234,8 +234,9 @@ class QueryTest(TestBase):
def test_order_by_label(self):
"""test that a label within an ORDER BY works on each backend.
- simple labels in ORDER BYs now render as the actual labelname
- which not every database supports.
+ This test should be modified to support [ticket:1068] when that ticket
+ is implemented. For now, you need to put the actual string in the
+ ORDER BY.
"""
users.insert().execute(
@@ -246,26 +247,30 @@ class QueryTest(TestBase):
concat = ("test: " + users.c.user_name).label('thedata')
eq_(
- select([concat]).order_by(concat).execute().fetchall(),
+ select([concat]).order_by("thedata").execute().fetchall(),
[("test: ed",), ("test: fred",), ("test: jack",)]
)
eq_(
- select([concat]).order_by(concat).execute().fetchall(),
+ select([concat]).order_by("thedata").execute().fetchall(),
[("test: ed",), ("test: fred",), ("test: jack",)]
)
concat = ("test: " + users.c.user_name).label('thedata')
eq_(
- select([concat]).order_by(desc(concat)).execute().fetchall(),
+ select([concat]).order_by(desc('thedata')).execute().fetchall(),
[("test: jack",), ("test: fred",), ("test: ed",)]
)
- concat = ("test: " + users.c.user_name).label('thedata')
- eq_(
- select([concat]).order_by(concat + "x").execute().fetchall(),
- [("test: ed",), ("test: fred",), ("test: jack",)]
- )
+ @testing.fails_on('postgresql', 'only simple labels allowed')
+ @testing.fails_on('sybase', 'only simple labels allowed')
+ def go():
+ concat = ("test: " + users.c.user_name).label('thedata')
+ eq_(
+ select([concat]).order_by(literal_column('thedata') + "x").execute().fetchall(),
+ [("test: ed",), ("test: fred",), ("test: jack",)]
+ )
+ go()
def test_row_comparison(self):
@@ -768,11 +773,18 @@ class QueryTest(TestBase):
assert len(r) == 0
@testing.emits_warning('.*empty sequence.*')
- @testing.fails_on('firebird', "kinterbasdb doesn't send full type information")
+ @testing.fails_on('firebird', "uses sql-92 rules")
+ @testing.fails_on('sybase', "uses sql-92 rules")
@testing.fails_if(lambda:
testing.against('mssql+pyodbc') and not testing.db.dialect.freetds,
- "not supported by Windows ODBC driver")
+ "uses sql-92 rules")
def test_bind_in(self):
+ """test calling IN against a bind parameter.
+
+ this isn't allowed on several platforms since we
+ generate ? = ?.
+
+ """
users.insert().execute(user_id = 7, user_name = 'jack')
users.insert().execute(user_id = 8, user_name = 'fred')
users.insert().execute(user_id = 9, user_name = None)
@@ -784,7 +796,21 @@ class QueryTest(TestBase):
assert len(r) == 3
r = s.execute(search_key=None).fetchall()
assert len(r) == 0
+
+ @testing.emits_warning('.*empty sequence.*')
+ def test_literal_in(self):
+ """similar to test_bind_in but use a bind with a value."""
+
+ users.insert().execute(user_id = 7, user_name = 'jack')
+ users.insert().execute(user_id = 8, user_name = 'fred')
+ users.insert().execute(user_id = 9, user_name = None)
+ s = users.select(not_(literal("john").in_([])))
+ r = s.execute().fetchall()
+ assert len(r) == 3
+
+
+
@testing.emits_warning('.*empty sequence.*')
@testing.fails_on('firebird', 'FIXME: unknown')
@testing.fails_on('maxdb', 'FIXME: unknown')
diff --git a/test/sql/test_select.py b/test/sql/test_select.py
index 33bbe5ff4..d27819c18 100644
--- a/test/sql/test_select.py
+++ b/test/sql/test_select.py
@@ -684,58 +684,94 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
def test_orderby_groupby(self):
self.assert_compile(
table2.select(order_by = [table2.c.otherid, asc(table2.c.othername)]),
- "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername ASC"
+ "SELECT myothertable.otherid, myothertable.othername FROM "
+ "myothertable ORDER BY myothertable.otherid, myothertable.othername ASC"
)
self.assert_compile(
table2.select(order_by = [table2.c.otherid, table2.c.othername.desc()]),
- "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername DESC"
+ "SELECT myothertable.otherid, myothertable.othername FROM "
+ "myothertable ORDER BY myothertable.otherid, myothertable.othername DESC"
)
# generative order_by
self.assert_compile(
table2.select().order_by(table2.c.otherid).order_by(table2.c.othername.desc()),
- "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername DESC"
+ "SELECT myothertable.otherid, myothertable.othername FROM "
+ "myothertable ORDER BY myothertable.otherid, myothertable.othername DESC"
)
self.assert_compile(
- table2.select().order_by(table2.c.otherid).order_by(table2.c.othername.desc()).order_by(None),
+ table2.select().order_by(table2.c.otherid).
+ order_by(table2.c.othername.desc()).order_by(None),
"SELECT myothertable.otherid, myothertable.othername FROM myothertable"
)
self.assert_compile(
- select([table2.c.othername, func.count(table2.c.otherid)], group_by = [table2.c.othername]),
- "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 FROM myothertable GROUP BY myothertable.othername"
+ select(
+ [table2.c.othername, func.count(table2.c.otherid)],
+ group_by = [table2.c.othername]),
+ "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 "
+ "FROM myothertable GROUP BY myothertable.othername"
)
# generative group by
self.assert_compile(
- select([table2.c.othername, func.count(table2.c.otherid)]).group_by(table2.c.othername),
- "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 FROM myothertable GROUP BY myothertable.othername"
+ select([table2.c.othername, func.count(table2.c.otherid)]).
+ group_by(table2.c.othername),
+ "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 "
+ "FROM myothertable GROUP BY myothertable.othername"
)
self.assert_compile(
- select([table2.c.othername, func.count(table2.c.otherid)]).group_by(table2.c.othername).group_by(None),
- "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 FROM myothertable"
+ select([table2.c.othername, func.count(table2.c.otherid)]).
+ group_by(table2.c.othername).group_by(None),
+ "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 "
+ "FROM myothertable"
)
self.assert_compile(
- select([table2.c.othername, func.count(table2.c.otherid)], group_by = [table2.c.othername], order_by = [table2.c.othername]),
- "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 FROM myothertable GROUP BY myothertable.othername ORDER BY myothertable.othername"
+ select([table2.c.othername, func.count(table2.c.otherid)],
+ group_by = [table2.c.othername],
+ order_by = [table2.c.othername]),
+ "SELECT myothertable.othername, count(myothertable.otherid) AS count_1 "
+ "FROM myothertable GROUP BY myothertable.othername ORDER BY myothertable.othername"
)
def test_for_update(self):
- self.assert_compile(table1.select(table1.c.myid==7, for_update=True), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
+ self.assert_compile(
+ table1.select(table1.c.myid==7, for_update=True),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
- self.assert_compile(table1.select(table1.c.myid==7, for_update="nowait"), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
+ self.assert_compile(
+ table1.select(table1.c.myid==7, for_update="nowait"),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
- self.assert_compile(table1.select(table1.c.myid==7, for_update="nowait"), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE NOWAIT", dialect=oracle.dialect())
+ self.assert_compile(
+ table1.select(table1.c.myid==7, for_update="nowait"),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE NOWAIT",
+ dialect=oracle.dialect())
- self.assert_compile(table1.select(table1.c.myid==7, for_update="read"), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = %s LOCK IN SHARE MODE", dialect=mysql.dialect())
+ self.assert_compile(
+ table1.select(table1.c.myid==7, for_update="read"),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = %s LOCK IN SHARE MODE",
+ dialect=mysql.dialect())
- self.assert_compile(table1.select(table1.c.myid==7, for_update=True), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = %s FOR UPDATE", dialect=mysql.dialect())
+ self.assert_compile(
+ table1.select(table1.c.myid==7, for_update=True),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = %s FOR UPDATE",
+ dialect=mysql.dialect())
- self.assert_compile(table1.select(table1.c.myid==7, for_update=True), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE", dialect=oracle.dialect())
+ self.assert_compile(
+ table1.select(table1.c.myid==7, for_update=True),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE",
+ dialect=oracle.dialect())
def test_alias(self):
# test the alias for a table1. column names stay the same, table name "changes" to "foo".
@@ -750,32 +786,42 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
,dialect=dialect)
self.assert_compile(
- select([table1.alias()])
- ,"SELECT mytable_1.myid, mytable_1.name, mytable_1.description FROM mytable AS mytable_1")
+ select([table1.alias()]),
+ "SELECT mytable_1.myid, mytable_1.name, mytable_1.description "
+ "FROM mytable AS mytable_1")
- # create a select for a join of two tables. use_labels means the column names will have
- # labels tablename_columnname, which become the column keys accessible off the Selectable object.
- # also, only use one column from the second table and all columns from the first table1.
- q = select([table1, table2.c.otherid], table1.c.myid == table2.c.otherid, use_labels = True)
+ # create a select for a join of two tables. use_labels
+ # means the column names will have labels tablename_columnname,
+ # which become the column keys accessible off the Selectable object.
+ # also, only use one column from the second table and all columns
+ # from the first table1.
+ q = select(
+ [table1, table2.c.otherid],
+ table1.c.myid == table2.c.otherid, use_labels = True
+ )
- # make an alias of the "selectable". column names stay the same (i.e. the labels), table name "changes" to "t2view".
+ # make an alias of the "selectable". column names
+ # stay the same (i.e. the labels), table name "changes" to "t2view".
a = alias(q, 't2view')
# select from that alias, also using labels. two levels of labels should produce two underscores.
# also, reference the column "mytable_myid" off of the t2view alias.
self.assert_compile(
a.select(a.c.mytable_myid == 9, use_labels = True),
- "SELECT t2view.mytable_myid AS t2view_mytable_myid, t2view.mytable_name AS t2view_mytable_name, \
-t2view.mytable_description AS t2view_mytable_description, t2view.myothertable_otherid AS t2view_myothertable_otherid FROM \
-(SELECT mytable.myid AS mytable_myid, mytable.name AS mytable_name, mytable.description AS mytable_description, \
-myothertable.otherid AS myothertable_otherid FROM mytable, myothertable \
-WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid = :mytable_myid_1"
+ "SELECT t2view.mytable_myid AS t2view_mytable_myid, t2view.mytable_name "
+ "AS t2view_mytable_name, t2view.mytable_description AS t2view_mytable_description, "
+ "t2view.myothertable_otherid AS t2view_myothertable_otherid FROM "
+ "(SELECT mytable.myid AS mytable_myid, mytable.name AS mytable_name, "
+ "mytable.description AS mytable_description, myothertable.otherid AS "
+ "myothertable_otherid FROM mytable, myothertable WHERE mytable.myid = "
+ "myothertable.otherid) AS t2view WHERE t2view.mytable_myid = :mytable_myid_1"
)
def test_prefixes(self):
self.assert_compile(table1.select().prefix_with("SQL_CALC_FOUND_ROWS").prefix_with("SQL_SOME_WEIRD_MYSQL_THING"),
- "SELECT SQL_CALC_FOUND_ROWS SQL_SOME_WEIRD_MYSQL_THING mytable.myid, mytable.name, mytable.description FROM mytable"
+ "SELECT SQL_CALC_FOUND_ROWS SQL_SOME_WEIRD_MYSQL_THING "
+ "mytable.myid, mytable.name, mytable.description FROM mytable"
)
def test_text(self):
@@ -789,16 +835,20 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
["foobar(a)", "pk_foo_bar(syslaal)"],
"a = 12",
from_obj = ["foobar left outer join lala on foobar.foo = lala.foo"]
- ),
- "SELECT foobar(a), pk_foo_bar(syslaal) FROM foobar left outer join lala on foobar.foo = lala.foo WHERE a = 12")
+ ),
+ "SELECT foobar(a), pk_foo_bar(syslaal) FROM foobar "
+ "left outer join lala on foobar.foo = lala.foo WHERE a = 12"
+ )
# test unicode
self.assert_compile(select(
[u"foobar(a)", u"pk_foo_bar(syslaal)"],
u"a = 12",
from_obj = [u"foobar left outer join lala on foobar.foo = lala.foo"]
- ),
- u"SELECT foobar(a), pk_foo_bar(syslaal) FROM foobar left outer join lala on foobar.foo = lala.foo WHERE a = 12")
+ ),
+ "SELECT foobar(a), pk_foo_bar(syslaal) FROM foobar "
+ "left outer join lala on foobar.foo = lala.foo WHERE a = 12"
+ )
# test building a select query programmatically with text
s = select()
@@ -808,11 +858,13 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
s.append_whereclause("column2=19")
s = s.order_by("column1")
s.append_from("table1")
- self.assert_compile(s, "SELECT column1, column2 FROM table1 WHERE column1=12 AND column2=19 ORDER BY column1")
+ self.assert_compile(s, "SELECT column1, column2 FROM table1 WHERE "
+ "column1=12 AND column2=19 ORDER BY column1")
self.assert_compile(
select(["column1", "column2"], from_obj=table1).alias('somealias').select(),
- "SELECT somealias.column1, somealias.column2 FROM (SELECT column1, column2 FROM mytable) AS somealias"
+ "SELECT somealias.column1, somealias.column2 FROM "
+ "(SELECT column1, column2 FROM mytable) AS somealias"
)
# test that use_labels doesnt interfere with literal columns
@@ -827,14 +879,13 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
"SELECT column1 AS foobar, column2 AS hoho, mytable.myid AS mytable_myid FROM mytable"
)
- print "---------------------------------------------"
s1 = select(["column1 AS foobar", "column2 AS hoho", table1.c.myid], from_obj=[table1])
- print "---------------------------------------------"
# test that "auto-labeling of subquery columns" doesnt interfere with literal columns,
# exported columns dont get quoted
self.assert_compile(
select(["column1 AS foobar", "column2 AS hoho", table1.c.myid], from_obj=[table1]).select(),
- "SELECT column1 AS foobar, column2 AS hoho, myid FROM (SELECT column1 AS foobar, column2 AS hoho, mytable.myid AS myid FROM mytable)"
+ "SELECT column1 AS foobar, column2 AS hoho, myid FROM "
+ "(SELECT column1 AS foobar, column2 AS hoho, mytable.myid AS myid FROM mytable)"
)
self.assert_compile(
@@ -844,7 +895,8 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
def test_binds_in_text(self):
self.assert_compile(
- text("select * from foo where lala=:bar and hoho=:whee", bindparams=[bindparam('bar', 4), bindparam('whee', 7)]),
+ text("select * from foo where lala=:bar and hoho=:whee",
+ bindparams=[bindparam('bar', 4), bindparam('whee', 7)]),
"select * from foo where lala=:bar and hoho=:whee",
checkparams={'bar':4, 'whee': 7},
)
@@ -858,7 +910,8 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
dialect = postgresql.dialect()
self.assert_compile(
- text("select * from foo where lala=:bar and hoho=:whee", bindparams=[bindparam('bar',4), bindparam('whee',7)]),
+ text("select * from foo where lala=:bar and hoho=:whee",
+ bindparams=[bindparam('bar',4), bindparam('whee',7)]),
"select * from foo where lala=%(bar)s and hoho=%(whee)s",
checkparams={'bar':4, 'whee': 7},
dialect=dialect
@@ -875,7 +928,8 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
dialect = sqlite.dialect()
self.assert_compile(
- text("select * from foo where lala=:bar and hoho=:whee", bindparams=[bindparam('bar',4), bindparam('whee',7)]),
+ text("select * from foo where lala=:bar and hoho=:whee",
+ bindparams=[bindparam('bar',4), bindparam('whee',7)]),
"select * from foo where lala=? and hoho=?",
checkparams={'bar':4, 'whee':7},
dialect=dialect
@@ -889,25 +943,80 @@ WHERE mytable.myid = myothertable.otherid) AS t2view WHERE t2view.mytable_myid =
table1.c.myid == table2.c.otherid,
)
),
- "SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, sysdate(), foo, bar, lala \
-FROM mytable, myothertable WHERE foo.id = foofoo(lala) AND datetime(foo) = Today AND mytable.myid = myothertable.otherid")
+ "SELECT mytable.myid, mytable.name, mytable.description, "
+ "myothertable.otherid, sysdate(), foo, bar, lala "
+ "FROM mytable, myothertable WHERE foo.id = foofoo(lala) AND "
+ "datetime(foo) = Today AND mytable.myid = myothertable.otherid")
self.assert_compile(select(
[alias(table1, 't'), "foo.f"],
"foo.f = t.id",
from_obj = ["(select f from bar where lala=heyhey) foo"]
),
- "SELECT t.myid, t.name, t.description, foo.f FROM mytable AS t, (select f from bar where lala=heyhey) foo WHERE foo.f = t.id")
+ "SELECT t.myid, t.name, t.description, foo.f FROM mytable AS t, "
+ "(select f from bar where lala=heyhey) foo WHERE foo.f = t.id")
# test Text embedded within select_from(), using binds
- generate_series = text("generate_series(:x, :y, :z) as s(a)", bindparams=[bindparam('x'), bindparam('y'), bindparam('z')])
-
- s =select([(func.current_date() + literal_column("s.a")).label("dates")]).select_from(generate_series)
- self.assert_compile(s, "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)", checkparams={'y': None, 'x': None, 'z': None})
+ generate_series = text(
+ "generate_series(:x, :y, :z) as s(a)",
+ bindparams=[bindparam('x'), bindparam('y'), bindparam('z')]
+ )
+
+ s =select([
+ (func.current_date() + literal_column("s.a")).label("dates")
+ ]).select_from(generate_series)
+ self.assert_compile(
+ s,
+ "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)",
+ checkparams={'y': None, 'x': None, 'z': None}
+ )
+
+ self.assert_compile(
+ s.params(x=5, y=6, z=7),
+ "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)",
+ checkparams={'y': 6, 'x': 5, 'z': 7}
+ )
+
+ @testing.emits_warning('.*empty sequence.*')
+ def test_render_binds_as_literal(self):
+ """test a compiler that renders binds inline into
+ SQL in the columns clause."""
- self.assert_compile(s.params(x=5, y=6, z=7), "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)", checkparams={'y': 6, 'x': 5, 'z': 7})
+ dialect = default.DefaultDialect()
+ class Compiler(dialect.statement_compiler):
+ ansi_bind_rules = True
+ dialect.statement_compiler = Compiler
+ self.assert_compile(
+ select([literal("someliteral")]),
+ "SELECT 'someliteral'",
+ dialect=dialect
+ )
+
+ self.assert_compile(
+ select([table1.c.myid + 3]),
+ "SELECT mytable.myid + 3 AS anon_1 FROM mytable",
+ dialect=dialect
+ )
+ self.assert_compile(
+ select([table1.c.myid.in_([4, 5, 6])]),
+ "SELECT mytable.myid IN (4, 5, 6) AS anon_1 FROM mytable",
+ dialect=dialect
+ )
+
+ self.assert_compile(
+ select([literal("foo").in_([])]),
+ "SELECT 'foo' != 'foo' AS anon_1",
+ dialect=dialect
+ )
+
+ assert_raises(
+ exc.CompileError,
+ bindparam("foo").in_([]).compile, dialect=dialect
+ )
+
+
def test_literal(self):
self.assert_compile(select([literal('foo')]), "SELECT :param_1")