QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 : [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 #include <spatialite.h>
24 
25 int spatialite_database_unique_ptr::open( const QString &path )
26 {
27  auto &deleter = get_deleter();
28  deleter.mSpatialiteContext = spatialite_alloc_connection();
29 
30  sqlite3 *database = nullptr;
31  int result = sqlite3_open( path.toUtf8(), &database );
32  std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
33 
34  if ( result == SQLITE_OK )
35  spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
36 
37  return result;
38 }
39 
41 {
42  std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset();
43 }
44 
45 int spatialite_database_unique_ptr::open_v2( const QString &path, int flags, const char *zVfs )
46 {
47  auto &deleter = get_deleter();
48  deleter.mSpatialiteContext = spatialite_alloc_connection();
49 
50  sqlite3 *database = nullptr;
51  int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
52  std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
53 
54  if ( result == SQLITE_OK )
55  spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
56 
57  return result;
58 }
59 
61 {
62  return QString( sqlite3_errmsg( get() ) );
63 }
64 
66 {
67  sqlite3_stmt *preparedStatement = nullptr;
68  const char *tail = nullptr;
69  const QByteArray sqlUtf8 = sql.toUtf8();
70  resultCode = sqlite3_prepare( get(), sqlUtf8, sqlUtf8.length(), &preparedStatement, &tail );
72  s.reset( preparedStatement );
73  return s;
74 }
75 
77 {
78  int res = sqlite3_close_v2( handle );
79  if ( res != SQLITE_OK )
80  {
81  QgsDebugMsg( QStringLiteral( "sqlite3_close_v2() failed: %1" ).arg( res ) );
82  }
83 
84  spatialite_cleanup_ex( mSpatialiteContext );
85  mSpatialiteContext = nullptr;
86 }
int open(const QString &path)
Opens the database at the specified file path.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
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.
struct sqlite3 sqlite3
void operator()(sqlite3 *database)
Closes an spatialite database.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode)
Prepares a sql statement, returning the result.