Last active
September 22, 2021 19:03
-
-
Save alixaxel/7286763 to your computer and use it in GitHub Desktop.
SQLite UDFs for LIKE Operator Overloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db->sqliteCreateFunction('like', | |
function ($pattern, $data, $escape = null) use ($db) | |
{ | |
static $modifiers = null; | |
if (isset($modifiers) !== true) | |
{ | |
$modifiers = ((strncmp($db->query('PRAGMA case_sensitive_like;')->fetchColumn(), '1', 1) === 0) ? '' : 'i') . 'suS'; | |
} | |
if (isset($data) === true) | |
{ | |
if (strpbrk($pattern = preg_quote($pattern, '~'), '%_') !== false) | |
{ | |
$regex = array | |
( | |
'~%+~S' => '.*', | |
'~_~S' => '.', | |
); | |
if (strlen($escape = preg_quote($escape, '~')) > 0) | |
{ | |
$regex = array | |
( | |
'~(?<!' . $escape . ')%+~S' => '.*', | |
'~(?<!' . $escape . ')_~S' => '.', | |
'~(?:' . preg_quote($escape, '~') . ')([%_])~S' => '$1', | |
); | |
} | |
$pattern = preg_replace(array_keys($regex), $regex, $pattern); | |
} | |
return (preg_match(sprintf('~^%s$~%s', $pattern, $modifiers), $data) > 0); | |
} | |
return false; | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db->sqliteCreateFunction('like', | |
function ($pattern, $data, $escape = null) use ($db) | |
{ | |
static $modifiers = null; | |
if (isset($modifiers) !== true) | |
{ | |
$modifiers = ((strncmp($db->query('PRAGMA case_sensitive_like;')->fetchColumn(), '1', 1) === 0) ? '' : 'i') . 'suS'; | |
} | |
if (isset($data) === true) | |
{ | |
foreach (array('pattern', 'data') as $key) | |
{ | |
if (strpos(${$key} = htmlentities(${$key}, ENT_QUOTES, 'UTF-8'), '&') !== false) | |
{ | |
${$key} = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|caron|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~iS', '$1', ${$key}), ENT_QUOTES, 'UTF-8'); | |
} | |
if (preg_match('~[^[:ascii:]]~S', ${$key}) > 0) | |
{ | |
$latin = array | |
( | |
'a' => '~[ǎăāǻą]~uS', | |
'A' => '~[ǍĂĀǺĄ]~uS', | |
'ae' => '~[ǽǣ]~uS', | |
'AE' => '~[ǼǢ]~uS', | |
'b' => '~[ɓ]~uS', | |
'B' => '~[Ɓ]~uS', | |
'c' => '~[ćċĉč]~uS', | |
'C' => '~[ĆĊĈČ]~uS', | |
'd' => '~[ďḍđɗð]~uS', | |
'D' => '~[ĎḌĐƊÐ]~uS', | |
'e' => '~[ėěĕēęẹǝəɛ]~uS', | |
'E' => '~[ĖĚĔĒĘẸƎƏƐ]~uS', | |
'g' => '~[ġĝǧğģɣ]~uS', | |
'G' => '~[ĠĜǦĞĢƔ]~uS', | |
'h' => '~[ĥḥħ]~uS', | |
'H' => '~[ĤḤĦ]~uS', | |
'I' => '~[IǏĬĪĨĮỊİ]~uS', | |
'i' => '~[ıǐĭīĩįịİ]~uS', | |
'ij' => '~[ij]~uS', | |
'IJ' => '~[IJ]~uS', | |
'j' => '~[ĵ]~uS', | |
'J' => '~[Ĵ]~uS', | |
'k' => '~[ķƙĸ]~uS', | |
'K' => '~[ĶƘĸ]~uS', | |
'l' => '~[ĺļłľŀ]~uS', | |
'L' => '~[ĹĻŁĽĿ]~uS', | |
'n' => '~[ʼnń̈ňņŋ]~uS', | |
'N' => '~[ʼnŃ̈ŇŅŊ]~uS', | |
'o' => '~[ǒŏōőǫọǿơ]~uS', | |
'O' => '~[ǑŎŌŐǪỌǾƠ]~uS', | |
'r' => '~[ŕřŗ]~uS', | |
'R' => '~[ŔŘŖ]~uS', | |
'S' => '~[SŚŜŞȘṢ]~uS', | |
's' => '~[ſśŝşșṣ]~uS', | |
't' => '~[ťţṭŧ]~uS', | |
'T' => '~[ŤŢṬŦ]~uS', | |
'u' => '~[ǔŭūũűůųụư]~uS', | |
'U' => '~[ǓŬŪŨŰŮŲỤƯ]~uS', | |
'w' => '~[ẃẁŵẅƿ]~uS', | |
'W' => '~[ẂẀŴẄǷ]~uS', | |
'y' => '~[ỳŷȳỹƴ]~uS', | |
'Y' => '~[ỲŶȲỸƳ]~uS', | |
'z' => '~[źżžẓ]~uS', | |
'Z' => '~[ŹŻŽẒ]~uS', | |
); | |
${$key} = preg_replace($latin, array_keys($latin), ${$key}); | |
} | |
} | |
if (strpbrk($pattern = preg_quote($pattern, '~'), '%_') !== false) | |
{ | |
$regex = array | |
( | |
'~%+~S' => '.*', | |
'~_~S' => '.', | |
); | |
if (strlen($escape = preg_quote($escape, '~')) > 0) | |
{ | |
$regex = array | |
( | |
'~(?<!' . $escape . ')%+~S' => '.*', | |
'~(?<!' . $escape . ')_~S' => '.', | |
'~(?:' . preg_quote($escape, '~') . ')([%_])~S' => '$1', | |
); | |
} | |
$pattern = preg_replace(array_keys($regex), $regex, $pattern); | |
} | |
return (preg_match(sprintf('~^%s$~%s', $pattern, $modifiers), $data) > 0); | |
} | |
return false; | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db->sqliteCreateFunction('like', | |
function ($pattern, $data, $escape = null) use ($db) | |
{ | |
static $modifiers = null; | |
if (isset($modifiers) !== true) | |
{ | |
$modifiers = ((strncmp($db->query('PRAGMA case_sensitive_like;')->fetchColumn(), '1', 1) === 0) ? '' : 'i') . 'suS'; | |
} | |
if (isset($data) === true) | |
{ | |
foreach (array('pattern', 'data') as $key) | |
{ | |
if (strpos(${$key} = htmlentities(${$key}, ENT_QUOTES, 'UTF-8'), '&') !== false) | |
{ | |
${$key} = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|caron|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~iS', '$1', ${$key}), ENT_QUOTES, 'UTF-8'); | |
} | |
} | |
if (strpbrk($pattern = preg_quote($pattern, '~'), '%_') !== false) | |
{ | |
$regex = array | |
( | |
'~%+~S' => '.*', | |
'~_~S' => '.', | |
); | |
if (strlen($escape = preg_quote($escape, '~')) > 0) | |
{ | |
$regex = array | |
( | |
'~(?<!' . $escape . ')%+~S' => '.*', | |
'~(?<!' . $escape . ')_~S' => '.', | |
'~(?:' . preg_quote($escape, '~') . ')([%_])~S' => '$1', | |
); | |
} | |
$pattern = preg_replace(array_keys($regex), $regex, $pattern); | |
} | |
return (preg_match(sprintf('~^%s$~%s', $pattern, $modifiers), $data) > 0); | |
} | |
return false; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment