]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
removed all "tablename + '_' + columname" code and replaced with column._label, to...
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 25 Mar 2006 21:14:54 +0000 (21:14 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 25 Mar 2006 21:14:54 +0000 (21:14 +0000)
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
lib/sqlalchemy/mapping/mapper.py
lib/sqlalchemy/mapping/properties.py
lib/sqlalchemy/sql.py
test/select.py

index 322af156216b7b665485ed06afc8a747cfd56033..f13a7b84fcda62512f5d03e474e1e0938c0f889c 100644 (file)
@@ -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)
index 907d412faae5480f67c7c3624dac342506848769..0d3cbb3006d64cbee1e36cc6740b3cf596730364 100644 (file)
@@ -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. "<tablename>_<colname>"
-                            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:
index 33405423362623c9b0a2661fb57c77863154907f..7f5489de6091b5ffce043537aee3fdf0f1d316f1 100644 (file)
@@ -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)
index d0ab4578aae4ffba17aaad5b4c1b57e5eba1afe3..c4cb4d1486f6ae679629da97dc12f77dc000b503 100644 (file)
@@ -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
index 2f4aa47b3645674a12a89d9f9dd42a10ec607a82..cac641fc09540299749343442a43bc38d00791dc 100644 (file)
@@ -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