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
+1 -2
View File
@@ -155,7 +155,6 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
public function count(): int public function count(): int
{ {
return $this->buildCommand($this->builder->count())->rowCount(); 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 public function exists(): bool
{ {
return $this->buildCommand($this->builder->one())->rowCount() > 0; return $this->buildCommand($this->limit(1)->builder->exists())->exists();
} }
+12 -40
View File
@@ -75,6 +75,15 @@ class Command extends Component
return $this->search('fetch'); return $this->search('fetch');
} }
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
return count($this->search('fetch')) > 0;
}
/** /**
* @return mixed * @return mixed
* @throws * @throws
@@ -88,48 +97,11 @@ class Command extends Component
* @return mixed * @return mixed
* @throws * @throws
*/ */
public function rowCount(): mixed public function rowCount(): int
{ {
$client = $this->connection->getConnection(); $data = $this->search('fetch');
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]);
}
$prepare->execute($this->params); return !$data ? 0 : +current($data);
$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;
} }
+2 -1
View File
@@ -115,10 +115,11 @@ class Db extends QueryTrait implements ISqlBuilder
/** /**
* @return bool * @return bool
* @throws Exception
*/ */
public function exists(): bool 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();
} }
/** /**
+10
View File
@@ -239,6 +239,16 @@ class SqlBuilder extends Component
} }
/**
* @return string
* @throws
*/
public function exists(): string
{
return $this->makeSelect(['0']) . $this->make();
}
/** /**
* @param string $table * @param string $table
* @return string * @return string