public function actionInstallDatabase() { $db = Yii::app()->getDb(); $tableName = $this->getTableName(); $createTable = new CDbCommand($db, "\n CREATE TABLE `{$tableName}` (\n `id` int(11) unsigned NOT NULL auto_increment,\n `lft` int(11) default NULL,\n `rgt` int(11) default NULL,\n `level` smallint(3) default NULL,\n `parent_id` int(11) default NULL,\n `type` smallint(1) default NULL,\n `url` varchar(255) default NULL,\n `name` varchar(255) default NULL,\n `title` varchar(255) default NULL,\n `content` text,\n `layout` varchar(255) default NULL,\n `section` varchar(255) default NULL,\n `subsection` varchar(255) default NULL,\n `overview_page` tinyint(1) default NULL,\n `keywords` varchar(255) default NULL,\n `description` varchar(255) default NULL,\n `access_level` tinyint(1) default NULL,\n PRIMARY KEY (`id`),\n KEY `lft` (`lft`),\n KEY `rgt` (`rgt`),\n KEY `level` (`level`),\n KEY `name` (`name`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n "); $tableCreated = $createTable->execute(); $initialData = new CDbCommand($db, "\n insert into `{$tableName}` \n (`id`,`lft`,`rgt`,`level`,`parent_id`,`type`,`url`,`name`,`title`,`content`,`layout`,`section`,`subsection`,`overview_page`,`keywords`,`description`,`access_level`) \n values (1,0,1,0,0,0,'/','Site index','','',NULL,NULL,NULL,NULL,NULL,NULL,NULL)\n "); $tableFilled = $initialData->execute(); if ($tableCreated == 0 && $tableFilled) { $this->render('index', array('result' => "Database installed successfully!!! Table name: {$tableName}")); } }
/** * 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; } } }
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); } }
/** * Execute a bookmark */ public function actionExecute() { $id = Yii::app()->getRequest()->getParam('id'); $response = new AjaxResponse(); $response->refresh = true; $bookmark = Yii::app()->user->settings->get('bookmarks', 'database', $this->schema, 'id', $id); try { $cmd = new CDbCommand($this->db, $bookmark['query']); $cmd->execute(); $response->addNotification('success', Yii::t('core', 'successExecuteBookmark', array('{name}' => $bookmark['name'])), null, $bookmark['query']); } catch (Exception $ex) { $response->addNotification('error', $ex->getMessage(), $bookmark['query'], array('isSticky' => true)); } $this->sendJSON($response); }
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); } }
public function actionReset_count() { $db_command = new CDbCommand(Yii::app()->db, 'truncate table `adtest`'); $db_command->execute(); $this->redirect(array('default/index', '#' => 'banner-id-54449')); }