From f7afe052907c9343640e5e2569a47d62aa2328a5 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 25 Mar 2006 21:14:54 +0000 Subject: [PATCH] removed all "tablename + '_' + columname" code and replaced with column._label, to take advantage of column labeling rules bind param compilation,when it unique-ifys the name of bind params, maintains the length of the bind parameter name instead of appending to it --- lib/sqlalchemy/ansisql.py | 5 ++++- lib/sqlalchemy/mapping/mapper.py | 4 ++-- lib/sqlalchemy/mapping/properties.py | 6 +++--- lib/sqlalchemy/sql.py | 2 +- test/select.py | 20 ++++++++++---------- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/sqlalchemy/ansisql.py b/lib/sqlalchemy/ansisql.py index 322af15621..f13a7b84fc 100644 --- a/lib/sqlalchemy/ansisql.py +++ b/lib/sqlalchemy/ansisql.py @@ -262,7 +262,10 @@ class ANSICompiler(sql.Compiled): # redefine the generated name of the bind param in the case # that we have multiple conflicting bind parameters. while self.binds.setdefault(key, bindparam) is not bindparam: - key = "%s_%d" % (bindparam.key, count) + # insure the name doesn't expand the length of the string + # in case we're at the edge of max identifier length + tag = "_%d" % count + key = bindparam.key[0 : len(bindparam.key) - len(tag)] + tag count += 1 bindparam.key = key self.strings[bindparam] = self.bindparam_string(key) diff --git a/lib/sqlalchemy/mapping/mapper.py b/lib/sqlalchemy/mapping/mapper.py index 907d412faa..0d3cbb3006 100644 --- a/lib/sqlalchemy/mapping/mapper.py +++ b/lib/sqlalchemy/mapping/mapper.py @@ -610,7 +610,7 @@ class Mapper(object): if not isinsert: # doing an UPDATE? put primary key values as "WHERE" parameters # matching the bindparam we are creating below, i.e. "_" - params[col.table.name + "_" + col.key] = self._getattrbycolumn(obj, col) + params[col._label] = self._getattrbycolumn(obj, col) else: # doing an INSERT, primary key col ? # if the primary key values are not populated, @@ -658,7 +658,7 @@ class Mapper(object): if len(update): clause = sql.and_() for col in self.pks_by_table[table]: - clause.clauses.append(col == sql.bindparam(col.table.name + "_" + col.key)) + clause.clauses.append(col == sql.bindparam(col._label)) statement = table.update(clause) rows = 0 for rec in update: diff --git a/lib/sqlalchemy/mapping/properties.py b/lib/sqlalchemy/mapping/properties.py index 3340542336..7f5489de60 100644 --- a/lib/sqlalchemy/mapping/properties.py +++ b/lib/sqlalchemy/mapping/properties.py @@ -601,7 +601,7 @@ class LazyLoader(PropertyLoader): if self.use_get: ident = [] for primary_key in self.mapper.pks_by_table[self.mapper.table]: - ident.append(params[self.mapper.table.name + "_" + primary_key.name]) + ident.append(params[primary_key._label]) return self.mapper.get(*ident) elif self.order_by is not False: order_by = self.order_by @@ -643,12 +643,12 @@ def create_lazy_clause(table, primaryjoin, secondaryjoin, foreignkey): circular = isinstance(binary.left, schema.Column) and isinstance(binary.right, schema.Column) and binary.left.table is binary.right.table if isinstance(binary.left, schema.Column) and isinstance(binary.right, schema.Column) and ((not circular and binary.left.table is table) or (circular and binary.right is foreignkey)): binary.left = binds.setdefault(binary.left, - sql.BindParamClause(binary.right.table.name + "_" + binary.right.name, None, shortname = binary.left.name)) + sql.BindParamClause(binary.right._label, None, shortname = binary.left.name)) binary.swap() if isinstance(binary.right, schema.Column) and isinstance(binary.left, schema.Column) and ((not circular and binary.right.table is table) or (circular and binary.left is foreignkey)): binary.right = binds.setdefault(binary.right, - sql.BindParamClause(binary.left.table.name + "_" + binary.left.name, None, shortname = binary.right.name)) + sql.BindParamClause(binary.left._label, None, shortname = binary.right.name)) if secondaryjoin is not None: lazywhere = sql.and_(primaryjoin, secondaryjoin) diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index d0ab4578aa..c4cb4d1486 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -1039,7 +1039,7 @@ class ColumnClause(ColumnElement): if self.table.name is None: return BindParamClause(self.text, obj, shortname=self.text, type=self.type) else: - return BindParamClause(self.table.name + "_" + self.text, obj, shortname = self.text, type=self.type) + return BindParamClause(self._label, obj, shortname = self.text, type=self.type) def _make_proxy(self, selectable, name = None): c = ColumnClause(name or self.text, selectable) c._original = self.original diff --git a/test/select.py b/test/select.py index 2f4aa47b36..cac641fc09 100644 --- a/test/select.py +++ b/test/select.py @@ -176,7 +176,7 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A order_by = ['dist', places.c.nm] ) - self.runtest(q,"SELECT places.id, places.nm, zips.zipcode, latlondist((SELECT zips.latitude AS latitude FROM zips WHERE zips.zipcode = :zips_zipcode_1), (SELECT zips.longitude AS longitude FROM zips WHERE zips.zipcode = :zips_zipcode_2)) AS dist FROM places, zips WHERE zips.zipcode = :zips_zipcode ORDER BY dist, places.nm") + self.runtest(q,"SELECT places.id, places.nm, zips.zipcode, latlondist((SELECT zips.latitude AS latitude FROM zips WHERE zips.zipcode = :zips_zipco_1), (SELECT zips.longitude AS longitude FROM zips WHERE zips.zipcode = :zips_zipco_2)) AS dist FROM places, zips WHERE zips.zipcode = :zips_zipcode ORDER BY dist, places.nm") zalias = zips.alias('main_zip') qlat = select([zips.c.latitude], zips.c.zipcode == zalias.c.zipcode, scalar=True) @@ -199,8 +199,8 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A or_(table2.c.othername=='asdf', table2.c.othername == 'foo', table2.c.otherid == 9), "sysdate() = today()", )), - "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :mytable_myid AND (myothertable.othername = :myothertable_othername OR myothertable.othername = :myothertable_othername_1 OR myothertable.otherid = :myothertable_otherid) AND sysdate() = today()", - checkparams = {'myothertable_othername': 'asdf', 'myothertable_othername_1':'foo', 'myothertable_otherid': 9, 'mytable_myid': 12} + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :mytable_myid AND (myothertable.othername = :myothertable_othername OR myothertable.othername = :myothertable_otherna_1 OR myothertable.otherid = :myothertable_otherid) AND sysdate() = today()", + checkparams = {'myothertable_othername': 'asdf', 'myothertable_otherna_1':'foo', 'myothertable_otherid': 9, 'mytable_myid': 12} ) def testoperators(self): @@ -210,13 +210,13 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A ) self.runtest( - literal("a") + literal("b") * literal("c"), ":literal + :literal_1 * :literal_2", db + literal("a") + literal("b") * literal("c"), ":literal + :liter_1 * :liter_2", db ) def testmultiparam(self): self.runtest( select(["*"], or_(table1.c.myid == 12, table1.c.myid=='asdf', table1.c.myid == 'foo')), - "SELECT * FROM mytable WHERE mytable.myid = :mytable_myid OR mytable.myid = :mytable_myid_1 OR mytable.myid = :mytable_myid_2" + "SELECT * FROM mytable WHERE mytable.myid = :mytable_myid OR mytable.myid = :mytable_my_1 OR mytable.myid = :mytable_my_2" ) def testorderby(self): @@ -339,13 +339,13 @@ FROM mytable, myothertable WHERE foo.id = foofoo(lala) AND datetime(foo) = Today def testliteral(self): self.runtest(select([literal("foo") + literal("bar")], from_obj=[table1]), - "SELECT :literal + :literal_1 FROM mytable") + "SELECT :literal + :liter_1 FROM mytable") def testfunction(self): """tests the generation of functions using the func keyword""" # test an expression with a function self.runtest(func.lala(3, 4, literal("five"), table1.c.myid) * table2.c.otherid, - "lala(:lala, :lala_1, :literal, mytable.myid) * myothertable.otherid") + "lala(:lala, :la_1, :literal, mytable.myid) * myothertable.otherid") # test it in a SELECT self.runtest(select([func.count(table1.c.myid)]), @@ -416,7 +416,7 @@ mytable.description FROM myothertable JOIN mytable ON mytable.myid = myothertabl self.runtest(x, "SELECT mytable.myid, mytable.name, mytable.description \ FROM mytable WHERE mytable.myid = :mytable_myid UNION \ SELECT mytable.myid, mytable.name, mytable.description \ -FROM mytable WHERE mytable.myid = :mytable_myid_1 ORDER BY mytable.myid") +FROM mytable WHERE mytable.myid = :mytable_my_1 ORDER BY mytable.myid") self.runtest( union( @@ -487,7 +487,7 @@ FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable 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_myid_1, :mytable_myid_2)") + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid IN (:mytable_myid, :mytable_my_1, :mytable_my_2)") self.runtest(select([table1], table1.c.myid.in_(select([table2.c.otherid]))), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid IN (SELECT myothertable.otherid AS otherid FROM myothertable)") @@ -549,7 +549,7 @@ class CRUDTest(SQLTest): values = { table1.c.name : table1.c.name + "lala", table1.c.myid : func.do_stuff(table1.c.myid, literal('hoho')) - }), "UPDATE mytable SET myid=do_stuff(mytable.myid, :literal_2), name=mytable.name + :mytable_name WHERE mytable.myid = hoho(:hoho) AND mytable.name = :literal + mytable.name + :literal_1") + }), "UPDATE mytable SET myid=do_stuff(mytable.myid, :liter_2), name=mytable.name + :mytable_name WHERE mytable.myid = hoho(:hoho) AND mytable.name = :literal + mytable.name + :liter_1") def testcorrelatedupdate(self): # test against a straight text subquery -- 2.47.2