Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | # pysqlite2/dbapi2.py: the DB-API 2.0 interface |
| 2 | # |
Florent Xicluna | e01de8f | 2010-08-30 14:05:50 +0000 | [diff] [blame] | 3 | # Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4 | # |
| 5 | # This file is part of pysqlite. |
| 6 | # |
| 7 | # This software is provided 'as-is', without any express or implied |
| 8 | # warranty. In no event will the authors be held liable for any damages |
| 9 | # arising from the use of this software. |
| 10 | # |
| 11 | # Permission is granted to anyone to use this software for any purpose, |
| 12 | # including commercial applications, and to alter it and redistribute it |
| 13 | # freely, subject to the following restrictions: |
| 14 | # |
| 15 | # 1. The origin of this software must not be misrepresented; you must not |
| 16 | # claim that you wrote the original software. If you use this software |
| 17 | # in a product, an acknowledgment in the product documentation would be |
| 18 | # appreciated but is not required. |
| 19 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 20 | # misrepresented as being the original software. |
| 21 | # 3. This notice may not be removed or altered from any source distribution. |
| 22 | |
| 23 | import datetime |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 24 | import time |
| 25 | |
| 26 | from _sqlite3 import * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | |
| 28 | paramstyle = "qmark" |
| 29 | |
| 30 | threadsafety = 1 |
| 31 | |
| 32 | apilevel = "2.0" |
| 33 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | Date = datetime.date |
| 35 | |
| 36 | Time = datetime.time |
| 37 | |
| 38 | Timestamp = datetime.datetime |
| 39 | |
| 40 | def DateFromTicks(ticks): |
Guido van Rossum | 3b843cc | 2006-08-17 22:37:44 +0000 | [diff] [blame] | 41 | return Date(*time.localtime(ticks)[:3]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | |
| 43 | def TimeFromTicks(ticks): |
Guido van Rossum | 3b843cc | 2006-08-17 22:37:44 +0000 | [diff] [blame] | 44 | return Time(*time.localtime(ticks)[3:6]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | |
| 46 | def TimestampFromTicks(ticks): |
Guido van Rossum | 3b843cc | 2006-08-17 22:37:44 +0000 | [diff] [blame] | 47 | return Timestamp(*time.localtime(ticks)[:6]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 48 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 49 | version_info = tuple([int(x) for x in version.split(".")]) |
| 50 | sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 52 | Binary = memoryview |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 54 | def register_adapters_and_converters(): |
| 55 | def adapt_date(val): |
| 56 | return val.isoformat() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 58 | def adapt_datetime(val): |
| 59 | return val.isoformat(" ") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 60 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 61 | def convert_date(val): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 62 | return datetime.date(*map(int, val.split(b"-"))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 64 | def convert_timestamp(val): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 65 | datepart, timepart = val.split(b" ") |
| 66 | year, month, day = map(int, datepart.split(b"-")) |
| 67 | timepart_full = timepart.split(b".") |
| 68 | hours, minutes, seconds = map(int, timepart_full[0].split(b":")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 69 | if len(timepart_full) == 2: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 70 | microseconds = int(timepart_full[1]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 71 | else: |
| 72 | microseconds = 0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) |
| 75 | return val |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 76 | |
| 77 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 78 | register_adapter(datetime.date, adapt_date) |
| 79 | register_adapter(datetime.datetime, adapt_datetime) |
| 80 | register_converter("date", convert_date) |
| 81 | register_converter("timestamp", convert_timestamp) |
| 82 | |
| 83 | register_adapters_and_converters() |
| 84 | |
| 85 | # Clean up namespace |
| 86 | |
| 87 | del(register_adapters_and_converters) |