Postgres::createTable PHP Метод

createTable() публичный Метод

Creates a new table in the database
public createTable ( $name, $fields, $field, $type, $array, $length, $notnull, $default, $withoutoids, $colcomment, $tblcomment, $tablespace, $uniquekey, $primarykey ) : -1
$name The name of the table
$fields The number of fields
$field An array of field names
$type An array of field types
$array An array of '' or '[]' for each type if it's an array or not
$length An array of field lengths
$notnull An array of not null
$default An array of default values
$withoutoids True if WITHOUT OIDS, false otherwise
$colcomment An array of comments
$tablespace The tablespace name ('' means none/default)
$uniquekey An Array indicating the fields that are unique (those indexes that are set)
$primarykey An Array indicating the field used for the primarykey (those indexes that are set)
Результат -1
    function createTable($name, $fields, $field, $type, $array, $length, $notnull, $default, $withoutoids, $colcomment, $tblcomment, $tablespace, $uniquekey, $primarykey)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($name);
        $status = $this->beginTransaction();
        if ($status != 0) {
            return -1;
        }
        $found = false;
        $first = true;
        $comment_sql = '';
        //Accumulate comments for the columns
        $sql = "CREATE TABLE \"{$f_schema}\".\"{$name}\" (";
        for ($i = 0; $i < $fields; $i++) {
            $this->fieldClean($field[$i]);
            $this->clean($type[$i]);
            $this->clean($length[$i]);
            $this->clean($colcomment[$i]);
            // Skip blank columns - for user convenience
            if ($field[$i] == '' || $type[$i] == '') {
                continue;
            }
            // If not the first column, add a comma
            if (!$first) {
                $sql .= ", ";
            } else {
                $first = false;
            }
            switch ($type[$i]) {
                // Have to account for weird placing of length for with/without
                // time zone types
                case 'timestamp with time zone':
                case 'timestamp without time zone':
                    $qual = substr($type[$i], 9);
                    $sql .= "\"{$field[$i]}\" timestamp";
                    if ($length[$i] != '') {
                        $sql .= "({$length[$i]})";
                    }
                    $sql .= $qual;
                    break;
                case 'time with time zone':
                case 'time without time zone':
                    $qual = substr($type[$i], 4);
                    $sql .= "\"{$field[$i]}\" time";
                    if ($length[$i] != '') {
                        $sql .= "({$length[$i]})";
                    }
                    $sql .= $qual;
                    break;
                default:
                    $sql .= "\"{$field[$i]}\" {$type[$i]}";
                    if ($length[$i] != '') {
                        $sql .= "({$length[$i]})";
                    }
            }
            // Add array qualifier if necessary
            if ($array[$i] == '[]') {
                $sql .= '[]';
            }
            // Add other qualifiers
            if (!isset($primarykey[$i])) {
                if (isset($uniquekey[$i])) {
                    $sql .= " UNIQUE";
                }
                if (isset($notnull[$i])) {
                    $sql .= " NOT NULL";
                }
            }
            if ($default[$i] != '') {
                $sql .= " DEFAULT {$default[$i]}";
            }
            if ($colcomment[$i] != '') {
                $comment_sql .= "COMMENT ON COLUMN \"{$name}\".\"{$field[$i]}\" IS '{$colcomment[$i]}';\n";
            }
            $found = true;
        }
        if (!$found) {
            return -1;
        }
        // PRIMARY KEY
        $primarykeycolumns = array();
        for ($i = 0; $i < $fields; $i++) {
            if (isset($primarykey[$i])) {
                $primarykeycolumns[] = "\"{$field[$i]}\"";
            }
        }
        if (count($primarykeycolumns) > 0) {
            $sql .= ", PRIMARY KEY (" . implode(", ", $primarykeycolumns) . ")";
        }
        $sql .= ")";
        // WITHOUT OIDS
        if ($withoutoids) {
            $sql .= ' WITHOUT OIDS';
        } else {
            $sql .= ' WITH OIDS';
        }
        // Tablespace
        if ($this->hasTablespaces() && $tablespace != '') {
            $this->fieldClean($tablespace);
            $sql .= " TABLESPACE \"{$tablespace}\"";
        }
        $status = $this->execute($sql);
        if ($status) {
            $this->rollbackTransaction();
            return -1;
        }
        if ($tblcomment != '') {
            $status = $this->setComment('TABLE', '', $name, $tblcomment, true);
            if ($status) {
                $this->rollbackTransaction();
                return -1;
            }
        }
        if ($comment_sql != '') {
            $status = $this->execute($comment_sql);
            if ($status) {
                $this->rollbackTransaction();
                return -1;
            }
        }
        return $this->endTransaction();
    }
Postgres