Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit b25448f

Browse files
committed
feat(service): Add LEFT() function back
After upgrading the SQLite version, it works now.
1 parent c237ba5 commit b25448f

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

src/Service/Types/SchemaDatabase.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ private static function setUp(\SQLite3 $db): \SQLite3
6767
);
6868

6969
// MySQL-compatible functions
70-
$db->createFunction('YEAR', $dateop('Y'), 1, SQLITE3_DETERMINISTIC);
71-
$db->createFunction('MONTH', $dateop('n'), 1, SQLITE3_DETERMINISTIC);
72-
$db->createFunction('DAY', $dateop('j'), 1, SQLITE3_DETERMINISTIC);
70+
$db->createFunction('YEAR', $dateop('Y'), 1, \SQLITE3_DETERMINISTIC);
71+
$db->createFunction('MONTH', $dateop('n'), 1, \SQLITE3_DETERMINISTIC);
72+
$db->createFunction('DAY', $dateop('j'), 1, \SQLITE3_DETERMINISTIC);
73+
$db->createFunction('LEFT', fn (string $str, int $len) => substr($str, 0, $len), 2, \SQLITE3_DETERMINISTIC);
7374
$db->createFunction(
7475
'IF',
7576
fn (bool $condition, mixed $true, mixed $false) => $condition ? $true : $false,
7677
3,
77-
SQLITE3_DETERMINISTIC
78+
\SQLITE3_DETERMINISTIC
7879
);
7980

8081
return $db;
@@ -96,8 +97,7 @@ private static function initialize(string $filename, string $schema): void
9697
return;
9798
}
9899

99-
$db = new \SQLite3($filename);
100-
$db->enableExceptions(true);
100+
$db = self::setUp(new \SQLite3($filename));
101101

102102
try {
103103
$db->exec('BEGIN EXCLUSIVE');

tests/Service/DbRunnerTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,66 @@ public static function runQueryProvider(): array
224224
], /* result */
225225
null, /* exception */
226226
],
227+
[
228+
"CREATE TABLE records (
229+
RecordID INTEGER PRIMARY KEY, -- Assuming a unique identifier for each record
230+
ClassNo varchar(5) NOT NULL, -- Stores the class number as a string
231+
YMD DATE NOT NULL, -- Stores the date in 'YYYY-MM-DD' format
232+
Leave INTEGER DEFAULT 0, -- Stores the leave count for personal leave
233+
SickLeave INTEGER DEFAULT 0, -- Stores the leave count for sick leave
234+
PublicLeave INTEGER DEFAULT 0, -- Stores the leave count for public leave
235+
Absent INTEGER DEFAULT 0 -- Stores the count for absences
236+
);
237+
238+
INSERT INTO records (RecordID, ClassNo, YMD, Leave, SickLeave, PublicLeave, Absent) VALUES
239+
(1, '101A', '2018-03-15', 2, 1, 0, 0),
240+
(2, '101B', '2018-03-16', 0, 0, 1, 1),
241+
(3, '102A', '2018-03-17', 1, 0, 2, 0),
242+
(4, '101A', '2018-04-15', 0, 1, 0, 1),
243+
(5, '102B', '2018-05-20', 3, 0, 0, 0),
244+
(6, '101B', '2018-06-25', 0, 2, 0, 1),
245+
(7, '101C', '2018-07-10', 1, 1, 1, 0),
246+
(8, '103A', '2018-08-30', 0, 0, 3, 1),
247+
(9, '101A', '2019-09-01', 2, 1, 0, 1), -- Different year for variety
248+
(10, '102A', '2018-10-11', 0, 0, 1, 0);",
249+
'SELECT
250+
LEFT(records.ClassNo, 3) AS 班級,
251+
SUM(records.Leave) AS 事假總計,
252+
SUM(records.SickLeave) AS 病假總計,
253+
SUM(records.PublicLeave) AS 公假總計,
254+
SUM(records.Absent) AS 曠課總計
255+
FROM
256+
records
257+
WHERE
258+
YEAR(YMD) = 2018
259+
group BY
260+
LEFT(records.ClassNo, 3)
261+
',
262+
[
263+
[
264+
'班級' => '101',
265+
'事假總計' => 3,
266+
'病假總計' => 5,
267+
'公假總計' => 2,
268+
'曠課總計' => 3,
269+
],
270+
[
271+
'班級' => '102',
272+
'事假總計' => 4,
273+
'病假總計' => 0,
274+
'公假總計' => 3,
275+
'曠課總計' => 0,
276+
],
277+
[
278+
'班級' => '103',
279+
'事假總計' => 0,
280+
'病假總計' => 0,
281+
'公假總計' => 3,
282+
'曠課總計' => 1,
283+
],
284+
], /* result */
285+
null, /* exception */
286+
],
227287
];
228288
}
229289

@@ -342,6 +402,18 @@ public function testRunQueryIf(): void
342402
self::assertEquals([['if(0, 2, 3)' => 3]], $dbrunner->runQuery('', 'SELECT if(0, 2, 3)'));
343403
}
344404

405+
public function testRunQueryLeft(): void
406+
{
407+
$dbrunner = new DbRunner();
408+
409+
self::assertEquals([['left("abcdef", 3)' => 'abc']], $dbrunner->runQuery('', 'SELECT left("abcdef", 3)'));
410+
self::assertEquals([['left("1234567", 8)' => '1234567']], $dbrunner->runQuery('', 'SELECT left("1234567", 8)'));
411+
self::assertEquals([['left("hello", 2)' => 'he']], $dbrunner->runQuery('', 'SELECT left("hello", 2)'));
412+
self::assertEquals([['left("hello", 0)' => '']], $dbrunner->runQuery('', 'SELECT left("hello", 0)'));
413+
self::assertEquals([['left("hello", 6)' => 'hello']], $dbrunner->runQuery('', 'SELECT left("hello", 6)'));
414+
self::assertEquals([['left(c, 6)' => 'hello']], $dbrunner->runQuery('', 'SELECT left(c, 6) FROM (SELECT \'hello\' AS c)'));
415+
}
416+
345417
public function testRunQuerySum(): void
346418
{
347419
$dbrunner = new DbRunner();

0 commit comments

Comments
 (0)