Postgres::createCompositeType PHP Method

createCompositeType() public method

Creates a new composite type in the database
public createCompositeType ( $name, $fields, $field, $type, $array, $length, $colcomment, $typcomment ) : -1
$name The name of the type
$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
$colcomment An array of comments
$typcomment Type comment
return -1
    function createCompositeType($name, $fields, $field, $type, $array, $length, $colcomment, $typcomment)
    {
        $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 TYPE \"{$f_schema}\".\"{$name}\" AS (";
        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 .= '[]';
            }
            if ($colcomment[$i] != '') {
                $comment_sql .= "COMMENT ON COLUMN \"{$f_schema}\".\"{$name}\".\"{$field[$i]}\" IS '{$colcomment[$i]}';\n";
            }
            $found = true;
        }
        if (!$found) {
            return -1;
        }
        $sql .= ")";
        $status = $this->execute($sql);
        if ($status) {
            $this->rollbackTransaction();
            return -1;
        }
        if ($typcomment != '') {
            $status = $this->setComment('TYPE', $name, '', $typcomment, 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