diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-03-03 21:02:26 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-03-03 21:02:26 +0000 |
| commit | e736817a92797a3a3ce7b1c2cc9622643186f65b (patch) | |
| tree | d8af6807cd998cf6bc3e5d78428259a49a7c01c6 /test/sql/select.py | |
| parent | c8a78834f5a11394105942f9c8d9483451415e12 (diff) | |
| download | sqlalchemy-e736817a92797a3a3ce7b1c2cc9622643186f65b.tar.gz | |
- bindparam() names are now repeatable! specify two
distinct bindparam()s with the same name in a single statement,
and the key will be shared. proper positional/named args translate
at compile time. for the old behavior of "aliasing" bind parameters
with conflicting names, specify "unique=True" - this option is
still used internally for all the auto-genererated (value-based)
bind parameters.
Diffstat (limited to 'test/sql/select.py')
| -rw-r--r-- | test/sql/select.py | 88 |
1 files changed, 75 insertions, 13 deletions
diff --git a/test/sql/select.py b/test/sql/select.py index a021bd5b9..b6f769959 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -597,25 +597,87 @@ myothertable.othername != :myothertable_othername AND EXISTS (select yay from fo self.runtest(query.select(), "SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername, thirdtable.userid, thirdtable.otherstuff FROM mytable, myothertable, thirdtable WHERE mytable.myid = myothertable.otherid(+) AND thirdtable.userid(+) = myothertable.otherid", dialect=oracle.dialect(use_ansi=False)) def testbindparam(self): - for stmt, assertion in [ - ( - select( - [table1, table2], - and_(table1.c.myid == table2.c.otherid, - table1.c.name == bindparam('mytablename'))), - "SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable.name = :mytablename" - ) - ]: - - self.runtest(stmt, assertion) - + for ( + stmt, + expected_named_stmt, + expected_positional_stmt, + expected_default_params_dict, + expected_default_params_list, + test_param_dict, + expected_test_params_dict, + expected_test_params_list + ) in [ + ( + select( + [table1, table2], + and_( + table1.c.myid == table2.c.otherid, + table1.c.name == bindparam('mytablename') + )), + """SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable.name = :mytablename""", + """SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable.name = ?""", + {'mytablename':None}, [None], + {'mytablename':5}, {'mytablename':5}, [5] + ), + ( + select([table1], or_(table1.c.myid==bindparam('myid'), table2.c.otherid==bindparam('myid'))), + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myid", + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?", + {'myid':None}, [None, None], + {'myid':5}, {'myid':5}, [5,5] + ), + ( + text("SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myid"), + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myid", + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?", + {'myid':None}, [None, None], + {'myid':5}, {'myid':5}, [5,5] + ), + ( + select([table1], or_(table1.c.myid==bindparam('myid', unique=True), table2.c.otherid==bindparam('myid', unique=True))), + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :my_1", + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?", + {'myid':None, 'my_1':None}, [None, None], + {'myid':5, 'my_1': 6}, {'myid':5, 'my_1':6}, [5,6] + ), + ( + select([table1], or_(table1.c.myid==bindparam('myid', value=7, unique=True), table2.c.otherid==bindparam('myid', value=8, unique=True))), + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :my_1", + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?", + {'myid':7, 'my_1':8}, [7,8], + {'myid':5, 'my_1':6}, {'myid':5, 'my_1':6}, [5,6] + ), + ][2:3]: + + self.runtest(stmt, expected_named_stmt, params=expected_default_params_dict) + self.runtest(stmt, expected_positional_stmt, dialect=sqlite.dialect()) + nonpositional = stmt.compile() + positional = stmt.compile(dialect=sqlite.dialect()) + assert positional.get_params().get_raw_list() == expected_default_params_list + assert nonpositional.get_params(**test_param_dict).get_raw_dict() == expected_test_params_dict, "expected :%s got %s" % (str(expected_test_params_dict), str(nonpositional.get_params(**test_param_dict).get_raw_dict())) + assert positional.get_params(**test_param_dict).get_raw_list() == expected_test_params_list + + # check that conflicts with "unique" params are caught + s = select([table1], or_(table1.c.myid==7, table1.c.myid==bindparam('mytable_myid'))) + try: + str(s) + assert False + except exceptions.CompileError, err: + assert str(err) == "Bind parameter 'mytable_myid' conflicts with unique bind parameter of the same name" + + s = select([table1], or_(table1.c.myid==7, table1.c.myid==8, table1.c.myid==bindparam('mytable_my_1'))) + try: + str(s) + assert False + except exceptions.CompileError, err: + assert str(err) == "Bind parameter 'mytable_my_1' conflicts with unique bind parameter of the same name" + # check that the bind params sent along with a compile() call # get preserved when the params are retreived later s = select([table1], table1.c.myid == bindparam('test')) c = s.compile(parameters = {'test' : 7}) self.assert_(c.get_params() == {'test' : 7}) - def testin(self): self.runtest(select([table1], table1.c.myid.in_(1, 2, 3)), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid IN (:mytable_myid, :mytable_my_1, :mytable_my_2)") |
