File: sqlite3_36_create_collation.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (44 lines) | stat: -rw-r--r-- 821 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
--TEST--
Test SQLite3::createCollation() by adding strnatcmp() as an SQL COLLATE sequence
--EXTENSIONS--
sqlite3
--FILE--
<?php

require_once __DIR__ .  '/new_db.inc';

$db->createCollation('NAT', 'strnatcmp');

$db->exec('CREATE TABLE t (s varchar(4))');

$stmt = $db->prepare('INSERT INTO t VALUES (?)');
foreach(array('a1', 'a10', 'a2') as $s){
    $stmt->bindParam(1, $s);
    $stmt->execute();
}

$defaultSort = $db->query('SELECT s FROM t ORDER BY s');             //memcmp() sort
$naturalSort = $db->query('SELECT s FROM t ORDER BY s COLLATE NAT'); //strnatcmp() sort

echo "default\n";
while ($row = $defaultSort->fetchArray()){
    echo $row['s'], "\n";
}

echo "natural\n";
while ($row = $naturalSort->fetchArray()){
    echo $row['s'], "\n";
}

$db->close();

?>
--EXPECT--
default
a1
a10
a2
natural
a1
a2
a10