- WIP on Collation

This commit is contained in:
mcndave committed 2024-12-16 18:46:24 +00:00
1 parent b601939459
commit b5e96eb7e0
8 files changed
+61 -11

No files matched your search

+33 -1
View File
@@ -2,9 +2,9 @@
namespace Ulmus\Adapter;
use Collator;
use Ulmus\Common\PdoObject;
use Ulmus\ConnectionAdapter;
use Ulmus\Entity;
use Ulmus\Migration\FieldDefinition;
use Ulmus\{Migration\MigrateInterface, Repository, QueryBuilder};
@@ -37,6 +37,7 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
};
$this->exportFunctions($pdo);
$this->exportCollations($pdo);
$this->registerPragma($pdo, $this->pragmaBegin);
}
catch(\PDOException $ex){
@@ -196,6 +197,37 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
$pdo->sqliteCreateFunction('year', fn($date) => ( new \DateTime($date) )->format('Y'), 1);
}
public function exportCollations(PdoObject $pdo) : void
{
if ( class_exists('Locale') ) {
# @TODO debug this, case-sensitivity not working properly !!
$collator = new Collator(\Locale::getDefault());
$collator->setStrength(Collator::TERTIARY);
$collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
$pdo->sqliteCreateCollation('locale_cmp', fn($e1, $e2) => $collator->compare($e1, $e2));
$pdo->sqliteCreateCollation('locale_cmp_desc', fn($e1, $e2) => $collator->compare($e2, $e1));
$collatorCi = new Collator(\Locale::getDefault());
$collatorCi->setStrength(Collator::SECONDARY);
$collatorCi->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
$pdo->sqliteCreateCollation('locale_cmp_ci', fn($e1, $e2) => $collatorCi->compare($e1, $e2));
$pdo->sqliteCreateCollation('locale_cmp_ci_desc', fn($e1, $e2) => $collatorCi->compare($e2, $e1));
}
else {
$comp = fn(string $e1, string $e2) => \iconv('UTF-8', 'ASCII//TRANSLIT', $e1) <=> \iconv('UTF-8', 'ASCII//TRANSLIT', $e2);
$pdo->sqliteCreateCollation('locale_cmp', fn($e1, $e2) => $comp);
$pdo->sqliteCreateCollation('locale_cmp_desc', fn($e2, $e1) => $comp);
$compCi = fn(string $e1, string $e2) => strtoupper(\iconv('UTF-8', 'ASCII//TRANSLIT', $e1)) <=> strtoupper(\iconv('UTF-8', 'ASCII//TRANSLIT', $e2));
$pdo->sqliteCreateCollation('locale_cmp_ci', fn($e1, $e2) => $compCi);
$pdo->sqliteCreateCollation('locale_cmp_ci_desc', fn($e2, $e1) => $compCi);
}
}
public static function registerPragma(PdoObject $pdo, array $pragmaList) : void
{
$builder = new QueryBuilder\Sql\SqliteQueryBuilder();