Skip to content

Commit d343829

Browse files
arontsangjakubmisek
authored andcommitted
Implement sqliteCreateCollation
1 parent dce7eda commit d343829

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/PDO/Peachpie.Library.PDO.Sqlite/PDOSqliteDriver.cs

+33-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,21 @@ private static PhpValue sqliteCreateAggregate(Context ctx, PDO pdo, PhpArray arg
8888
}
8989
private static PhpValue sqliteCreateCollation(Context ctx, PDO pdo, PhpArray arguments)
9090
{
91-
return PhpValue.False;
91+
if (pdo.GetCurrentConnection<SqliteConnection>() is not {} connection)
92+
return PhpValue.False;
93+
94+
95+
var name = arguments[0].String;
96+
var callable = arguments[1].AsCallable();
97+
98+
var handle = connection.Handle;
99+
raw.sqlite3_create_collation(
100+
handle,
101+
name,
102+
null,
103+
CreateSortFunction(ctx, callable)
104+
);
105+
return PhpValue.True;
92106
}
93107

94108
private static PhpValue sqliteCreateFunction(Context ctx, PDO pdo, PhpArray arguments)
@@ -180,6 +194,24 @@ private static delegate_function_aggregate_final CreateAggregateFinalize(Context
180194
};
181195
}
182196

197+
private static strdelegate_collation CreateSortFunction(Context ctx, IPhpCallable callback)
198+
{
199+
return (data, left, right) =>
200+
{
201+
var ret = callback.Invoke(
202+
ctx,
203+
PhpValue.FromClr(left),
204+
PhpValue.FromClr(right)
205+
);
206+
if (ret.IsInteger())
207+
{
208+
return ret.ToInt();
209+
}
210+
211+
return 0;
212+
};
213+
}
214+
183215
private static delegate_function_scalar CreateScalarFunction(Context ctx, IPhpCallable callback)
184216
{
185217
return (sqliteContext, data, args) =>

tests/pdo/sqlite_udf_sort.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
$pdo = new \PDO("sqlite::memory:");
5+
$pdo->exec("CREATE TABLE test (col1 string)");
6+
$pdo->exec("INSERT INTO test VALUES ('a1')");
7+
$pdo->exec("INSERT INTO test VALUES ('a10')");
8+
$pdo->exec("INSERT INTO test VALUES ('a2')");
9+
10+
$pdo->sqliteCreateCollation('NATURAL_CMP', strnatcmp);
11+
12+
13+
echo "default:\n";
14+
foreach ($pdo->query("SELECT col1 FROM test ORDER BY col1") as $row){
15+
echo $row['col1'], "\n";
16+
}
17+
18+
echo "\nnatural:\n";
19+
foreach ($pdo->query("SELECT col1 FROM test ORDER BY col1 COLLATE NATURAL_CMP") as $row){
20+
echo $row['col1'], "\n";
21+
}

0 commit comments

Comments
 (0)