QGIS API Documentation  3.0.2-Girona (307d082)
qgssqliteutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssqliteutils.cpp
3  -------------------
4  begin : Nov, 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
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 #include "qgssqliteutils.h"
19 
20 #include <sqlite3.h>
21 
23 {
24  sqlite3_close_v2( database );
25 }
26 
27 void QgsSqlite3StatementFinalizer::operator()( sqlite3_stmt *statement )
28 {
29  sqlite3_finalize( statement );
30 }
31 
33 {
34  return sqlite3_step( get() );
35 }
36 
37 QString sqlite3_statement_unique_ptr::columnName( int column ) const
38 {
39  return QString::fromUtf8( static_cast<const char *>( sqlite3_column_name( get(), column ) ) );
40 }
41 
43 {
44  return sqlite3_column_double( get(), column );
45 }
46 
48 {
49  return sqlite3_column_count( get() );
50 }
51 
52 QString sqlite3_statement_unique_ptr::columnAsText( int column ) const
53 {
54  return QString::fromUtf8( reinterpret_cast<const char *>( sqlite3_column_text( get(), column ) ) );
55 }
56 
57 qlonglong sqlite3_statement_unique_ptr::columnAsInt64( int column ) const
58 {
59  return sqlite3_column_int64( get(), column );
60 }
61 
62 int sqlite3_database_unique_ptr::open( const QString &path )
63 {
64  sqlite3 *database = nullptr;
65  int result = sqlite3_open( path.toUtf8(), &database );
66  reset( database );
67  return result;
68 }
69 
70 int sqlite3_database_unique_ptr::open_v2( const QString &path, int flags, const char *zVfs )
71 {
72  sqlite3 *database = nullptr;
73  int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
74  reset( database );
75  return result;
76 }
77 
79 {
80  return QString( sqlite3_errmsg( get() ) );
81 }
82 
83 sqlite3_statement_unique_ptr sqlite3_database_unique_ptr::prepare( const QString &sql, int &resultCode ) const
84 {
85  sqlite3_stmt *preparedStatement = nullptr;
86  const char *tail = nullptr;
87  resultCode = sqlite3_prepare( get(), sql.toUtf8(), sql.toUtf8().length(), &preparedStatement, &tail );
89  s.reset( preparedStatement );
90  return s;
91 }
void operator()(sqlite3_stmt *statement)
Finalizes an sqlite3 statement.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QString errorMessage() const
Returns the most recent error message encountered by the database.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
int columnCount() const
Get the number of columns that this statement returns.
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
struct sqlite3 sqlite3
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
void operator()(sqlite3 *database)
Closes an sqlite database.
QString columnName(int column) const
Returns the name of column.
double columnAsDouble(int column) const
Get column value from the current statement row as a double.
qlonglong columnAsInt64(int column) const
Get column value from the current statement row as a long long integer (64 bits). ...