コード例 #1
0
ファイル: Forge.php プロジェクト: titounnes/CodeIgniter4
 /**
  * ALTER TABLE
  *
  * @param    string $alter_type ALTER type
  * @param    string $table      Table name
  * @param    mixed  $field      Column definition
  *
  * @return    string|string[]
  */
 protected function _alterTable($alter_type, $table, $field)
 {
     if (in_array($alter_type, ['DROP', 'ADD'], true)) {
         return parent::_alterTable($alter_type, $table, $field);
     }
     $sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
     $sqls = [];
     for ($i = 0, $c = count($field); $i < $c; $i++) {
         if ($field[$i]['_literal'] !== false) {
             return false;
         }
         if (version_compare($this->db->getVersion(), '8', '>=') && isset($field[$i]['type'])) {
             $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name']) . " TYPE {$field[$i]['type']}{$field[$i]['length']}";
         }
         if (!empty($field[$i]['default'])) {
             $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name']) . " SET DEFAULT {$field[$i]['default']}";
         }
         if (isset($field[$i]['null'])) {
             $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name']) . ($field[$i]['null'] === true ? ' DROP' : ' SET') . ' NOT NULL';
         }
         if (!empty($field[$i]['new_name'])) {
             $sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name']) . ' TO ' . $this->db->escapeIdentifiers($field[$i]['new_name']);
         }
         if (!empty($field[$i]['comment'])) {
             $sqls[] = 'COMMENT ON COLUMN' . $this->db->escapeIdentifiers($table) . '.' . $this->db->escapeIdentifiers($field[$i]['name']) . " IS {$field[$i]['comment']}";
         }
     }
     return $sqls;
 }
コード例 #2
0
ファイル: Forge.php プロジェクト: titounnes/CodeIgniter4
 /**
  * ALTER TABLE
  *
  * @param	string	$alter_type	ALTER type
  * @param	string	$table		Table name
  * @param	mixed	$field		Column definition
  * @return	string|string[]
  */
 protected function _alterTable($alter_type, $table, $field)
 {
     if ($alter_type === 'DROP') {
         return parent::_alterTable($alter_type, $table, $field);
     }
     $sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
     for ($i = 0, $c = count($field); $i < $c; $i++) {
         if ($field[$i]['_literal'] !== FALSE) {
             $field[$i] = $alter_type === 'ADD' ? "\n\tADD " . $field[$i]['_literal'] : "\n\tMODIFY " . $field[$i]['_literal'];
         } else {
             if ($alter_type === 'ADD') {
                 $field[$i]['_literal'] = "\n\tADD ";
             } else {
                 $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
             }
             $field[$i] = $field[$i]['_literal'] . $this->_processColumn($field[$i]);
         }
     }
     return array($sql . implode(',', $field));
 }