26#include <spatialite.h>
30#ifdef SPATIALITE_PRINT_ALL_SQL
34static int trace_callback(
unsigned,
void *ctx,
void *p,
void * )
36 sqlite3_stmt *stmt = ( sqlite3_stmt * )p;
37 char *sql = sqlite3_expanded_sql( stmt );
38 qDebug() <<
"SPATIALITE" << QThread::currentThreadId() << (
sqlite3 * ) ctx << sql;
48 auto &deleter = get_deleter();
49 deleter.mSpatialiteContext = spatialite_alloc_connection();
53 const int result = sqlite3_open( path.toUtf8(), &database );
54 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
57 if ( result == SQLITE_OK )
58 spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
66 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset();
72 auto &deleter = get_deleter();
73 deleter.mSpatialiteContext = spatialite_alloc_connection();
77 const int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
78 std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );
81 if ( result == SQLITE_OK )
82 spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
85#ifdef SPATIALITE_PRINT_ALL_SQL
100 return QString( sqlite3_errmsg( get() ) );
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 );
116 const int res = sqlite3_close_v2( handle );
117 if ( res != SQLITE_OK )
119 QgsDebugError( QStringLiteral(
"sqlite3_close_v2() failed: %1" ).arg( res ) );
122#ifdef HAVE_SPATIALITE
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 ...
#define QgsDebugError(str)
void operator()(sqlite3 *database)
Closes an spatialite database.
void * mSpatialiteContext
Keep track of the spatialite context.