QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #include <cstdarg>
22 
24 {
25  sqlite3_close_v2( database );
26 }
27 
28 void QgsSqlite3StatementFinalizer::operator()( sqlite3_stmt *statement )
29 {
30  sqlite3_finalize( statement );
31 }
32 
34 {
35  return sqlite3_step( get() );
36 }
37 
38 QString sqlite3_statement_unique_ptr::columnName( int column ) const
39 {
40  return QString::fromUtf8( static_cast<const char *>( sqlite3_column_name( get(), column ) ) );
41 }
42 
44 {
45  return sqlite3_column_double( get(), column );
46 }
47 
49 {
50  return sqlite3_column_count( get() );
51 }
52 
53 QString sqlite3_statement_unique_ptr::columnAsText( int column ) const
54 {
55  return QString::fromUtf8( reinterpret_cast<const char *>( sqlite3_column_text( get(), column ) ) );
56 }
57 
58 qlonglong sqlite3_statement_unique_ptr::columnAsInt64( int column ) const
59 {
60  return sqlite3_column_int64( get(), column );
61 }
62 
63 int sqlite3_database_unique_ptr::open( const QString &path )
64 {
65  sqlite3 *database = nullptr;
66  int result = sqlite3_open( path.toUtf8(), &database );
67  reset( database );
68  return result;
69 }
70 
71 int sqlite3_database_unique_ptr::open_v2( const QString &path, int flags, const char *zVfs )
72 {
73  sqlite3 *database = nullptr;
74  int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
75  reset( database );
76  return result;
77 }
78 
80 {
81  return QString( sqlite3_errmsg( get() ) );
82 }
83 
84 sqlite3_statement_unique_ptr sqlite3_database_unique_ptr::prepare( const QString &sql, int &resultCode ) const
85 {
86  sqlite3_stmt *preparedStatement = nullptr;
87  const char *tail = nullptr;
88  resultCode = sqlite3_prepare( get(), sql.toUtf8(), sql.toUtf8().length(), &preparedStatement, &tail );
90  s.reset( preparedStatement );
91  return s;
92 }
93 
94 int sqlite3_database_unique_ptr::exec( const QString &sql, QString &errorMessage ) const
95 {
96  char *errMsg;
97 
98  int ret = sqlite3_exec( get(), sql.toUtf8(), nullptr, nullptr, &errMsg );
99 
100  if ( errMsg )
101  {
102  errorMessage = QString::fromUtf8( errMsg );
103  sqlite3_free( errMsg );
104  }
105 
106  return ret;
107 }
108 
109 QString QgsSqliteUtils::quotedString( const QString &value )
110 {
111  if ( value.isNull() )
112  return QStringLiteral( "NULL" );
113 
114  QString v = value;
115  v.replace( '\'', QLatin1String( "''" ) );
116  return v.prepend( '\'' ).append( '\'' );
117 }
118 
119 QString QgsSqlite3Mprintf( const char *format, ... )
120 {
121  va_list ap;
122  va_start( ap, format );
123  char *c_str = sqlite3_vmprintf( format, ap );
124  va_end( ap );
125  QString res( QString::fromUtf8( c_str ) );
126  sqlite3_free( c_str );
127  return res;
128 }
QString columnName(int column) const
Returns the name of column.
void operator()(sqlite3_stmt *statement)
Finalizes an sqlite3 statement.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
int exec(const QString &sql, QString &errorMessage) const
Executes the sql command in the database.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
qlonglong columnAsInt64(int column) const
Gets column value from the current statement row as a long long integer (64 bits).
double columnAsDouble(int column) const
Gets column value from the current statement row as a double.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
int columnCount() const
Gets the number of columns that this statement returns.
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 QgsSqlite3Mprintf(const char *format,...)
Wraps sqlite3_mprintf() by automatically freeing the memory.
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
QString errorMessage() const
Returns the most recent error message encountered by the database.
static QString quotedString(const QString &value)
Returns a quoted string value, surround by &#39; characters and with special characters correctly escaped...