Skip to content

Commit 5aed037

Browse files
author
jef
committed
add 'internal' pyspatialite
git-svn-id: http://svn.osgeo.org/qgis/trunk@15015 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent fb0e20e commit 5aed037

25 files changed

+5636
-0
lines changed

python/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
SUBDIRS(plugins)
22

33
IF (WITH_INTERNAL_SPATIALITE)
4+
SUBDIRS(pyspatialite)
5+
46
INCLUDE_DIRECTORIES(
57
../src/core/spatialite/headers
68
../src/core/spatialite/headers/spatialite

python/pyspatialite/CMakeLists.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
INCLUDE_DIRECTORIES(
2+
../../src/core/spatialite/headers
3+
../../src/core/spatialite/headers/spatialite
4+
5+
${PYTHON_INCLUDE_PATH}
6+
${GEOS_INCLUDE_DIR}
7+
${PROJ_INCLUDE_DIR}
8+
)
9+
10+
SET(PYSPATIALITE_SRC
11+
src/cache.c
12+
src/connection.c
13+
src/cursor.c
14+
src/microprotocols.c
15+
src/module.c
16+
src/prepare_protocol.c
17+
src/row.c
18+
src/statement.c
19+
src/util.c
20+
)
21+
22+
ADD_DEFINITIONS(-DMODULE_NAME=\\\"spatialite.dbapi2\\\")
23+
24+
IF (CYGWIN OR APPLE)
25+
ADD_LIBRARY(pyspatialite MODULE ${PYSPATIALITE_SRC})
26+
ELSE (CYGWIN OR APPLE)
27+
ADD_LIBRARY(pyspatialite SHARED ${PYSPATIALITE_SRC})
28+
ENDIF (CYGWIN OR APPLE)
29+
30+
IF (NOT APPLE)
31+
TARGET_LINK_LIBRARIES(pyspatialite ${PYTHON_LIBRARY})
32+
ENDIF (NOT APPLE)
33+
34+
TARGET_LINK_LIBRARIES(pyspatialite ${EXTRA_LINK_LIBRARIES})
35+
36+
IF (APPLE)
37+
SET_TARGET_PROPERTIES(pyspatialite PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
38+
ENDIF (APPLE)
39+
40+
SET_TARGET_PROPERTIES(pyspatialite PROPERTIES PREFIX "" OUTPUT_NAME _spatialite)
41+
42+
IF (WIN32)
43+
SET_TARGET_PROPERTIES(pyspatialite PROPERTIES SUFFIX ".pyd")
44+
ENDIF (WIN32)
45+
46+
INSTALL(TARGETS pyspatialite DESTINATION "${PYTHON_SITE_PACKAGES_DIR}/pyspatialite")
47+
INSTALL(FILES
48+
lib/__init__.py
49+
lib/dbapi2.py
50+
lib/dump.py
51+
DESTINATION "${PYTHON_SITE_PACKAGES_DIR}/pyspatialite")

python/pyspatialite/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-2007 Gerhard Häring
2+
3+
This software is provided 'as-is', without any express or implied warranty. In
4+
no event will the authors be held liable for any damages arising from the use
5+
of this software.
6+
7+
Permission is granted to anyone to use this software for any purpose,
8+
including commercial applications, and to alter it and redistribute it freely,
9+
subject to the following restrictions:
10+
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software in
13+
a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
16+
2. Altered source versions must be plainly marked as such, and must not be
17+
misrepresented as being the original software.
18+
19+
3. This notice may not be removed or altered from any source distribution.

python/pyspatialite/lib/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#-*- coding: ISO-8859-1 -*-
2+
# pysqlite2/__init__.py: the pysqlite2 package.
3+
#
4+
# Copyright (C) 2005-2007 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.

python/pyspatialite/lib/dbapi2.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#-*- coding: ISO-8859-1 -*-
2+
# pyspatialite/dbapi2.py: the DB-API 2.0 interface
3+
#
4+
# Copyright (C) 2004-2007 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+
24+
import datetime
25+
import time
26+
27+
from pyspatialite._spatialite import *
28+
29+
paramstyle = "qmark"
30+
31+
threadsafety = 1
32+
33+
apilevel = "2.0"
34+
35+
Date = datetime.date
36+
37+
Time = datetime.time
38+
39+
Timestamp = datetime.datetime
40+
41+
def DateFromTicks(ticks):
42+
return Date(*time.localtime(ticks)[:3])
43+
44+
def TimeFromTicks(ticks):
45+
return Time(*time.localtime(ticks)[3:6])
46+
47+
def TimestampFromTicks(ticks):
48+
return Timestamp(*time.localtime(ticks)[:6])
49+
50+
version_info = tuple([int(x) for x in version.split(".")])
51+
sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
52+
53+
Binary = buffer
54+
55+
def register_adapters_and_converters():
56+
def adapt_date(val):
57+
return val.isoformat()
58+
59+
def adapt_datetime(val):
60+
return val.isoformat(" ")
61+
62+
def convert_date(val):
63+
return datetime.date(*map(int, val.split("-")))
64+
65+
def convert_timestamp(val):
66+
datepart, timepart = val.split(" ")
67+
year, month, day = map(int, datepart.split("-"))
68+
timepart_full = timepart.split(".")
69+
hours, minutes, seconds = map(int, timepart_full[0].split(":"))
70+
if len(timepart_full) == 2:
71+
microseconds = int(timepart_full[1])
72+
else:
73+
microseconds = 0
74+
75+
val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
76+
return val
77+
78+
79+
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+
84+
register_adapters_and_converters()
85+
86+
# Clean up namespace
87+
88+
del(register_adapters_and_converters)

python/pyspatialite/lib/dump.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Mimic the sqlite3 console shell's .dump command
2+
# Author: Paul Kippes <kippesp@gmail.com>
3+
4+
def _iterdump(connection):
5+
"""
6+
Returns an iterator to the dump of the database in an SQL text format.
7+
8+
Used to produce an SQL dump of the database. Useful to save an in-memory
9+
database for later restoration. This function should not be called
10+
directly but instead called from the Connection method, iterdump().
11+
"""
12+
13+
cu = connection.cursor()
14+
yield('BEGIN TRANSACTION;')
15+
16+
# sqlite_master table contains the SQL CREATE statements for the database.
17+
q = """
18+
SELECT name, type, sql
19+
FROM sqlite_master
20+
WHERE sql NOT NULL AND
21+
type == 'table'
22+
"""
23+
schema_res = cu.execute(q)
24+
for table_name, type, sql in schema_res.fetchall():
25+
if table_name == 'sqlite_sequence':
26+
yield('DELETE FROM sqlite_sequence;')
27+
elif table_name == 'sqlite_stat1':
28+
yield('ANALYZE sqlite_master;')
29+
elif table_name.startswith('sqlite_'):
30+
continue
31+
# NOTE: Virtual table support not implemented
32+
#elif sql.startswith('CREATE VIRTUAL TABLE'):
33+
# qtable = table_name.replace("'", "''")
34+
# yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
35+
# "VALUES('table','%s','%s',0,'%s');" %
36+
# qtable,
37+
# qtable,
38+
# sql.replace("''"))
39+
else:
40+
yield('%s;' % sql)
41+
42+
# Build the insert statement for each row of the current table
43+
res = cu.execute("PRAGMA table_info('%s')" % table_name)
44+
column_names = [str(table_info[1]) for table_info in res.fetchall()]
45+
q = "SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("
46+
q += ",".join(["'||quote(" + col + ")||'" for col in column_names])
47+
q += ")' FROM '%(tbl_name)s'"
48+
query_res = cu.execute(q % {'tbl_name': table_name})
49+
for row in query_res:
50+
yield("%s;" % row[0])
51+
52+
# Now when the type is 'index', 'trigger', or 'view'
53+
q = """
54+
SELECT name, type, sql
55+
FROM sqlite_master
56+
WHERE sql NOT NULL AND
57+
type IN ('index', 'trigger', 'view')
58+
"""
59+
schema_res = cu.execute(q)
60+
for name, type, sql in schema_res.fetchall():
61+
yield('%s;' % sql)
62+
63+
yield('COMMIT;')

0 commit comments

Comments
 (0)