Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions WebFiori/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ public function setPerformanceConfig(array $config): void {
*
* @throws DatabaseException
*/
public function setQuery(string $query) : Database {
public function setQuery(string $query, array $params = []) : Database {

return $this->raw($query);
return $this->raw($query, $params);
}
/**
* Sets the database query to a raw SQL query.
Expand All @@ -817,14 +817,14 @@ public function setQuery(string $query) : Database {
*
* @throws DatabaseException
*/
public function raw(string $query) : Database {
public function raw(string $query, array $params = []) : Database {
$t = $this->getQueryGenerator()->getTable();

if ($t !== null) {
$t->getSelect()->clear();
}
$this->getQueryGenerator()->setQuery($query);

$this->getQueryGenerator()->setBindings($params);
return $this;
}
/**
Expand Down
19 changes: 19 additions & 0 deletions WebFiori/Database/MySql/MySQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,25 @@ public function resetBinding() {
];
}
public function setBindings(array $bindings, string $merge = 'none') {
// Check if it's a simple array (raw parameters) vs structured array
if (!isset($bindings['bind']) && !isset($bindings['values'])) {
// Simple array - convert to structured format
$bindString = '';
foreach ($bindings as $value) {
if (is_int($value)) {
$bindString .= 'i';
} elseif (is_float($value) || is_double($value)) {
$bindString .= 'd';
} else {
$bindString .= 's';
}
}
$bindings = [
'bind' => $bindString,
'values' => $bindings
];
}

$currentBinding = $this->bindings['bind'];
$values = $this->bindings['values'];

Expand Down
18 changes: 18 additions & 0 deletions tests/WebFiori/Tests/Database/MsSql/MSSQLQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1348,4 +1348,22 @@ public function testStoredProcedureExecution() {
// Clean up
$schema->raw("DROP PROCEDURE GetUserCount")->execute();
}

/**
* @test
*/
public function testRawSelectWithParameters() {
try {
$schema = new MSSQLTestSchema();

$schema->raw("CREATE TABLE #test_params (id INT, name NVARCHAR(50), age INT)")->execute();
$schema->raw("INSERT INTO #test_params VALUES (1, 'John', 25), (2, 'Jane', 30)")->execute();

$result = $schema->raw("SELECT * FROM #test_params WHERE age = ?", [25])->execute();
$this->assertEquals(1, $result->getRowsCount());

} catch (\Exception $e) {
$this->markTestSkipped('MSSQL test failed: ' . $e->getMessage());
}
}
}
31 changes: 31 additions & 0 deletions tests/WebFiori/Tests/Database/MySql/MySQLQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2203,4 +2203,35 @@ public function testStoredProcedureExecution() {
// Clean up
$schema->raw("DROP PROCEDURE GetUserCount")->execute();
}

/**
* @test
*/
public function testRawSelectWithParameters() {
$schema = new MySQLTestSchema();

$schema->raw("CREATE TEMPORARY TABLE test_params (id INT, name VARCHAR(50), age INT)")->execute();
$schema->raw("INSERT INTO test_params VALUES (1, 'John', 25), (2, 'Jane', 30), (3, 'Bob', 25)")->execute();

$result = $schema->raw("SELECT * FROM test_params WHERE age = ?", [25])->execute();
$this->assertEquals(2, $result->getRowsCount());

$result = $schema->raw("SELECT * FROM test_params WHERE name = ? AND age = ?", ['John', 25])->execute();
$this->assertEquals(1, $result->getRowsCount());
$this->assertEquals('John', $result->getRows()[0]['name']);
}

/**
* @test
*/
public function testRawInsertWithParameters() {
$schema = new MySQLTestSchema();

$schema->raw("CREATE TEMPORARY TABLE test_insert (id INT, name VARCHAR(50), email VARCHAR(100))")->execute();
$schema->raw("INSERT INTO test_insert (id, name, email) VALUES (?, ?, ?)", [1, 'Alice', 'alice@test.com'])->execute();

$result = $schema->raw("SELECT * FROM test_insert WHERE id = ?", [1])->execute();
$this->assertEquals(1, $result->getRowsCount());
$this->assertEquals('Alice', $result->getRows()[0]['name']);
}
}
Loading