|
- <?php
-
-
- namespace yii\db;
-
- use Yii;
- use yii\base\Component;
- use yii\base\NotSupportedException;
-
-
- class Command extends Component
- {
-
-
- public $db;
-
-
- public $pdoStatement;
-
-
- public $fetchMode = \PDO::FETCH_ASSOC;
-
-
- public $params = [];
-
-
- public $queryCacheDuration;
-
-
- public $queryCacheDependency;
-
-
-
- private $_pendingParams = [];
-
-
- private $_sql;
-
-
- private $_refreshTableName;
-
-
-
-
- public function cache($duration = null, $dependency = null)
- {
- $this->queryCacheDuration = $duration === null ? $this->db->queryCacheDuration : $duration;
- $this->queryCacheDependency = $dependency;
- return $this;
- }
-
-
-
- public function noCache()
- {
- $this->queryCacheDuration = -1;
- return $this;
- }
-
-
-
- public function getSql()
- {
- return $this->_sql;
- }
-
-
-
- public function setSql($sql)
- {
- if ($sql !== $this->_sql) {
- $this->cancel();
- $this->_sql = $this->db->quoteSql($sql);
- $this->_pendingParams = [];
- $this->params = [];
- $this->_refreshTableName = null;
- }
-
- return $this;
- }
-
-
-
- public function getRawSql()
- {
- if (empty($this->params)) {
- return $this->_sql;
- }
- $params = [];
- foreach ($this->params as $name => $value) {
- if (is_string($name) && strncmp(':', $name, 1)) {
- $name = ':' . $name;
- }
- if (is_string($value)) {
- $params[$name] = $this->db->quoteValue($value);
- } elseif (is_bool($value)) {
- $params[$name] = ($value ? 'TRUE' : 'FALSE');
- } elseif ($value === null) {
- $params[$name] = 'NULL';
- } elseif (!is_object($value) && !is_resource($value)) {
- $params[$name] = $value;
- }
- }
- if (!isset($params[1])) {
- return strtr($this->_sql, $params);
- }
- $sql = '';
- foreach (explode('?', $this->_sql) as $i => $part) {
- $sql .= (isset($params[$i]) ? $params[$i] : '') . $part;
- }
-
- return $sql;
- }
-
-
-
- public function prepare($forRead = null)
- {
- if ($this->pdoStatement) {
- $this->bindPendingParams();
- return;
- }
-
- $sql = $this->getSql();
-
- if ($this->db->getTransaction()) {
-
- $forRead = false;
- }
- if ($forRead || $forRead === null && $this->db->getSchema()->isReadQuery($sql)) {
- $pdo = $this->db->getSlavePdo();
- } else {
- $pdo = $this->db->getMasterPdo();
- }
-
- try {
- $this->pdoStatement = $pdo->prepare($sql);
- $this->bindPendingParams();
- } catch (\Exception $e) {
- $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
- $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
- throw new Exception($message, $errorInfo, (int) $e->getCode(), $e);
- }
- }
-
-
-
- public function cancel()
- {
- $this->pdoStatement = null;
- }
-
-
-
- public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null)
- {
- $this->prepare();
-
- if ($dataType === null) {
- $dataType = $this->db->getSchema()->getPdoType($value);
- }
- if ($length === null) {
- $this->pdoStatement->bindParam($name, $value, $dataType);
- } elseif ($driverOptions === null) {
- $this->pdoStatement->bindParam($name, $value, $dataType, $length);
- } else {
- $this->pdoStatement->bindParam($name, $value, $dataType, $length, $driverOptions);
- }
- $this->params[$name] =& $value;
-
- return $this;
- }
-
-
-
- protected function bindPendingParams()
- {
- foreach ($this->_pendingParams as $name => $value) {
- $this->pdoStatement->bindValue($name, $value[0], $value[1]);
- }
- $this->_pendingParams = [];
- }
-
-
-
- public function bindValue($name, $value, $dataType = null)
- {
- if ($dataType === null) {
- $dataType = $this->db->getSchema()->getPdoType($value);
- }
- $this->_pendingParams[$name] = [$value, $dataType];
- $this->params[$name] = $value;
-
- return $this;
- }
-
-
-
- public function bindValues($values)
- {
- if (empty($values)) {
- return $this;
- }
-
- $schema = $this->db->getSchema();
- foreach ($values as $name => $value) {
- if (is_array($value)) {
- $this->_pendingParams[$name] = $value;
- $this->params[$name] = $value[0];
- } else {
- $type = $schema->getPdoType($value);
- $this->_pendingParams[$name] = [$value, $type];
- $this->params[$name] = $value;
- }
- }
-
- return $this;
- }
-
-
-
- public function query()
- {
- return $this->queryInternal('');
- }
-
-
-
- public function queryAll($fetchMode = null)
- {
- return $this->queryInternal('fetchAll', $fetchMode);
- }
-
-
-
- public function queryOne($fetchMode = null)
- {
- return $this->queryInternal('fetch', $fetchMode);
- }
-
-
-
- public function queryScalar()
- {
- $result = $this->queryInternal('fetchColumn', 0);
- if (is_resource($result) && get_resource_type($result) === 'stream') {
- return stream_get_contents($result);
- } else {
- return $result;
- }
- }
-
-
-
- public function queryColumn()
- {
- return $this->queryInternal('fetchAll', \PDO::FETCH_COLUMN);
- }
-
-
-
- public function insert($table, $columns)
- {
- $params = [];
- $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
-
- return $this->setSql($sql)->bindValues($params);
- }
-
-
-
- public function batchInsert($table, $columns, $rows)
- {
- $sql = $this->db->getQueryBuilder()->batchInsert($table, $columns, $rows);
-
- return $this->setSql($sql);
- }
-
-
-
- public function update($table, $columns, $condition = '', $params = [])
- {
- $sql = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params);
-
- return $this->setSql($sql)->bindValues($params);
- }
-
-
-
- public function delete($table, $condition = '', $params = [])
- {
- $sql = $this->db->getQueryBuilder()->delete($table, $condition, $params);
-
- return $this->setSql($sql)->bindValues($params);
- }
-
-
-
- public function createTable($table, $columns, $options = null)
- {
- $sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);
-
- return $this->setSql($sql);
- }
-
-
-
- public function renameTable($table, $newName)
- {
- $sql = $this->db->getQueryBuilder()->renameTable($table, $newName);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function dropTable($table)
- {
- $sql = $this->db->getQueryBuilder()->dropTable($table);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function truncateTable($table)
- {
- $sql = $this->db->getQueryBuilder()->truncateTable($table);
-
- return $this->setSql($sql);
- }
-
-
-
- public function addColumn($table, $column, $type)
- {
- $sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function dropColumn($table, $column)
- {
- $sql = $this->db->getQueryBuilder()->dropColumn($table, $column);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function renameColumn($table, $oldName, $newName)
- {
- $sql = $this->db->getQueryBuilder()->renameColumn($table, $oldName, $newName);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function alterColumn($table, $column, $type)
- {
- $sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $type);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function addPrimaryKey($name, $table, $columns)
- {
- $sql = $this->db->getQueryBuilder()->addPrimaryKey($name, $table, $columns);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function dropPrimaryKey($name, $table)
- {
- $sql = $this->db->getQueryBuilder()->dropPrimaryKey($name, $table);
-
- return $this->setSql($sql)->requireTableSchemaRefresh($table);
- }
-
-
-
- public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
- {
- $sql = $this->db->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);
-
- return $this->setSql($sql);
- }
-
-
-
- public function dropForeignKey($name, $table)
- {
- $sql = $this->db->getQueryBuilder()->dropForeignKey($name, $table);
-
- return $this->setSql($sql);
- }
-
-
-
- public function createIndex($name, $table, $columns, $unique = false)
- {
- $sql = $this->db->getQueryBuilder()->createIndex($name, $table, $columns, $unique);
-
- return $this->setSql($sql);
- }
-
-
-
- public function dropIndex($name, $table)
- {
- $sql = $this->db->getQueryBuilder()->dropIndex($name, $table);
-
- return $this->setSql($sql);
- }
-
-
-
- public function resetSequence($table, $value = null)
- {
- $sql = $this->db->getQueryBuilder()->resetSequence($table, $value);
-
- return $this->setSql($sql);
- }
-
-
-
- public function checkIntegrity($check = true, $schema = '', $table = '')
- {
- $sql = $this->db->getQueryBuilder()->checkIntegrity($check, $schema, $table);
-
- return $this->setSql($sql);
- }
-
-
-
- public function addCommentOnColumn($table, $column, $comment)
- {
- $sql = $this->db->getQueryBuilder()->addCommentOnColumn($table, $column, $comment);
-
- return $this->setSql($sql);
- }
-
-
-
- public function addCommentOnTable($table, $comment)
- {
- $sql = $this->db->getQueryBuilder()->addCommentOnTable($table, $comment);
-
- return $this->setSql($sql);
- }
-
-
-
- public function dropCommentFromColumn($table, $column)
- {
- $sql = $this->db->getQueryBuilder()->dropCommentFromColumn($table, $column);
-
- return $this->setSql($sql);
- }
-
-
-
- public function dropCommentFromTable($table)
- {
- $sql = $this->db->getQueryBuilder()->dropCommentFromTable($table);
-
- return $this->setSql($sql);
- }
-
-
-
- public function execute()
- {
- $sql = $this->getSql();
-
- $rawSql = $this->getRawSql();
-
- Yii::info($rawSql, __METHOD__);
-
- if ($sql == '') {
- return 0;
- }
-
- $this->prepare(false);
-
- $token = $rawSql;
- try {
- Yii::beginProfile($token, __METHOD__);
-
- $this->pdoStatement->execute();
- $n = $this->pdoStatement->rowCount();
-
- Yii::endProfile($token, __METHOD__);
-
- $this->refreshTableSchema();
-
- return $n;
- } catch (\Exception $e) {
- Yii::endProfile($token, __METHOD__);
- throw $this->db->getSchema()->convertException($e, $rawSql);
- }
- }
-
-
-
- protected function queryInternal($method, $fetchMode = null)
- {
- $rawSql = $this->getRawSql();
-
- Yii::info($rawSql, 'yii\db\Command::query');
-
- if ($method !== '') {
- $info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency);
- if (is_array($info)) {
-
- $cache = $info[0];
- $cacheKey = [
- __CLASS__,
- $method,
- $fetchMode,
- $this->db->dsn,
- $this->db->username,
- $rawSql,
- ];
- $result = $cache->get($cacheKey);
- if (is_array($result) && isset($result[0])) {
- Yii::trace('Query result served from cache', 'yii\db\Command::query');
- return $result[0];
- }
- }
- }
-
- $this->prepare(true);
-
- $token = $rawSql;
- try {
- Yii::beginProfile($token, 'yii\db\Command::query');
-
- $this->pdoStatement->execute();
-
- if ($method === '') {
- $result = new DataReader($this);
- } else {
- if ($fetchMode === null) {
- $fetchMode = $this->fetchMode;
- }
- $result = call_user_func_array([$this->pdoStatement, $method], (array) $fetchMode);
- $this->pdoStatement->closeCursor();
- }
-
- Yii::endProfile($token, 'yii\db\Command::query');
- } catch (\Exception $e) {
- Yii::endProfile($token, 'yii\db\Command::query');
- throw $this->db->getSchema()->convertException($e, $rawSql);
- }
-
- if (isset($cache, $cacheKey, $info)) {
- $cache->set($cacheKey, [$result], $info[1], $info[2]);
- Yii::trace('Saved query result in cache', 'yii\db\Command::query');
- }
-
- return $result;
- }
-
-
-
- protected function requireTableSchemaRefresh($name)
- {
- $this->_refreshTableName = $name;
- return $this;
- }
-
-
-
- protected function refreshTableSchema()
- {
- if ($this->_refreshTableName !== null) {
- $this->db->getSchema()->refreshTableSchema($this->_refreshTableName);
- }
- }
- }
|