blob: 0d4dcaf62073fe9bd2abf48a8c5675bd786649dc [file] [log] [blame]
Petri Lehtinene41a4632013-02-26 21:32:02 +02001# -*- coding: iso-8859-1 -*-
Anthony Baxterc51ee692006-04-01 00:57:31 +00002# 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
Serhiy Storchaka30080fd2014-05-28 12:57:38 +030024import collections
Anthony Baxterc51ee692006-04-01 00:57:31 +000025import datetime
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000026import time
27
28from _sqlite3 import *
Anthony Baxterc51ee692006-04-01 00:57:31 +000029
30paramstyle = "qmark"
31
32threadsafety = 1
33
34apilevel = "2.0"
35
Anthony Baxterc51ee692006-04-01 00:57:31 +000036Date = datetime.date
37
38Time = datetime.time
39
40Timestamp = datetime.datetime
41
42def DateFromTicks(ticks):
Brett Cannoncf297cd2008-08-04 21:19:41 +000043 return Date(*time.localtime(ticks)[:3])
Anthony Baxterc51ee692006-04-01 00:57:31 +000044
45def TimeFromTicks(ticks):
Brett Cannoncf297cd2008-08-04 21:19:41 +000046 return Time(*time.localtime(ticks)[3:6])
Anthony Baxterc51ee692006-04-01 00:57:31 +000047
48def TimestampFromTicks(ticks):
Brett Cannoncf297cd2008-08-04 21:19:41 +000049 return Timestamp(*time.localtime(ticks)[:6])
Anthony Baxterc51ee692006-04-01 00:57:31 +000050
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000051version_info = tuple([int(x) for x in version.split(".")])
52sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
Anthony Baxterc51ee692006-04-01 00:57:31 +000053
54Binary = buffer
Serhiy Storchaka30080fd2014-05-28 12:57:38 +030055collections.Sequence.register(Row)
Anthony Baxterc51ee692006-04-01 00:57:31 +000056
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000057def register_adapters_and_converters():
58 def adapt_date(val):
59 return val.isoformat()
Anthony Baxterc51ee692006-04-01 00:57:31 +000060
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000061 def adapt_datetime(val):
62 return val.isoformat(" ")
Anthony Baxterc51ee692006-04-01 00:57:31 +000063
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000064 def convert_date(val):
65 return datetime.date(*map(int, val.split("-")))
Anthony Baxterc51ee692006-04-01 00:57:31 +000066
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000067 def convert_timestamp(val):
68 datepart, timepart = val.split(" ")
69 year, month, day = map(int, datepart.split("-"))
70 timepart_full = timepart.split(".")
71 hours, minutes, seconds = map(int, timepart_full[0].split(":"))
72 if len(timepart_full) == 2:
Petri Lehtinene41a4632013-02-26 21:32:02 +020073 microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000074 else:
75 microseconds = 0
Anthony Baxterc51ee692006-04-01 00:57:31 +000076
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000077 val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
78 return val
Anthony Baxterc51ee692006-04-01 00:57:31 +000079
80
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000081 register_adapter(datetime.date, adapt_date)
82 register_adapter(datetime.datetime, adapt_datetime)
83 register_converter("date", convert_date)
84 register_converter("timestamp", convert_timestamp)
85
86register_adapters_and_converters()
87
88# Clean up namespace
89
90del(register_adapters_and_converters)