QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
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 : matthias@opengis.ch
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
#include <QString>
26
27
using namespace
Qt::StringLiterals;
28
29
#ifdef HAVE_SPATIALITE
30
#include <spatialite.h>
31
#endif
32
33
// Define this variable to print all spatialite SQL statements
34
#ifdef SPATIALITE_PRINT_ALL_SQL
35
// Debugging code
36
#include <QDebug>
37
#include <QThread>
38
static
int
trace_callback(
unsigned
,
void
*ctx,
void
*p,
void
* )
39
{
40
sqlite3_stmt *stmt = ( sqlite3_stmt * ) p;
41
char
*sql = sqlite3_expanded_sql( stmt );
42
qDebug() <<
"SPATIALITE"
<< QThread::currentThreadId() << (
sqlite3
* ) ctx << sql;
43
sqlite3_free( sql );
44
return
0;
45
}
46
#endif
47
48
49
int
spatialite_database_unique_ptr::open
(
const
QString &path )
50
{
51
#ifdef HAVE_SPATIALITE
52
auto
&deleter = get_deleter();
53
deleter.mSpatialiteContext = spatialite_alloc_connection();
54
#endif
55
56
sqlite3
*database =
nullptr
;
57
const
int
result = sqlite3_open( path.toUtf8(), &database );
58
std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
59
60
#ifdef HAVE_SPATIALITE
61
if
( result == SQLITE_OK )
62
spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
63
#endif
64
65
return
result;
66
}
67
68
void
spatialite_database_unique_ptr::reset
()
69
{
70
std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset();
71
}
72
73
int
spatialite_database_unique_ptr::open_v2
(
const
QString &path,
int
flags,
const
char
*zVfs )
74
{
75
#ifdef HAVE_SPATIALITE
76
auto
&deleter = get_deleter();
77
deleter.mSpatialiteContext = spatialite_alloc_connection();
78
#endif
79
80
sqlite3
*database =
nullptr
;
81
const
int
result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
82
std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
83
84
#ifdef HAVE_SPATIALITE
85
if
( result == SQLITE_OK )
86
spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
87
#endif
88
89
#ifdef SPATIALITE_PRINT_ALL_SQL
90
// Log all queries
91
sqlite3_trace_v2( database, SQLITE_TRACE_STMT, trace_callback, database );
92
#endif
93
94
return
result;
95
}
96
97
QString
spatialite_database_unique_ptr::errorMessage
()
const
98
{
99
return
QString( sqlite3_errmsg( get() ) );
100
}
101
102
sqlite3_statement_unique_ptr
spatialite_database_unique_ptr::prepare
(
const
QString &sql,
int
&resultCode )
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 );
108
sqlite3_statement_unique_ptr
s;
109
s.reset( preparedStatement );
110
return
s;
111
}
112
113
void
QgsSpatialiteCloser::operator()
(
sqlite3
*handle )
114
{
115
const
int
res = sqlite3_close_v2( handle );
116
if
( res != SQLITE_OK )
117
{
118
QgsDebugError
( u
"sqlite3_close_v2() failed: %1"
_s.arg( res ) );
119
}
120
121
#ifdef HAVE_SPATIALITE
122
spatialite_cleanup_ex(
mSpatialiteContext
);
123
#endif
124
mSpatialiteContext
=
nullptr
;
125
}
spatialite_database_unique_ptr::open
int open(const QString &path)
Opens the database at the specified file path.
Definition
qgsspatialiteutils.cpp:49
spatialite_database_unique_ptr::prepare
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode)
Prepares a sql statement, returning the result.
Definition
qgsspatialiteutils.cpp:102
spatialite_database_unique_ptr::open_v2
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Definition
qgsspatialiteutils.cpp:73
spatialite_database_unique_ptr::errorMessage
QString errorMessage() const
Returns the most recent error message encountered by the database.
Definition
qgsspatialiteutils.cpp:97
spatialite_database_unique_ptr::reset
void reset()
Will close the connection and set the internal pointer to nullptr.
Definition
qgsspatialiteutils.cpp:68
sqlite3_statement_unique_ptr
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
Definition
qgssqliteutils.h:67
sqlite3
struct sqlite3 sqlite3
Definition
qgscoordinatereferencesystem.h:61
qgslogger.h
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
qgsspatialiteutils.h
QgsSpatialiteCloser::operator()
void operator()(sqlite3 *database)
Closes an spatialite database.
Definition
qgsspatialiteutils.cpp:113
QgsSpatialiteCloser::mSpatialiteContext
void * mSpatialiteContext
Keep track of the spatialite context.
Definition
qgsspatialiteutils.h:45
Generated on
for QGIS API Documentation by
1.17.0