Skip to content

Commit 1c0fb24

Browse files
Implement numeric placeholders
1 parent 8acff67 commit 1c0fb24

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/NamedSqlParams.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class NamedSqlParams
2121
* @var array
2222
*/
2323
private $quoted_tokens;
24+
/**
25+
* @var bool
26+
*/
27+
private $numeric_placeholders;
2428

2529
/**
2630
* @param array $options
@@ -30,11 +34,13 @@ public function __construct(array $options = [])
3034
$quoted_tokens = [':', false];
3135
$unquoted_tokens = [':!', false];
3236
$debug_fn = [$this, 'debug'];
37+
$numeric_placeholders = false;
3338
extract($options, EXTR_IF_EXISTS);
3439

3540
$this->quoted_tokens = $quoted_tokens;
3641
$this->unquoted_tokens = $unquoted_tokens;
3742
$this->debug_fn = $debug_fn;
43+
$this->numeric_placeholders = $numeric_placeholders;
3844
}
3945

4046
/**
@@ -47,9 +53,10 @@ public function prep($sql, array $params = [], array $options = [])
4753
{
4854
$debug = (!empty($options['debug']) ? $options['debug'] : false);
4955
$prepared_params = [];
56+
$placeholder_index = 0;
5057
$prepared_sql = preg_replace_callback(
5158
$this->getTokenRegex(),
52-
function ($matches) use ($params, &$prepared_params, $debug) {
59+
function ($matches) use ($params, &$prepared_params, &$placeholder_index, $debug) {
5360
$quote_string = $matches[1];
5461
$param = $matches[2];
5562

@@ -78,9 +85,12 @@ function ($matches) use ($params, &$prepared_params, $debug) {
7885
);
7986
} else {
8087
$prepared_params[] = $one_value;
81-
$placeholders[] = '?';
88+
if ($this->numeric_placeholders) {
89+
$placeholders[] = '$' . ++$placeholder_index;
90+
} else {
91+
$placeholders[] = '?';
92+
}
8293
}
83-
8494
}
8595

8696
return implode(', ', $placeholders);

tests/testNamedSqlParams.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,45 @@ function(Test $test) {
7171
$test->equals($params, ['1'], 'should return 1 parameter');
7272
}
7373
);
74+
75+
Test::create(
76+
"numeric placeholders",
77+
function(Test $test) {
78+
$named = new NamedSqlParams(['numeric_placeholders' => true]);
79+
list($p_sql, $p_params) = $named->prep(
80+
"SELECT 1 FROM whatever WHERE one = :one AND two = :two",
81+
['one' => 1, 'two' => 2]
82+
);
83+
$test->equals(
84+
$p_params,
85+
[1, 2],
86+
"should return array of prepped params"
87+
);
88+
$test->equals(
89+
$p_sql,
90+
"SELECT 1 FROM whatever WHERE one = $1 AND two = $2",
91+
"should return sql with numeric placeholders"
92+
);
93+
}
94+
);
95+
96+
Test::create(
97+
"numeric array values",
98+
function(Test $test) {
99+
$named = new NamedSqlParams(['numeric_placeholders' => true]);
100+
list($p_sql, $p_params) = $named->prep(
101+
"SELECT 1 FROM whatever WHERE id IN (:ids) AND id2 IN (:ids2)",
102+
['ids' => [1, 2], 'ids2' => [3, 4]]
103+
);
104+
$test->equals(
105+
$p_params,
106+
[1, 2, 3, 4],
107+
"should return single array for all values"
108+
);
109+
$test->equals(
110+
$p_sql,
111+
"SELECT 1 FROM whatever WHERE id IN ($1, $2) AND id2 IN ($3, $4)",
112+
"should return sql with numeric placeholders"
113+
);
114+
}
115+
);

0 commit comments

Comments
 (0)