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