blob: d051f0432fa53eff67dd7fae3b5ee7b47fae06ab [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001#-*- coding: ISO-8859-1 -*-
2# pysqlite2/dbapi2.py: the DB-API 2.0 interface
3#
4# Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
5#
6# This file is part of pysqlite.
7#
8# This software is provided 'as-is', without any express or implied
9# warranty. In no event will the authors be held liable for any damages
10# arising from the use of this software.
11#
12# Permission is granted to anyone to use this software for any purpose,
13# including commercial applications, and to alter it and redistribute it
14# freely, subject to the following restrictions:
15#
16# 1. The origin of this software must not be misrepresented; you must not
17# claim that you wrote the original software. If you use this software
18# in a product, an acknowledgment in the product documentation would be
19# appreciated but is not required.
20# 2. Altered source versions must be plainly marked as such, and must not be
21# misrepresented as being the original software.
22# 3. This notice may not be removed or altered from any source distribution.
23
24import datetime
Thomas Wouters477c8d52006-05-27 19:21:47 +000025import time
26
27from _sqlite3 import *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028
29paramstyle = "qmark"
30
31threadsafety = 1
32
33apilevel = "2.0"
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035Date = datetime.date
36
37Time = datetime.time
38
39Timestamp = datetime.datetime
40
41def DateFromTicks(ticks):
Guido van Rossum3b843cc2006-08-17 22:37:44 +000042 return Date(*time.localtime(ticks)[:3])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043
44def TimeFromTicks(ticks):
Guido van Rossum3b843cc2006-08-17 22:37:44 +000045 return Time(*time.localtime(ticks)[3:6])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046
47def TimestampFromTicks(ticks):
Guido van Rossum3b843cc2006-08-17 22:37:44 +000048 return Timestamp(*time.localtime(ticks)[:6])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049
Thomas Wouters477c8d52006-05-27 19:21:47 +000050version_info = tuple([int(x) for x in version.split(".")])
51sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
Guido van Rossumbae07c92007-10-08 02:46:15 +000053Binary = memoryview
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054
Thomas Wouters477c8d52006-05-27 19:21:47 +000055def register_adapters_and_converters():
56 def adapt_date(val):
57 return val.isoformat()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058
Thomas Wouters477c8d52006-05-27 19:21:47 +000059 def adapt_datetime(val):
60 return val.isoformat(" ")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062 def convert_date(val):
Guido van Rossum98297ee2007-11-06 21:34:58 +000063 return datetime.date(*map(int, val.split(b"-")))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000064
Thomas Wouters477c8d52006-05-27 19:21:47 +000065 def convert_timestamp(val):
Guido van Rossum98297ee2007-11-06 21:34:58 +000066 datepart, timepart = val.split(b" ")
67 year, month, day = map(int, datepart.split(b"-"))
68 timepart_full = timepart.split(b".")
69 hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
Thomas Wouters477c8d52006-05-27 19:21:47 +000070 if len(timepart_full) == 2:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000071 microseconds = int(timepart_full[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +000072 else:
73 microseconds = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074
Thomas Wouters477c8d52006-05-27 19:21:47 +000075 val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
76 return val
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079 register_adapter(datetime.date, adapt_date)
80 register_adapter(datetime.datetime, adapt_datetime)
81 register_converter("date", convert_date)
82 register_converter("timestamp", convert_timestamp)
83
84register_adapters_and_converters()
85
86# Clean up namespace
87
88del(register_adapters_and_converters)