Skip to content

Commit

Permalink
add 'internal' pyspatialite
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@15015 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jan 10, 2011
1 parent fb0e20e commit 5aed037
Show file tree
Hide file tree
Showing 25 changed files with 5,636 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/CMakeLists.txt
@@ -1,6 +1,8 @@
SUBDIRS(plugins)

IF (WITH_INTERNAL_SPATIALITE)
SUBDIRS(pyspatialite)

INCLUDE_DIRECTORIES(
../src/core/spatialite/headers
../src/core/spatialite/headers/spatialite
Expand Down
51 changes: 51 additions & 0 deletions python/pyspatialite/CMakeLists.txt
@@ -0,0 +1,51 @@
INCLUDE_DIRECTORIES(
../../src/core/spatialite/headers
../../src/core/spatialite/headers/spatialite

${PYTHON_INCLUDE_PATH}
${GEOS_INCLUDE_DIR}
${PROJ_INCLUDE_DIR}
)

SET(PYSPATIALITE_SRC
src/cache.c
src/connection.c
src/cursor.c
src/microprotocols.c
src/module.c
src/prepare_protocol.c
src/row.c
src/statement.c
src/util.c
)

ADD_DEFINITIONS(-DMODULE_NAME=\\\"spatialite.dbapi2\\\")

IF (CYGWIN OR APPLE)
ADD_LIBRARY(pyspatialite MODULE ${PYSPATIALITE_SRC})
ELSE (CYGWIN OR APPLE)
ADD_LIBRARY(pyspatialite SHARED ${PYSPATIALITE_SRC})
ENDIF (CYGWIN OR APPLE)

IF (NOT APPLE)
TARGET_LINK_LIBRARIES(pyspatialite ${PYTHON_LIBRARY})
ENDIF (NOT APPLE)

TARGET_LINK_LIBRARIES(pyspatialite ${EXTRA_LINK_LIBRARIES})

IF (APPLE)
SET_TARGET_PROPERTIES(pyspatialite PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
ENDIF (APPLE)

SET_TARGET_PROPERTIES(pyspatialite PROPERTIES PREFIX "" OUTPUT_NAME _spatialite)

IF (WIN32)
SET_TARGET_PROPERTIES(pyspatialite PROPERTIES SUFFIX ".pyd")
ENDIF (WIN32)

INSTALL(TARGETS pyspatialite DESTINATION "${PYTHON_SITE_PACKAGES_DIR}/pyspatialite")
INSTALL(FILES
lib/__init__.py
lib/dbapi2.py
lib/dump.py
DESTINATION "${PYTHON_SITE_PACKAGES_DIR}/pyspatialite")
19 changes: 19 additions & 0 deletions python/pyspatialite/LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2004-2007 Gerhard Häring

This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use
of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.
22 changes: 22 additions & 0 deletions python/pyspatialite/lib/__init__.py
@@ -0,0 +1,22 @@
#-*- coding: ISO-8859-1 -*-
# pysqlite2/__init__.py: the pysqlite2 package.
#
# Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
#
# This file is part of pysqlite.
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
88 changes: 88 additions & 0 deletions python/pyspatialite/lib/dbapi2.py
@@ -0,0 +1,88 @@
#-*- coding: ISO-8859-1 -*-
# pyspatialite/dbapi2.py: the DB-API 2.0 interface
#
# Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
#
# This file is part of pysqlite.
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

import datetime
import time

from pyspatialite._spatialite import *

paramstyle = "qmark"

threadsafety = 1

apilevel = "2.0"

Date = datetime.date

Time = datetime.time

Timestamp = datetime.datetime

def DateFromTicks(ticks):
return Date(*time.localtime(ticks)[:3])

def TimeFromTicks(ticks):
return Time(*time.localtime(ticks)[3:6])

def TimestampFromTicks(ticks):
return Timestamp(*time.localtime(ticks)[:6])

version_info = tuple([int(x) for x in version.split(".")])
sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])

Binary = buffer

def register_adapters_and_converters():
def adapt_date(val):
return val.isoformat()

def adapt_datetime(val):
return val.isoformat(" ")

def convert_date(val):
return datetime.date(*map(int, val.split("-")))

def convert_timestamp(val):
datepart, timepart = val.split(" ")
year, month, day = map(int, datepart.split("-"))
timepart_full = timepart.split(".")
hours, minutes, seconds = map(int, timepart_full[0].split(":"))
if len(timepart_full) == 2:
microseconds = int(timepart_full[1])
else:
microseconds = 0

val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
return val


register_adapter(datetime.date, adapt_date)
register_adapter(datetime.datetime, adapt_datetime)
register_converter("date", convert_date)
register_converter("timestamp", convert_timestamp)

register_adapters_and_converters()

# Clean up namespace

del(register_adapters_and_converters)
63 changes: 63 additions & 0 deletions python/pyspatialite/lib/dump.py
@@ -0,0 +1,63 @@
# Mimic the sqlite3 console shell's .dump command
# Author: Paul Kippes <kippesp@gmail.com>

def _iterdump(connection):
"""
Returns an iterator to the dump of the database in an SQL text format.
Used to produce an SQL dump of the database. Useful to save an in-memory
database for later restoration. This function should not be called
directly but instead called from the Connection method, iterdump().
"""

cu = connection.cursor()
yield('BEGIN TRANSACTION;')

# sqlite_master table contains the SQL CREATE statements for the database.
q = """
SELECT name, type, sql
FROM sqlite_master
WHERE sql NOT NULL AND
type == 'table'
"""
schema_res = cu.execute(q)
for table_name, type, sql in schema_res.fetchall():
if table_name == 'sqlite_sequence':
yield('DELETE FROM sqlite_sequence;')
elif table_name == 'sqlite_stat1':
yield('ANALYZE sqlite_master;')
elif table_name.startswith('sqlite_'):
continue
# NOTE: Virtual table support not implemented
#elif sql.startswith('CREATE VIRTUAL TABLE'):
# qtable = table_name.replace("'", "''")
# yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
# "VALUES('table','%s','%s',0,'%s');" %
# qtable,
# qtable,
# sql.replace("''"))
else:
yield('%s;' % sql)

# Build the insert statement for each row of the current table
res = cu.execute("PRAGMA table_info('%s')" % table_name)
column_names = [str(table_info[1]) for table_info in res.fetchall()]
q = "SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("
q += ",".join(["'||quote(" + col + ")||'" for col in column_names])
q += ")' FROM '%(tbl_name)s'"
query_res = cu.execute(q % {'tbl_name': table_name})
for row in query_res:
yield("%s;" % row[0])

# Now when the type is 'index', 'trigger', or 'view'
q = """
SELECT name, type, sql
FROM sqlite_master
WHERE sql NOT NULL AND
type IN ('index', 'trigger', 'view')
"""
schema_res = cu.execute(q)
for name, type, sql in schema_res.fetchall():
yield('%s;' % sql)

yield('COMMIT;')

0 comments on commit 5aed037

Please sign in to comment.