QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
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
21#include <sqlite3.h>
22
23#include "qgslogger.h"
24
25#ifdef HAVE_SPATIALITE
26#include <spatialite.h>
27#endif
28
29// Define this variable to print all spatialite SQL statements
30#ifdef SPATIALITE_PRINT_ALL_SQL
31// Debugging code
32#include <QDebug>
33#include <QThread>
34static int trace_callback( unsigned, void *ctx, void *p, void * )
35{
36 sqlite3_stmt *stmt = ( sqlite3_stmt * )p;
37 char *sql = sqlite3_expanded_sql( stmt );
38 qDebug() << "SPATIALITE" << QThread::currentThreadId() << ( sqlite3 * ) ctx << sql;
39 sqlite3_free( sql );
40 return 0;
41}
42#endif
43
44
45int spatialite_database_unique_ptr::open( const QString &path )
46{
47#ifdef HAVE_SPATIALITE
48 auto &deleter = get_deleter();
49 deleter.mSpatialiteContext = spatialite_alloc_connection();
50#endif
51
52 sqlite3 *database = nullptr;
53 const int result = sqlite3_open( path.toUtf8(), &database );
54 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
55
56#ifdef HAVE_SPATIALITE
57 if ( result == SQLITE_OK )
58 spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
59#endif
60
61 return result;
62}
63
65{
66 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset();
67}
68
69int spatialite_database_unique_ptr::open_v2( const QString &path, int flags, const char *zVfs )
70{
71#ifdef HAVE_SPATIALITE
72 auto &deleter = get_deleter();
73 deleter.mSpatialiteContext = spatialite_alloc_connection();
74#endif
75
76 sqlite3 *database = nullptr;
77 const int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
78 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
79
80#ifdef HAVE_SPATIALITE
81 if ( result == SQLITE_OK )
82 spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
83#endif
84
85#ifdef SPATIALITE_PRINT_ALL_SQL
86 // Log all queries
87 sqlite3_trace_v2(
88 database,
89 SQLITE_TRACE_STMT,
90 trace_callback,
91 database
92 );
93#endif
94
95 return result;
96}
97
99{
100 return QString( sqlite3_errmsg( get() ) );
101}
102
104{
105 sqlite3_stmt *preparedStatement = nullptr;
106 const char *tail = nullptr;
107 const QByteArray sqlUtf8 = sql.toUtf8();
108 resultCode = sqlite3_prepare( get(), sqlUtf8, sqlUtf8.length(), &preparedStatement, &tail );
110 s.reset( preparedStatement );
111 return s;
112}
113
115{
116 const int res = sqlite3_close_v2( handle );
117 if ( res != SQLITE_OK )
118 {
119 QgsDebugError( QStringLiteral( "sqlite3_close_v2() failed: %1" ).arg( res ) );
120 }
121
122#ifdef HAVE_SPATIALITE
123 spatialite_cleanup_ex( mSpatialiteContext );
124#endif
125 mSpatialiteContext = nullptr;
126}
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 QgsDebugError(str)
Definition qgslogger.h:57
void operator()(sqlite3 *database)
Closes an spatialite database.
void * mSpatialiteContext
Keep track of the spatialite context.