Search in sources :

Example 16 with FastsqlException

use of com.alibaba.druid.FastsqlException in project druid by alibaba.

the class MySqlCharExpr method output.

public void output(Appendable buf) {
    try {
        if (charset != null) {
            buf.append(charset);
            buf.append(' ');
        }
        if (super.text != null) {
            super.output(buf);
        }
        if (collate != null) {
            buf.append(" COLLATE ");
            buf.append(collate);
        }
    } catch (IOException ex) {
        throw new FastsqlException("output error", ex);
    }
}
Also used : FastsqlException(com.alibaba.druid.FastsqlException) IOException(java.io.IOException)

Example 17 with FastsqlException

use of com.alibaba.druid.FastsqlException 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)

Example 18 with FastsqlException

use of com.alibaba.druid.FastsqlException in project druid by alibaba.

the class SQLEvalVisitorUtils method eval.

public static Object eval(DbType dbType, SQLObject sqlObject, List<Object> parameters, boolean throwError) {
    SQLEvalVisitor visitor = createEvalVisitor(dbType);
    visitor.setParameters(parameters);
    Object value;
    if (sqlObject instanceof SQLValuableExpr) {
        value = ((SQLValuableExpr) sqlObject).getValue();
    } else {
        sqlObject.accept(visitor);
        value = getValue(sqlObject);
        if (value == null) {
            if (throwError && !sqlObject.containsAttribute(EVAL_VALUE)) {
                throw new FastsqlException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType));
            }
        }
    }
    return value;
}
Also used : SQLEvalVisitor(com.alibaba.druid.sql.visitor.SQLEvalVisitor) FastsqlException(com.alibaba.druid.FastsqlException) SQLObject(com.alibaba.druid.sql.ast.SQLObject)

Example 19 with FastsqlException

use of com.alibaba.druid.FastsqlException in project druid by alibaba.

the class SQLDataTypeValidator method validate.

public void validate(SQLDataType x) {
    long hash = x.nameHashCode64();
    if (Arrays.binarySearch(supportTypeHashCodes, hash) < 0) {
        String msg = "illegal dataType : " + x.getName();
        final SQLObject parent = x.getParent();
        if (parent instanceof SQLColumnDefinition) {
            SQLColumnDefinition column = (SQLColumnDefinition) parent;
            if (column.getName() != null) {
                msg += ", column " + column.getName();
            }
        }
        throw new FastsqlException(msg);
    }
}
Also used : FastsqlException(com.alibaba.druid.FastsqlException) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 20 with FastsqlException

use of com.alibaba.druid.FastsqlException in project druid by alibaba.

the class CalciteMySqlNodeVisitor method buildIdentifier.

void buildIdentifier(SQLPropertyExpr x, List<String> names) {
    String name = SQLUtils.normalize(x.getName());
    SQLExpr owner = x.getOwner();
    if (owner instanceof SQLIdentifierExpr) {
        names.add(((SQLIdentifierExpr) owner).normalizedName());
    } else if (owner instanceof SQLPropertyExpr) {
        buildIdentifier((SQLPropertyExpr) owner, names);
    } else {
        throw new FastsqlException("not support : " + owner);
    }
    names.add(name);
}
Also used : FastsqlException(com.alibaba.druid.FastsqlException) DateString(org.apache.calcite.util.DateString) TimestampString(org.apache.calcite.util.TimestampString) TimeString(org.apache.calcite.util.TimeString)

Aggregations

FastsqlException (com.alibaba.druid.FastsqlException)22 IOException (java.io.IOException)16 DateString (org.apache.calcite.util.DateString)2 TimeString (org.apache.calcite.util.TimeString)2 TimestampString (org.apache.calcite.util.TimestampString)2 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)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 SQLEvalVisitor (com.alibaba.druid.sql.visitor.SQLEvalVisitor)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1