变更
This commit is contained in:
+23
-22
@@ -34,6 +34,9 @@ class Command extends Component
|
|||||||
/** @var Connection */
|
/** @var Connection */
|
||||||
public Connection $db;
|
public Connection $db;
|
||||||
|
|
||||||
|
|
||||||
|
public PDO $pdo;
|
||||||
|
|
||||||
/** @var ?string */
|
/** @var ?string */
|
||||||
public ?string $sql = '';
|
public ?string $sql = '';
|
||||||
|
|
||||||
@@ -66,10 +69,9 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function all(): null|bool|array
|
public function all(): null|bool|array
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getPdo();
|
|
||||||
try {
|
try {
|
||||||
if (($prepare = $pdo->query($this->sql)) === false) {
|
if (($prepare = $this->pdo->query($this->sql)) === false) {
|
||||||
throw new Exception($pdo->errorInfo()[1]);
|
throw new Exception($this->pdo->errorInfo()[1]);
|
||||||
}
|
}
|
||||||
$prepare->execute($this->params);
|
$prepare->execute($this->params);
|
||||||
return $prepare->fetchAll(PDO::FETCH_ASSOC);
|
return $prepare->fetchAll(PDO::FETCH_ASSOC);
|
||||||
@@ -79,7 +81,7 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return $this->error($throwable);
|
return $this->error($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$this->db->release($pdo);
|
$this->db->release($this->pdo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,10 +91,9 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function one(): null|bool|array
|
public function one(): null|bool|array
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getPdo();
|
|
||||||
try {
|
try {
|
||||||
if (($prepare = $pdo->query($this->sql)) === false) {
|
if (($prepare = $this->pdo->query($this->sql)) === false) {
|
||||||
throw new Exception($pdo->errorInfo()[1]);
|
throw new Exception($this->pdo->errorInfo()[1]);
|
||||||
}
|
}
|
||||||
$prepare->execute($this->params);
|
$prepare->execute($this->params);
|
||||||
return $prepare->fetch(PDO::FETCH_ASSOC);
|
return $prepare->fetch(PDO::FETCH_ASSOC);
|
||||||
@@ -102,7 +103,7 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return $this->error($throwable);
|
return $this->error($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$this->db->release($pdo);
|
$this->db->release($this->pdo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,10 +113,9 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function fetchColumn(): null|bool|array
|
public function fetchColumn(): null|bool|array
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getPdo();
|
|
||||||
try {
|
try {
|
||||||
if (($prepare = $pdo->query($this->sql)) === false) {
|
if (($prepare = $this->pdo->query($this->sql)) === false) {
|
||||||
throw new Exception($pdo->errorInfo()[1]);
|
throw new Exception($this->pdo->errorInfo()[1]);
|
||||||
}
|
}
|
||||||
$prepare->execute($this->params);
|
$prepare->execute($this->params);
|
||||||
return $prepare->fetchColumn(PDO::FETCH_ASSOC);
|
return $prepare->fetchColumn(PDO::FETCH_ASSOC);
|
||||||
@@ -125,7 +125,7 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return $this->error($throwable);
|
return $this->error($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$this->db->release($pdo);
|
$this->db->release($this->pdo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,10 +135,9 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function rowCount(): int|bool
|
public function rowCount(): int|bool
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getPdo();
|
|
||||||
try {
|
try {
|
||||||
if (($prepare = $pdo->query($this->sql)) === false) {
|
if (($prepare = $this->pdo->query($this->sql)) === false) {
|
||||||
throw new Exception($pdo->errorInfo()[1]);
|
throw new Exception($this->pdo->errorInfo()[1]);
|
||||||
}
|
}
|
||||||
$prepare->execute($this->params);
|
$prepare->execute($this->params);
|
||||||
return $prepare->rowCount();
|
return $prepare->rowCount();
|
||||||
@@ -148,7 +147,7 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return $this->error($throwable);
|
return $this->error($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$this->db->release($pdo);
|
$this->db->release($this->pdo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,13 +168,15 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
private function _execute(): bool|int
|
private function _execute(): bool|int
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getPdo();
|
|
||||||
try {
|
try {
|
||||||
$prepare = $pdo->prepare($this->sql);
|
$prepare = $this->pdo->prepare($this->sql);
|
||||||
if ($prepare === false || $prepare->execute($this->params) === false) {
|
if ($prepare === false) {
|
||||||
throw new Exception(($prepare ?? $pdo)->errorInfo()[1]);
|
throw new Exception($this->pdo->errorInfo()[1]);
|
||||||
}
|
}
|
||||||
$result = $pdo->lastInsertId();
|
if ($prepare->execute($this->params) === false) {
|
||||||
|
throw new Exception($prepare->errorInfo()[1]);
|
||||||
|
}
|
||||||
|
$result = $this->pdo->lastInsertId();
|
||||||
$prepare->closeCursor();
|
$prepare->closeCursor();
|
||||||
return $result == 0 ? true : $result;
|
return $result == 0 ? true : $result;
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
@@ -184,7 +185,7 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return $this->error($throwable);
|
return $this->error($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$this->db->release($pdo);
|
$this->db->release($this->pdo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -248,7 +248,7 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function createCommand($sql = null, array $attributes = []): Command
|
public function createCommand($sql = null, array $attributes = []): Command
|
||||||
{
|
{
|
||||||
$command = new Command(['db' => $this, 'sql' => $sql]);
|
$command = new Command(['db' => $this, 'pdo' => $this->getMasterClient(), 'sql' => $sql]);
|
||||||
return $command->bindValues($attributes);
|
return $command->bindValues($attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user