Skip to content

Commit 091e2c6

Browse files
committed
Explicitly cast bool to int to avoid legacy PHP test failures
1 parent 13a2c5e commit 091e2c6

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

res/sqlite-worker.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,24 @@
134134
} else {
135135
$statement = $db->prepare($data->params[0]);
136136
foreach ($data->params[1] as $index => $value) {
137+
if ($value === null) {
138+
$type = \SQLITE3_NULL;
139+
} elseif ($value === true || $value === false) {
140+
// explicitly cast bool to int because SQLite does not have a native boolean
141+
$type = \SQLITE3_INTEGER;
142+
$value = (int)$value;
143+
} elseif (\is_int($value)) {
144+
$type = \SQLITE3_INTEGER;
145+
} elseif (\is_float($value)) {
146+
$type = \SQLITE3_FLOAT;
147+
} else {
148+
$type = \SQLITE3_TEXT;
149+
}
150+
137151
$statement->bindValue(
138152
$index + 1,
139153
$value,
140-
$value === null ? \SQLITE3_NULL : \is_int($value) || \is_bool($value) ? \SQLITE3_INTEGER : \is_float($value) ? \SQLITE3_FLOAT : \SQLITE3_TEXT
154+
$type
141155
);
142156
}
143157
$result = @$statement->execute();
@@ -173,10 +187,24 @@
173187
} elseif ($data->method === 'query' && $db !== null && \count($data->params) === 2 && \is_string($data->params[0]) && \is_object($data->params[1])) {
174188
$statement = $db->prepare($data->params[0]);
175189
foreach ($data->params[1] as $index => $value) {
190+
if ($value === null) {
191+
$type = \SQLITE3_NULL;
192+
} elseif ($value === true || $value === false) {
193+
// explicitly cast bool to int because SQLite does not have a native boolean
194+
$type = \SQLITE3_INTEGER;
195+
$value = (int)$value;
196+
} elseif (\is_int($value)) {
197+
$type = \SQLITE3_INTEGER;
198+
} elseif (\is_float($value)) {
199+
$type = \SQLITE3_FLOAT;
200+
} else {
201+
$type = \SQLITE3_TEXT;
202+
}
203+
176204
$statement->bindValue(
177205
$index,
178206
$value,
179-
$value === null ? \SQLITE3_NULL : \is_int($value) || \is_bool($value) ? \SQLITE3_INTEGER : \is_float($value) ? \SQLITE3_FLOAT : \SQLITE3_TEXT
207+
$type
180208
);
181209
}
182210
$result = @$statement->execute();

tests/FunctionalDatabaseTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,21 @@ public function testQueryStringResolvesWithResultWithTypeStringAndRunsUntilQuit(
281281

282282
public function provideSqlDataWillBeReturnedWithType()
283283
{
284-
return [
285-
['42', 42],
286-
['1.5', 1.5],
287-
['null', null],
288-
['"hello"', 'hello'],
289-
['"hellö"', 'hellö'],
290-
['true', 1],
291-
['false', 0],
292-
];
284+
return array_merge(
285+
[
286+
['42', 42],
287+
['2.5', 2.5],
288+
['null', null],
289+
['"hello"', 'hello'],
290+
['"hellö"', 'hellö']
291+
],
292+
SQLite3::version()['versionNumber'] < 3023000 ? [] : [
293+
// boolean identifiers exist only as of SQLite 3.23.0 (2018-04-02)
294+
// @link https://www.sqlite.org/lang_expr.html#booleanexpr
295+
['true', 1],
296+
['false', 0]
297+
]
298+
);
293299
}
294300

295301
/**

0 commit comments

Comments
 (0)