PHP-SQL-Parser icon indicating copy to clipboard operation
PHP-SQL-Parser copied to clipboard

Create does not support column COMMENT

Open gallex opened this issue 10 years ago • 4 comments

it would throw Exception : unknown [expr_type] = comment in "CREATE TABLE column-type subtree"

gallex avatar Oct 06 '14 10:10 gallex

Can you provide a full example?

witchi avatar Oct 08 '14 10:10 witchi

I use programme something like this: $parser = new PHPSQLParser(); $parsed = $parser->parse($sql);
... $creator = new PHPSQLCreator(); $new_sql=$creator->create($parsed); ...

when $sql is : CREATE TABLE table1 ( id INT NOT NULL AUTO_INCREMENT COMMENT 'Main KEY', obj BLOB , PRIMARY KEY (id) );

get Exception : unknown [expr_type] = comment in "CREATE TABLE column-type subtree"

gallex avatar Oct 10 '14 15:10 gallex

i fixed it a bit dirty: in ColumnTypeBuilder.php i inserterted a method buildComment() and changed the build method appropriatly:

    protected function buildComment($parsed) {
        if ($parsed['expr_type'] == ExpressionType::COMMENT) {
            return $parsed['base_expr'];
        }
    }

    public function build(array $parsed) {
        if ($parsed['expr_type'] !== ExpressionType::COLUMN_TYPE) {
            return "";
        }
        $sql = "";
        foreach ($parsed['sub_tree'] as $k => $v) {
            $len = strlen($sql);
            $sql .= $this->buildDataType($v);
            $sql .= $this->buildColumnTypeBracketExpression($v);
            $sql .= $this->buildReserved($v);
            $sql .= $this->buildDefaultValue($v);
            $sql .= $this->buildComment($v);

            if ($len == strlen($sql)) {
                throw new UnableToCreateSQLException('CREATE TABLE column-type subtree', $k, $v, 'expr_type');
            }

            $sql .= " ";
        }

        return substr($sql, 0, -1);
    }

roelfsche avatar Feb 12 '15 15:02 roelfsche

Any update on this?

gitetsu avatar Dec 15 '16 07:12 gitetsu