Search in sources :

Example 1 with SQLShowColumnsStatement

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

the class SchemaRepository method console.

public String console(String input) {
    try {
        StringBuffer buf = new StringBuffer();
        List<SQLStatement> stmtList = SQLUtils.parseStatements(input, dbType);
        for (SQLStatement stmt : stmtList) {
            if (stmt instanceof SQLShowColumnsStatement) {
                SQLShowColumnsStatement showColumns = ((SQLShowColumnsStatement) stmt);
                SQLName db = showColumns.getDatabase();
                Schema schema;
                if (db == null) {
                    schema = getDefaultSchema();
                } else {
                    schema = findSchema(db.getSimpleName());
                }
                SQLName table = null;
                SchemaObject schemaObject = null;
                if (schema != null) {
                    table = showColumns.getTable();
                    schemaObject = schema.findTable(table.nameHashCode64());
                }
                if (schemaObject == null) {
                    buf.append("ERROR 1146 (42S02): Table '" + table + "' doesn't exist\n");
                } else {
                    MySqlCreateTableStatement createTableStmt = (MySqlCreateTableStatement) schemaObject.getStatement();
                    createTableStmt.showCoumns(buf);
                }
            } else if (stmt instanceof SQLShowCreateTableStatement) {
                SQLShowCreateTableStatement showCreateTableStmt = (SQLShowCreateTableStatement) stmt;
                SQLName table = showCreateTableStmt.getName();
                SchemaObject schemaObject = findTable(table);
                if (schemaObject == null) {
                    buf.append("ERROR 1146 (42S02): Table '" + table + "' doesn't exist\n");
                } else {
                    MySqlCreateTableStatement createTableStmt = (MySqlCreateTableStatement) schemaObject.getStatement();
                    createTableStmt.output(buf);
                }
            } else if (stmt instanceof MySqlRenameTableStatement) {
                MySqlRenameTableStatement renameStmt = (MySqlRenameTableStatement) stmt;
                for (MySqlRenameTableStatement.Item item : renameStmt.getItems()) {
                    renameTable(item.getName(), item.getTo());
                }
            } else if (stmt instanceof SQLShowTablesStatement) {
                SQLShowTablesStatement showTables = (SQLShowTablesStatement) stmt;
                SQLName database = showTables.getDatabase();
                Schema schema;
                if (database == null) {
                    schema = getDefaultSchema();
                } else {
                    schema = findSchema(database.getSimpleName());
                }
                if (schema != null) {
                    for (String table : schema.showTables()) {
                        buf.append(table);
                        buf.append('\n');
                    }
                }
            } else {
                stmt.accept(consoleVisitor);
            }
        }
        if (buf.length() == 0) {
            return "\n";
        }
        return buf.toString();
    } catch (IOException ex) {
        throw new FastsqlException("exeucte command error.", ex);
    }
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) IOException(java.io.IOException) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLShowCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLShowCreateTableStatement) FastsqlException(com.alibaba.druid.FastsqlException) SQLShowColumnsStatement(com.alibaba.druid.sql.ast.statement.SQLShowColumnsStatement) MySqlRenameTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)

Aggregations

FastsqlException (com.alibaba.druid.FastsqlException)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLShowColumnsStatement (com.alibaba.druid.sql.ast.statement.SQLShowColumnsStatement)1 SQLShowCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLShowCreateTableStatement)1 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)1 MySqlRenameTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement)1 IOException (java.io.IOException)1