NukeViet\Core\Database::columns_add PHP Method

columns_add() public method

public columns_add ( string $table, string $column, string $type, integer $length = null, boolean $null = true, string $default = null )
$table string
$column string
$type string
$length integer
$null boolean
$default string
    public function columns_add($table, $column, $type, $length = null, $null = true, $default = null)
    {
        //'type' => 'string|integer
        if ($this->dbtype == 'mysql') {
            if ($type == 'integer') {
                $length = $length ? $length : 2147483647;
                if ($length <= 127) {
                    $type = 'TINYINT';
                } elseif ($length <= 32767) {
                    $type = 'SMALLINT';
                } elseif ($length <= 8388607) {
                    $type = 'MEDIUMINT';
                } elseif ($length <= 2147483647) {
                    $type = 'INT';
                } else {
                    $type = 'BIGINT';
                }
            } else {
                $length = $length ? $length : 65535;
                if ($length <= 255) {
                    $type = 'VARCHAR(' . $length . ')';
                } elseif ($length <= 65535) {
                    $type = 'TEXT';
                } elseif ($length <= 16777215) {
                    $type = 'MEDIUMTEXT';
                } else {
                    $type = 'LONGTEXT';
                }
            }
            $sql = 'ALTER TABLE ' . $table . ' ADD ' . $column . ' ' . $type;
            if ($default !== null) {
                $sql .= ' DEFAULT ';
                if (is_bool($default)) {
                    $sql .= $default ? 'true' : 'false';
                }
                if (is_string($default)) {
                    $sql .= "'" . $default . "'";
                } else {
                    $sql .= $default;
                }
            }
            if (!$null) {
                $sql .= ' NOT NULL';
            }
        } elseif ($this->dbtype == 'oci') {
            if ($type == 'integer') {
                $length = $length ? $length : 2147483647;
                if ($length <= 127) {
                    $type = 'NUMBER(3,0)';
                } elseif ($length <= 32767) {
                    $type = 'NUMBER(5,0)';
                } elseif ($length <= 8388607) {
                    $type = 'NUMBER(8,0)';
                } elseif ($length <= 2147483647) {
                    $type = 'NUMBER(11,0)';
                } else {
                    $type = 'NUMBER(22,0)';
                }
            } else {
                $length = $length ? $length : 65535;
                if ($length <= 4000) {
                    $type = 'VARCHAR2(' . $length . ' CHAR)';
                } else {
                    $type = 'CLOB';
                }
            }
            $sql = 'ALTER TABLE ' . $table . ' ADD (' . $column . ' ' . $type;
            if ($default !== null) {
                $sql .= ' DEFAULT ';
                if (is_bool($default)) {
                    $sql .= $default ? 'true' : 'false';
                }
                if (is_string($default)) {
                    $sql .= "'" . $default . "'";
                } else {
                    $sql .= $default;
                }
            }
            if (!$null) {
                $sql .= ' NOT NULL ENABLE';
            }
            $sql .= ')';
        } else {
            return false;
        }
        try {
            return $this->query($sql);
        } catch (PDOException $e) {
            return false;
        }
    }