Exemplo n.º 1
0
 /**
  * Shows the table structure
  */
 public function actionStructure()
 {
     $table = $this->loadTable();
     if (!$table instanceof Table) {
         $response = new AjaxResponse();
         $response->addNotification("error", Yii::t("core", "tableLoadErrorTitle", array("{table}" => $this->table)), Yii::t("core", "tableLoadErrorMessage", array("{table}" => $this->table)));
         $response->executeJavaScript("sideBar.loadTables(schema)");
         $this->sendJSON($response);
     }
     // Constraints
     if (StorageEngine::check($table->ENGINE, StorageEngine::SUPPORTS_FOREIGN_KEYS)) {
         $foreignKeys = array();
         $sql = 'SELECT * FROM KEY_COLUMN_USAGE ' . 'WHERE TABLE_SCHEMA = :tableSchema ' . 'AND TABLE_NAME = :tableName ' . 'AND REFERENCED_TABLE_SCHEMA IS NOT NULL';
         $table->foreignKeys = ForeignKey::model()->findAllBySql($sql, array('tableSchema' => $table->TABLE_SCHEMA, 'tableName' => $table->TABLE_NAME));
         foreach ($table->foreignKeys as $key) {
             $foreignKeys[] = $key->COLUMN_NAME;
         }
     } else {
         $foreignKeys = false;
     }
     // Indices
     $sql = 'SELECT * FROM STATISTICS ' . 'WHERE TABLE_SCHEMA = :tableSchema ' . 'AND TABLE_NAME = :tableName ' . 'GROUP BY INDEX_NAME ' . 'ORDER BY INDEX_NAME = \'PRIMARY\' DESC, INDEX_NAME';
     $table->indices = Index::model()->findAllBySql($sql, array('tableSchema' => $table->TABLE_SCHEMA, 'tableName' => $table->TABLE_NAME));
     foreach ($table->indices as $index) {
         $index->columns = IndexColumn::model()->findAllByAttributes(array('TABLE_SCHEMA' => $table->TABLE_SCHEMA, 'TABLE_NAME' => $table->TABLE_NAME, 'INDEX_NAME' => $index->INDEX_NAME));
     }
     // Indices (seperate for each column)
     $indicesRaw = Index::model()->findAllByAttributes(array('TABLE_SCHEMA' => $table->TABLE_SCHEMA, 'TABLE_NAME' => $table->TABLE_NAME));
     // Triggers
     $table->triggers = Trigger::model()->findAllByAttributes(array('EVENT_OBJECT_SCHEMA' => $table->TABLE_SCHEMA, 'EVENT_OBJECT_TABLE' => $table->TABLE_NAME));
     $this->render('structure', array('table' => $table, 'canAlter' => Yii::app()->user->privileges->checkTable($table->TABLE_SCHEMA, $table->TABLE_NAME, 'ALTER'), 'foreignKeys' => $foreignKeys, 'indicesRaw' => $indicesRaw));
 }
Exemplo n.º 2
0
 public function testUpdatePrimaryKey()
 {
     // Load index
     $index = Index::model()->findByAttributes(array('INDEX_NAME' => 'UNIQUE', 'TABLE_NAME' => 'table4', 'TABLE_SCHEMA' => 'indextest'));
     $index->setType('PRIMARY');
     // Try saving
     $index->save();
     // Reload index and load index columns
     $index->refresh();
     // Check properties
     $this->assertEquals('PRIMARY', $index->INDEX_NAME);
     $this->assertEquals('PRIMARY', $index->getType());
 }
Exemplo n.º 3
0
 public function actionDrop()
 {
     // Get post vars
     $indexName = Yii::app()->request->getPost('index');
     $response = new AjaxResponse();
     try {
         $index = Index::model()->findByAttributes(array('TABLE_SCHEMA' => $this->schema, 'TABLE_NAME' => $this->table, 'INDEX_NAME' => $indexName));
         $index->throwExceptions = true;
         $sql = $index->delete();
         $response->addNotification('success', Yii::t('core', 'successDropIndex', array('{index}' => $index->INDEX_NAME)), null, $sql);
         $response->addData('success', true);
     } catch (DbException $ex) {
         $response->addNotification('error', Yii::t('core', 'errorDropIndex', array('{index}' => $indexName)), $ex->getText(), $ex->getSql());
         $response->addData('success', false);
     }
     $this->sendJSON($response);
 }