Skip to content

Commit e49580c

Browse files
committedApr 12, 2016
Fixed bug #68849 bindValue is not using the right data type
1 parent 4e585eb commit e49580c

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
 

‎ext/sqlite3/sqlite3.c

+30
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,26 @@ static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *pa
14061406
}
14071407
/* }}} */
14081408

1409+
/* {{{ Best try to map between PHP and SQLite. Default is still text. */
1410+
#define PHP_SQLITE3_SET_TYPE(z, p) \
1411+
switch (Z_TYPE_P(z)) { \
1412+
default: \
1413+
(p).type = SQLITE_TEXT; \
1414+
break; \
1415+
case IS_LONG: \
1416+
case IS_TRUE: \
1417+
case IS_FALSE: \
1418+
(p).type = SQLITE_INTEGER; \
1419+
break; \
1420+
case IS_DOUBLE: \
1421+
(p).type = SQLITE_FLOAT; \
1422+
break; \
1423+
case IS_NULL: \
1424+
(p).type = SQLITE_NULL; \
1425+
break; \
1426+
}
1427+
/* }}} */
1428+
14091429
/* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
14101430
Bind Parameter to a stmt variable. */
14111431
PHP_METHOD(sqlite3stmt, bindParam)
@@ -1430,6 +1450,10 @@ PHP_METHOD(sqlite3stmt, bindParam)
14301450

14311451
ZVAL_COPY(&param.parameter, parameter);
14321452

1453+
if (ZEND_NUM_ARGS() < 3) {
1454+
PHP_SQLITE3_SET_TYPE(parameter, param);
1455+
}
1456+
14331457
if (!register_bound_parameter_to_sqlite(&param, stmt_obj)) {
14341458
if (!Z_ISUNDEF(param.parameter)) {
14351459
zval_ptr_dtor(&(param.parameter));
@@ -1465,6 +1489,10 @@ PHP_METHOD(sqlite3stmt, bindValue)
14651489

14661490
ZVAL_COPY(&param.parameter, parameter);
14671491

1492+
if (ZEND_NUM_ARGS() < 3) {
1493+
PHP_SQLITE3_SET_TYPE(parameter, param);
1494+
}
1495+
14681496
if (!register_bound_parameter_to_sqlite(&param, stmt_obj)) {
14691497
if (!Z_ISUNDEF(param.parameter)) {
14701498
zval_ptr_dtor(&(param.parameter));
@@ -1476,6 +1504,8 @@ PHP_METHOD(sqlite3stmt, bindValue)
14761504
}
14771505
/* }}} */
14781506

1507+
#undef PHP_SQLITE3_SET_TYPE
1508+
14791509
/* {{{ proto SQLite3Result SQLite3Stmt::execute()
14801510
Executes a prepared statement and returns a result set object. */
14811511
PHP_METHOD(sqlite3stmt, execute)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
Bug #68849 bindValue is not using the right data type
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
8+
$db = new SQLite3(':memory:');
9+
10+
$db->exec("CREATE TABLE test (a INTEGER, b TEXT, c REAL);" .
11+
"INSERT INTO test VALUES (1, 'hello', 3.14);" .
12+
"INSERT INTO test VALUES (3, 'world', 3.15);" .
13+
"INSERT INTO test VALUES (0, '42', 0.42);"
14+
);
15+
16+
$s = $db->prepare('SELECT * FROM test WHERE (a+2) = ?;');
17+
$s->bindValue(1, 3);
18+
$r = $s->execute();
19+
var_dump($r->fetchArray(SQLITE3_ASSOC));
20+
21+
$s = $db->prepare('SELECT * FROM test WHERE a = ?;');
22+
$s->bindValue(1, true);
23+
$r = $s->execute();
24+
var_dump($r->fetchArray(SQLITE3_ASSOC));
25+
26+
$s = $db->prepare('SELECT * FROM test WHERE a = ?;');
27+
$s->bindValue(1, false);
28+
$r = $s->execute();
29+
var_dump($r->fetchArray(SQLITE3_ASSOC));
30+
31+
$s = $db->prepare('SELECT * FROM test WHERE c = ?;');
32+
$s->bindValue(1, 3.15);
33+
$r = $s->execute();
34+
var_dump($r->fetchArray(SQLITE3_ASSOC));
35+
36+
?>
37+
==DONE==
38+
--EXPECTF--
39+
array(3) {
40+
["a"]=>
41+
int(1)
42+
["b"]=>
43+
string(5) "hello"
44+
["c"]=>
45+
float(3.14)
46+
}
47+
array(3) {
48+
["a"]=>
49+
int(1)
50+
["b"]=>
51+
string(5) "hello"
52+
["c"]=>
53+
float(3.14)
54+
}
55+
array(3) {
56+
["a"]=>
57+
int(0)
58+
["b"]=>
59+
string(2) "42"
60+
["c"]=>
61+
float(0.42)
62+
}
63+
array(3) {
64+
["a"]=>
65+
int(3)
66+
["b"]=>
67+
string(5) "world"
68+
["c"]=>
69+
float(3.15)
70+
}
71+
==DONE==

0 commit comments

Comments
 (0)