/** * Alter Table method * * @param string $type Type of operation to be done * @param array $tables List of tables and fields * @return boolean Return true in case of success, otherwise false * @access protected */ protected function _alterTable($type, $tables) { foreach ($tables as $table => $fields) { $indexes = array(); if (isset($fields['indexes'])) { $indexes = $fields['indexes']; unset($fields['indexes']); } foreach ($fields as $field => $col) { $model = new Model(array('table' => $table, 'ds' => $this->connection)); $tableFields = $this->db->describe($model); if ($type === 'drop') { $field = $col; } if ($type !== 'add' && !isset($tableFields[$field])) { throw new MigrationException($this, sprintf(__d('migrations', 'Field "%s" does not exists in "%s".', true), $field, $table)); } switch ($type) { case 'add': if (isset($tableFields[$field])) { throw new MigrationException($this, sprintf(__d('migrations', 'Field "%s" already exists in "%s".', true), $field, $table)); } $sql = $this->db->alterSchema(array($table => array('add' => array($field => $col)))); break; case 'drop': $sql = $this->db->alterSchema(array($table => array('drop' => array($field => array())))); break; case 'change': $sql = $this->db->alterSchema(array($table => array('change' => array($field => array_merge($tableFields[$field], $col))))); break; case 'rename': $sql = $this->db->alterSchema(array($table => array('change' => array($field => array_merge($tableFields[$field], array('name' => $col)))))); break; } if ($type == 'rename') { $data = array('table' => $table, 'old_name' => $field, 'new_name' => $col); } else { $data = array('table' => $table, 'field' => $field); } $this->_invokeCallbacks('beforeAction', $type . '_field', $data); if (@$this->db->execute($sql) === false) { throw new MigrationException($this, sprintf(__d('migrations', 'SQL Error: %s', true), $this->db->error)); } $this->_invokeCallbacks('afterAction', $type . '_field', $data); } foreach ($indexes as $key => $index) { if (is_numeric($key)) { $key = $index; $index = array(); } $sql = $this->db->alterSchema(array($table => array($type => array('indexes' => array($key => $index))))); $this->_invokeCallbacks('beforeAction', $type . '_index', array('table' => $table, 'index' => $key)); if (@$this->db->execute($sql) === false) { throw new MigrationException($this, sprintf(__d('migrations', 'SQL Error: %s', true), $this->db->error)); } $this->_invokeCallbacks('afterAction', $type . '_index', array('table' => $table, 'index' => $key)); } } return true; }
protected function _execute($sql) { if (@$this->db->execute($sql) === false) { throw new Exception($this, sprintf(__d('Auditable', 'SQL Error: %s'), $this->db->lastError())); } return true; }
/** * (non-PHPdoc) * @see Syncroton_Backend_IBackend::update() */ public function update($model) { if (!$model instanceof $this->_modelInterfaceName) { throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName); } $data = $this->_convertModelToArray($model); $this->_db->execute(sprintf('UPDATE %1$s SET %2$s %3$s', $this->_tablePrefix . $this->_tableName, implode('=?, ', array_keys($data)) . '=?', $this->_db->conditions(array('id' => $model->id))), array(), array_values($data)); return $this->get($model->id); }
/** * Alter Table method * * @param string $type Type of operation to be done * @param array $tables List of tables and fields * @return boolean Return true in case of success, otherwise false * @access protected */ public function _alterTable($type, $tables) { foreach ($tables as $table => $fields) { $indexes = array(); if (isset($fields['indexes'])) { $indexes = $fields['indexes']; unset($fields['indexes']); } foreach ($fields as $field => $col) { switch ($type) { case 'add': $sql = $this->db->alterSchema(array($table => array('add' => array($field => $col)))); break; case 'drop': $field = $col; $sql = $this->db->alterSchema(array($table => array('drop' => array($field => array())))); break; case 'change': $model = new Model(array('table' => $table, 'ds' => $this->connection)); $tableFields = $this->db->describe($model); $sql = $this->db->alterSchema(array($table => array('change' => array($field => array_merge($tableFields[$field], $col))))); break; case 'rename': $model = new Model(array('table' => $table, 'ds' => $this->connection)); $tableFields = $this->db->describe($model); $sql = $this->db->alterSchema(array($table => array('change' => array($field => array_merge($tableFields[$field], array('name' => $col)))))); break; } if ($type == 'rename') { $data = array('table' => $table, 'old_name' => $field, 'new_name' => $col); } else { $data = array('table' => $table, 'field' => $field); } $this->__invokeCallbacks('beforeAction', $type . '_field', $data); $this->db->execute($sql); $this->__invokeCallbacks('afterAction', $type . '_field', $data); } foreach ($indexes as $key => $index) { if (is_numeric($key)) { $key = $index; $index = array(); } $sql = $this->db->alterSchema(array($table => array($type => array('indexes' => array($key => $index))))); $this->__invokeCallbacks('beforeAction', $type . '_index', array('table' => $table, 'index' => $key)); $this->db->execute($sql); $this->__invokeCallbacks('afterAction', $type . '_index', array('table' => $table, 'index' => $key)); } } return true; }
/** * Alter Indexes method * * @param array $indexes List of indexes * @param string $type Type of operation to be done * @param array $tables List of tables and fields * @return void */ protected function _alterIndexes($indexes, $type, $table) { foreach ($indexes as $key => $index) { if (is_numeric($key)) { $key = $index; $index = array(); } $sql = $this->db->alterSchema(array($table => array($type => array('indexes' => array($key => $index))))); $this->_invokeCallbacks('beforeAction', $type . '_index', array('table' => $table, 'index' => $key)); if (@$this->db->execute($sql) === false) { throw new MigrationException($this, sprintf(__d('migrations', 'SQL Error: %s'), $this->db->error)); } $this->_invokeCallbacks('afterAction', $type . '_index', array('table' => $table, 'index' => $key)); } }
/** * @param DataSource $sourceDb * @param $viewName * * @return mixed */ protected function getCreateStatement(DataSource $sourceDb, $viewName) { $resultSet = $sourceDb->execute(sprintf('SHOW CREATE VIEW %s', $viewName)); foreach ($resultSet as $result) { if (array_key_exists('Create View', $result) && !empty($result['Create View'])) { preg_match('/(?:CREATE(?:\\s+OR\\s+REPLACE)?\\s+)' . '(ALGORITHM=[^\\s]+\\s+)?(?:DEFINER=`[^`]+`@`[^`]+`\\s+)?' . '(?:SQL\\s+SECURITY\\s+(?:DEFINER|INVOKER)\\s+)?VIEW\\s+`([^`]+)`\\s+AS\\s+(.*)$/', $result['Create View'], $matches); return sprintf('CREATE OR REPLACE %sVIEW `%s` AS %s', $matches[1], $matches[2], $matches[3]); } } throw new InternalErrorException(sprintf('Could not fetch the create statement for view %s in schema %s. Check your MySQL View fixture settings!', $viewName, $sourceDb->getSchemaName())); }