]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- fixed remainder of [ticket:853]
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Nov 2007 22:25:01 +0000 (22:25 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Nov 2007 22:25:01 +0000 (22:25 +0000)
- bindparam 'shortname' is deprecated
- fixed testing.assert_compile() to actually generate bind param dict before asserting
- added bind param assertions to CRUDTest.test_update

CHANGES
lib/sqlalchemy/orm/strategies.py
lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/expression.py
test/orm/relationships.py
test/sql/select.py
test/testlib/testing.py

diff --git a/CHANGES b/CHANGES
index 3ae068bc4950e26f47648a7bd7afced14ca9f950..e29a5c561212e81cab4ac48e6e2aa34c1cb6d438 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,9 @@ CHANGES
 
 - sql
 
+  - the "shortname" keyword parameter on bindparam() has been 
+    deprecated.
+  
   - Added contains operator (generates a "LIKE %<other>%" clause).
 
   - Removed regular expression step from most statement compilations.
index aea1ffdda65e105b216ebfde5ff8542adc33670b..d517be007ee940e00e363d302318bf1a22e0c89d 100644 (file)
@@ -433,7 +433,7 @@ class LazyLoader(AbstractRelationLoader):
             if should_bind(leftcol, rightcol):
                 col = leftcol
                 binary.left = binds.setdefault(leftcol,
-                        sql.bindparam(None, None, shortname=leftcol.name, type_=binary.right.type, unique=True))
+                        sql.bindparam(None, None, type_=binary.right.type, unique=True))
                 reverse[rightcol] = binds[col]
 
             # the "left is not right" compare is to handle part of a join clause that is "table.c.col1==table.c.col1",
@@ -441,7 +441,7 @@ class LazyLoader(AbstractRelationLoader):
             if leftcol is not rightcol and should_bind(rightcol, leftcol):
                 col = rightcol
                 binary.right = binds.setdefault(rightcol,
-                        sql.bindparam(None, None, shortname=rightcol.name, type_=binary.left.type, unique=True))
+                        sql.bindparam(None, None, type_=binary.left.type, unique=True))
                 reverse[leftcol] = binds[col]
 
         lazywhere = primaryjoin
index ba48422785113513accca04e5c24e0a737269093..9c82cd4aa6963fe67e35a5944a4e039b85569cbb 100644 (file)
@@ -362,9 +362,6 @@ class DefaultCompiler(engine.Compiled):
     def visit_bindparam(self, bindparam, **kwargs):
         # apply truncation to the ultimate generated name
 
-        if bindparam.shortname != bindparam.key:
-            self.binds.setdefault(bindparam.shortname, bindparam)
-
         if bindparam.unique:
             count = 1
             key = bindparam.key
index 75c809004f87c4e1c73e6184eb7a0075fd5b2398..479ce642538d535f67f6e956e36790169cdfa846 100644 (file)
@@ -666,22 +666,20 @@ def table(name, *columns):
 
     return TableClause(name, *columns)
 
-def bindparam(key, value=None, type_=None, shortname=None, unique=False):
+def bindparam(key, value=None, shortname=None, type_=None, unique=False):
     """Create a bind parameter clause with the given key.
 
     value
       a default value for this bind parameter.  a bindparam with a
       value is called a ``value-based bindparam``.
 
-    shortname
-      an ``alias`` for this bind parameter.  usually used to alias the
-      ``key`` nd ``label`` of a column, i.e. ``somecolname`` and
-      ``sometable_somecolname``
-
     type
       a sqlalchemy.types.TypeEngine object indicating the type of this
       bind param, will invoke type-specific bind parameter processing
 
+    shortname
+      deprecated.  
+      
     unique
       if True, bind params sharing the same name will have their
       underlying ``key`` modified to a uniquely generated name.
@@ -689,9 +687,9 @@ def bindparam(key, value=None, type_=None, shortname=None, unique=False):
     """
 
     if isinstance(key, _ColumnClause):
-        return _BindParamClause(key.name, value, type_=key.type, shortname=shortname, unique=unique)
+        return _BindParamClause(key.name, value, type_=key.type, unique=unique, shortname=shortname)
     else:
-        return _BindParamClause(key, value, type_=type_, shortname=shortname, unique=unique)
+        return _BindParamClause(key, value, type_=type_, unique=unique, shortname=shortname)
 
 def outparam(key, type_=None):
     """Create an 'OUT' parameter for usage in functions (stored procedures), for databases which support them.
@@ -807,7 +805,7 @@ def _literal_as_binds(element, name='literal', type_=None):
         if element is None:
             return null()
         else:
-            return _BindParamClause(name, element, shortname=name, type_=type_, unique=True)
+            return _BindParamClause(name, element, type_=type_, unique=True)
     else:
         return element
 
@@ -1325,7 +1323,7 @@ class _CompareMixin(ColumnOperators):
         return lambda other: self.__operate(operator, other)
 
     def _bind_param(self, obj):
-        return _BindParamClause('literal', obj, shortname=None, type_=self.type, unique=True)
+        return _BindParamClause('literal', obj, type_=self.type, unique=True)
 
     def _check_literal(self, other):
         if isinstance(other, Operators):
@@ -1706,7 +1704,7 @@ class _BindParamClause(ClauseElement, _CompareMixin):
 
     __visit_name__ = 'bindparam'
 
-    def __init__(self, key, value, shortname=None, type_=None, unique=False, isoutparam=False):
+    def __init__(self, key, value, type_=None, unique=False, isoutparam=False, shortname=None):
         """Construct a _BindParamClause.
 
         key
@@ -1723,12 +1721,8 @@ class _BindParamClause(ClauseElement, _CompareMixin):
           compilation/execution.
 
         shortname
-          Defaults to the key, a *short name* that will also identify
-          this bind parameter, similar to an alias.  the bind
-          parameter keys sent to a statement compilation or compiled
-          execution may match either the key or the shortname of the
-          corresponding ``_BindParamClause`` objects.
-
+          deprecated.
+          
         type\_
           A ``TypeEngine`` object that will be used to pre-process the
           value corresponding to this ``_BindParamClause`` at
@@ -1747,10 +1741,10 @@ class _BindParamClause(ClauseElement, _CompareMixin):
 
         self.key = key or "{ANON %d param}" % id(self)
         self.value = value
-        self.shortname = shortname or key
         self.unique = unique
         self.isoutparam = isoutparam
-
+        self.shortname = shortname
+        
         if type_ is None:
             self.type = self.type_map.get(type(value), sqltypes.NullType)()
         elif isinstance(type_, type):
@@ -2607,7 +2601,7 @@ class _ColumnClause(ColumnElement):
             return []
 
     def _bind_param(self, obj):
-        return _BindParamClause(self._label, obj, shortname=self.name, type_=self.type, unique=True)
+        return _BindParamClause(self._label, obj, type_=self.type, unique=True)
 
     def _make_proxy(self, selectable, name = None):
         # propigate the "is_literal" flag only if we are keeping our name,
index 282d1cafc0252745bca75ccbc96030111cd727d9..0af70975ab4e9d7a407b706d7b1b12a8cecdc2de 100644 (file)
@@ -401,7 +401,7 @@ class RelationTest4(ORMTest):
         except exceptions.AssertionError, e:
             assert str(e).startswith("Dependency rule tried to blank-out primary key column 'B.id' on instance ")
             
-    def test_no_nullPK_sBtoA(self):
+    def test_no_nullPK_BtoA(self):
         class A(object):pass
         class B(object):pass
         mapper(B, tableB, properties={
@@ -421,17 +421,26 @@ class RelationTest4(ORMTest):
         except exceptions.AssertionError, e:
             assert str(e).startswith("Dependency rule tried to blank-out primary key column 'B.id' on instance ")
 
-    def test_nullPKsOK_sBtoA(self):
+    @testing.supported('sqlite', 'mysql')
+    def test_nullPKsOK_BtoA(self):
+        # postgres cant handle a nullable PK column...?
+        tableC = Table('tablec', tableA.metadata, 
+            Column('id', Integer, primary_key=True),
+            Column('a_id', Integer, ForeignKey('A.id'), primary_key=True, autoincrement=False, nullable=True))
+        tableC.create()
+        
         class A(object):pass
-        class B(object):pass
-        mapper(B, tableB, properties={
+        class C(object):pass
+        mapper(C, tableC, properties={
             'a':relation(A, cascade="save-update")
         }, allow_null_pks=True)
         mapper(A, tableA)
-        b1 = B()
-        b1.a = None
+        c1 = C()
+        c1.id = 5
+        c1.a = None
         sess = create_session()
-        sess.save(b1)
+        sess.save(c1)
+        # test that no error is raised.
         sess.flush()
         
     def test_delete_cascade_BtoA(self):
index 1999b52a0749f4c024aa86964d4c311203b6fef3..678085fc18a2cd6a0435412141bf968d2fef715f 100644 (file)
@@ -1175,9 +1175,10 @@ class CRUDTest(SQLCompileTest):
         self.assert_compile(update(table1, table1.c.myid == 7), "UPDATE mytable SET name=:name WHERE mytable.myid = :mytable_myid", params = {table1.c.name:'fred'})
         self.assert_compile(update(table1, table1.c.myid == 7), "UPDATE mytable SET name=:name WHERE mytable.myid = :mytable_myid", params = {'name':'fred'})
         self.assert_compile(update(table1, values = {table1.c.name : table1.c.myid}), "UPDATE mytable SET name=mytable.myid")
-        self.assert_compile(update(table1, whereclause = table1.c.name == bindparam('crit'), values = {table1.c.name : 'hi'}), "UPDATE mytable SET name=:name WHERE mytable.name = :crit", params = {'crit' : 'notthere'})
-        self.assert_compile(update(table1, table1.c.myid == 12, values = {table1.c.name : table1.c.myid}), "UPDATE mytable SET name=mytable.myid, description=:description WHERE mytable.myid = :mytable_myid", params = {'description':'test'})
+        self.assert_compile(update(table1, whereclause = table1.c.name == bindparam('crit'), values = {table1.c.name : 'hi'}), "UPDATE mytable SET name=:name WHERE mytable.name = :crit", params = {'crit' : 'notthere'}, checkparams={'crit':'notthere', 'name':'hi'})
+        self.assert_compile(update(table1, table1.c.myid == 12, values = {table1.c.name : table1.c.myid}), "UPDATE mytable SET name=mytable.myid, description=:description WHERE mytable.myid = :mytable_myid", params = {'description':'test'}, checkparams={'description':'test', 'mytable_myid':12})
         self.assert_compile(update(table1, table1.c.myid == 12, values = {table1.c.myid : 9}), "UPDATE mytable SET myid=:myid, description=:description WHERE mytable.myid = :mytable_myid", params = {'mytable_myid': 12, 'myid': 9, 'description': 'test'})
+        self.assert_compile(update(table1, table1.c.myid ==12), "UPDATE mytable SET myid=:myid WHERE mytable.myid = :mytable_myid", params={'myid':18}, checkparams={'myid':18, 'mytable_myid':12})
         s = table1.update(table1.c.myid == 12, values = {table1.c.name : 'lala'})
         c = s.compile(column_keys=['mytable_id', 'name'])
         self.assert_compile(update(table1, table1.c.myid == 12, values = {table1.c.name : table1.c.myid}).values({table1.c.name:table1.c.name + 'foo'}), "UPDATE mytable SET name=(mytable.name || :mytable_name), description=:description WHERE mytable.myid = :mytable_myid", params = {'description':'test'})
index 6342ce898de2699daa28172f4b7eb06c89fec302..95071a4751c908a05b56a5fd814d07fce09986c3 100644 (file)
@@ -295,7 +295,7 @@ class SQLCompileTest(PersistTest):
         self.assert_(cc == result, "\n'" + cc + "'\n does not match \n'" + result + "'")
 
         if checkparams is not None:
-            self.assert_(c.params == checkparams, "params dont match" + repr(c.params))
+            self.assert_(c.construct_params(params) == checkparams, "params dont match" + repr(c.params))
 
 class AssertMixin(PersistTest):
     """given a list-based structure of keys/properties which represent information within an object structure, and