Search in sources :

Example 1 with SQLTableElement

use of com.alibaba.druid.sql.ast.statement.SQLTableElement in project druid by alibaba.

the class SQLCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    SQLCreateTableStatement createTable = newCreateStatement();
    if (identifierEquals("GLOBAL")) {
        lexer.nextToken();
        if (identifierEquals("TEMPORARY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
        }
    } else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) {
        lexer.nextToken();
        if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error");
        }
    }
    accept(Token.TABLE);
    createTable.setName(this.exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            if (//
            lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            } else if (//
            lexer.token == Token.PRIMARY || //
            lexer.token == Token.UNIQUE || //
            lexer.token == Token.CHECK || lexer.token == Token.CONSTRAINT) {
                SQLConstraint constraint = this.exprParser.parseConstaint();
                constraint.setParent(createTable);
                createTable.getTableElementList().add((SQLTableElement) constraint);
            } else if (lexer.token() == Token.TABLESPACE) {
                throw new ParserException("TODO " + lexer.token());
            } else {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                if (lexer.token() == Token.RPAREN) {
                    // compatible for sql server
                    break;
                }
                continue;
            }
            break;
        }
        // while
        // (this.tokenList.current().equals(OracleToken.ConstraintToken)) {
        // parseConstaint(table.getConstraints());
        //
        // if (this.tokenList.current().equals(OracleToken.CommaToken))
        // ;
        // lexer.nextToken();
        // }
        accept(Token.RPAREN);
        if (identifierEquals("INHERITS")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            SQLName inherits = this.exprParser.name();
            createTable.setInherits(new SQLExprTableSource(inherits));
            accept(Token.RPAREN);
        }
    }
    return createTable;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 2 with SQLTableElement

use of com.alibaba.druid.sql.ast.statement.SQLTableElement in project dble by actiontech.

the class DruidMysqlCreateTableTest method hasColumn.

private static boolean hasColumn(SQLStatement statement) {
    for (SQLTableElement tableElement : ((SQLCreateTableStatement) statement).getTableElementList()) {
        SQLName sqlName = null;
        if (tableElement instanceof SQLColumnDefinition) {
            sqlName = ((SQLColumnDefinition) tableElement).getName();
        }
        if (sqlName != null) {
            String simpleName = sqlName.getSimpleName();
            simpleName = StringUtil.removeBackQuote(simpleName);
            if (tableElement instanceof SQLColumnDefinition && "_slot".equalsIgnoreCase(simpleName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 3 with SQLTableElement

use of com.alibaba.druid.sql.ast.statement.SQLTableElement in project dble by actiontech.

the class DruidCreateTableParser method removeGlobalColumnIfExist.

private static void removeGlobalColumnIfExist(SQLCreateTableStatement statement) {
    for (SQLTableElement tableElement : statement.getTableElementList()) {
        SQLName sqlName = null;
        if (tableElement instanceof SQLColumnDefinition) {
            sqlName = ((SQLColumnDefinition) tableElement).getName();
        }
        if (sqlName != null) {
            String simpleName = sqlName.getSimpleName();
            simpleName = StringUtil.removeBackQuote(simpleName);
            if (GlobalTableUtil.GLOBAL_TABLE_CHECK_COLUMN.equalsIgnoreCase(simpleName)) {
                statement.getTableElementList().remove(tableElement);
                break;
            }
        }
    }
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 4 with SQLTableElement

use of com.alibaba.druid.sql.ast.statement.SQLTableElement in project Mycat-Server by MyCATApache.

the class GlobalTableUtil method hasGlobalColumn.

private static boolean hasGlobalColumn(SQLStatement statement) {
    for (SQLTableElement tableElement : ((SQLCreateTableStatement) statement).getTableElementList()) {
        SQLName sqlName = null;
        if (tableElement instanceof SQLColumnDefinition) {
            sqlName = ((SQLColumnDefinition) tableElement).getName();
        }
        if (sqlName != null) {
            String simpleName = sqlName.getSimpleName();
            simpleName = StringUtil.removeBackquote(simpleName);
            if (tableElement instanceof SQLColumnDefinition && GLOBAL_TABLE_MYCAT_COLUMN.equalsIgnoreCase(simpleName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 5 with SQLTableElement

use of com.alibaba.druid.sql.ast.statement.SQLTableElement in project druid by alibaba.

the class MySqlShowColumnOutpuVisitor method visit.

public boolean visit(MySqlCreateTableStatement x) {
    List<SQLColumnDefinition> columns = new ArrayList<SQLColumnDefinition>();
    List<String> dataTypes = new ArrayList<String>();
    List<String> defaultValues = new ArrayList<String>();
    int name_len = -1, dataType_len = -1, defaultVal_len = 7, extra_len = 5;
    for (SQLTableElement element : x.getTableElementList()) {
        if (element instanceof SQLColumnDefinition) {
            SQLColumnDefinition column = (SQLColumnDefinition) element;
            columns.add(column);
            String name = SQLUtils.normalize(column.getName().getSimpleName());
            if (name_len < name.length()) {
                name_len = name.length();
            }
            String dataType = column.getDataType().getName();
            if (column.getDataType().getArguments().size() > 0) {
                dataType += "(";
                for (int i = 0; i < column.getDataType().getArguments().size(); i++) {
                    if (i != 0) {
                        dataType += ",";
                    }
                    SQLExpr arg = column.getDataType().getArguments().get(i);
                    dataType += arg.toString();
                }
                dataType += ")";
            }
            if (dataType_len < dataType.length()) {
                dataType_len = dataType.length();
            }
            dataTypes.add(dataType);
            if (column.getDefaultExpr() == null) {
                defaultValues.add(null);
            } else {
                String defaultVal = SQLUtils.toMySqlString(column.getDefaultExpr());
                if (defaultVal.length() > 2 && defaultVal.charAt(0) == '\'' && defaultVal.charAt(defaultVal.length() - 1) == '\'') {
                    defaultVal = defaultVal.substring(1, defaultVal.length() - 1);
                }
                defaultValues.add(defaultVal);
                if (defaultVal_len < defaultVal.length()) {
                    defaultVal_len = defaultVal.length();
                }
            }
            if (column.isAutoIncrement()) {
                extra_len = "auto_increment".length();
            } else if (column.getOnUpdate() != null) {
                extra_len = "on update CURRENT_TIMESTAMP".length();
            }
        }
    }
    print("+-");
    print('-', name_len);
    print("-+-");
    print('-', dataType_len);
    print("-+------+-----+-");
    print('-', defaultVal_len);
    print("-+-");
    print('-', extra_len);
    print("-+\n");
    print("| ");
    print("Field", name_len, ' ');
    print(" | ");
    print("Type", dataType_len, ' ');
    print(" | Null | Key | ");
    print("Default", defaultVal_len, ' ');
    print(" | ");
    print("Extra", extra_len, ' ');
    print(" |\n");
    print("+-");
    print('-', name_len);
    print("-+-");
    print('-', dataType_len);
    print("-+------+-----+-");
    print('-', defaultVal_len);
    print("-+-");
    print('-', extra_len);
    print("-+\n");
    for (int i = 0; i < columns.size(); i++) {
        SQLColumnDefinition column = columns.get(i);
        String name = SQLUtils.normalize(column.getName().getSimpleName());
        print("| ");
        print(name, name_len, ' ');
        print(" | ");
        print(dataTypes.get(i), dataType_len, ' ');
        print(" | ");
        if (column.containsNotNullConstaint()) {
            print("NO ");
        } else {
            print("YES");
        }
        print("  | ");
        MySqlUnique unique = null;
        if (x.isPrimaryColumn(name)) {
            print("PRI");
        } else if (x.isUNI(name)) {
            print("UNI");
        } else if (x.isMUL(name)) {
            print("MUL");
        } else {
            print("   ");
        }
        print(" | ");
        String defaultVal = defaultValues.get(i);
        if (defaultVal == null) {
            print("NULL", defaultVal_len, ' ');
        } else {
            print(defaultVal, defaultVal_len, ' ');
        }
        print(" | ");
        if (column.isAutoIncrement()) {
            print("auto_increment", extra_len, ' ');
        } else if (column.getOnUpdate() != null) {
            print("on update CURRENT_TIMESTAMP", extra_len, ' ');
        } else {
            print(' ', extra_len);
        }
        print(" |");
        print("\n");
    }
    print("+-");
    print('-', name_len);
    print("-+-");
    print('-', dataType_len);
    print("-+------+-----+-");
    print('-', defaultVal_len);
    print("-+-");
    print('-', extra_len);
    print("-+\n");
    return false;
}
Also used : MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) ArrayList(java.util.ArrayList) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)13 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)10 SQLName (com.alibaba.druid.sql.ast.SQLName)9 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)7 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLConstraint (com.alibaba.druid.sql.ast.statement.SQLConstraint)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLListExpr (com.alibaba.druid.sql.ast.expr.SQLListExpr)1 SQLColumnConstraint (com.alibaba.druid.sql.ast.statement.SQLColumnConstraint)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLNotNullConstraint (com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint)1 SQLNullConstraint (com.alibaba.druid.sql.ast.statement.SQLNullConstraint)1 SQLUnique (com.alibaba.druid.sql.ast.statement.SQLUnique)1 AntsparkCreateTableStatement (com.alibaba.druid.sql.dialect.antspark.ast.AntsparkCreateTableStatement)1 BlinkCreateTableStatement (com.alibaba.druid.sql.dialect.blink.ast.BlinkCreateTableStatement)1 HiveInputOutputFormat (com.alibaba.druid.sql.dialect.hive.ast.HiveInputOutputFormat)1 MySqlUnique (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique)1