Search in sources :

Example 6 with Context

use of com.developmentontheedge.sql.format.Context in project be5 by DevelopmentOnTheEdge.

the class OrderByFilterTest method testOrderByFilterUnion.

@Test
public void testOrderByFilterUnion() {
    AstStart start = SqlQuery.parse("SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
    Map<String, String> columns = Collections.singletonMap("name", "DESC");
    new OrderByFilter().apply(start, columns);
    assertEquals("SELECT * FROM (SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%') " + "tmp ORDER BY 1 DESC", new Formatter().format(start, new Context(Dbms.MYSQL), new DefaultParserContext()));
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) OrderByFilter(com.developmentontheedge.sql.format.OrderByFilter) Test(org.junit.Test)

Example 7 with Context

use of com.developmentontheedge.sql.format.Context in project be5 by DevelopmentOnTheEdge.

the class ParserTarget method format.

protected void format(String dbmsName, Element element) {
    Dbms dbms;
    if (Dbms.DB2.getName().equals(dbmsName))
        dbms = Dbms.DB2;
    else if (Dbms.MYSQL.getName().equals(dbmsName))
        dbms = Dbms.MYSQL;
    else if (Dbms.ORACLE.getName().equals(dbmsName))
        dbms = Dbms.ORACLE;
    else if (Dbms.POSTGRESQL.getName().equals(dbmsName))
        dbms = Dbms.POSTGRESQL;
    else if (Dbms.SQLSERVER.getName().equals(dbmsName))
        dbms = Dbms.SQLSERVER;
    else {
        test.getResult().addError(test, new Exception("Unknown DBMS \"" + dbmsName + "\". DBMS should be: db2, mysql, oracle, postgresql or sqlserver."));
        return;
    }
    Formatter formatter = new Formatter();
    String formatResult = trimAllLine(formatter.format(astStart, new Context(dbms), parser.getContext()));
    String result = trimAllLine(XmlTest.getCData(element));
    assertEquals("Incorrect result, dbms=" + dbms.getName(), result, formatResult);
    if (!formatResult.equals(result))
        test.getResult().addFailure(test, new ComparisonFailure("Incorrect result, dbms=" + dbms.getName(), result, formatResult));
}
Also used : Context(com.developmentontheedge.sql.format.Context) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) Dbms(com.developmentontheedge.sql.format.Dbms) Formatter(com.developmentontheedge.sql.format.Formatter) ComparisonFailure(junit.framework.ComparisonFailure)

Example 8 with Context

use of com.developmentontheedge.sql.format.Context in project be5 by DevelopmentOnTheEdge.

the class Project method translateSQL.

/**
 * Try to translate the SQL query to the current DBMS using com.developmentontheedge.sql
 * @param sql
 * @return
 */
public String translateSQL(String sql) {
    if (beSQL > 0) {
        if (--beSQL == 0)
            reconfigureFreemarker();
    }
    if (sqlParser == null) {
        throw new IllegalStateException("translateSQL was called without enterSQL");
    }
    sqlParser.parse(sql);
    List<String> messages = sqlParser.getMessages();
    if (!messages.isEmpty()) {
        throw new IllegalArgumentException(("SQL cannot be parsed:\nQuery:" + sql + "\nErrors: " + String.join("\n", messages)).replace("\r", "").replace("\n", System.lineSeparator()));
    }
    AstStart ast = sqlParser.getStartNode();
    if (databaseSystem != Rdbms.BESQL) {
        new MacroExpander().expandMacros(ast);
        Dbms dbms = databaseSystem == null ? Dbms.POSTGRESQL : Dbms.valueOf(databaseSystem.name());
        DbmsTransformer dbmsTransformer = new Context(dbms).getDbmsTransformer();
        dbmsTransformer.setParserContext(sqlParser.getContext());
        dbmsTransformer.transformAst(ast);
    }
    return ast.format();
}
Also used : LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Context(com.developmentontheedge.sql.format.Context) AstStart(com.developmentontheedge.sql.model.AstStart) MacroExpander(com.developmentontheedge.sql.format.MacroExpander) Dbms(com.developmentontheedge.sql.format.Dbms) DbmsTransformer(com.developmentontheedge.sql.format.DbmsTransformer)

Example 9 with Context

use of com.developmentontheedge.sql.format.Context in project be5 by DevelopmentOnTheEdge.

the class TestMain method testModule5DB.

public static StatContext testModule5DB(String name, List<String> okQueries) throws ProjectLoadException, ReadException {
    LoadContext ctx = new LoadContext();
    Project project = ModuleUtils.loadModule(name, ctx);
    ModuleUtils.addModuleScripts(project);
    ctx.check();
    SqlParser parser = new SqlParser();
    Formatter formatter = new Formatter();
    DefaultParserContext parserContext = new DefaultParserContext();
    parser.setContext(parserContext);
    Rdbms[] dbms = { Rdbms.MYSQL, Rdbms.DB2, Rdbms.ORACLE, Rdbms.SQLSERVER, Rdbms.POSTGRESQL };
    StatContext stat = new StatContext(name + "/5DB", StreamEx.of(dbms).map(Rdbms::name).prepend("Count", "PF").append("Success").toArray(String[]::new));
    for (String entityName : project.getEntityNames()) {
        Entity entity = project.getEntity(entityName);
        for (Query query : entity.getQueries()) {
            if (!allowedTypes.contains(query.getType()))
                continue;
            if (query.getName().equals("Table definition"))
                continue;
            if (query.getQuery().trim().isEmpty())
                continue;
            project.setDatabaseSystem(Rdbms.BESQL);
            String queryText;
            try {
                queryText = query.getQueryCompiled().validate();
            } catch (ProjectElementException e) {
                continue;
            }
            stat.inc("Count");
            parser.parse(queryText);
            if (!parser.getMessages().isEmpty()) {
                stat.inc("PF");
                continue;
            }
            try {
                formatter.format(parser.getStartNode(), new Context(Dbms.POSTGRESQL), parserContext);
            } catch (IllegalArgumentException e) {
                System.out.println(entityName + "." + query.getName() + ": " + e.getMessage());
                stat.inc("PF");
                continue;
            }
            boolean success = true;
            for (Rdbms db : dbms) {
                project.setDatabaseSystem(db);
                String expected;
                try {
                    expected = sanitizeValue(query.getQueryCompiled().validate());
                } catch (ProjectElementException e) {
                    continue;
                }
                String formatted;
                try {
                    formatted = formatter.format(parser.getStartNode(), new Context(Dbms.valueOf(db.name())), parserContext);
                } catch (IllegalArgumentException e) {
                    System.out.println(entityName + "." + query.getName() + ": " + e.getMessage());
                    stat.inc(db.name());
                    success = false;
                    continue;
                }
                String actual = sanitizeValue(formatted);
                if (!expected.equals(actual)) {
                    if (db == Rdbms.ORACLE) {
                        System.out.println(entityName + "." + query.getName());
                        System.out.println("BESQL: " + queryText);
                        System.out.println("Actual: " + actual);
                        System.out.println("Expected: " + expected);
                    }
                    stat.inc(db.name());
                    success = false;
                }
            }
            if (success) {
                stat.inc("Success");
            // okQueries.add( entityName+"."+query.getName() );
            }
        }
    }
    return stat;
}
Also used : DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Context(com.developmentontheedge.sql.format.Context) LoadContext(com.beanexplorer.enterprise.metadata.serialization.LoadContext) Entity(com.beanexplorer.enterprise.metadata.model.Entity) ProjectElementException(com.beanexplorer.enterprise.metadata.exception.ProjectElementException) Rdbms(com.beanexplorer.enterprise.metadata.sql.Rdbms) Query(com.beanexplorer.enterprise.metadata.model.Query) Formatter(com.developmentontheedge.sql.format.Formatter) SqlParser(com.developmentontheedge.sql.model.SqlParser) Project(com.beanexplorer.enterprise.metadata.model.Project) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) LoadContext(com.beanexplorer.enterprise.metadata.serialization.LoadContext)

Example 10 with Context

use of com.developmentontheedge.sql.format.Context in project be5 by DevelopmentOnTheEdge.

the class FilterApplierTest method testSetFilterApplierUnion.

@Test
public void testSetFilterApplierUnion() {
    AstStart query = SqlQuery.parse("SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
    Map<ColumnRef, Object> conditions = Collections.singletonMap(ColumnRef.resolve(query, "name"), "name");
    new FilterApplier().setFilter(query, conditions);
    assertEquals("SELECT * FROM (SELECT name FROM bbc UNION SELECT name FROM actor) tmp WHERE name ='name'", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
// conditions = Collections.singletonMap( ColumnRef.resolve( query, "name1" ), "name1" );
// new FilterApplier().setFilter( query, conditions );
// assertEquals( "SELECT * FROM (SELECT name FROM bbc UNION SELECT name FROM actor) tmp WHERE name1 ='name1'",
// new Formatter().format( query, new Context( Dbms.POSTGRESQL ), new DefaultParserContext() ) );
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) FilterApplier(com.developmentontheedge.sql.format.FilterApplier) ColumnRef(com.developmentontheedge.sql.format.ColumnRef) Test(org.junit.Test)

Aggregations

Context (com.developmentontheedge.sql.format.Context)14 Formatter (com.developmentontheedge.sql.format.Formatter)13 AstStart (com.developmentontheedge.sql.model.AstStart)12 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)11 Test (org.junit.Test)10 ColumnRef (com.developmentontheedge.sql.format.ColumnRef)4 Dbms (com.developmentontheedge.sql.format.Dbms)4 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)4 SqlParser (com.developmentontheedge.sql.model.SqlParser)3 ProjectElementException (com.beanexplorer.enterprise.metadata.exception.ProjectElementException)2 Entity (com.beanexplorer.enterprise.metadata.model.Entity)2 Project (com.beanexplorer.enterprise.metadata.model.Project)2 Query (com.beanexplorer.enterprise.metadata.model.Query)2 LoadContext (com.beanexplorer.enterprise.metadata.serialization.LoadContext)2 Rdbms (com.beanexplorer.enterprise.metadata.sql.Rdbms)2 MacroExpander (com.developmentontheedge.sql.format.MacroExpander)2 OrderByFilter (com.developmentontheedge.sql.format.OrderByFilter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2