Skip to content

Commit

Permalink
[FEATURE][spatialite] Add a REGEXP function to use for SQL filter et cie
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed May 6, 2019
1 parent 1148ecb commit 47faac5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/providers/spatialite/qgsspatialiteconnection.cpp
Expand Up @@ -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 );
}




Expand Down Expand Up @@ -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 );

Expand Down

0 comments on commit 47faac5

Please sign in to comment.