Search in sources :

Example 96 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project DataX by alibaba.

the class WriterUtil method preCheckPrePareSQL.

public static void preCheckPrePareSQL(Configuration originalConfig, DataBaseType type) {
    List<Object> conns = originalConfig.getList(Constant.CONN_MARK, Object.class);
    Configuration connConf = Configuration.from(conns.get(0).toString());
    String table = connConf.getList(Key.TABLE, String.class).get(0);
    List<String> preSqls = originalConfig.getList(Key.PRE_SQL, String.class);
    List<String> renderedPreSqls = WriterUtil.renderPreOrPostSqls(preSqls, table);
    if (null != renderedPreSqls && !renderedPreSqls.isEmpty()) {
        LOG.info("Begin to preCheck preSqls:[{}].", StringUtils.join(renderedPreSqls, ";"));
        for (String sql : renderedPreSqls) {
            try {
                DBUtil.sqlValid(sql, type);
            } catch (ParserException e) {
                throw RdbmsException.asPreSQLParserException(type, e, sql);
            }
        }
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) Configuration(com.alibaba.datax.common.util.Configuration)

Example 97 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project DataX by alibaba.

the class WriterUtil method preCheckPostSQL.

public static void preCheckPostSQL(Configuration originalConfig, DataBaseType type) {
    List<Object> conns = originalConfig.getList(Constant.CONN_MARK, Object.class);
    Configuration connConf = Configuration.from(conns.get(0).toString());
    String table = connConf.getList(Key.TABLE, String.class).get(0);
    List<String> postSqls = originalConfig.getList(Key.POST_SQL, String.class);
    List<String> renderedPostSqls = WriterUtil.renderPreOrPostSqls(postSqls, table);
    if (null != renderedPostSqls && !renderedPostSqls.isEmpty()) {
        LOG.info("Begin to preCheck postSqls:[{}].", StringUtils.join(renderedPostSqls, ";"));
        for (String sql : renderedPostSqls) {
            try {
                DBUtil.sqlValid(sql, type);
            } catch (ParserException e) {
                throw RdbmsException.asPostSQLParserException(type, e, sql);
            }
        }
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) Configuration(com.alibaba.datax.common.util.Configuration)

Example 98 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project DataX by alibaba.

the class PreCheckTask method call.

@Override
public Boolean call() throws DataXException {
    String jdbcUrl = this.connection.getString(Key.JDBC_URL);
    List<Object> querySqls = this.connection.getList(Key.QUERY_SQL, Object.class);
    List<Object> splitPkSqls = this.connection.getList(Key.SPLIT_PK_SQL, Object.class);
    List<Object> tables = this.connection.getList(Key.TABLE, Object.class);
    Connection conn = DBUtil.getConnectionWithoutRetry(this.dataBaseType, jdbcUrl, this.userName, password);
    int fetchSize = 1;
    if (DataBaseType.MySql.equals(dataBaseType) || DataBaseType.DRDS.equals(dataBaseType)) {
        fetchSize = Integer.MIN_VALUE;
    }
    try {
        for (int i = 0; i < querySqls.size(); i++) {
            String splitPkSql = null;
            String querySql = querySqls.get(i).toString();
            String table = null;
            if (tables != null && !tables.isEmpty()) {
                table = tables.get(i).toString();
            }
            /*verify query*/
            ResultSet rs = null;
            try {
                DBUtil.sqlValid(querySql, dataBaseType);
                if (i == 0) {
                    rs = DBUtil.query(conn, querySql, fetchSize);
                }
            } catch (ParserException e) {
                throw RdbmsException.asSqlParserException(this.dataBaseType, e, querySql);
            } catch (Exception e) {
                throw RdbmsException.asQueryException(this.dataBaseType, e, querySql, table, userName);
            } finally {
                DBUtil.closeDBResources(rs, null, null);
            }
            /*verify splitPK*/
            try {
                if (splitPkSqls != null && !splitPkSqls.isEmpty()) {
                    splitPkSql = splitPkSqls.get(i).toString();
                    DBUtil.sqlValid(splitPkSql, dataBaseType);
                    if (i == 0) {
                        SingleTableSplitUtil.precheckSplitPk(conn, splitPkSql, fetchSize, table, userName);
                    }
                }
            } catch (ParserException e) {
                throw RdbmsException.asSqlParserException(this.dataBaseType, e, splitPkSql);
            } catch (DataXException e) {
                throw e;
            } catch (Exception e) {
                throw RdbmsException.asSplitPKException(this.dataBaseType, e, splitPkSql, this.splitPkId.trim());
            }
        }
    } finally {
        DBUtil.closeDBResources(null, conn);
    }
    return true;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) DataXException(com.alibaba.datax.common.exception.DataXException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ParserException(com.alibaba.druid.sql.parser.ParserException) RdbmsException(com.alibaba.datax.plugin.rdbms.util.RdbmsException) DataXException(com.alibaba.datax.common.exception.DataXException)

Aggregations

ParserException (com.alibaba.druid.sql.parser.ParserException)98 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)25 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLName (com.alibaba.druid.sql.ast.SQLName)16 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 Token (com.alibaba.druid.sql.parser.Token)9 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)5 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)5 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)4 OracleConstraint (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)4 DbType (com.alibaba.druid.DbType)3 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)3 SQLPartitionByHash (com.alibaba.druid.sql.ast.SQLPartitionByHash)3