]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
The newly added SQLite DATETIME arguments storage_format and
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 13 Jul 2013 01:52:54 +0000 (21:52 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 13 Jul 2013 01:53:28 +0000 (21:53 -0400)
regexp apparently were not fully implemented correctly; while the
arguments were accepted, in practice they would have no effect;
this has been fixed.
[ticket:2781]

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

index a7d28430a04d4e3b2237751c76e996e37abe60bc..55fd7949a107039244ca1c5313e1fd9d99da6932 100644 (file)
@@ -6,6 +6,15 @@
 .. changelog::
     :version: 0.8.3
 
+    .. change::
+        :tags: bug, sqlite
+        :tickets: 2781
+
+        The newly added SQLite DATETIME arguments storage_format and
+        regexp apparently were not fully implemented correctly; while the
+        arguments were accepted, in practice they would have no effect;
+        this has been fixed.
+
     .. change::
         :tags: bug, sql, postgresql
         :tickets: 2780
index 085ff51f12d53c595bcf338fd36e3917f6099bc0..6032a62de9cd71d23801e1d079474c8d4c4f62aa 100644 (file)
@@ -153,6 +153,12 @@ class _DateTimeMixin(object):
         if storage_format is not None:
             self._storage_format = storage_format
 
+    def adapt(self, cls, **kw):
+        if self._storage_format:
+            kw["storage_format"] = self._storage_format
+        if self._reg:
+            kw["regexp"] = self._reg
+        return util.constructor_copy(self, cls, **kw)
 
 class DATETIME(_DateTimeMixin, sqltypes.DateTime):
     """Represent a Python datetime object in SQLite using a string.
index e70485cfd77281d059c28bfb48e8b805b6555c88..cf5a4f521f9386ea0f963cefc082b53d22b716ab 100644 (file)
@@ -93,6 +93,52 @@ class TestTypes(fixtures.TestBase, AssertsExecutionResults):
             t.drop(engine)
             engine.dispose()
 
+    @testing.provide_metadata
+    def test_custom_datetime(self):
+        sqlite_date = sqlite.DATETIME(
+                # 2004-05-21T00:00:00
+                storage_format="%(year)04d-%(month)02d-%(day)02d"
+                    "T%(hour)02d:%(minute)02d:%(second)02d",
+                regexp=r"(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)",
+            )
+        t = Table('t', self.metadata, Column('d', sqlite_date))
+        self.metadata.create_all(testing.db)
+        testing.db.execute(t.insert().
+                        values(d=datetime.datetime(2010, 10, 15, 12, 37, 0)))
+        testing.db.execute("insert into t (d) values ('2004-05-21T00:00:00')")
+        eq_(
+            testing.db.execute("select * from t order by d").fetchall(),
+            [(u'2004-05-21T00:00:00',), (u'2010-10-15T12:37:00',)]
+        )
+        eq_(
+            testing.db.execute(select([t.c.d]).order_by(t.c.d)).fetchall(),
+            [(datetime.datetime(2004, 5, 21, 0, 0),),
+            (datetime.datetime(2010, 10, 15, 12, 37),)]
+        )
+
+    @testing.provide_metadata
+    def test_custom_date(self):
+        sqlite_date = sqlite.DATE(
+                # 2004-05-21T00:00:00
+                storage_format="%(year)04d|%(month)02d|%(day)02d",
+                regexp=r"(\d+)\|(\d+)\|(\d+)",
+            )
+        t = Table('t', self.metadata, Column('d', sqlite_date))
+        self.metadata.create_all(testing.db)
+        testing.db.execute(t.insert().
+                        values(d=datetime.date(2010, 10, 15)))
+        testing.db.execute("insert into t (d) values ('2004|05|21')")
+        eq_(
+            testing.db.execute("select * from t order by d").fetchall(),
+            [(u'2004|05|21',), (u'2010|10|15',)]
+        )
+        eq_(
+            testing.db.execute(select([t.c.d]).order_by(t.c.d)).fetchall(),
+            [(datetime.date(2004, 5, 21),),
+            (datetime.date(2010, 10, 15),)]
+        )
+
+
     def test_no_convert_unicode(self):
         """test no utf-8 encoding occurs"""
 
@@ -218,7 +264,6 @@ class DateTimeTest(fixtures.TestBase, AssertsCompiledSQL):
         rp = sldt.result_processor(None, None)
         eq_(rp(bp(dt)), dt)
 
-
 class DateTest(fixtures.TestBase, AssertsCompiledSQL):
 
     def test_default(self):