net.sf.jsqlparser.parser.CCJSqlParserManager Java Examples

The following examples show how to use net.sf.jsqlparser.parser.CCJSqlParserManager. 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 File: TableNameReplacerTests.java    From DDF with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startServer() throws Exception {
    Thread.sleep(1000);
    // LOG = LoggerFactory.getLogger(BaseTest.class);
    // manager = DDFManager.get("spark");
    /*
    manager = DDFManager.get("jdbc", new JDBCDataSourceDescriptor(new
            DataSourceURI("jdbc:mysql://localhost/testdb"), new
            JDBCDataSourceDescriptor.JDBCDataSourceCredentials("pauser",
            "papwd"), null));
    DataSourceDescriptor ds = manager.getDataSourceDescriptor();
    if (ds instanceof
            JDBCDataSourceDescriptor) {
        System.out.println("hello");
    }
    DDF ret = manager.sql2ddf("select * from testtable", "jdbc");*/
    // Add 2 test ddfs.
    manager = DDFManager.get(DDFManager.EngineType.SPARK);

    parser = new CCJSqlParserManager();
}
 
Example #2
Source File: JSQLParserAdapter.java    From ddal with Apache License 2.0 5 votes vote down vote up
/**
 * To make ddal-jsqlparser work well, JSqlParser should include the feature of 'support getting jdbc parameter index'.
 * And this feature is provided on the version of {@link <a href="https://github.com/JSQLParser/JSqlParser/releases/tag/jsqlparser-0.9.7">0.9.7</a>}.
 * This method is designed to check the necessary feature.
 */
public static void checkJSqlParserFeature() throws JSQLParserException {
    CCJSqlParserManager parserManager = new CCJSqlParserManager();
    String sql = "SELECT * FROM tab_1 WHERE tab_1.col_1 = ? AND col_2 IN (SELECT DISTINCT col_2 FROM tab_2 WHERE col_3 LIKE ? AND col_4 > ?) LIMIT ?, ?";
    Select select = (Select) parserManager.parse(new StringReader(sql));
    PlainSelect selectBody = (PlainSelect) select.getSelectBody();
    //
    AndExpression andExpression = (AndExpression) selectBody.getWhere();
    EqualsTo equalsTo = (EqualsTo) andExpression.getLeftExpression();
    JdbcParameter jdbcParameter = (JdbcParameter) equalsTo.getRightExpression();
    Integer index1 = jdbcParameter.getIndex();
    if (index1 != 1) {
        throw new IllegalStateException("Current version of JSQLParser doesn't support the feature of 'support "
                                        + "get jdbc parameter index'");
    }
    //
    InExpression inExpression = (InExpression) andExpression.getRightExpression();
    SubSelect subSelect = (SubSelect) inExpression.getRightItemsList();
    PlainSelect subSelectBody = (PlainSelect) subSelect.getSelectBody();
    AndExpression subAndExpression = (AndExpression) subSelectBody.getWhere();
    LikeExpression likeExpression = (LikeExpression) subAndExpression.getLeftExpression();
    if (((JdbcParameter) likeExpression.getRightExpression()).getIndex() != 2) {
        throw new IllegalStateException(
                                        "Current version of JSQLParser doesn't support the feature of 'support get jdbc parameter index'");
    }
    //
    GreaterThan greaterThan = (GreaterThan) subAndExpression.getRightExpression();
    if (((JdbcParameter) greaterThan.getRightExpression()).getIndex() != 3) {
        throw new IllegalStateException(
                                        "Current version of JSQLParser doesn't support the feature of 'support get jdbc parameter index'");
    }
    //
    Expression offset = selectBody.getLimit().getOffset();
    Expression rowCount = selectBody.getLimit().getRowCount();
    if (((JdbcParameter) offset).getIndex() != 4 || ((JdbcParameter) rowCount).getIndex() != 5) {
        throw new IllegalStateException(
                                        "Current version of JSQLParser doesn't support the feature of 'support get jdbc parameter index'");
    }
}
 
Example #3
Source File: QueryTranslator.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
public FqlQuery translate(String sql) {
    ExtendedSqlStatement extendedSqlStatement = metastatementMatcher.parse(sql);
    if(null != extendedSqlStatement) {
        ExtendedSqlParser parser = new ExtendedSqlParser();
        extendedSqlStatement.receive(parser);
        return parser.getQuery();
    }

    CCJSqlParserManager ccjSqlParserManager = new CCJSqlParserManager();
    Statement statement;
    try {
        statement = ccjSqlParserManager.parse(new StringReader(sql));
    } catch (JSQLParserException e) {
        throw new FqlParsingException(e.getMessage(), e);
    }
    Select select = (Select)statement;
    select.accept(this);
    ActionRequest request = null;
    switch (queryType) {
        case SELECT:
            request = createSelectActionRequest();
            break;

        case GROUP:
            request = createGroupActionRequest();
            break;

        case TREND:
            request = createTrendActionRequest();
            break;

        case STATSTREND:
            request = createStatsTrendActionRequest();
            break;

        case STATS:
            request = createStatsActionRequest();
            break;

        case HISTOGRAM:
            request = createHistogramActionRequest();
            break;

        case COUNT:
            request = createCountActionRequest();
            break;


        case DISTINCT:
            request = createDistinctActionRequest();
            break;

        default:
            break;
    }
    if(null == request) {
        throw new FqlParsingException("Could not parse provided FQL.");
    }
    return new FqlActionQuery(request, selectedColumns);
}
 
Example #4
Source File: SqlConverterFactory.java    From mybatis-shard with Eclipse Public License 1.0 4 votes vote down vote up
private SqlConverterFactory() {

        pm = new CCJSqlParserManager();
        register();
    }