示例#1
0
 /**
  * Executes the given sql statement(s).
  *
  * @param	mixed					sql statement(s)
  * @return	mixed					sql statement(s) (imploded) or false
  */
 private function executeSql($sql)
 {
     try {
         $sql = (array) $sql;
         foreach ($sql as $sql1) {
             $cmd = new CDbCommand(self::$db, $sql1);
             $cmd->prepare();
             $cmd->execute();
             $this->afterSave();
             $this->refresh();
         }
         return implode("\n", $sql);
     } catch (CDbException $ex) {
         $this->afterSave();
         if ($this->throwExceptions) {
             throw new DbException($cmd);
         } else {
             $errorInfo = $cmd->getPdoStatement()->errorInfo();
             $this->addError(null, Yii::t('core', 'sqlErrorOccured', array('{errno}' => $errorInfo[1], '{errmsg}' => $errorInfo[2])));
             return false;
         }
     }
 }
示例#2
0
文件: Row.php 项目: cebe/chive
 public function update($attributes = null)
 {
     if ($this->getIsNewRecord()) {
         throw new CDbException(Yii::t('core', 'The active record cannot be updated because it is new.'));
     }
     if (!$this->beforeSave()) {
         return false;
     }
     $sql = '';
     $table = Table::model()->findByPk(array('TABLE_NAME' => self::$table, 'TABLE_SCHEMA' => self::$schema));
     // Check if there has been changed any attribute
     $changedAttributes = array();
     foreach ($this->originalAttributes as $column => $value) {
         if ($this->getAttribute($column) !== $value || $this->getFunction($column)) {
             // SET datatype
             $changedAttributes[$column] = $this->getAttribute($column);
         }
     }
     $changedAttributesCount = count($changedAttributes);
     if ($changedAttributesCount > 0) {
         $sql = 'UPDATE ' . self::$db->quoteTableName(self::$table) . ' SET ' . "\n";
         foreach ($changedAttributes as $column => $value) {
             $columnInfo = $this->getColumnInfo($table, $column);
             $function = $this->getFunction($column);
             $sql .= "\t" . self::$db->quoteColumnName($column) . ' = ';
             if ($function !== null) {
                 $sql .= self::$functions[$function] . '(' . ($value === null ? 'NULL' : self::$db->quoteValue($value)) . ')';
             } elseif ($columnInfo->IS_NULLABLE === "YES" && is_null($value)) {
                 $sql .= 'NULL';
             } elseif ($this->isHex($column)) {
                 $sql .= $value;
             } elseif (is_array($value)) {
                 $sql .= self::$db->quoteValue(implode(",", $value));
             } elseif ($columnInfo->DATA_TYPE == "int") {
                 $sql .= (int) $value;
             } elseif ($columnInfo->DATA_TYPE == "bit") {
                 $sql .= (int) $value;
             } else {
                 $sql .= is_null($value) ? 'NULL' : self::$db->quoteValue($value);
             }
             $changedAttributesCount--;
             if ($changedAttributesCount > 0) {
                 $sql .= ',' . "\n";
             }
         }
         $sql .= "\n" . 'WHERE ' . "\n";
         $identifier = $this->getOriginalIdentifier();
         // Create find criteria
         $count = count($identifier);
         foreach ($identifier as $column => $value) {
             if (is_null($value)) {
                 $sql .= "\t" . self::$db->quoteColumnName($column) . ' IS NULL ';
             } else {
                 $sql .= "\t" . self::$db->quoteColumnName($column) . ' = ' . self::$db->quoteValue($this->originalAttributes[$column]) . ' ';
             }
             $count--;
             if ($count > 0) {
                 $sql .= 'AND ' . "\n";
             }
         }
         $sql .= "\n" . 'LIMIT 1;';
     }
     $cmd = new CDbCommand(self::$db, $sql);
     try {
         $cmd->prepare();
         $cmd->execute();
         $this->afterSave();
         return $sql;
     } catch (CDbException $ex) {
         throw new DbException($cmd);
     }
 }
 public function prepare()
 {
     parent::prepare();
     $this->toExplain = strncasecmp(ltrim($this->getText()), "select", strlen("select")) === 0;
     if ($this->toExplain && $this->_statement_explain == null) {
         try {
             $this->_statement_explain = $this->getConnection()->getPdoInstance()->prepare("EXPLAIN " . $this->getText());
         } catch (Exception $e) {
             Yii::log('Error in preparing Explain SQL: ' . $this->getText(), CLogger::LEVEL_ERROR, 'system.db.CDbCommand');
             $errorInfo = $e instanceof PDOException ? $e->errorInfo : null;
             throw new CDbException(Yii::t('yii', 'CDbCommand failed to prepare the SQL statement: {error}', array('{error}' => $e->getMessage())), (int) $e->getCode(), $errorInfo);
         }
     }
 }
示例#4
0
文件: Column.php 项目: cebe/chive
 public function move($command)
 {
     $sql = 'ALTER TABLE ' . self::$db->quoteTableName($this->TABLE_NAME) . "\n" . "\t" . 'MODIFY ' . $this->getColumnDefinition() . ' ' . (substr($command, 0, 6) == 'AFTER ' ? 'AFTER ' . self::$db->quoteColumnName(substr($command, 6)) : 'FIRST') . ';';
     $cmd = new CDbCommand(self::$db, $sql);
     try {
         $cmd->prepare();
         $cmd->execute();
         return $sql;
     } catch (CDbException $ex) {
         throw new DbException($cmd);
     }
 }