]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
(no commit message)
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 22 Sep 2005 07:53:23 +0000 (07:53 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 22 Sep 2005 07:53:23 +0000 (07:53 +0000)
lib/sqlalchemy/types.py

index ab441a8ea5b1652e43e049d9109a0942c45140c1..3b9f3cee89e78820269e7fe1383c3158e0ff79a8 100644 (file)
@@ -1,15 +1,27 @@
+# types.py
+# Copyright (C) 2005 Michael Bayer mike_mp@zzzcomputing.com
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
 __ALL__ = [
             'INT', 'CHAR', 'VARCHAR', 'TEXT', 'FLOAT', 'DECIMAL', 
             'TIMESTAMP', 'DATETIME', 'CLOB', 'BLOB', 'BOOLEAN', 'String', 'Integer', 'Numeric', 'DateTime', 'Binary', 'Boolean'
             ]
 
 
-class TypeEngineMeta(type):
-    typeself = property(lambda cls:cls.singleton)        
-    typeclass = property(lambda cls: cls)
-    
 class TypeEngine(object):
-    __metaclass__ = TypeEngineMeta
     def get_col_spec(self, typeobj):
         raise NotImplementedError()
     def convert_bind_param(self, value):
@@ -18,17 +30,20 @@ class TypeEngine(object):
         raise NotImplementedError()
     def adapt(self, typeobj):
         return typeobj()
-    def type_descriptor(self, conversion, cls = None):
-        t = cls or self.__class__
-        for t in t.__mro__[0:-1]:
-            try:
-                return self.adapt(conversion[t])
-            except KeyError, e:
-                pass
-        return self.adapt(cls or self.__class__)
-
-    typeclass = property(lambda s: s.__class__)
-    typeself = property(lambda s:s)
+    def adapt_args(self):
+        return self
+        
+def adapt_type(typeobj, colspecs):
+    if type(typeobj) is type:
+        typeobj = typeobj()
+    typeobj = typeobj.adapt_args()
+    t = typeobj.__class__
+    for t in t.__mro__[0:-1]:
+        try:
+            return typeobj.adapt(colspecs[t])
+        except KeyError, e:
+            pass
+    return typeobj.adapt(typeobj.__class__)
     
 class NullTypeEngine(TypeEngine):
     def get_col_spec(self, typeobj):
@@ -39,45 +54,38 @@ class NullTypeEngine(TypeEngine):
         return value
 
 class String(TypeEngine):
-    def __init__(self, length):
+    def __init__(self, length = None):
         self.length = length
     def adapt(self, typeobj):
         return typeobj(self.length)
-    def type_descriptor(self, conversion):
+    def adapt_args(self):
         if self.length is None:
-            return super(String, self).type_descriptor(conversion, TEXT)
+            return TEXT()
         else:
-            return super(String, self).type_descriptor(conversion)
-            
-String.singleton = String(-1)
+            return self
 
 class Integer(TypeEngine):
     """integer datatype"""
     pass
-Integer.singleton = Integer()
 
 class Numeric(TypeEngine):
-    def __init__(self, precision, length):
+    def __init__(self, precision = 10, length = 2):
         self.precision = precision
         self.length = length
     def adapt(self, typeobj):
         return typeobj(self.precision, self.length)
-Numeric.singleton = Numeric(10, 2)
 
 class DateTime(TypeEngine):
     pass
-DateTime.singleton = DateTime()
 
 class Binary(TypeEngine):
     pass
-Binary.singleton = Binary()
 
 class Boolean(TypeEngine):
     pass
-Boolean.singleton = Boolean()
 
 class FLOAT(Numeric):pass
-class TEXT(String): pass
+class TEXT(String):pass
 class DECIMAL(Numeric):pass
 class INT(Integer):pass
 INTEGER = INT
@@ -89,4 +97,3 @@ class CHAR(String):pass
 class BLOB(Binary): pass
 class BOOLEAN(Boolean): pass
 
-