QGIS API Documentation 3.27.0-Master (75dc696944)
qgsspatialiteutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsspatialiteutils.cpp
3 -------------------
4 begin : Nov, 2017
5 copyright : (C) 2017 by Matthias Kuhn
6 email : mat[email protected]
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19#include "qgsspatialiteutils.h"
20#include "qgslogger.h"
21
22#include <sqlite3.h>
23
24#ifdef HAVE_SPATIALITE
25#include <spatialite.h>
26#endif
27
28// Define this variable to print all spatialite SQL statements
29#ifdef SPATIALITE_PRINT_ALL_SQL
30// Debugging code
31#include <QDebug>
32#include <QThread>
33static int trace_callback( unsigned, void *ctx, void *p, void * )
34{
35 sqlite3_stmt *stmt = ( sqlite3_stmt * )p;
36 char *sql = sqlite3_expanded_sql( stmt );
37 qDebug() << "SPATIALITE" << QThread::currentThreadId() << ( sqlite3 * ) ctx << sql;
38 sqlite3_free( sql );
39 return 0;
40}
41#endif
42
43
44int spatialite_database_unique_ptr::open( const QString &path )
45{
46#ifdef HAVE_SPATIALITE
47 auto &deleter = get_deleter();
48 deleter.mSpatialiteContext = spatialite_alloc_connection();
49#endif
50
51 sqlite3 *database = nullptr;
52 const int result = sqlite3_open( path.toUtf8(), &database );
53 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
54
55#ifdef HAVE_SPATIALITE
56 if ( result == SQLITE_OK )
57 spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
58#endif
59
60 return result;
61}
62
64{
65 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset();
66}
67
68int spatialite_database_unique_ptr::open_v2( const QString &path, int flags, const char *zVfs )
69{
70#ifdef HAVE_SPATIALITE
71 auto &deleter = get_deleter();
72 deleter.mSpatialiteContext = spatialite_alloc_connection();
73#endif
74
75 sqlite3 *database = nullptr;
76 const int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
77 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
78
79#ifdef HAVE_SPATIALITE
80 if ( result == SQLITE_OK )
81 spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
82#endif
83
84#ifdef SPATIALITE_PRINT_ALL_SQL
85 // Log all queries
86 sqlite3_trace_v2(
87 database,
88 SQLITE_TRACE_STMT,
89 trace_callback,
90 database
91 );
92#endif
93
94 return result;
95}
96
98{
99 return QString( sqlite3_errmsg( get() ) );
100}
101
103{
104 sqlite3_stmt *preparedStatement = nullptr;
105 const char *tail = nullptr;
106 const QByteArray sqlUtf8 = sql.toUtf8();
107 resultCode = sqlite3_prepare( get(), sqlUtf8, sqlUtf8.length(), &preparedStatement, &tail );
109 s.reset( preparedStatement );
110 return s;
111}
112
114{
115 const int res = sqlite3_close_v2( handle );
116 if ( res != SQLITE_OK )
117 {
118 QgsDebugMsg( QStringLiteral( "sqlite3_close_v2() failed: %1" ).arg( res ) );
119 }
120
121#ifdef HAVE_SPATIALITE
122 spatialite_cleanup_ex( mSpatialiteContext );
123#endif
124 mSpatialiteContext = nullptr;
125}
int open(const QString &path)
Opens the database at the specified file path.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode)
Prepares a sql statement, returning the result.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
QString errorMessage() const
Returns the most recent error message encountered by the database.
void reset()
Will close the connection and set the internal pointer to nullptr.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
struct sqlite3 sqlite3
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void operator()(sqlite3 *database)
Closes an spatialite database.
void * mSpatialiteContext
Keep track of the spatialite context.