diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-02-04 16:44:00 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-02-04 16:44:00 +0000 |
commit | e416129179d58879ac1d3facc48a4c640e78c05d (patch) | |
tree | 5888c2c5a850d6b1abfff98def7ce97279a3ca62 /lib/sqlalchemy/databases | |
parent | 0324af702aeaf28ba5a07b5707f19a189d75f47f (diff) | |
download | sqlalchemy-e416129179d58879ac1d3facc48a4c640e78c05d.tar.gz |
Rick Morrison's patch adding Smallint, Date, and Time support !
Diffstat (limited to 'lib/sqlalchemy/databases')
-rw-r--r-- | lib/sqlalchemy/databases/mysql.py | 15 | ||||
-rw-r--r-- | lib/sqlalchemy/databases/oracle.py | 10 | ||||
-rw-r--r-- | lib/sqlalchemy/databases/postgres.py | 49 | ||||
-rw-r--r-- | lib/sqlalchemy/databases/sqlite.py | 35 |
4 files changed, 102 insertions, 7 deletions
diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index fda41a3fc..268639b69 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -28,9 +28,18 @@ class MSFloat(sqltypes.Float): class MSInteger(sqltypes.Integer): def get_col_spec(self): return "INTEGER" +class MSSmallInteger(sqltypes.Smallinteger): + def get_col_spec(self): + return "SMALLINT" class MSDateTime(sqltypes.DateTime): def get_col_spec(self): return "DATETIME" +class MSDate(sqltypes.Date): + def get_col_spec(self): + return "DATE" +class MSTime(sqltypes.Time): + def get_col_spec(self): + return "TIME" class MSText(sqltypes.TEXT): def get_col_spec(self): return "TEXT" @@ -54,9 +63,12 @@ class MSBoolean(sqltypes.Boolean): colspecs = { sqltypes.Integer : MSInteger, + sqltypes.Smallinteger : MSSmallInteger, sqltypes.Numeric : MSNumeric, sqltypes.Float : MSFloat, sqltypes.DateTime : MSDateTime, + sqltypes.Date : MSDate, + sqltypes.Time : MSTime, sqltypes.String : MSString, sqltypes.Binary : MSBinary, sqltypes.Boolean : MSBoolean, @@ -66,6 +78,7 @@ colspecs = { ischema_names = { 'int' : MSInteger, + 'smallint' : MSSmallInteger, 'varchar' : MSString, 'char' : MSChar, 'text' : MSText, @@ -73,6 +86,8 @@ ischema_names = { 'float' : MSFloat, 'timestamp' : MSDateTime, 'datetime' : MSDateTime, + 'date' : MSDate, + 'time' : MSTime, 'binary' : MSBinary, 'blob' : MSBinary, } diff --git a/lib/sqlalchemy/databases/oracle.py b/lib/sqlalchemy/databases/oracle.py index c3214c669..de4a76c3e 100644 --- a/lib/sqlalchemy/databases/oracle.py +++ b/lib/sqlalchemy/databases/oracle.py @@ -24,9 +24,17 @@ class OracleNumeric(sqltypes.Numeric): class OracleInteger(sqltypes.Integer): def get_col_spec(self): return "INTEGER" +class OracleSmallInteger(sqltypes.Smallinteger): + def get_col_spec(self): + return "SMALLINT" class OracleDateTime(sqltypes.DateTime): def get_col_spec(self): return "DATE" +# Note: +# Oracle DATE == DATETIME +# Oracle does not allow milliseconds in DATE +# Oracle does not support TIME columns + class OracleText(sqltypes.TEXT): def get_col_spec(self): return "CLOB" @@ -45,8 +53,10 @@ class OracleBoolean(sqltypes.Boolean): colspecs = { sqltypes.Integer : OracleInteger, + sqltypes.Smallinteger : OracleSmallInteger, sqltypes.Numeric : OracleNumeric, sqltypes.DateTime : OracleDateTime, + sqltypes.Date : OracleDateTime, sqltypes.String : OracleString, sqltypes.Binary : OracleBinary, sqltypes.Boolean : OracleBoolean, diff --git a/lib/sqlalchemy/databases/postgres.py b/lib/sqlalchemy/databases/postgres.py index 3a2918684..2d6adc165 100644 --- a/lib/sqlalchemy/databases/postgres.py +++ b/lib/sqlalchemy/databases/postgres.py @@ -33,6 +33,9 @@ class PGFloat(sqltypes.Float): class PGInteger(sqltypes.Integer): def get_col_spec(self): return "INTEGER" +class PGSmallInteger(sqltypes.Smallinteger): + def get_col_spec(self): + return "SMALLINT" class PG2DateTime(sqltypes.DateTime): def get_col_spec(self): return "TIMESTAMP" @@ -46,6 +49,32 @@ class PG1DateTime(sqltypes.DateTime): return value def get_col_spec(self): return "TIMESTAMP" +class PG2Date(sqltypes.Date): + def get_col_spec(self): + return "DATE" +class PG1Date(sqltypes.Date): + def convert_bind_param(self, value, engine): + # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime + # this one doesnt seem to work with the "emulation" mode + return psycopg.DateFromMx(value) + def convert_result_value(self, value, engine): + # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime + return value + def get_col_spec(self): + return "DATE" +class PG2Time(sqltypes.Date): + def get_col_spec(self): + return "TIME" +class PG1Time(sqltypes.Date): + def convert_bind_param(self, value, engine): + # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime + # this one doesnt seem to work with the "emulation" mode + return psycopg.TimeFromMx(value) + def convert_result_value(self, value, engine): + # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime + return value + def get_col_spec(self): + return "TIME" class PGText(sqltypes.TEXT): def get_col_spec(self): return "TEXT" @@ -64,9 +93,12 @@ class PGBoolean(sqltypes.Boolean): pg2_colspecs = { sqltypes.Integer : PGInteger, + sqltypes.Smallinteger : PGSmallInteger, sqltypes.Numeric : PGNumeric, sqltypes.Float : PGFloat, sqltypes.DateTime : PG2DateTime, + sqltypes.Date : PG2Date, + sqltypes.Time : PG2Time, sqltypes.String : PGString, sqltypes.Binary : PGBinary, sqltypes.Boolean : PGBoolean, @@ -74,11 +106,16 @@ pg2_colspecs = { sqltypes.CHAR: PGChar, } pg1_colspecs = pg2_colspecs.copy() -pg1_colspecs[sqltypes.DateTime] = PG1DateTime +pg1_colspecs.update({ + sqltypes.DateTime : PG1DateTime, + sqltypes.Date : PG1Date, + sqltypes.Time : PG1Time + }) pg2_ischema_names = { 'integer' : PGInteger, 'bigint' : PGInteger, + 'smallint' : PGSmallInteger, 'character varying' : PGString, 'character' : PGChar, 'text' : PGText, @@ -88,12 +125,18 @@ pg2_ischema_names = { 'double precision' : PGFloat, 'timestamp with time zone' : PG2DateTime, 'timestamp without time zone' : PG2DateTime, + 'date' : PG2Date, + 'time': PG2Time, 'bytea' : PGBinary, 'boolean' : PGBoolean, } pg1_ischema_names = pg2_ischema_names.copy() -pg1_ischema_names['timestamp with time zone'] = \ - pg1_ischema_names['timestamp without time zone'] = PG1DateTime +pg1_ischema_names.update({ + 'timestamp with time zone' : PG1DateTime, + 'timestamp without time zone' : PG1DateTime, + 'date' : PG1Date, + 'time' : PG1Time + }) def engine(opts, **params): return PGSQLEngine(opts, **params) diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py index 9ada952ca..a403e3c1a 100644 --- a/lib/sqlalchemy/databases/sqlite.py +++ b/lib/sqlalchemy/databases/sqlite.py @@ -15,6 +15,8 @@ import sqlalchemy.types as sqltypes from sqlalchemy.ansisql import * import datetime,time +pysqlite2_timesupport = False # Change this if the init.d guys ever get around to supporting time cols + try: from pysqlite2 import dbapi2 as sqlite except: @@ -26,10 +28,13 @@ class SLNumeric(sqltypes.Numeric): class SLInteger(sqltypes.Integer): def get_col_spec(self): return "INTEGER" +class SLSmallInteger(sqltypes.Smallinteger): + def get_col_spec(self): + return "SMALLINT" class SLDateTime(sqltypes.DateTime): def get_col_spec(self): return "TIMESTAMP" - def convert_result_value(self, value, engine): + def _cvt(self, value, engine, fmt): if value is None: return None parts = value.split('.') @@ -38,9 +43,22 @@ class SLDateTime(sqltypes.DateTime): microsecond = int(microsecond) except ValueError: (value, microsecond) = (value, 0) - tup = time.strptime(value, "%Y-%m-%d %H:%M:%S") - return datetime.datetime(microsecond=microsecond, *tup[0:6]) - + return time.strptime(value, fmt)[0:6] + (microsecond,) + def convert_result_value(self, value, engine): + tup = self._cvt(value, engine, "%Y-%m-%d %H:%M:%S") + return tup and datetime.datetime(*tup) +class SLDate(SLDateTime): + def get_col_spec(self): + return "DATE" + def convert_result_value(self, value, engine): + tup = self._cvt(value, engine, "%Y-%m-%d") + return tup and datetime.date(*tup[0:3]) +class SLTime(SLDateTime): + def get_col_spec(self): + return "TIME" + def convert_result_value(self, value, engine): + tup = self._cvt(value, engine, "%H:%M:%S") + return tup and datetime.time(*tup[4:7]) class SLText(sqltypes.TEXT): def get_col_spec(self): return "TEXT" @@ -59,9 +77,11 @@ class SLBoolean(sqltypes.Boolean): colspecs = { sqltypes.Integer : SLInteger, + sqltypes.Smallinteger : SLSmallInteger, sqltypes.Numeric : SLNumeric, sqltypes.Float : SLNumeric, sqltypes.DateTime : SLDateTime, + sqltypes.Date : SLDate, sqltypes.String : SLString, sqltypes.Binary : SLBinary, sqltypes.Boolean : SLBoolean, @@ -71,15 +91,22 @@ colspecs = { pragma_names = { 'INTEGER' : SLInteger, + 'SMALLINT' : SLSmallInteger, 'VARCHAR' : SLString, 'CHAR' : SLChar, 'TEXT' : SLText, 'NUMERIC' : SLNumeric, 'FLOAT' : SLNumeric, 'TIMESTAMP' : SLDateTime, + 'DATETIME' : SLDateTime, + 'DATE' : SLDate, 'BLOB' : SLBinary, } +if pysqlite2_timesupport: + colspecs.update({sqltypes.Time : SLTime}) + pragma_names.update({'TIME' : SLTime}) + def engine(opts, **params): return SQLiteSQLEngine(opts, **params) |