Java Code Examples for com.alibaba.fastsql.sql.dialect.mysql.parser.MySqlStatementParser

The following examples show how to use com.alibaba.fastsql.sql.dialect.mysql.parser.MySqlStatementParser. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: canal-1.1.3   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 解析sql
 *
 * @param sql sql
 * @return 视图对象
 */
public static SchemaItem parse(String sql) {
    try {
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
        MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) statement.getSelect().getQuery();

        SchemaItem schemaItem = new SchemaItem();
        schemaItem.setSql(SQLUtils.toMySqlString(sqlSelectQueryBlock));
        SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
        List<TableItem> tableItems = new ArrayList<>();
        SqlParser.visitSelectTable(schemaItem, sqlTableSource, tableItems, null);
        tableItems.forEach(tableItem -> schemaItem.getAliasTableItems().put(tableItem.getAlias(), tableItem));

        List<FieldItem> fieldItems = collectSelectQueryFields(sqlSelectQueryBlock);
        fieldItems.forEach(fieldItem -> schemaItem.getSelectFields().put(fieldItem.getFieldName(), fieldItem));

        schemaItem.init();

        if (schemaItem.getAliasTableItems().isEmpty() || schemaItem.getSelectFields().isEmpty()) {
            throw new ParserException("Parse sql error");
        }
        return schemaItem;
    } catch (Exception e) {
        throw new ParserException();
    }
}
 
Example 2
Source Project: Mycat2   Source File: InserParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
public InserParser(String createTableStmttext, Iterator<String> lines) {
    super(SQL2ResultSetUtil.getMycatRowMetaData(createTableStmttext), StreamSupport.stream(Spliterators.spliteratorUnknownSize(lines, 0), false).flatMap(s -> {
        MySqlStatementParser sqlStatementParser = new MySqlStatementParser(s, UseInsertColumnsCache, InsertValueNative, OptimizedForParameterized, OptimizedForForParameterizedSkipValue, InsertValueCheckType);
        MySqlInsertStatement insertStatement = (MySqlInsertStatement) sqlStatementParser.parseInsert();
        return insertStatement.getValuesList().stream();
    }).map(valuesClause -> {
        List valuesClause1 = valuesClause.getValues();
        if (valuesClause1.isEmpty()) {
            return EMPTY;
        } else {
            Object o = valuesClause1.get(0);
            if (o instanceof String) {
                return (String[]) valuesClause1.toArray(EMPTY);
            } else {
                String[] objects = new String[valuesClause.getValues().size()];
                int i = 0;
                for (Object value : valuesClause.getValues()) {
                    if (value instanceof SQLValuableExpr) {
                        objects[i++] = ((SQLCharExpr) value).getText();
                    } else if (value instanceof String) {
                        throw new UnsupportedOperationException();
                    }
                }
                return objects;
            }
        }
    }).iterator());
}
 
Example 3
Source Project: canal   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 解析sql
 *
 * @param sql sql
 * @return 视图对象
 */
public static SchemaItem parse(String sql) {
    try {
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
        MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) statement.getSelect().getQuery();

        SchemaItem schemaItem = new SchemaItem();
        schemaItem.setSql(SQLUtils.toMySqlString(sqlSelectQueryBlock));
        SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
        List<TableItem> tableItems = new ArrayList<>();
        SqlParser.visitSelectTable(schemaItem, sqlTableSource, tableItems, null);
        tableItems.forEach(tableItem -> schemaItem.getAliasTableItems().put(tableItem.getAlias(), tableItem));

        List<FieldItem> fieldItems = collectSelectQueryFields(sqlSelectQueryBlock);
        fieldItems.forEach(fieldItem -> schemaItem.getSelectFields().put(fieldItem.getFieldName(), fieldItem));

        schemaItem.init();

        if (schemaItem.getAliasTableItems().isEmpty() || schemaItem.getSelectFields().isEmpty()) {
            throw new ParserException("Parse sql error");
        }
        return schemaItem;
    } catch (Exception e) {
        throw new ParserException();
    }
}
 
Example 4
Source Project: canal   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
public static MySqlSelectQueryBlock parseSQLSelectQueryBlock(String sql) {
    if (sql == null || "".equals(sql)) {
        return null;
    }
    SQLStatementParser parser = new MySqlStatementParser(sql);
    SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
    return (MySqlSelectQueryBlock) statement.getSelect().getQuery();
}