Skip to content

Commit a030d2f

Browse files
committed
Add "Sent SQL" to debug dump for emulated prepares
1 parent c406d1e commit a030d2f

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,14 @@ static PHP_METHOD(PDOStatement, debugDumpParams)
21222122
stmt->query_stringlen,
21232123
(int) stmt->query_stringlen, stmt->query_string);
21242124

2125+
/* show parsed SQL if emulated prepares enabled */
2126+
/* pointers will be equal if PDO::query() was invoked */
2127+
if (stmt->active_query_string != NULL && stmt->active_query_string != stmt->query_string) {
2128+
php_stream_printf(out, "Sent SQL: [%zd] %.*s\n",
2129+
stmt->active_query_stringlen,
2130+
(int) stmt->active_query_stringlen, stmt->active_query_string);
2131+
}
2132+
21252133
php_stream_printf(out, "Params: %d\n",
21262134
stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0);
21272135

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
PDO Common: PDOStatement::debugDumpParams() with emulated prepares
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo')) die('skip');
6+
$dir = getenv('REDIR_TEST_DIR');
7+
if (false == $dir) die('skip no driver');
8+
require_once $dir . 'pdo_test.inc';
9+
PDOTest::skip();
10+
11+
$db = PDOTest::factory();
12+
if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') die('skip pgsql has its own test for this feature');
13+
if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES) && !$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true)) die('skip driver cannot emulate prepared statements');
14+
?>
15+
--FILE--
16+
<?php
17+
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
18+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
19+
20+
$db = PDOTest::factory();
21+
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
22+
23+
$stmt = $db->query('SELECT 1');
24+
25+
// "Sent SQL" shouldn't display
26+
var_dump($stmt->debugDumpParams());
27+
28+
$stmt = $db->prepare('SELECT :bool, :int, :string, :null');
29+
$stmt->bindValue(':bool', true, PDO::PARAM_BOOL);
30+
$stmt->bindValue(':int', 123, PDO::PARAM_INT);
31+
$stmt->bindValue(':string', 'foo', PDO::PARAM_STR);
32+
$stmt->bindValue(':null', null, PDO::PARAM_NULL);
33+
$stmt->execute();
34+
35+
// "Sent SQL" should display
36+
var_dump($stmt->debugDumpParams());
37+
38+
?>
39+
--EXPECT--
40+
SQL: [8] SELECT 1
41+
Params: 0
42+
NULL
43+
SQL: [34] SELECT :bool, :int, :string, :null
44+
Sent SQL: [26] SELECT 1, 123, 'foo', NULL
45+
Params: 4
46+
Key: Name: [5] :bool
47+
paramno=-1
48+
name=[5] ":bool"
49+
is_param=1
50+
param_type=5
51+
Key: Name: [4] :int
52+
paramno=-1
53+
name=[4] ":int"
54+
is_param=1
55+
param_type=1
56+
Key: Name: [7] :string
57+
paramno=-1
58+
name=[7] ":string"
59+
is_param=1
60+
param_type=2
61+
Key: Name: [5] :null
62+
paramno=-1
63+
name=[5] ":null"
64+
is_param=1
65+
param_type=0
66+
NULL
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
PDO PgSQL PDOStatement::debugDumpParams() with emulated prepares
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6+
require dirname(__FILE__) . '/config.inc';
7+
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
8+
PDOTest::skip();
9+
?>
10+
--FILE--
11+
<?php
12+
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
13+
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
14+
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
15+
16+
$stmt = $db->prepare('SELECT :bool, :int, :string, :null');
17+
$stmt->bindValue(':bool', true, PDO::PARAM_BOOL);
18+
$stmt->bindValue(':int', 123, PDO::PARAM_INT);
19+
$stmt->bindValue(':string', 'foo', PDO::PARAM_STR);
20+
$stmt->bindValue(':null', null, PDO::PARAM_NULL);
21+
$stmt->execute();
22+
23+
var_dump($stmt->debugDumpParams());
24+
25+
?>
26+
--EXPECT--
27+
SQL: [34] SELECT :bool, :int, :string, :null
28+
Sent SQL: [28] SELECT 't', 123, 'foo', NULL
29+
Params: 4
30+
Key: Name: [5] :bool
31+
paramno=-1
32+
name=[5] ":bool"
33+
is_param=1
34+
param_type=2
35+
Key: Name: [4] :int
36+
paramno=-1
37+
name=[4] ":int"
38+
is_param=1
39+
param_type=1
40+
Key: Name: [7] :string
41+
paramno=-1
42+
name=[7] ":string"
43+
is_param=1
44+
param_type=2
45+
Key: Name: [5] :null
46+
paramno=-1
47+
name=[5] ":null"
48+
is_param=1
49+
param_type=0
50+
NULL

0 commit comments

Comments
 (0)