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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ tests/webfiori/database/tests/mysql/UserEntity.php
*.Identifier
/tests/.phpunit.cache
.php-cs-fixer.cache
/.vscode
22 changes: 12 additions & 10 deletions WebFiori/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Connection {
private $executedQueries;
/**
*
* @var string
* @var string|int
*
*/
private $lastErrCode;
Expand All @@ -46,7 +46,7 @@ abstract class Connection {
/**
* The result set which contains fetched data.
*
* @var ResultSet
* @var ResultSet|MultiResultSet
*/
private $resultSet;
/**
Expand All @@ -62,6 +62,8 @@ abstract class Connection {
public function __construct(ConnectionInfo $connInfo) {
$this->connParams = $connInfo;
$this->executedQueries = [];
$this->lastErrCode = 0;
$this->lastErrMsg = 'NO ERROR';

if (!$this->connect()) {
throw new DatabaseException('Unable to connect to database: '.$this->getLastErrCode().' - '.$this->getLastErrMessage(), $this->getLastErrCode());
Expand Down Expand Up @@ -115,16 +117,16 @@ public function getExecutedQueries() : array {
* @return int|string Last error code at which that was generated by executing last query.
*
*/
public function getLastErrCode() {
public function getLastErrCode() : string|int {
return $this->lastErrCode;
}
/**
* Returns the last message at which that was generated by executing a query.
*
* @return string The last message at which that was generated by executing a query.
* @return string|null The last message at which that was generated by executing a query.
*
*/
public function getLastErrMessage() : string {
public function getLastErrMessage() : ?string {
return $this->lastErrMsg;
}
/**
Expand All @@ -140,7 +142,7 @@ public function getLastQuery() {
/**
* Returns last result set.
*
* @return ResultSet|null The result set. If the result set is not set, the
* @return ResultSet|MultiResultSet|null The result set. If the result set is not set, the
* method will return null.
*
*/
Expand All @@ -158,14 +160,14 @@ public abstract function rollBack(?string $name = null);
* query object. If set, it should execute it.
*
*/
public abstract function runQuery(?AbstractQuery $query = null);
public abstract function runQuery(?AbstractQuery $query = null) : bool;
/**
* Sets error code at which that was generated by executing last query.
*
* @param int|string $code An integer value or any code that represents error code.
*
*/
public function setErrCode($code) {
public function setErrCode(string|int $code) {
$this->lastErrCode = $code;
}
/**
Expand All @@ -189,10 +191,10 @@ public function setLastQuery(AbstractQuery $query) {
/**
* Sets result set.
*
* @param ResultSet $result An object that represents result set.
* @param ResultSet|MultiResultSet $result An object that represents result set.
*
*/
public function setResultSet(ResultSet $result) {
public function setResultSet(ResultSet|MultiResultSet $result) {
$this->resultSet = $result;
}
}
4 changes: 2 additions & 2 deletions WebFiori/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function enablePerformanceMonitoring(): void {
* <li>An error has occurred while executing the query.</li>
* </ul>
*
* @return ResultSet|null If the last executed query was a select, show or
* @return ResultSet|MultiRsultSet|null If the last executed query was a select, show or
* describe query, the method will return an object of type 'ResultSet' that
* holds fetched records. Other than that, the method will return null.
*
Expand Down Expand Up @@ -447,7 +447,7 @@ public function getLastQuery() : string {
* Returns the last result set which was generated from executing a query such
* as a 'select' query.
*
* @return ResultSet|null The last result set. If no result set is available,
* @return ResultSet|MultiResultSet|null The last result set. If no result set is available,
* the method will return null.
*/
public function getLastResultSet() {
Expand Down
93 changes: 64 additions & 29 deletions WebFiori/Database/MsSql/MSSQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use WebFiori\Database\AbstractQuery;
use WebFiori\Database\Connection;
use WebFiori\Database\MultiResultSet;
use WebFiori\Database\ConnectionInfo;
use WebFiori\Database\DatabaseException;
use WebFiori\Database\ResultSet;
Expand Down Expand Up @@ -176,13 +177,14 @@ public function rollBack(?string $name = null) {
* Execute MSSQL query.
*
* @param AbstractQuery $query A query builder that has the generated MSSQL
* query.
/**
* Execute a query and return execution status.
*
* @return bool If the query successfully executed, the method will return
* true. Other than that, the method will return false.
* @param AbstractQuery|null $query The query to execute. If null, uses the last set query.
*
* @return bool True if the query executed successfully, false if there were errors.
*/
public function runQuery(?AbstractQuery $query = null) {
public function runQuery(?AbstractQuery $query = null): bool {
$this->addToExecuted($query->getQuery());
$this->setLastQuery($query);

Expand Down Expand Up @@ -224,7 +226,7 @@ private function runInsertQuery() {
$sql = $this->getLastQuery()->getQuery();

if ($insertBuilder === null) {
return false;
return $this->runOtherQuery();
}

$params = $insertBuilder->getQueryParams();
Expand All @@ -235,51 +237,84 @@ private function runInsertQuery() {
return $this->checkInsertOrUpdateResult($r);
}
private function runOtherQuery() {
$sql = $this->getLastQuery()->getQuery();
$queryBulder = $this->getLastQuery();

$r = sqlsrv_query($this->link, $sql, $queryBulder->getBindings());
$query = $this->getLastQuery();
$sql = $query->getQuery();
$r = sqlsrv_query($this->link, $sql, $query->getBindings());

if (!is_resource($r)) {
$this->setSqlErr();

return false;
}

// Collect all result sets
$allResults = [];

// First result set
if (sqlsrv_has_rows($r)) {
$data = [];
while ($row = sqlsrv_fetch_array($r, SQLSRV_FETCH_ASSOC)) {
$data[] = $row;
}
$allResults[] = $data;
} else {


if (sqlsrv_has_rows($r)) {
$data = [];
while ($row = sqlsrv_fetch_array($r, SQLSRV_FETCH_ASSOC)) {
$data[] = $row;
}
$this->setResultSet(new ResultSet($data));
$allResults[] = [];
}

// Additional result sets
while (sqlsrv_next_result($r)) {
$data = [];
while ($row = sqlsrv_fetch_array($r, SQLSRV_FETCH_ASSOC)) {
$data[] = $row;
}

$allResults[] = $data;
}

// Set result
if (count($allResults) > 1) {
$this->setResultSet(new MultiResultSet($allResults));
} else {
$this->setResultSet(new ResultSet($allResults[0]));
}

return true;
}
private function runSelectQuery() {
$queryBulder = $this->getLastQuery();
$sql = $queryBulder->getQuery();
$query = $this->getLastQuery();
$sql = $query->getQuery();
$r = sqlsrv_query($this->link, $sql, $query->getBindings());

$r = sqlsrv_query($this->link, $sql, $queryBulder->getBindings());
if (!is_resource($r)) {
$this->setSqlErr();
return false;
}

// Collect all result sets
$allResults = [];

// First result set
$data = [];
while ($row = sqlsrv_fetch_array($r, SQLSRV_FETCH_ASSOC)) {
$data[] = $row;
}
$allResults[] = $data;

if (is_resource($r)) {
// Additional result sets
while (sqlsrv_next_result($r)) {
$data = [];

while ($row = sqlsrv_fetch_array($r, SQLSRV_FETCH_ASSOC)) {
$data[] = $row;
}
$this->setResultSet(new ResultSet($data));
$allResults[] = $data;
}

return true;
// Set result
if (count($allResults) > 1) {
$this->setResultSet(new MultiResultSet($allResults));
} else {
$this->setSqlErr();

return false;
$this->setResultSet(new ResultSet($allResults[0]));
}

return true;
}
private function runUpdateQuery() {
$params = $this->getLastQuery()->getBindings();
Expand Down
Loading
Loading