Skip to content

Commit

Permalink
New way to retrieve resultSet when query
Browse files Browse the repository at this point in the history
  • Loading branch information
jabaruben authored May 2, 2022
1 parent d212ef8 commit 6af8432
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/DbSQLServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class DbSQLServer
public $columnCount = 0;
public $querycount = 0;

private $retryAttempt = 0; // 失败重试次数
private $retryAttempt = 0;
const AUTO_RECONNECT = true;
const RETRY_ATTEMPTS = 3; // 最大失败重试次数
const RETRY_ATTEMPTS = 3;

/**
* DB constructor.
Expand Down Expand Up @@ -199,13 +199,24 @@ public function inTransaction()

/**
* execute a sql query, returns an result array in the select operation, and returns the number of rows affected in other operations
*
* If you need to specify the kind of query because have IFs or WHILEs or something, can put at first a comment with "/ *UPDATE* /" (without spaces) to specify the kind of query
*
* @param string $query
* @param null $params
* @param int $fetchMode
* @return array|false|int|null
*/
public function query($query, $params = null, $fetchMode = PDO::FETCH_ASSOC)
{
$response = [
"sqlState" => "00000",
"driverErrorCode" => 0,
"errorText" => "",
"rowCount" => 0,
"rows" => []
];

$query = trim($query);
$rawStatement = explode(" ", $query);

Expand All @@ -215,27 +226,34 @@ public function query($query, $params = null, $fetchMode = PDO::FETCH_ASSOC)
throw $e;
}

$statement = strtolower($rawStatement[0]);
$statement = strtolower(str_replace("/", "", str_replace("*", "", $rawStatement[0])));

$errorInfo = $this->sQuery->errorInfo();
$response["sqlState"] = $errorInfo[0];
$response["driverErrorCode"] = $errorInfo[1];
$response["errorText"] = $errorInfo[2];

if ($statement === 'declare' || $statement === 'with') {
while ($this->sQuery->columnCount() === 0 && $this->sQuery->nextRowset()) {
// Advance rowset until we get to a rowset with data
}

if ($this->sQuery->columnCount() <= 0) {
return [];
if ($this->sQuery->columnCount() > 0) {
$response["rows"] = $this->sQuery->fetchAll($fetchMode);
}

return $this->sQuery->fetchAll($fetchMode);
} elseif ($statement === 'select' || $statement === 'show') {
return $this->sQuery->fetchAll($fetchMode);
$response["rows"] = $this->sQuery->fetchAll($fetchMode);
} elseif (
$statement === 'insert' || $statement === 'update'
|| $statement === 'delete' || $statement === 'exec'
) {
return $this->sQuery->rowCount();
$response["rowCount"] = $this->sQuery->rowCount();
} else {
return NULL;
$response["sqlState"] = "66666";
$response["errorText"] = "Error desconocido o resultado no esperado.";
}

return $response;
}

/**
Expand Down

0 comments on commit 6af8432

Please sign in to comment.