annotate Lib/sqlite3/dbapi2.py @ 83447:678564fc0962 3.3

#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 19 Apr 2013 05:45:27 +0300
parents ae25a38e6c17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
1 # pysqlite2/dbapi2.py: the DB-API 2.0 interface
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
2 #
64316
040ee94a9350 remove pointless coding cookies
Florent Xicluna <florent.xicluna@gmail.com>
parents: 43777
diff changeset
3 # Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
4 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
5 # This file is part of pysqlite.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
6 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
7 # This software is provided 'as-is', without any express or implied
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
8 # warranty. In no event will the authors be held liable for any damages
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
9 # arising from the use of this software.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
10 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
11 # Permission is granted to anyone to use this software for any purpose,
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
12 # including commercial applications, and to alter it and redistribute it
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
13 # freely, subject to the following restrictions:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
14 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
15 # 1. The origin of this software must not be misrepresented; you must not
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
16 # claim that you wrote the original software. If you use this software
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
17 # in a product, an acknowledgment in the product documentation would be
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
18 # appreciated but is not required.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
19 # 2. Altered source versions must be plainly marked as such, and must not be
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
20 # misrepresented as being the original software.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
21 # 3. This notice may not be removed or altered from any source distribution.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
22
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
23 import datetime
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
24 import time
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
25
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
26 from _sqlite3 import *
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
27
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
28 paramstyle = "qmark"
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
29
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
30 threadsafety = 1
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
31
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
32 apilevel = "2.0"
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
33
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
34 Date = datetime.date
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
35
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
36 Time = datetime.time
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
37
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
38 Timestamp = datetime.datetime
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
39
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
40 def DateFromTicks(ticks):
39546
1edde2349f78 Make unit tests pass: replace apply() and adapt to different attribute message.
Guido van Rossum <guido@python.org>
parents: 37938
diff changeset
41 return Date(*time.localtime(ticks)[:3])
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
42
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
43 def TimeFromTicks(ticks):
39546
1edde2349f78 Make unit tests pass: replace apply() and adapt to different attribute message.
Guido van Rossum <guido@python.org>
parents: 37938
diff changeset
44 return Time(*time.localtime(ticks)[3:6])
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
45
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
46 def TimestampFromTicks(ticks):
39546
1edde2349f78 Make unit tests pass: replace apply() and adapt to different attribute message.
Guido van Rossum <guido@python.org>
parents: 37938
diff changeset
47 return Timestamp(*time.localtime(ticks)[:6])
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
48
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
49 version_info = tuple([int(x) for x in version.split(".")])
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
50 sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
51
43487
8c10aac37037 Breaking ground for PEP 3137 implementation:
Guido van Rossum <guido@python.org>
parents: 40643
diff changeset
52 Binary = memoryview
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
53
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
54 def register_adapters_and_converters():
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
55 def adapt_date(val):
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
56 return val.isoformat()
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
57
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
58 def adapt_datetime(val):
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
59 return val.isoformat(" ")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
60
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
61 def convert_date(val):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
62 return datetime.date(*map(int, val.split(b"-")))
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
63
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
64 def convert_timestamp(val):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
65 datepart, timepart = val.split(b" ")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
66 year, month, day = map(int, datepart.split(b"-"))
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
67 timepart_full = timepart.split(b".")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
68 hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
69 if len(timepart_full) == 2:
82404
ae25a38e6c17 Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior
Petri Lehtinen <petri@digip.org>
parents: 82349
diff changeset
70 microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
71 else:
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
72 microseconds = 0
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
73
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
74 val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
75 return val
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
76
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
77
37938
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
78 register_adapter(datetime.date, adapt_date)
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
79 register_adapter(datetime.datetime, adapt_datetime)
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
80 register_converter("date", convert_date)
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
81 register_converter("timestamp", convert_timestamp)
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
82
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
83 register_adapters_and_converters()
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
84
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
85 # Clean up namespace
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
86
7da4af95ebad Updated the sqlite3 module to the external pysqlite 2.2.2 version.
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
87 del(register_adapters_and_converters)