Commit b0cc86df by Qiang Xue

added Command::getRawSql()

parent 597082a1
...@@ -99,6 +99,39 @@ class Command extends \yii\base\Component ...@@ -99,6 +99,39 @@ class Command extends \yii\base\Component
} }
/** /**
* Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]].
* Note that the return value of this method should mainly be used for logging purpose.
* It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders.
* @return string the raw SQL
*/
public function getRawSql()
{
if ($this->_params === array()) {
return $this->_sql;
} else {
$params = array();
foreach ($this->_params as $name => $value) {
if (is_string($value)) {
$params[$name] = $this->db->quoteValue($value);
} elseif ($value === null) {
$params[$name] = 'NULL';
} else {
$params[$name] = $value;
}
}
if (isset($params[1])) {
$sql = '';
foreach (explode('?', $this->_sql) as $i => $part) {
$sql .= (isset($params[$i]) ? $params[$i] : '') . $part;
}
return $sql;
} else {
return strtr($this->_sql, $params);
}
}
}
/**
* Prepares the SQL statement to be executed. * Prepares the SQL statement to be executed.
* For complex SQL statement that is to be executed multiple times, * For complex SQL statement that is to be executed multiple times,
* this may improve performance. * this may improve performance.
...@@ -222,6 +255,7 @@ class Command extends \yii\base\Component ...@@ -222,6 +255,7 @@ class Command extends \yii\base\Component
'boolean' => \PDO::PARAM_BOOL, 'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT, 'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR, 'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL, 'NULL' => \PDO::PARAM_NULL,
); );
$type = gettype($data); $type = gettype($data);
...@@ -239,13 +273,9 @@ class Command extends \yii\base\Component ...@@ -239,13 +273,9 @@ class Command extends \yii\base\Component
{ {
$sql = $this->getSql(); $sql = $this->getSql();
if ($this->_params === array()) { $rawSql = $this->getRawSql();
$paramLog = '';
} else {
$paramLog = "\nParameters: " . var_export($this->_params, true);
}
Yii::trace("Executing SQL: {$sql}{$paramLog}", __METHOD__); Yii::trace("Executing SQL: $rawSql", __METHOD__);
if ($sql == '') { if ($sql == '') {
return 0; return 0;
...@@ -271,7 +301,7 @@ class Command extends \yii\base\Component ...@@ -271,7 +301,7 @@ class Command extends \yii\base\Component
} }
$message = $e->getMessage(); $message = $e->getMessage();
Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __METHOD__); Yii::error("$message\nFailed to execute SQL: $rawSql", __METHOD__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode()); throw new Exception($message, $errorInfo, (int)$e->getCode());
...@@ -357,13 +387,9 @@ class Command extends \yii\base\Component ...@@ -357,13 +387,9 @@ class Command extends \yii\base\Component
{ {
$db = $this->db; $db = $this->db;
$sql = $this->getSql(); $sql = $this->getSql();
if ($this->_params === array()) { $rawSql = $this->getRawSql();
$paramLog = '';
} else {
$paramLog = "\nParameters: " . var_export($this->_params, true);
}
Yii::trace("Querying SQL: {$sql}{$paramLog}", __METHOD__); Yii::trace("Querying SQL: $rawSql", __METHOD__);
/** @var $cache \yii\caching\Cache */ /** @var $cache \yii\caching\Cache */
if ($db->enableQueryCache && $method !== '') { if ($db->enableQueryCache && $method !== '') {
...@@ -375,8 +401,7 @@ class Command extends \yii\base\Component ...@@ -375,8 +401,7 @@ class Command extends \yii\base\Component
__CLASS__, __CLASS__,
$db->dsn, $db->dsn,
$db->username, $db->username,
$sql, $rawSql,
$paramLog,
)); ));
if (($result = $cache->get($cacheKey)) !== false) { if (($result = $cache->get($cacheKey)) !== false) {
Yii::trace('Query result served from cache', __METHOD__); Yii::trace('Query result served from cache', __METHOD__);
...@@ -418,7 +443,7 @@ class Command extends \yii\base\Component ...@@ -418,7 +443,7 @@ class Command extends \yii\base\Component
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
} }
$message = $e->getMessage(); $message = $e->getMessage();
Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __METHOD__); Yii::error("$message\nCommand::$method() failed: $rawSql", __METHOD__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode()); throw new Exception($message, $errorInfo, (int)$e->getCode());
} }
......
...@@ -83,7 +83,7 @@ abstract class Schema extends \yii\base\Object ...@@ -83,7 +83,7 @@ abstract class Schema extends \yii\base\Object
} }
$db = $this->db; $db = $this->db;
$realName = $this->getRealTableName($name); $realName = $this->getRawTableName($name);
if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) { if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) {
/** @var $cache Cache */ /** @var $cache Cache */
...@@ -318,13 +318,13 @@ abstract class Schema extends \yii\base\Object ...@@ -318,13 +318,13 @@ abstract class Schema extends \yii\base\Object
} }
/** /**
* Returns the real name of a table name. * Returns the actual name of a given table name.
* This method will strip off curly brackets from the given table name * This method will strip off curly brackets from the given table name
* and replace the percentage character '%' with [[Connection::tablePrefix]]. * and replace the percentage character '%' with [[Connection::tablePrefix]].
* @param string $name the table name to be converted * @param string $name the table name to be converted
* @return string the real name of the given table name * @return string the real name of the given table name
*/ */
public function getRealTableName($name) public function getRawTableName($name)
{ {
if (strpos($name, '{{') !== false) { if (strpos($name, '{{') !== false) {
$name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name); $name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment