Skip to content

Commit 0094803

Browse files
authored
fix: clear bindings when resetting compiled queries (#10692)
1 parent 9dbf715 commit 0094803

11 files changed

Lines changed: 193 additions & 56 deletions

File tree

‎system/Database/BaseBuilder.php‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,13 +2095,15 @@ public function getSetData(bool $clean = false): array
20952095
*/
20962096
public function getCompiledSelect(bool $reset = true): string
20972097
{
2098-
$select = $this->compileSelect();
2098+
$select = $this->compileFinalQuery($this->compileSelect());
20992099

21002100
if ($reset) {
21012101
$this->resetSelect();
2102+
$this->binds = [];
2103+
$this->bindsKeyCount = [];
21022104
}
21032105

2104-
return $this->compileFinalQuery($select);
2106+
return $select;
21052107
}
21062108

21072109
/**
@@ -2939,11 +2941,15 @@ public function getCompiledInsert(bool $reset = true)
29392941
array_values($this->QBSet),
29402942
);
29412943

2944+
$sql = $this->compileFinalQuery($sql);
2945+
29422946
if ($reset) {
29432947
$this->resetWrite();
2948+
$this->binds = [];
2949+
$this->bindsKeyCount = [];
29442950
}
29452951

2946-
return $this->compileFinalQuery($sql);
2952+
return $sql;
29472953
}
29482954

29492955
/**
@@ -3110,11 +3116,15 @@ public function getCompiledUpdate(bool $reset = true)
31103116

31113117
$sql = $this->_update($this->QBFrom[0], $this->QBSet);
31123118

3119+
$sql = $this->compileFinalQuery($sql);
3120+
31133121
if ($reset) {
31143122
$this->resetWrite();
3123+
$this->binds = [];
3124+
$this->bindsKeyCount = [];
31153125
}
31163126

3117-
return $this->compileFinalQuery($sql);
3127+
return $sql;
31183128
}
31193129

31203130
/**
@@ -3417,8 +3427,14 @@ public function getCompiledDelete(bool $reset = true): string
34173427
{
34183428
$sql = $this->testMode()->delete('', null, $reset);
34193429
$this->testMode(false);
3430+
$sql = $this->compileFinalQuery($sql);
34203431

3421-
return $this->compileFinalQuery($sql);
3432+
if ($reset) {
3433+
$this->binds = [];
3434+
$this->bindsKeyCount = [];
3435+
}
3436+
3437+
return $sql;
34223438
}
34233439

34243440
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Builder;
15+
16+
use CodeIgniter\Database\BaseBuilder;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\Mock\MockConnection;
19+
use PHPUnit\Framework\Attributes\DataProvider;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('Others')]
26+
final class BindingLifecycleTest extends CIUnitTestCase
27+
{
28+
#[DataProvider('provideCompiledQuery')]
29+
public function testCompiledQueryClearsBindingsOnReset(string $method, string $sql): void
30+
{
31+
$builder = new BaseBuilder('jobs', new MockConnection([]));
32+
33+
for ($id = 1; $id <= 3; $id++) {
34+
$this->prepareQuery($builder, $method, $id);
35+
36+
$this->assertSameSql(sprintf($sql, $id), $builder->{$method}());
37+
$this->assertSame([], $builder->getBinds());
38+
}
39+
}
40+
41+
#[DataProvider('provideCompiledQuery')]
42+
public function testCompiledQueryPreservesBindingsWithoutReset(string $method, string $sql): void
43+
{
44+
$builder = new BaseBuilder('jobs', new MockConnection([]));
45+
$this->prepareQuery($builder, $method, 1);
46+
$binds = $builder->getBinds();
47+
48+
for ($i = 0; $i < 2; $i++) {
49+
$this->assertSameSql(sprintf($sql, 1), $builder->{$method}(false));
50+
$this->assertSame($binds, $builder->getBinds());
51+
}
52+
53+
$this->assertSameSql(sprintf($sql, 1), $builder->{$method}());
54+
$this->assertSame([], $builder->getBinds());
55+
}
56+
57+
/**
58+
* @return iterable<string, array{string, string}>
59+
*/
60+
public static function provideCompiledQuery(): iterable
61+
{
62+
yield 'select' => ['getCompiledSelect', 'SELECT * FROM "jobs" WHERE "id" = %d'];
63+
64+
yield 'insert' => ['getCompiledInsert', 'INSERT INTO "jobs" ("name", "id") VALUES (\'O\'\'Reilly\', %d)'];
65+
66+
yield 'update' => ['getCompiledUpdate', 'UPDATE "jobs" SET "name" = \'O\'\'Reilly\' WHERE "id" = %d'];
67+
68+
yield 'delete' => ['getCompiledDelete', 'DELETE FROM "jobs" WHERE "id" = %d'];
69+
}
70+
71+
private function prepareQuery(BaseBuilder $builder, string $method, int $id): void
72+
{
73+
if ($method === 'getCompiledInsert' || $method === 'getCompiledUpdate') {
74+
$builder->set('name', "O'Reilly");
75+
}
76+
77+
if ($method === 'getCompiledInsert') {
78+
$builder->set('id', $id);
79+
} else {
80+
$builder->where('id', $id);
81+
}
82+
}
83+
}

‎tests/system/Database/Builder/GroupTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testHavingBetweenMethods(string $method, string $sql): void
9494
],
9595
];
9696

97-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
97+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
9898
$this->assertSame($expectedBinds, $builder->getBinds());
9999
}
100100

@@ -172,7 +172,7 @@ public function testHavingBetweenNoEscape(): void
172172
],
173173
];
174174

175-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
175+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
176176
$this->assertSame($expectedBinds, $builder->getBinds());
177177
}
178178

‎tests/system/Database/Builder/InsertTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testInsertArray(): void
5757
],
5858
];
5959

60-
$this->assertSameSql($expectedSQL, $builder->getCompiledInsert());
60+
$this->assertSameSql($expectedSQL, $builder->getCompiledInsert(false));
6161
$this->assertSame($expectedBinds, $builder->getBinds());
6262
}
6363

@@ -111,7 +111,7 @@ public function testInsertObject(): void
111111
],
112112
];
113113

114-
$this->assertSameSql($expectedSQL, $builder->getCompiledInsert());
114+
$this->assertSameSql($expectedSQL, $builder->getCompiledInsert(false));
115115
$this->assertSame($expectedBinds, $builder->getBinds());
116116
}
117117

@@ -155,7 +155,7 @@ public function testInsertWithTableAlias(): void
155155
],
156156
];
157157

158-
$this->assertSameSql($expectedSQL, $builder->getCompiledInsert());
158+
$this->assertSameSql($expectedSQL, $builder->getCompiledInsert(false));
159159
$this->assertSame($expectedBinds, $builder->getBinds());
160160
}
161161

‎tests/system/Database/Builder/LikeTest.php‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testSimpleLike(): void
5050
],
5151
];
5252

53-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
53+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
5454
$this->assertSame($expectedBinds, $builder->getBinds());
5555
}
5656

@@ -72,7 +72,7 @@ public function testLikeAny(): void
7272
],
7373
];
7474

75-
$this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect())));
75+
$this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect(false))));
7676
$this->assertSame($expectedBinds, $builder->getBinds());
7777
}
7878

@@ -118,7 +118,7 @@ public function testLikeAnyCaseInsensitiveSearch(): void
118118
],
119119
];
120120

121-
$this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect())));
121+
$this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect(false))));
122122
$this->assertSame($expectedBinds, $builder->getBinds());
123123
}
124124

@@ -141,7 +141,7 @@ public function testLikeAnyWithRawSqlField(): void
141141
],
142142
];
143143

144-
$this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect())));
144+
$this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect(false))));
145145
$this->assertSame($expectedBinds, $builder->getBinds());
146146
}
147147

@@ -191,7 +191,7 @@ public function testLikeWithRawSql(): void
191191
],
192192
];
193193

194-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
194+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
195195
$this->assertSame($expectedBinds, $builder->getBinds());
196196
}
197197

@@ -209,7 +209,7 @@ public function testLikeNoSide(): void
209209
],
210210
];
211211

212-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
212+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
213213
$this->assertSame($expectedBinds, $builder->getBinds());
214214
}
215215

@@ -227,7 +227,7 @@ public function testLikeBeforeOnly(): void
227227
],
228228
];
229229

230-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
230+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
231231
$this->assertSame($expectedBinds, $builder->getBinds());
232232
}
233233

@@ -245,7 +245,7 @@ public function testLikeAfterOnly(): void
245245
],
246246
];
247247

248-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
248+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
249249
$this->assertSame($expectedBinds, $builder->getBinds());
250250
}
251251

@@ -267,7 +267,7 @@ public function testOrLike(): void
267267
],
268268
];
269269

270-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
270+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
271271
$this->assertSame($expectedBinds, $builder->getBinds());
272272
}
273273

@@ -285,7 +285,7 @@ public function testNotLike(): void
285285
],
286286
];
287287

288-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
288+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
289289
$this->assertSame($expectedBinds, $builder->getBinds());
290290
}
291291

@@ -307,7 +307,7 @@ public function testOrNotLike(): void
307307
],
308308
];
309309

310-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
310+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
311311
$this->assertSame($expectedBinds, $builder->getBinds());
312312
}
313313

@@ -325,7 +325,7 @@ public function testCaseInsensitiveLike(): void
325325
],
326326
];
327327

328-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
328+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
329329
$this->assertSame($expectedBinds, $builder->getBinds());
330330
}
331331

@@ -348,7 +348,7 @@ public function testDBPrefixAndCoulmnWithTablename(): void
348348
true,
349349
],
350350
];
351-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
351+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
352352
$this->assertSame($expectedBinds, $builder->getBinds());
353353
}
354354

@@ -373,7 +373,7 @@ public function testLikeMultipleFields(): void
373373
],
374374
];
375375

376-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
376+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect(false)));
377377
$this->assertSame($expectedBinds, $builder->getBinds());
378378
}
379379

@@ -398,7 +398,7 @@ public function testLikeMultipleCallsWithRawSqlAndString(): void
398398
],
399399
];
400400

401-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
401+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect(false)));
402402
$this->assertSame($expectedBinds, $builder->getBinds());
403403
}
404404

@@ -423,7 +423,7 @@ public function testOrLikeMultipleFields(): void
423423
],
424424
];
425425

426-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
426+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect(false)));
427427
$this->assertSame($expectedBinds, $builder->getBinds());
428428
}
429429

@@ -445,7 +445,7 @@ public function testLikeMethodsWithEmptyArray(string $method): void
445445
],
446446
];
447447

448-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
448+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect(false)));
449449
$this->assertSame($expectedBinds, $builder->getBinds());
450450
}
451451

‎tests/system/Database/Builder/PrefixTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testPrefixesSetOnTableNamesWithWhereBetweenClause(): void
8686

8787
$builder->whereBetween('users.created_at', [1, 10]);
8888

89-
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
89+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect(false));
9090
$this->assertSame($expectedBinds, $builder->getBinds());
9191
}
9292

0 commit comments

Comments
 (0)