33 sqlite3_close_v2( database );
38 sqlite3_finalize( statement );
43 return sqlite3_step( get() );
48 return QString::fromUtf8(
static_cast<const char *
>( sqlite3_column_name( get(), column ) ) );
53 return sqlite3_column_double( get(), column );
58 return sqlite3_column_count( get() );
63 return QString::fromUtf8(
reinterpret_cast<const char *
>( sqlite3_column_text( get(), column ) ) );
68 const void *blob = sqlite3_column_blob( get(), column );
69 int size = sqlite3_column_bytes( get(), column );
70 return QByteArray(
reinterpret_cast<const char *
>( blob ), size );
75 return sqlite3_column_int64( get(), column );
81 int result = sqlite3_open( path.toUtf8(), &database );
89 int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
96 return QString( sqlite3_errmsg( get() ) );
101 sqlite3_stmt *preparedStatement =
nullptr;
102 const char *tail =
nullptr;
103 resultCode = sqlite3_prepare( get(), sql.toUtf8(), sql.toUtf8().length(), &preparedStatement, &tail );
105 s.reset( preparedStatement );
113 int ret = sqlite3_exec( get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg );
118 sqlite3_free( errMsg );
126 QSet<QString> uniqueFieldsResults;
128 std::vector<std::string> rows;
131 "where type='table' and name=%q", tableNameUtf8.constData() );
138 static_cast<std::vector<std::string>*
>( data )->push_back( argv[0] );
142 int rc = sqlite3_exec( connection, sql.toUtf8(), cb, (
void * )&rows, &zErrMsg );
143 if ( rc != SQLITE_OK )
145 errorMessage = zErrMsg;
146 sqlite3_free( zErrMsg );
147 return uniqueFieldsResults;
151 std::smatch uniqueFieldMatch;
152 static const std::regex sFieldIdentifierRe { R
"raw(\s*(["`]([^"`]+)["`])|(([^\s]+)\s).*)raw" };
153 for (
auto tableDefinition : rows )
155 tableDefinition = tableDefinition.substr( tableDefinition.find(
'(' ), tableDefinition.rfind(
')' ) );
156 std::stringstream tableDefinitionStream { tableDefinition };
157 while ( tableDefinitionStream.good() )
159 std::string fieldStr;
160 std::getline( tableDefinitionStream, fieldStr,
',' );
161 std::string upperCaseFieldStr { fieldStr };
162 std::transform( upperCaseFieldStr.begin(), upperCaseFieldStr.end(), upperCaseFieldStr.begin(), ::toupper );
163 if ( upperCaseFieldStr.find(
"UNIQUE" ) != std::string::npos )
165 if ( std::regex_search( fieldStr, uniqueFieldMatch, sFieldIdentifierRe ) )
167 const std::string quoted { uniqueFieldMatch.str( 2 ) };
168 uniqueFieldsResults.insert( QString::fromStdString( quoted.length() ? quoted : uniqueFieldMatch.str( 4 ) ) );
177 " tbl_name='%q' AND sql LIKE 'CREATE UNIQUE INDEX%%'", tableNameUtf8.constData() );
178 rc = sqlite3_exec( connection, sql.toUtf8(), cb, (
void * )&rows, &zErrMsg );
179 if ( rc != SQLITE_OK )
181 errorMessage = zErrMsg;
182 sqlite3_free( zErrMsg );
183 return uniqueFieldsResults;
186 if ( rows.size() > 0 )
188 static const std::regex sFieldIndexIdentifierRe { R
"raw(\(\s*[`"]?([^",`\)]+)["`]?\s*\))raw" };
189 for (
auto indexDefinition : rows )
191 std::string upperCaseIndexDefinition { indexDefinition };
192 std::transform( upperCaseIndexDefinition.begin(), upperCaseIndexDefinition.end(), upperCaseIndexDefinition.begin(), ::toupper );
193 if ( upperCaseIndexDefinition.find(
"UNIQUE" ) != std::string::npos )
195 indexDefinition = indexDefinition.substr( indexDefinition.find(
'(' ), indexDefinition.rfind(
')' ) );
196 if ( std::regex_search( indexDefinition, uniqueFieldMatch, sFieldIndexIdentifierRe ) )
198 uniqueFieldsResults.insert( QString::fromStdString( uniqueFieldMatch.str( 1 ) ) );
203 return uniqueFieldsResults;
208 if ( value.isNull() )
209 return QStringLiteral(
"NULL" );
212 v.replace(
'\'', QLatin1String(
"''" ) );
213 return v.prepend(
'\'' ).append(
'\'' );
218 QString id( identifier );
219 id.replace(
'\"', QLatin1String(
"\"\"" ) );
220 return id.prepend(
'\"' ).append(
'\"' );
225 if ( value.isNull() )
226 return QStringLiteral(
"NULL" );
228 switch ( value.type() )
231 case QVariant::LongLong:
232 case QVariant::Double:
233 return value.toString();
237 return value.toBool() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" );
240 case QVariant::String:
241 QString v = value.toString();
246 return v.replace(
'\'', QLatin1String(
"''" ) ).prepend(
'\'' ).append(
'\'' );
252 return QStringList() << QStringLiteral(
"SpatialIndex" ) << QStringLiteral(
"geom_cols_ref_sys" ) << QStringLiteral(
"geometry_columns" )
253 << QStringLiteral(
"geometry_columns_auth" ) << QStringLiteral(
"views_geometry_columns" ) << QStringLiteral(
"virts_geometry_columns" )
254 << QStringLiteral(
"spatial_ref_sys" ) << QStringLiteral(
"spatial_ref_sys_all" ) << QStringLiteral(
"spatial_ref_sys_aux" )
255 << QStringLiteral(
"sqlite_sequence" ) << QStringLiteral(
"tableprefix_metadata" ) << QStringLiteral(
"tableprefix_rasters" )
256 << QStringLiteral(
"layer_params" ) << QStringLiteral(
"layer_statistics" ) << QStringLiteral(
"layer_sub_classes" )
257 << QStringLiteral(
"layer_table_layout" ) << QStringLiteral(
"pattern_bitmaps" ) << QStringLiteral(
"symbol_bitmaps" )
258 << QStringLiteral(
"project_defs" ) << QStringLiteral(
"raster_pyramids" ) << QStringLiteral(
"sqlite_stat1" ) << QStringLiteral(
"sqlite_stat2" )
259 << QStringLiteral(
"spatialite_history" ) << QStringLiteral(
"geometry_columns_field_infos" ) << QStringLiteral(
"geometry_columns_statistics" )
260 << QStringLiteral(
"geometry_columns_time" ) << QStringLiteral(
"sql_statements_log" ) << QStringLiteral(
"vector_layers" )
261 << QStringLiteral(
"vector_layers_auth" ) << QStringLiteral(
"vector_layers_field_infos" ) << QStringLiteral(
"vector_layers_statistics" )
262 << QStringLiteral(
"views_geometry_columns_auth" ) << QStringLiteral(
"views_geometry_columns_field_infos" )
263 << QStringLiteral(
"views_geometry_columns_statistics" ) << QStringLiteral(
"virts_geometry_columns_auth" )
264 << QStringLiteral(
"virts_geometry_columns_field_infos" ) << QStringLiteral(
"virts_geometry_columns_statistics" )
265 << QStringLiteral(
"virts_layer_statistics" ) << QStringLiteral(
"views_layer_statistics" )
266 << QStringLiteral(
"ElementaryGeometries" );
272 va_start( ap, format );
273 char *c_str = sqlite3_vmprintf( format, ap );
275 QString res( QString::fromUtf8( c_str ) );
276 sqlite3_free( c_str );