Search in sources :

Example 6 with ValueInputToken

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

the class StatementProcessorTest method exec.

private String[] exec(final boolean checkForDuplicateBinds, final String statement, final Object... binds) {
    final Collection<String> duplicates = new ArrayList<>();
    final AbstractSqlService sqlService = new AbstractSqlService() {
    };
    new StatementProcessor(sqlService, statement, binds) {

        @Override
        protected boolean isBindDuplicateCheckEnabled() {
            return checkForDuplicateBinds;
        }

        @Override
        protected void onDuplicateBind(IToken token) {
            if (token instanceof ValueInputToken) {
                duplicates.add(((ValueInputToken) token).getName());
            } else if (token instanceof ValueOutputToken) {
                duplicates.add(((ValueOutputToken) token).getName());
            } else {
                throw new ProcessingException("Unexpected token: {}", token);
            }
        }
    }.simulate();
    return duplicates.toArray(new String[duplicates.size()]);
}
Also used : ValueOutputToken(org.eclipse.scout.rt.server.jdbc.parsers.token.ValueOutputToken) IToken(org.eclipse.scout.rt.server.jdbc.parsers.token.IToken) ArrayList(java.util.ArrayList) AbstractSqlService(org.eclipse.scout.rt.server.jdbc.AbstractSqlService) ValueInputToken(org.eclipse.scout.rt.server.jdbc.parsers.token.ValueInputToken) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 7 with ValueInputToken

use of org.eclipse.scout.rt.server.jdbc.parsers.token.ValueInputToken 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

ValueInputToken (org.eclipse.scout.rt.server.jdbc.parsers.token.ValueInputToken)7 IToken (org.eclipse.scout.rt.server.jdbc.parsers.token.IToken)5 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)2 FunctionInputToken (org.eclipse.scout.rt.server.jdbc.parsers.token.FunctionInputToken)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 NVPair (org.eclipse.scout.rt.platform.holders.NVPair)1 AbstractSqlService (org.eclipse.scout.rt.server.jdbc.AbstractSqlService)1 DatabaseSpecificToken (org.eclipse.scout.rt.server.jdbc.parsers.token.DatabaseSpecificToken)1 ValueOutputToken (org.eclipse.scout.rt.server.jdbc.parsers.token.ValueOutputToken)1