Skip to content

Commit 7f80836

Browse files
committed
ext/pdo_sqlite: adding PDO::SQLITE_ATTR_BUSY_STATEMENT
allow to check if a statement is still running before reusage.
1 parent e3cfa4b commit 7f80836

File tree

10 files changed

+56
-22
lines changed

10 files changed

+56
-22
lines changed

UPGRADING

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ PHP 8.5 UPGRADE NOTES
194194
IntlListFormatter::WIDTH_NARROW widths.
195195
It is supported from icu 67.
196196

197+
- PDO_Sqlite:
198+
. Added class constant Pdo_Sqlite::ATTR_BUSY_STATEMENT.
199+
197200
- SOAP:
198201
. Enumeration cases are now dumped in __getTypes().
199202

@@ -424,6 +427,9 @@ PHP 8.5 UPGRADE NOTES
424427
- PCRE:
425428
. Upgraded to pcre2lib from 10.44 to 10.45.
426429

430+
- PDO_Sqlite:
431+
. Increased minimum release version support from 3.7.7 to 3.7.17.
432+
427433
- Readline:
428434
. The return types of readline_add_history(), readline_clear_history(), and
429435
readline_callback_handler_install() have been changed to true, rather

build/php.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ dnl
19231923
dnl Common setup macro for SQLite library.
19241924
dnl
19251925
AC_DEFUN([PHP_SETUP_SQLITE], [
1926-
PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.7.7], [
1926+
PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.7.17], [
19271927
PHP_EVAL_INCLINE([$SQLITE_CFLAGS])
19281928
PHP_EVAL_LIBLINE([$SQLITE_LIBS], [$1])
19291929
])

ext/pdo_sqlite/pdo_sqlite.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,10 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v
385385
zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned",
386386
ZSTR_VAL(func_name), zend_zval_value_name(&retval));
387387
zend_string_release(func_name);
388-
ret = FAILURE;
388+
zval_ptr_dtor(&retval);
389+
return FAILURE;
389390
}
390-
if (Z_LVAL(retval) > 0) {
391-
ret = 1;
392-
} else if (Z_LVAL(retval) < 0) {
393-
ret = -1;
394-
}
395-
zval_ptr_dtor(&retval);
391+
ret = ZEND_NORMALIZE_BOOL(Z_LVAL(retval));
396392
}
397393

398394
return ret;

ext/pdo_sqlite/pdo_sqlite.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Sqlite extends \PDO
3333
/** @cvalue PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES */
3434
public const int ATTR_EXTENDED_RESULT_CODES = UNKNOWN;
3535

36+
/** @cvalue PDO_SQLITE_ATTR_BUSY_STATEMENT */
37+
public const int ATTR_BUSY_STATEMENT = UNKNOWN;
38+
3639
/** @cvalue SQLITE_OK */
3740
public const int OK = UNKNOWN;
3841

ext/pdo_sqlite/pdo_sqlite_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pdo_sqlite/php_pdo_sqlite_int.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ extern const struct pdo_stmt_methods sqlite_stmt_methods;
7373
enum {
7474
PDO_SQLITE_ATTR_OPEN_FLAGS = PDO_ATTR_DRIVER_SPECIFIC,
7575
PDO_SQLITE_ATTR_READONLY_STATEMENT,
76-
PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES
76+
PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES,
77+
PDO_SQLITE_ATTR_BUSY_STATEMENT
7778
};
7879

7980
typedef int pdo_sqlite_create_collation_callback(void*, int, const void*, int, const void*);

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,10 @@ static int php_sqlite3_collation_callback(void *context, int string1_len, const
492492
zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned",
493493
ZSTR_VAL(func_name), zend_zval_value_name(&retval));
494494
zend_string_release(func_name);
495-
ret = FAILURE;
496-
}
497-
if (Z_LVAL(retval) > 0) {
498-
ret = 1;
499-
} else if (Z_LVAL(retval) < 0) {
500-
ret = -1;
495+
zval_ptr_dtor(&retval);
496+
return FAILURE;
501497
}
502-
zval_ptr_dtor(&retval);
498+
ret = ZEND_NORMALIZE_BOOL(Z_LVAL(retval));
503499
}
504500

505501
return ret;

ext/pdo_sqlite/sqlite_statement.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,18 @@ static int pdo_sqlite_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval
375375
case PDO_SQLITE_ATTR_READONLY_STATEMENT:
376376
ZVAL_FALSE(val);
377377

378-
#if SQLITE_VERSION_NUMBER >= 3007004
379-
if (sqlite3_stmt_readonly(S->stmt)) {
380-
ZVAL_TRUE(val);
381-
}
382-
#endif
378+
if (sqlite3_stmt_readonly(S->stmt)) {
379+
ZVAL_TRUE(val);
380+
}
383381
break;
384382

383+
case PDO_SQLITE_ATTR_BUSY_STATEMENT:
384+
ZVAL_FALSE(val);
385+
386+
if (sqlite3_stmt_busy(S->stmt)) {
387+
ZVAL_TRUE(val);
388+
}
389+
break;
385390
default:
386391
return 0;
387392
}

ext/pdo_sqlite/tests/subclasses/pdo_sqlite_constants.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var_dump(Pdo\Sqlite::OPEN_READWRITE);
1313
var_dump(Pdo\Sqlite::OPEN_CREATE);
1414
var_dump(Pdo\Sqlite::ATTR_READONLY_STATEMENT);
1515
var_dump(Pdo\Sqlite::ATTR_EXTENDED_RESULT_CODES);
16+
var_dump(Pdo\Sqlite::ATTR_BUSY_STATEMENT);
1617

1718
?>
1819
--EXPECTF--
@@ -24,3 +25,4 @@ int(%d)
2425
int(%d)
2526
int(%d)
2627
int(%d)
28+
int(%d)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Pdo\Sqlite::ATTR_BUSY_STATEMENT usage
3+
--EXTENSIONS--
4+
pdo_sqlite
5+
--FILE--
6+
<?php
7+
8+
$db = new Pdo\Sqlite('sqlite::memory:');
9+
10+
$db->query('CREATE TABLE test_busy (a string);');
11+
$db->query('INSERT INTO test_busy VALUES ("interleaved"), ("statements")');
12+
$st = $db->prepare('SELECT a FROM test_busy');
13+
var_dump($st->getAttribute(Pdo\Sqlite::ATTR_BUSY_STATEMENT));
14+
$st->execute();
15+
var_dump($st->getAttribute(Pdo\Sqlite::ATTR_BUSY_STATEMENT));
16+
?>
17+
--EXPECTF--
18+
bool(false)
19+
bool(true)

0 commit comments

Comments
 (0)