Skip to content

Commit 963743b

Browse files
authored
chore: Merge pull request #117 from WebFiori/dev
feat: Parameterized Raw
2 parents 8e0e4ab + fb3d00e commit 963743b

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

WebFiori/Database/Database.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,9 +803,9 @@ public function setPerformanceConfig(array $config): void {
803803
*
804804
* @throws DatabaseException
805805
*/
806-
public function setQuery(string $query) : Database {
806+
public function setQuery(string $query, array $params = []) : Database {
807807

808-
return $this->raw($query);
808+
return $this->raw($query, $params);
809809
}
810810
/**
811811
* Sets the database query to a raw SQL query.
@@ -817,14 +817,14 @@ public function setQuery(string $query) : Database {
817817
*
818818
* @throws DatabaseException
819819
*/
820-
public function raw(string $query) : Database {
820+
public function raw(string $query, array $params = []) : Database {
821821
$t = $this->getQueryGenerator()->getTable();
822822

823823
if ($t !== null) {
824824
$t->getSelect()->clear();
825825
}
826826
$this->getQueryGenerator()->setQuery($query);
827-
827+
$this->getQueryGenerator()->setBindings($params);
828828
return $this;
829829
}
830830
/**

WebFiori/Database/MySql/MySQLQuery.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,25 @@ public function resetBinding() {
335335
];
336336
}
337337
public function setBindings(array $bindings, string $merge = 'none') {
338+
// Check if it's a simple array (raw parameters) vs structured array
339+
if (!isset($bindings['bind']) && !isset($bindings['values'])) {
340+
// Simple array - convert to structured format
341+
$bindString = '';
342+
foreach ($bindings as $value) {
343+
if (is_int($value)) {
344+
$bindString .= 'i';
345+
} elseif (is_float($value) || is_double($value)) {
346+
$bindString .= 'd';
347+
} else {
348+
$bindString .= 's';
349+
}
350+
}
351+
$bindings = [
352+
'bind' => $bindString,
353+
'values' => $bindings
354+
];
355+
}
356+
338357
$currentBinding = $this->bindings['bind'];
339358
$values = $this->bindings['values'];
340359

tests/WebFiori/Tests/Database/MsSql/MSSQLQueryBuilderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,4 +1348,22 @@ public function testStoredProcedureExecution() {
13481348
// Clean up
13491349
$schema->raw("DROP PROCEDURE GetUserCount")->execute();
13501350
}
1351+
1352+
/**
1353+
* @test
1354+
*/
1355+
public function testRawSelectWithParameters() {
1356+
try {
1357+
$schema = new MSSQLTestSchema();
1358+
1359+
$schema->raw("CREATE TABLE #test_params (id INT, name NVARCHAR(50), age INT)")->execute();
1360+
$schema->raw("INSERT INTO #test_params VALUES (1, 'John', 25), (2, 'Jane', 30)")->execute();
1361+
1362+
$result = $schema->raw("SELECT * FROM #test_params WHERE age = ?", [25])->execute();
1363+
$this->assertEquals(1, $result->getRowsCount());
1364+
1365+
} catch (\Exception $e) {
1366+
$this->markTestSkipped('MSSQL test failed: ' . $e->getMessage());
1367+
}
1368+
}
13511369
}

tests/WebFiori/Tests/Database/MySql/MySQLQueryBuilderTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,4 +2203,35 @@ public function testStoredProcedureExecution() {
22032203
// Clean up
22042204
$schema->raw("DROP PROCEDURE GetUserCount")->execute();
22052205
}
2206+
2207+
/**
2208+
* @test
2209+
*/
2210+
public function testRawSelectWithParameters() {
2211+
$schema = new MySQLTestSchema();
2212+
2213+
$schema->raw("CREATE TEMPORARY TABLE test_params (id INT, name VARCHAR(50), age INT)")->execute();
2214+
$schema->raw("INSERT INTO test_params VALUES (1, 'John', 25), (2, 'Jane', 30), (3, 'Bob', 25)")->execute();
2215+
2216+
$result = $schema->raw("SELECT * FROM test_params WHERE age = ?", [25])->execute();
2217+
$this->assertEquals(2, $result->getRowsCount());
2218+
2219+
$result = $schema->raw("SELECT * FROM test_params WHERE name = ? AND age = ?", ['John', 25])->execute();
2220+
$this->assertEquals(1, $result->getRowsCount());
2221+
$this->assertEquals('John', $result->getRows()[0]['name']);
2222+
}
2223+
2224+
/**
2225+
* @test
2226+
*/
2227+
public function testRawInsertWithParameters() {
2228+
$schema = new MySQLTestSchema();
2229+
2230+
$schema->raw("CREATE TEMPORARY TABLE test_insert (id INT, name VARCHAR(50), email VARCHAR(100))")->execute();
2231+
$schema->raw("INSERT INTO test_insert (id, name, email) VALUES (?, ?, ?)", [1, 'Alice', 'alice@test.com'])->execute();
2232+
2233+
$result = $schema->raw("SELECT * FROM test_insert WHERE id = ?", [1])->execute();
2234+
$this->assertEquals(1, $result->getRowsCount());
2235+
$this->assertEquals('Alice', $result->getRows()[0]['name']);
2236+
}
22062237
}

0 commit comments

Comments
 (0)