This commit is contained in:
2024-04-26 15:36:55 +08:00
parent 2fd37f90a3
commit 87f568bdfe
4 changed files with 25 additions and 43 deletions
+12 -40
View File
@@ -75,6 +75,15 @@ class Command extends Component
return $this->search('fetch');
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
return count($this->search('fetch')) > 0;
}
/**
* @return mixed
* @throws
@@ -88,48 +97,11 @@ class Command extends Component
* @return mixed
* @throws
*/
public function rowCount(): mixed
public function rowCount(): int
{
$client = $this->connection->getConnection();
try {
$client->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
if (($prepare = $client->query($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
}
$data = $this->search('fetch');
$prepare->execute($this->params);
$prepare->fetch();
$count = $prepare->rowCount();
$prepare->closeCursor();
$this->connection->println($this->sql, $this->params);
return $count;
} catch (Throwable $throwable) {
if ($this->isRefresh($throwable)) return $this->rowCount();
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
$this->getLogger()->failure($errorMsg . PHP_EOL, 'mysql');
return 0;
} finally {
$this->connection->release($client);
}
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
$total = $this->search('rowCount');
if ($total === false) {
throw new Exception('Query data is has error.');
}
return $total > 0;
return !$data ? 0 : +current($data);
}