@@ -640,6 +640,24 @@ bool QgsSpatiaLiteConnection::isDeclaredHidden( sqlite3 *handle, const QString &
640
640
641
641
642
642
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
+
643
661
644
662
645
663
@@ -702,6 +720,10 @@ QgsSqliteHandle *QgsSqliteHandle::openDb( const QString &dbPath, bool shared )
702
720
QgsDebugMsg ( QStringLiteral ( " Failure while connecting to: %1\n\n invalid metadata tables" ).arg ( dbPath ) );
703
721
return nullptr ;
704
722
}
723
+
724
+ // add REGEXP function
725
+ sqlite3_create_function ( database.get (), " REGEXP" , 2 , SQLITE_UTF8, nullptr , fcnRegexp, nullptr , nullptr );
726
+
705
727
// activating Foreign Key constraints
706
728
( void )sqlite3_exec ( database.get (), " PRAGMA foreign_keys = 1" , nullptr , nullptr , nullptr );
707
729
0 commit comments