变更
This commit is contained in:
+34
-53
@@ -118,22 +118,12 @@ class Command extends Component
|
|||||||
|
|
||||||
$result = $type !== static::EXECUTE ? $this->search($type) : $this->_execute();
|
$result = $type !== static::EXECUTE ? $this->search($type) : $this->_execute();
|
||||||
|
|
||||||
$this->logger->debug('Mysql:' . $this->print_r($time));
|
$this->longExecuteTime($time);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $time
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function print_r($time): string
|
|
||||||
{
|
|
||||||
return print_r(['time' => microtime(true) - $time, 'sql' => $this->sql, 'param' => $this->params], true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -159,7 +149,6 @@ class Command extends Component
|
|||||||
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
||||||
$this->db->restore(true);
|
$this->db->restore(true);
|
||||||
|
|
||||||
unset($pdo);
|
|
||||||
return $this->_execute();
|
return $this->_execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,34 +158,6 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param \PDO $pdo
|
|
||||||
* @return PDOStatement
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function queryPrev(\PDO $pdo): PDOStatement
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
if (($statement = $pdo->query($this->sql)) === false) {
|
|
||||||
throw new Exception($pdo->errorInfo()[1]);
|
|
||||||
}
|
|
||||||
foreach ($this->params as $key => $param) {
|
|
||||||
$statement->bindValue($key, $param);
|
|
||||||
}
|
|
||||||
return $statement;
|
|
||||||
} catch (\PDOException|\Throwable $throwable) {
|
|
||||||
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
|
||||||
$this->db->restore(false);
|
|
||||||
|
|
||||||
unset($pdo);
|
|
||||||
return $this->queryPrev($this->db->getSlaveClient());
|
|
||||||
}
|
|
||||||
throw new Exception($throwable->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return array|int|bool|null
|
* @return array|int|bool|null
|
||||||
@@ -206,22 +167,42 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
$pdo = $this->db->getSlaveClient();
|
$pdo = $this->db->getSlaveClient();
|
||||||
try {
|
try {
|
||||||
if ($type == self::FETCH_ALL) {
|
if (($statement = $pdo->query($this->sql)) === false) {
|
||||||
$data = $this->queryPrev($pdo)->fetchAll(\PDO::FETCH_ASSOC);
|
throw new Exception($pdo->errorInfo()[1]);
|
||||||
} else if ($type == self::FETCH) {
|
}
|
||||||
$data = $this->queryPrev($pdo)->fetch(\PDO::FETCH_ASSOC);
|
foreach ($this->params as $key => $param) {
|
||||||
} else if ($type == self::FETCH_COLUMN) {
|
$statement->bindValue($key, $param);
|
||||||
$data = $this->queryPrev($pdo)->fetchColumn(\PDO::FETCH_ASSOC);
|
}
|
||||||
} else if ($type == self::ROW_COUNT) {
|
$data = null;
|
||||||
$data = $this->queryPrev($pdo)->rowCount();
|
if ($type == self::FETCH_ALL) {
|
||||||
} else {
|
$data = $statement->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
$data = null;
|
} else if ($type == self::FETCH) {
|
||||||
|
$data = $statement->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
} else if ($type == self::FETCH_COLUMN) {
|
||||||
|
$data = $statement->fetchColumn(\PDO::FETCH_ASSOC);
|
||||||
|
} else if ($type == self::ROW_COUNT) {
|
||||||
|
$data = $statement->rowCount();
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
|
||||||
$data = $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
|
||||||
} finally {
|
|
||||||
$this->db->release($pdo, false);
|
$this->db->release($pdo, false);
|
||||||
return $data;
|
return $data;
|
||||||
|
} catch (\Throwable $throwable) {
|
||||||
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
||||||
|
$this->db->restore(false);
|
||||||
|
|
||||||
|
return $this->search($type);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->db->release($pdo, false);
|
||||||
|
|
||||||
|
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function longExecuteTime($time)
|
||||||
|
{
|
||||||
|
if (($over = microtime(true) - $time) >= 0.50) {
|
||||||
|
$this->logger->warning($this->sql . '. use time : ' . $over . 'ms');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user