/
qgsslconnect.cpp
87 lines (70 loc) · 2.37 KB
/
qgsslconnect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/***************************************************************************
qgsslconnect.cpp - thin wrapper class to connect to spatialite databases
----------------------
begin : May 2015
copyright : (C) 2015 by Jürgen fischer
email : jef at norbit dot de
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsslconnect.h"
#include <sqlite3.h>
#include <spatialite.h>
QHash<sqlite3 *, void *> QgsSLConnect::mSLconns;
int QgsSLConnect::sqlite3_open( const char *filename, sqlite3 **ppDb )
{
#if defined(SPATIALITE_VERSION_GE_4_0_0)
void *conn = spatialite_alloc_connection();
#else
spatialite_init( 0 );
#endif
int res = ::sqlite3_open( filename, ppDb );
#if defined(SPATIALITE_VERSION_GE_4_0_0)
if ( res == SQLITE_OK )
{
spatialite_init_ex( *ppDb, conn, 0 );
mSLconns.insert( *ppDb, conn );
}
#endif
return res;
}
int QgsSLConnect::sqlite3_close( sqlite3 *db )
{
int res = ::sqlite3_close( db );
#if defined(SPATIALITE_VERSION_GE_4_0_0)
if ( mSLconns.contains( db ) )
spatialite_cleanup_ex( mSLconns.take( db ) );
#endif
return res;
}
int QgsSLConnect::sqlite3_open_v2( const char *filename, sqlite3 **ppDb, int flags, const char *zVfs )
{
#if defined(SPATIALITE_VERSION_GE_4_0_0)
void *conn = spatialite_alloc_connection();
#else
spatialite_init( 0 );
#endif
int res = ::sqlite3_open_v2( filename, ppDb, flags, zVfs );
#if defined(SPATIALITE_VERSION_GE_4_0_0)
if ( res == SQLITE_OK )
{
spatialite_init_ex( *ppDb, conn, 0 );
mSLconns.insert( *ppDb, conn );
}
#endif
return res;
}
int QgsSLConnect::sqlite3_close_v2( sqlite3 *db )
{
int res = ::sqlite3_close( db );
#if defined(SPATIALITE_VERSION_GE_4_0_0)
if ( mSLconns.contains( db ) )
spatialite_cleanup_ex( mSLconns.take( db ) );
#endif
return res;
}