Search in sources :

Example 1 with DatabaseSpecificToken

use of org.eclipse.scout.rt.server.jdbc.parsers.token.DatabaseSpecificToken in project scout.rt by eclipse.

the class SqlFunctionReplaceTest method testFunctionReplacement.

/**
 * sql functions in sql style
 */
@Test
public void testFunctionReplacement() {
    String sql = "SELECT $$sysdate FROM DUAL";
    BindModel bindModel = new BindParser(sql).parse();
    IToken[] tokens = bindModel.getAllTokens();
    int dbSpecTokenFound = 0;
    for (IToken token : tokens) {
        if (token instanceof DatabaseSpecificToken) {
            assertEquals("sysdate", ((DatabaseSpecificToken) token).getName());
            dbSpecTokenFound++;
        }
    }
    assertTrue("no DatabaseSpecificToken found", dbSpecTokenFound > 0);
    sql = "SELECT $$sysdate FROM TABLE1 WHERE COLUMN1 != $$nvl(:val1,0)";
    bindModel = new BindParser(sql).parse();
    tokens = bindModel.getAllTokens();
    dbSpecTokenFound = 0;
    int dbSpecTokenCount = 0;
    for (IToken token : tokens) {
        if (token instanceof DatabaseSpecificToken) {
            if (dbSpecTokenCount == 0) {
                assertEquals("sysdate", ((DatabaseSpecificToken) token).getName());
            }
            if (dbSpecTokenCount == 1) {
                assertEquals("nvl", ((DatabaseSpecificToken) token).getName());
            }
            dbSpecTokenCount++;
            dbSpecTokenFound++;
        }
    }
    assertTrue("no DatabaseSpecificToken found", dbSpecTokenFound > 0);
    sql = "SELECT A, B, C " + "FROM   TABLE1 " + "WHERE  COLUMN1 != $$nvl(:val1,0) " + "INTO   :{a}, :{b}, {c} ";
    bindModel = new BindParser(sql).parse();
    tokens = bindModel.getAllTokens();
    dbSpecTokenFound = 0;
    int valueInputCount = 0;
    for (IToken token : tokens) {
        if (token instanceof DatabaseSpecificToken) {
            assertEquals("nvl", ((DatabaseSpecificToken) token).getName());
            dbSpecTokenFound++;
        }
        if (token instanceof ValueInputToken) {
            if (valueInputCount == 0) {
                assertEquals("val1", ((ValueInputToken) token).getName());
            }
            if (valueInputCount == 1) {
                assertEquals("a", ((ValueInputToken) token).getName());
            }
            if (valueInputCount == 2) {
                assertEquals("b", ((ValueInputToken) token).getName());
            }
            if (valueInputCount == 3) {
                assertEquals("c", ((ValueInputToken) token).getName());
            }
            valueInputCount++;
        }
    }
    assertTrue("no DatabaseSpecificToken found", dbSpecTokenFound > 0);
    sql = "INSERT INTO TABLE1 (A, B, C) " + "VALUES (:{a}, :{b}, :{c}) ";
    bindModel = new BindParser(sql).parse();
    tokens = bindModel.getAllTokens();
    valueInputCount = 0;
    for (IToken token : tokens) {
        if (token instanceof ValueInputToken) {
            if (valueInputCount == 0) {
                assertEquals("a", ((ValueInputToken) token).getName());
            }
            if (valueInputCount == 1) {
                assertEquals("b", ((ValueInputToken) token).getName());
            }
            if (valueInputCount == 2) {
                assertEquals("c", ((ValueInputToken) token).getName());
            }
            valueInputCount++;
        }
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.token.IToken) DatabaseSpecificToken(org.eclipse.scout.rt.server.jdbc.parsers.token.DatabaseSpecificToken) ValueInputToken(org.eclipse.scout.rt.server.jdbc.parsers.token.ValueInputToken) Test(org.junit.Test)

Aggregations

DatabaseSpecificToken (org.eclipse.scout.rt.server.jdbc.parsers.token.DatabaseSpecificToken)1 IToken (org.eclipse.scout.rt.server.jdbc.parsers.token.IToken)1 ValueInputToken (org.eclipse.scout.rt.server.jdbc.parsers.token.ValueInputToken)1 Test (org.junit.Test)1