Implement a special field2 function, to migitiate the argument count limit in sqlite

This fixes issue #332 on SQLite DBs
This commit is contained in:
Jan Böhmer 2023-07-26 23:23:25 +02:00
parent ed6b0057b7
commit d3ead8742e
3 changed files with 131 additions and 3 deletions

View file

@ -47,8 +47,9 @@ class SQLiteRegexExtension
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) {
$native_connection->sqliteCreateFunction('REGEXP', $this->regexp(...), 2);
$native_connection->sqliteCreateFunction('FIELD', $this->field(...));
$native_connection->sqliteCreateFunction('REGEXP', $this->regexp(...), 2, \PDO::SQLITE_DETERMINISTIC);
$native_connection->sqliteCreateFunction('FIELD', $this->field(...), -1, \PDO::SQLITE_DETERMINISTIC);
$native_connection->sqliteCreateFunction('FIELD2', $this->field2(...), 2, \PDO::SQLITE_DETERMINISTIC);
}
}
}
@ -68,6 +69,19 @@ class SQLiteRegexExtension
}
}
/**
* Very similar to the field function, but takes the array values as a comma separated string.
* This is needed as SQLite has a pretty low argument count limit.
* @param string|int|null $value
* @param string $imploded_array
* @return int
*/
private function field2(string|int|null $value, string $imploded_array): int
{
$array = explode(',', $imploded_array);
return $this->field($value, ...$array);
}
/**
* This function emulates the MySQL field function for SQLite
* This function returns the index (position) of the first argument in the subsequent arguments.#