]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
decruftify
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 28 Sep 2006 02:13:37 +0000 (02:13 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 28 Sep 2006 02:13:37 +0000 (02:13 +0000)
lib/sqlalchemy/ansisql.py
lib/sqlalchemy/schema.py
lib/sqlalchemy/sql.py
test/sql/query.py

index 66b917c208979797e2a92a23237bda06f2574cf7..a687fe70552d3b8635b588a7e103ce80a71f42ee 100644 (file)
@@ -140,14 +140,14 @@ class ANSICompiler(sql.Compiled):
 
         d = sql.ClauseParameters(self.dialect, self.positiontup)
         for b in self.binds.values():
-            d.set_parameter(b.key, b.value, b)
+            d.set_parameter(b, b.value)
 
         for key, value in bindparams.iteritems():
             try:
                 b = self.binds[key]
             except KeyError:
                 continue
-            d.set_parameter(b.key, value, b)
+            d.set_parameter(b, value)
 
         return d
 
@@ -181,7 +181,7 @@ class ANSICompiler(sql.Compiled):
                 self.strings[column] = self.preparer.format_column_with_table(column)
 
     def visit_fromclause(self, fromclause):
-        self.froms[fromclause] = fromclause.from_name
+        self.froms[fromclause] = fromclause.name
 
     def visit_index(self, index):
         self.strings[index] = index.name
index 3ee03f1eedd544fda4f4dbc51f7902f5f47e4084..3dab1f8679d9a0923c757debe3842e906eb00706 100644 (file)
@@ -435,12 +435,10 @@ class Column(SchemaItem, sql.ColumnClause):
 
     def __str__(self):
         if self.table is not None:
-            tname = self.table.displayname
-            if tname is not None:
-                return tname + "." + self.name
+            if self.table.named_with_column():
+                return self.table.name + "." + self.name
             else:
                 return self.name
-        else:
             return self.name
     
     def _derived_metadata(self):
index d332a2907e61c1145e84f420d5293e68b5cf60f3..9c04cdfcb36214ce81e85d4309879b135cda50fe 100644 (file)
@@ -217,16 +217,16 @@ def table(name, *columns):
     of this object."""
     return TableClause(name, *columns)
     
-def bindparam(key, value = None, type=None):
+def bindparam(key, value=None, type=None, shortname=None):
     """creates a bind parameter clause with the given key.  
     
     An optional default value can be specified by the value parameter, and the optional type parameter
     is a sqlalchemy.types.TypeEngine object which indicates bind-parameter and result-set translation for
     this bind parameter."""
     if isinstance(key, ColumnClause):
-        return BindParamClause(key.name, value, type=key.type)
+        return BindParamClause(key.name, value, type=key.type, shortname=shortname)
     else:
-        return BindParamClause(key, value, type=type)
+        return BindParamClause(key, value, type=type, shortname=shortname)
 
 def text(text, engine=None, *args, **kwargs):
     """creates literal text to be inserted into a query.  
@@ -301,9 +301,9 @@ class ClauseParameters(dict):
         self.dialect=dialect
         self.binds = {}
         self.positional = positional or []
-    def set_parameter(self, key, value, bindparam):
-        self[key] = value
-        self.binds[key] = bindparam
+    def set_parameter(self, bindparam, value):
+        self[bindparam.key] = value
+        self.binds[bindparam.key] = bindparam
     def get_original(self, key):
         """returns the given parameter as it was originally placed in this ClauseParameters object, without any Type conversion"""
         return super(ClauseParameters, self).__getitem__(key)
@@ -654,14 +654,8 @@ class ColumnElement(Selectable, CompareMixin):
 
 class FromClause(Selectable):
     """represents an element that can be used within the FROM clause of a SELECT statement."""
-    def __init__(self, from_name = None):
-        self.from_name = self.name = from_name
-    def _display_name(self):
-        if self.named_with_column():
-            return self.name
-        else:
-            return None
-    displayname = property(_display_name)
+    def __init__(self, name=None):
+        self.name = name
     def _get_from_objects(self):
         # this could also be [self], at the moment it doesnt matter to the Select object
         return []
@@ -678,7 +672,7 @@ class FromClause(Selectable):
     def join(self, right, *args, **kwargs):
         return Join(self, right, *args, **kwargs)
     def outerjoin(self, right, *args, **kwargs):
-        return Join(self, right, isouter = True, *args, **kwargs)
+        return Join(self, right, isouter=True, *args, **kwargs)
     def alias(self, name=None):
         return Alias(self, name)
     def named_with_column(self):
@@ -760,9 +754,26 @@ class FromClause(Selectable):
 class BindParamClause(ClauseElement, CompareMixin):
     """represents a bind parameter.  public constructor is the bindparam() function."""
     def __init__(self, key, value, shortname=None, type=None):
+        """construct a BindParamClause.
+        
+        key - the key for this bind param.  will be used in the generated SQL statement
+        for dialects that use named parameters.  this value may be modified when part of a 
+        compilation operation, if other BindParamClause objects exist with the same key, or if 
+        its length is too long and truncation is required.
+        
+        value - initial value for this bind param.  This value may be overridden by the
+        dictionary of parameters sent to statement 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.
+        
+        type - a TypeEngine object that will be used to pre-process the value corresponding
+        to this BindParamClause at execution time."""
         self.key = key
         self.value = value
-        self.shortname = shortname
+        self.shortname = shortname or key
         self.type = sqltypes.to_instance(type)
     def accept_visitor(self, visitor):
         visitor.visit_bindparam(self)
@@ -1506,7 +1517,7 @@ class Select(SelectBaseMixin, FromClause):
             setattr(self, attribute, condition)
 
     def clear_from(self, from_obj):
-        self._froms[from_obj] = FromClause(from_name = None)
+        self._froms[from_obj] = FromClause()
         
     def append_from(self, fromclause):
         if type(fromclause) == str:
index ccb998e9e3f73e006930cae1a66713bb9b5a35f3..75b1cea5cf7409acbb0c1e07b45bfd729bf4a0aa 100644 (file)
@@ -125,6 +125,15 @@ class QueryTest(PersistTest):
         s = self.users.select(or_(self.users.c.user_name==u, self.users.c.user_name==u))
         r = s.execute(uid='fred').fetchall()
         assert len(r) == 1
+    
+    def test_bindparam_shortname(self):
+        """test the 'shortname' field on BindParamClause."""
+        self.users.insert().execute(user_id = 7, user_name = 'jack')
+        self.users.insert().execute(user_id = 8, user_name = 'fred')
+        u = bindparam('uid', shortname='someshortname')
+        s = self.users.select(self.users.c.user_name==u)
+        r = s.execute(someshortname='fred').fetchall()
+        assert len(r) == 1
         
     def testdelete(self):
         self.users.insert().execute(user_id = 7, user_name = 'jack')