Skip to content

Commit 47faac5

Browse files
committed
[FEATURE][spatialite] Add a REGEXP function to use for SQL filter et cie
1 parent 1148ecb commit 47faac5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/providers/spatialite/qgsspatialiteconnection.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,24 @@ bool QgsSpatiaLiteConnection::isDeclaredHidden( sqlite3 *handle, const QString &
640640

641641

642642

643+
static void fcnRegexp( sqlite3_context *ctx, int /*argc*/, sqlite3_value *argv[] )
644+
{
645+
// Get arguments and check their values
646+
QRegExp arg1( reinterpret_cast<const char *>( sqlite3_value_text( argv[0] ) ) );
647+
QString arg2( reinterpret_cast<const char *>( sqlite3_value_text( argv[1] ) ) );
648+
if ( !arg1.isValid() )
649+
return sqlite3_result_error( ctx, "invalid operand", -1 );
650+
651+
// Set the pattern matching syntax to a Perl-like one. This is the default in Qt 4.x but Qt 5
652+
// changes this to a greedy one (QRegExp::RegExp2). To make sure the behaviour of our application
653+
// doesn't change depending on the build environment, we make sure to always set the same pattern
654+
// matching syntax.
655+
arg1.setPatternSyntax( QRegExp::RegExp );
656+
// Perform the actual matching and return the result. Note that Qt's QRegExp returns -1 if the regex
657+
// doesn't match and the position in the string otherwise; SQLite expects a 0 for not found and a 1 for found.
658+
sqlite3_result_int( ctx, arg1.indexIn( arg2 ) >= 0 );
659+
}
660+
643661

644662

645663

@@ -702,6 +720,10 @@ QgsSqliteHandle *QgsSqliteHandle::openDb( const QString &dbPath, bool shared )
702720
QgsDebugMsg( QStringLiteral( "Failure while connecting to: %1\n\ninvalid metadata tables" ).arg( dbPath ) );
703721
return nullptr;
704722
}
723+
724+
// add REGEXP function
725+
sqlite3_create_function( database.get(), "REGEXP", 2, SQLITE_UTF8, nullptr, fcnRegexp, nullptr, nullptr );
726+
705727
// activating Foreign Key constraints
706728
( void )sqlite3_exec( database.get(), "PRAGMA foreign_keys = 1", nullptr, nullptr, nullptr );
707729

0 commit comments

Comments
 (0)