36 sqlite3_close_v2( database );
41 sqlite3_finalize( statement );
46 return sqlite3_step( get() );
51 return QString::fromUtf8(
static_cast<const char *
>( sqlite3_column_name( get(), column ) ) );
56 return sqlite3_column_double( get(), column );
61 return sqlite3_column_count( get() );
66 return QString::fromUtf8(
reinterpret_cast<const char *
>( sqlite3_column_text( get(), column ) ) );
71 const void *blob = sqlite3_column_blob( get(), column );
72 const int size = sqlite3_column_bytes( get(), column );
73 return QByteArray(
reinterpret_cast<const char *
>( blob ), size );
78 return sqlite3_column_int64( get(), column );
84 const int result = sqlite3_open( path.toUtf8(), &database );
92 const int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
99 return QString( sqlite3_errmsg( get() ) );
104 sqlite3_stmt *preparedStatement =
nullptr;
105 const char *tail =
nullptr;
106 resultCode = sqlite3_prepare( get(), sql.toUtf8(), sql.toUtf8().length(), &preparedStatement, &tail );
108 s.reset( preparedStatement );
116 const int ret = sqlite3_exec( get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg );
121 sqlite3_free( errMsg );
129 QSet<QString> uniqueFieldsResults;
130 char *zErrMsg =
nullptr;
131 std::vector<std::string> rows;
132 const QByteArray tableNameUtf8 = tableName.toUtf8();
134 "where type='table' and name='%q'", tableNameUtf8.constData() );
141 static_cast<std::vector<std::string>*
>( data )->push_back( argv[0] );
145 int rc = sqlite3_exec( connection, sql.toUtf8(), cb, (
void * )&rows, &zErrMsg );
146 if ( rc != SQLITE_OK )
148 errorMessage = zErrMsg;
149 sqlite3_free( zErrMsg );
150 return uniqueFieldsResults;
154 std::smatch uniqueFieldMatch;
155 static const std::regex sFieldIdentifierRe { R
"raw(\s*(["`]([^"`]+)["`])|(([^\s]+)\s).*)raw" };
156 for (
auto tableDefinition : rows )
158 tableDefinition = tableDefinition.substr( tableDefinition.find(
'(' ), tableDefinition.rfind(
')' ) );
159 std::stringstream tableDefinitionStream { tableDefinition };
160 while ( tableDefinitionStream.good() )
162 std::string fieldStr;
163 std::getline( tableDefinitionStream, fieldStr,
',' );
164 std::string upperCaseFieldStr { fieldStr };
165 std::transform( upperCaseFieldStr.begin(), upperCaseFieldStr.end(), upperCaseFieldStr.begin(), ::toupper );
166 if ( upperCaseFieldStr.find(
"UNIQUE" ) != std::string::npos )
168 if ( std::regex_search( fieldStr, uniqueFieldMatch, sFieldIdentifierRe ) )
170 const std::string quoted { uniqueFieldMatch.str( 2 ) };
171 uniqueFieldsResults.insert( QString::fromStdString( quoted.length() ? quoted : uniqueFieldMatch.str( 4 ) ) );
180 " tbl_name='%q' AND sql LIKE 'CREATE UNIQUE INDEX%%'", tableNameUtf8.constData() );
181 rc = sqlite3_exec( connection, sql.toUtf8(), cb, (
void * )&rows, &zErrMsg );
182 if ( rc != SQLITE_OK )
184 errorMessage = zErrMsg;
185 sqlite3_free( zErrMsg );
186 return uniqueFieldsResults;
189 if ( rows.size() > 0 )
191 static const std::regex sFieldIndexIdentifierRe { R
"raw(\(\s*[`"]?([^",`\)]+)["`]?\s*\))raw" };
192 for (
auto indexDefinition : rows )
194 std::string upperCaseIndexDefinition { indexDefinition };
195 std::transform( upperCaseIndexDefinition.begin(), upperCaseIndexDefinition.end(), upperCaseIndexDefinition.begin(), ::toupper );
196 if ( upperCaseIndexDefinition.find(
"UNIQUE" ) != std::string::npos )
198 indexDefinition = indexDefinition.substr( indexDefinition.find(
'(' ), indexDefinition.rfind(
')' ) );
199 if ( std::regex_search( indexDefinition, uniqueFieldMatch, sFieldIndexIdentifierRe ) )
201 uniqueFieldsResults.insert( QString::fromStdString( uniqueFieldMatch.str( 1 ) ) );
206 return uniqueFieldsResults;
211 long long result { -1 };
216 dsPtr.reset( connection );
222 .arg( quotedTableName ), resultCode )};
223 if ( resultCode == SQLITE_OK && stmt.
step() )
225 result = sqlite3_column_int64( stmt.get(), 0 );
227 if ( sqlite3_column_count( stmt.get() ) == 0 )
229 dsPtr.
exec( QStringLiteral(
"INSERT INTO sqlite_sequence (name, seq) VALUES (%1, 1)" ).arg( quotedTableName ), errorMessage );
230 if ( errorMessage.isEmpty() )
236 errorMessage = QObject::tr(
"Error retrieving default value for %1" ).arg( tableName );
241 if ( dsPtr.
exec( QStringLiteral(
"UPDATE sqlite_sequence SET seq = %1 WHERE name = %2" )
242 .arg( QString::number( ++result ), quotedTableName ),
243 errorMessage ) != SQLITE_OK )
245 errorMessage = QObject::tr(
"Error retrieving default value for %1" ).arg( tableName );
252 ( void )dsPtr.release();
258 if ( value.isNull() )
259 return QStringLiteral(
"NULL" );
262 v.replace(
'\'', QLatin1String(
"''" ) );
263 return v.prepend(
'\'' ).append(
'\'' );
268 QString id( identifier );
269 id.replace(
'\"', QLatin1String(
"\"\"" ) );
270 return id.prepend(
'\"' ).append(
'\"' );
276 return QStringLiteral(
"NULL" );
278 switch ( value.userType() )
280 case QMetaType::Type::Int:
281 case QMetaType::Type::LongLong:
282 case QMetaType::Type::Double:
283 return value.toString();
285 case QMetaType::Type::Bool:
287 return value.toBool() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" );
290 case QMetaType::Type::QString:
291 QString v = value.toString();
296 return v.replace(
'\'', QLatin1String(
"''" ) ).prepend(
'\'' ).append(
'\'' );
302 return QStringList() << QStringLiteral(
"ElementaryGeometries" ) << QStringLiteral(
"SpatialIndex" )
303 << QStringLiteral(
"geom_cols_ref_sys" ) << QStringLiteral(
"geometry_columns" )
304 << QStringLiteral(
"geometry_columns_auth" ) << QStringLiteral(
"geometry_columns_field_infos" )
305 << QStringLiteral(
"geometry_columns_statistics" ) << QStringLiteral(
"geometry_columns_time" )
306 << QStringLiteral(
"layer_params" ) << QStringLiteral(
"layer_statistics" ) << QStringLiteral(
"layer_sub_classes" )
307 << QStringLiteral(
"layer_table_layout" ) << QStringLiteral(
"pattern_bitmaps" ) << QStringLiteral(
"project_defs" )
308 << QStringLiteral(
"raster_pyramids" ) << QStringLiteral(
"spatial_ref_sys" ) << QStringLiteral(
"spatial_ref_sys_all" )
309 << QStringLiteral(
"spatial_ref_sys_aux" ) << QStringLiteral(
"spatialite_history" ) << QStringLiteral(
"sql_statements_log" )
310 << QStringLiteral(
"sqlite_sequence" ) << QStringLiteral(
"sqlite_stat1" ) << QStringLiteral(
"sqlite_stat2" )
311 << QStringLiteral(
"symbol_bitmaps" ) << QStringLiteral(
"tableprefix_metadata" ) << QStringLiteral(
"tableprefix_rasters" )
312 << QStringLiteral(
"vector_layers" ) << QStringLiteral(
"vector_layers_auth" ) << QStringLiteral(
"vector_layers_field_infos" )
313 << QStringLiteral(
"vector_layers_statistics" ) << QStringLiteral(
"views_geometry_columns" )
314 << QStringLiteral(
"views_geometry_columns_auth" ) << QStringLiteral(
"views_geometry_columns_field_infos" )
315 << QStringLiteral(
"views_geometry_columns_statistics" ) << QStringLiteral(
"views_layer_statistics" )
316 << QStringLiteral(
"virts_geometry_columns" ) << QStringLiteral(
"virts_geometry_columns_auth" )
317 << QStringLiteral(
"virts_geometry_columns_field_infos" ) << QStringLiteral(
"virts_geometry_columns_statistics" )
318 << QStringLiteral(
"virts_layer_statistics" )
320 << QStringLiteral(
"all_buckets_objects" ) << QStringLiteral(
"byfoot" ) << QStringLiteral(
"byfoot_data" )
321 << QStringLiteral(
"data_licenses" ) << QStringLiteral(
"ISO_metadata" ) << QStringLiteral(
"ISO_metadata_reference" )
322 << QStringLiteral(
"ISO_metadata_view" ) << QStringLiteral(
"KNN2" ) << QStringLiteral(
"KNN" )
323 << QStringLiteral(
"networks" ) << QStringLiteral(
"raster_coverages" ) << QStringLiteral(
"raster_coverages_keyword" )
324 << QStringLiteral(
"raster_coverages_ref_sys" ) << QStringLiteral(
"raster_coverages_srid" )
325 << QStringLiteral(
"rl2map_configurations" ) << QStringLiteral(
"rl2map_configurations_view" )
327 << QStringLiteral(
"SE_external_graphics" ) << QStringLiteral(
"SE_external_graphics_view" )
328 << QStringLiteral(
"SE_fonts" ) << QStringLiteral(
"SE_fonts_view" ) << QStringLiteral(
"SE_group_styles" )
329 << QStringLiteral(
"SE_group_styles_view" ) << QStringLiteral(
"SE_raster_styled_layers" )
330 << QStringLiteral(
"SE_raster_styled_layers_view" ) << QStringLiteral(
"SE_raster_styles" )
331 << QStringLiteral(
"SE_raster_styles_view" ) << QStringLiteral(
"SE_styled_group_refs" )
332 << QStringLiteral(
"SE_styled_group_styles" ) << QStringLiteral(
"SE_styled_groups" )
333 << QStringLiteral(
"SE_styled_groups_view" ) << QStringLiteral(
"SE_vector_styled_layers" )
334 << QStringLiteral(
"SE_vector_styled_layers_view" ) << QStringLiteral(
"SE_vector_styles" )
335 << QStringLiteral(
"SE_vector_styles_view" )
336 << QStringLiteral(
"sqlite_stat3" ) << QStringLiteral(
"stored_procedures" ) << QStringLiteral(
"stored_variables" )
337 << QStringLiteral(
"topologies" ) << QStringLiteral(
"vector_coverages" ) << QStringLiteral(
"vector_coverages_keyword" )
338 << QStringLiteral(
"vector_coverages_ref_sys" ) << QStringLiteral(
"vector_coverages_srid" )
340 << QStringLiteral(
"wms_getcapabilities" ) << QStringLiteral(
"wms_getmap" ) << QStringLiteral(
"wms_ref_sys" )
341 << QStringLiteral(
"wms_settings" );
347 va_start( ap, format );
348 char *c_str = sqlite3_vmprintf( format, ap );
350 QString res( QString::fromUtf8( c_str ) );
351 sqlite3_free( c_str );
static QString quotedString(const QString &value)
Returns a quoted string value, surround by ' characters and with special characters correctly escaped...
static QStringList systemTables()
Returns a string list of SQLite (and spatialite) system tables.
static QString quotedIdentifier(const QString &identifier)
Returns a properly quoted version of identifier.
static QSet< QString > uniqueFields(sqlite3 *connection, const QString &tableName, QString &errorMessage)
Returns a list of field names for connection and tableName having a UNIQUE constraint,...
static long long nextSequenceValue(sqlite3 *connection, const QString &tableName, QString errorMessage)
Increments and returns an SQLITE sequence of the table "sqlite_sequence" for tableName and returns it...
static QString quotedValue(const QVariant &value)
Returns a properly quoted and escaped version of value for use in SQL strings.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
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.
QString errorMessage() const
Returns the most recent error message encountered by the database.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
int exec(const QString &sql, QString &errorMessage) const
Executes the sql command in the database.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
QString columnName(int column) const
Returns the name of column.
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.
qlonglong columnAsInt64(int column) const
Gets column value from the current statement row as a long long integer (64 bits).
int columnCount() const
Gets the number of columns that this statement returns.
QByteArray columnAsBlob(int column) const
Returns the column value from the current statement row as raw byte array.
QString qgs_sqlite3_mprintf(const char *format,...)
Wraps sqlite3_mprintf() by automatically freeing the memory.
QString CORE_EXPORT qgs_sqlite3_mprintf(const char *format,...)
Wraps sqlite3_mprintf() by automatically freeing the memory.
void operator()(sqlite3 *database) const
Closes an sqlite database.
void operator()(sqlite3_stmt *statement) const
Finalizes an sqlite3 statement.