diff --git a/ActiveQuery.php b/ActiveQuery.php index da32d84..d06f6b2 100644 --- a/ActiveQuery.php +++ b/ActiveQuery.php @@ -155,7 +155,6 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder public function count(): int { return $this->buildCommand($this->builder->count())->rowCount(); - return !$search ? 0 : current($search); } @@ -205,7 +204,7 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder */ public function exists(): bool { - return $this->buildCommand($this->builder->one())->rowCount() > 0; + return $this->buildCommand($this->limit(1)->builder->exists())->exists(); } diff --git a/Command.php b/Command.php index f6b6fd2..9b05afd 100644 --- a/Command.php +++ b/Command.php @@ -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); } diff --git a/Db.php b/Db.php index 73c9cc2..ba232e2 100644 --- a/Db.php +++ b/Db.php @@ -115,10 +115,11 @@ class Db extends QueryTrait implements ISqlBuilder /** * @return bool + * @throws Exception */ public function exists(): bool { - return $this->connection->createCommand(SqlBuilder::builder($this)->one())->rowCount() > 0; + return $this->connection->createCommand(SqlBuilder::builder($this->limit(1))->exists())->exists(); } /** diff --git a/SqlBuilder.php b/SqlBuilder.php index 294333f..cad9a9f 100644 --- a/SqlBuilder.php +++ b/SqlBuilder.php @@ -239,6 +239,16 @@ class SqlBuilder extends Component } + /** + * @return string + * @throws + */ + public function exists(): string + { + return $this->makeSelect(['0']) . $this->make(); + } + + /** * @param string $table * @return string