Skip to content
Permalink
Browse files

[FEATURE][spatialite] Add a REGEXP function to use for SQL filter et cie

  • Loading branch information
nirvn committed May 5, 2019
1 parent 1148ecb commit 47faac5661534457845e5d375486b619d34a6521
Showing with 22 additions and 0 deletions.
  1. +22 −0 src/providers/spatialite/qgsspatialiteconnection.cpp
@@ -640,6 +640,24 @@ bool QgsSpatiaLiteConnection::isDeclaredHidden( sqlite3 *handle, const QString &



static void fcnRegexp( sqlite3_context *ctx, int /*argc*/, sqlite3_value *argv[] )
{
// Get arguments and check their values
QRegExp arg1( reinterpret_cast<const char *>( sqlite3_value_text( argv[0] ) ) );
QString arg2( reinterpret_cast<const char *>( sqlite3_value_text( argv[1] ) ) );
if ( !arg1.isValid() )
return sqlite3_result_error( ctx, "invalid operand", -1 );

// Set the pattern matching syntax to a Perl-like one. This is the default in Qt 4.x but Qt 5
// changes this to a greedy one (QRegExp::RegExp2). To make sure the behaviour of our application
// doesn't change depending on the build environment, we make sure to always set the same pattern
// matching syntax.
arg1.setPatternSyntax( QRegExp::RegExp );
// Perform the actual matching and return the result. Note that Qt's QRegExp returns -1 if the regex
// doesn't match and the position in the string otherwise; SQLite expects a 0 for not found and a 1 for found.
sqlite3_result_int( ctx, arg1.indexIn( arg2 ) >= 0 );
}




@@ -702,6 +720,10 @@ QgsSqliteHandle *QgsSqliteHandle::openDb( const QString &dbPath, bool shared )
QgsDebugMsg( QStringLiteral( "Failure while connecting to: %1\n\ninvalid metadata tables" ).arg( dbPath ) );
return nullptr;
}

// add REGEXP function
sqlite3_create_function( database.get(), "REGEXP", 2, SQLITE_UTF8, nullptr, fcnRegexp, nullptr, nullptr );

// activating Foreign Key constraints
( void )sqlite3_exec( database.get(), "PRAGMA foreign_keys = 1", nullptr, nullptr, nullptr );

0 comments on commit 47faac5

Please sign in to comment.
You can’t perform that action at this time.