From 471c3f81022fd44ec8659528a67a2b9c0330f815 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 15 Jul 2007 15:29:41 +0000 Subject: [PATCH] updated interval type for [ticket:595] --- lib/sqlalchemy/types.py | 28 ++++++++++++++-------------- test/sql/testtypes.py | 9 ++++++++- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 7b2d49c378..3cceedae6b 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -357,31 +357,33 @@ class Interval(TypeDecorator): will be stored as DateTime = '2nd Jan 1970 00:00', see convert_bind_param and convert_result_value to actual conversion code """ - impl = None - - def __init__(self,*args,**kwargs): - #avoid of getting instance of None type in __init__ of TypeDecorator - pass + #Empty useless type, because at the moment of creation of instance we don't + #know what type will be decorated - it depends on used dialect. + impl = TypeEngine def load_dialect_impl(self, dialect): - import sqlalchemy.databases.postgres as pg """Checks if engine has native implementation of timedelta python type, if so it returns right class to handle it, if there is no native support, it fallback to engine's DateTime implementation class """ - + if not hasattr(self,'__supported'): + import sqlalchemy.databases.postgres as pg + self.__supported = {pg.PGDialect:pg.PGInterval} + del pg + if self.__hasNativeImpl(dialect): #For now, only PostgreSQL has native timedelta types support - return pg.PGInterval() + return self.__supported[dialect.__class__]() else: #All others should fallback to DateTime return dialect.type_descriptor(DateTime) def __hasNativeImpl(self,dialect): - import sqlalchemy.databases.postgres as pg - return dialect.__class__ in [pg.PGDialect] + return dialect.__class__ in self.__supported def convert_bind_param(self, value, dialect): + if value is None: + return None if not self.__hasNativeImpl(dialect): tmpval = dt.datetime.utcfromtimestamp(0) + value return self.impl.convert_bind_param(tmpval,dialect) @@ -389,15 +391,13 @@ class Interval(TypeDecorator): return self.impl.convert_bind_param(value,dialect) def convert_result_value(self, value, dialect): + if value is None: + return None retval = self.impl.convert_result_value(value,dialect) if not self.__hasNativeImpl(dialect): return retval - dt.datetime.utcfromtimestamp(0) else: return retval - - def is_mutable(self): - #neither datetime, nor PGInterval are mutable types - return False class FLOAT(Float):pass class TEXT(String):pass diff --git a/test/sql/testtypes.py b/test/sql/testtypes.py index 898592cd35..d858f748a6 100644 --- a/test/sql/testtypes.py +++ b/test/sql/testtypes.py @@ -368,7 +368,10 @@ class IntervalTest(AssertMixin): Column("interval", Interval), ) metadata.create_all() - + + def tearDown(self): + interval_table.delete().execute() + def tearDownAll(self): metadata.drop_all() @@ -376,6 +379,10 @@ class IntervalTest(AssertMixin): delta = datetime.datetime(2006, 10, 5) - datetime.datetime(2005, 8, 17) interval_table.insert().execute(interval=delta) assert interval_table.select().execute().fetchone()['interval'] == delta + + def test_null(self): + interval_table.insert().execute(id=1, inverval=None) + assert interval_table.select().execute().fetchone()['interval'] is None class BooleanTest(AssertMixin): def setUpAll(self): -- 2.47.2