mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
Add a REGEXP function in spatialite_connect() python function
This enables DB Manager and the Spatialite Execute SQL algorithm to use the 'string REGEXP pattern' syntax.
This commit is contained in:
parent
47faac5661
commit
eacc506a5a
@ -646,6 +646,12 @@ def spatialite_connect(*args, **kwargs):
|
||||
"""returns a dbapi2.Connection to a SpatiaLite db
|
||||
using the "mod_spatialite" extension (python3)"""
|
||||
import sqlite3
|
||||
import re
|
||||
|
||||
def fcnRegexp(pattern, string):
|
||||
result = re.search(pattern, string)
|
||||
return True if result else False
|
||||
|
||||
con = sqlite3.dbapi2.connect(*args, **kwargs)
|
||||
con.enable_load_extension(True)
|
||||
cur = con.cursor()
|
||||
@ -670,6 +676,7 @@ using the "mod_spatialite" extension (python3)"""
|
||||
raise RuntimeError("Cannot find any suitable spatialite module")
|
||||
cur.close()
|
||||
con.enable_load_extension(False)
|
||||
con.create_function("regexp", 2, fcnRegexp)
|
||||
return con
|
||||
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "qgssqliteutils.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QRegularExpression>
|
||||
#include <cstdlib> // atoi
|
||||
|
||||
#ifdef _MSC_VER
|
||||
@ -642,20 +643,13 @@ error:
|
||||
|
||||
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() )
|
||||
QRegularExpression re( reinterpret_cast<const char *>( sqlite3_value_text( argv[0] ) ) );
|
||||
QString string( reinterpret_cast<const char *>( sqlite3_value_text( argv[1] ) ) );
|
||||
|
||||
if ( !re.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 );
|
||||
sqlite3_result_int( ctx, string.contains( re ) );
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user