Search in sources :

Example 1 with Dbms

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

the class TestMain method testProject.

private static StatContext testProject(Project project, FailData failedQueries) {
    String name = project.getName();
    Rdbms dbms = project.getDatabaseSystem();
    StatContext stat = new StatContext(name + "/" + dbms, "Count", "FMF", "PE", "FE", "PF", "RFE", "RFM", "Success");
    SqlParser parser = new SqlParser();
    parser.setContext(new DefaultParserContext());
    Formatter formatter = new Formatter();
    Context context = new Context(Arrays.stream(Dbms.values()).filter(d -> d.getType() == dbms.getType()).findFirst().get());
    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;
            stat.inc("Count");
            String queryText;
            try {
                queryText = query.getQueryCompiled().validate();
            } catch (ProjectElementException e) {
                stat.inc("FMF");
                continue;
            }
            parser.parse(queryText);
            if (!parser.getMessages().isEmpty()) {
                if (knownNonExpanded.stream().anyMatch(queryText::contains)) {
                    stat.inc("FMF");
                    continue;
                }
                failedQueries.add(query, queryText);
                // System.out.println("----------------------\nQuery: "+entity.getName()+"."+query.getName()+"\n"+queryText+"\n\n"+String.join("\n", parser.getMessages()));
                stat.inc("PE");
                continue;
            }
            AstStart astStart = parser.getStartNode();
            String format;
            try {
                format = formatter.format(astStart, context, parser.getContext());
                Objects.requireNonNull(format);
            } catch (Exception e) {
                failedQueries.add(query, queryText);
                System.out.println("Err: " + entity.getName() + "." + query.getName() + ": " + e.getMessage());
                stat.inc("FE");
                continue;
            }
            parser.parse(format);
            if (!parser.getMessages().isEmpty()) {
                failedQueries.add(query, queryText);
                // System.out.println("PF: "+queryText+"\n"+"Formatted: "+format+"\n"+parser.getMessages());
                stat.inc("PF");
                continue;
            }
            astStart = parser.getStartNode();
            String format2;
            try {
                format2 = formatter.format(astStart, context, parser.getContext());
                Objects.requireNonNull(format2);
            } catch (Exception e) {
                stat.inc("RFF");
                continue;
            }
            if (!format.equals(format2)) {
                failedQueries.add(query, queryText);
                // System.out.println("Orig : "+queryText);
                // System.out.println("Form1: "+format);
                // System.out.println("Form2: "+format2);
                stat.inc("RFM");
                continue;
            }
            stat.inc("Success");
        }
    }
    return stat;
}
Also used : DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Context(com.developmentontheedge.sql.format.Context) LoadContext(com.beanexplorer.enterprise.metadata.serialization.LoadContext) Arrays(java.util.Arrays) ModuleUtils(com.beanexplorer.enterprise.metadata.util.ModuleUtils) AstStart(com.developmentontheedge.sql.model.AstStart) ProjectElementException(com.beanexplorer.enterprise.metadata.exception.ProjectElementException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Locale(java.util.Locale) Map(java.util.Map) ReadException(com.beanexplorer.enterprise.metadata.exception.ReadException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) Entity(com.beanexplorer.enterprise.metadata.model.Entity) Project(com.beanexplorer.enterprise.metadata.model.Project) Rdbms(com.beanexplorer.enterprise.metadata.sql.Rdbms) SqlModelReader(com.beanexplorer.enterprise.metadata.sql.SqlModelReader) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Path(java.nio.file.Path) Context(com.developmentontheedge.sql.format.Context) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) MoreCollectors(one.util.streamex.MoreCollectors) SqlParser(com.developmentontheedge.sql.model.SqlParser) Set(java.util.Set) IOException(java.io.IOException) EntryStream(one.util.streamex.EntryStream) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) ProcessInterruptedException(com.beanexplorer.enterprise.metadata.exception.ProcessInterruptedException) Query(com.beanexplorer.enterprise.metadata.model.Query) BeSqlExecutor(com.beanexplorer.enterprise.metadata.sql.BeSqlExecutor) List(java.util.List) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) ProjectLoadException(com.beanexplorer.enterprise.metadata.exception.ProjectLoadException) StreamEx(one.util.streamex.StreamEx) LoadContext(com.beanexplorer.enterprise.metadata.serialization.LoadContext) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Dbms(com.developmentontheedge.sql.format.Dbms) 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) ProjectElementException(com.beanexplorer.enterprise.metadata.exception.ProjectElementException) SQLException(java.sql.SQLException) ReadException(com.beanexplorer.enterprise.metadata.exception.ReadException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ProcessInterruptedException(com.beanexplorer.enterprise.metadata.exception.ProcessInterruptedException) ProjectLoadException(com.beanexplorer.enterprise.metadata.exception.ProjectLoadException) AstStart(com.developmentontheedge.sql.model.AstStart) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext)

Example 2 with Dbms

use of com.developmentontheedge.sql.format.Dbms 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 3 with Dbms

use of com.developmentontheedge.sql.format.Dbms 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)

Aggregations

Context (com.developmentontheedge.sql.format.Context)3 Dbms (com.developmentontheedge.sql.format.Dbms)3 Formatter (com.developmentontheedge.sql.format.Formatter)2 AstStart (com.developmentontheedge.sql.model.AstStart)2 ProcessInterruptedException (com.beanexplorer.enterprise.metadata.exception.ProcessInterruptedException)1 ProjectElementException (com.beanexplorer.enterprise.metadata.exception.ProjectElementException)1 ProjectLoadException (com.beanexplorer.enterprise.metadata.exception.ProjectLoadException)1 ReadException (com.beanexplorer.enterprise.metadata.exception.ReadException)1 Entity (com.beanexplorer.enterprise.metadata.model.Entity)1 Project (com.beanexplorer.enterprise.metadata.model.Project)1 Query (com.beanexplorer.enterprise.metadata.model.Query)1 LoadContext (com.beanexplorer.enterprise.metadata.serialization.LoadContext)1 BeSqlExecutor (com.beanexplorer.enterprise.metadata.sql.BeSqlExecutor)1 Rdbms (com.beanexplorer.enterprise.metadata.sql.Rdbms)1 SqlModelReader (com.beanexplorer.enterprise.metadata.sql.SqlModelReader)1 ModuleUtils (com.beanexplorer.enterprise.metadata.util.ModuleUtils)1 LoadContext (com.developmentontheedge.be5.metadata.serialization.LoadContext)1 ExtendedSqlException (com.developmentontheedge.dbms.ExtendedSqlException)1 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)1 DbmsTransformer (com.developmentontheedge.sql.format.DbmsTransformer)1