mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-06-02 05:48:52 -04:00
100 lines
2.9 KiB
Python
100 lines
2.9 KiB
Python
# 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 TypeEngine(object):
|
|
def get_col_spec(self, typeobj):
|
|
raise NotImplementedError()
|
|
def convert_bind_param(self, value):
|
|
raise NotImplementedError()
|
|
def convert_result_value(self, value):
|
|
raise NotImplementedError()
|
|
def adapt(self, typeobj):
|
|
return typeobj()
|
|
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):
|
|
raise NotImplementedError()
|
|
def convert_bind_param(self, value):
|
|
return value
|
|
def convert_result_value(self, value):
|
|
return value
|
|
|
|
class String(TypeEngine):
|
|
def __init__(self, length = None):
|
|
self.length = length
|
|
def adapt(self, typeobj):
|
|
return typeobj(self.length)
|
|
def adapt_args(self):
|
|
if self.length is None:
|
|
return TEXT()
|
|
else:
|
|
return self
|
|
|
|
class Integer(TypeEngine):
|
|
"""integer datatype"""
|
|
pass
|
|
|
|
class Numeric(TypeEngine):
|
|
def __init__(self, precision = 10, length = 2):
|
|
self.precision = precision
|
|
self.length = length
|
|
def adapt(self, typeobj):
|
|
return typeobj(self.precision, self.length)
|
|
|
|
class DateTime(TypeEngine):
|
|
pass
|
|
|
|
class Binary(TypeEngine):
|
|
pass
|
|
|
|
class Boolean(TypeEngine):
|
|
pass
|
|
|
|
class FLOAT(Numeric):pass
|
|
class TEXT(String):pass
|
|
class DECIMAL(Numeric):pass
|
|
class INT(Integer):pass
|
|
INTEGER = INT
|
|
class TIMESTAMP(DateTime): pass
|
|
class DATETIME(DateTime): pass
|
|
class CLOB(String): pass
|
|
class VARCHAR(String): pass
|
|
class CHAR(String):pass
|
|
class BLOB(Binary): pass
|
|
class BOOLEAN(Boolean): pass
|
|
|