Search in sources :

Example 1 with SqlExecutor

use of com.developmentontheedge.dbms.SqlExecutor in project be5 by DevelopmentOnTheEdge.

the class AppData method execute.

@Override
public void execute() throws MojoFailureException {
    init();
    PrintStream ps = null;
    try {
        if (logPath != null) {
            logPath.mkdirs();
            ps = new PrintStream(new File(logPath, be5Project.getName() + "_scripts_" + script.replace(';', '_').replace(':', '.') + ".sql"), "UTF-8");
        }
        ModuleLoader2.addModuleScripts(be5Project);
        List<FreemarkerScript> scripts = new ArrayList<>();
        for (String scriptName : script.split(";")) {
            int pos = scriptName.indexOf(':');
            FreemarkerCatalog scriptsCatalog = be5Project.getApplication().getFreemarkerScripts();
            if (pos > 0) {
                String moduleName = scriptName.substring(0, pos);
                scriptName = scriptName.substring(pos + 1);
                if (moduleName.equals("all")) {
                    for (Module module : be5Project.getModules()) {
                        scriptsCatalog = module.getFreemarkerScripts();
                        if (scriptsCatalog == null)
                            continue;
                        FreemarkerScript script = scriptsCatalog.optScript(scriptName);
                        if (script == null)
                            continue;
                        scripts.add(script);
                    }
                    FreemarkerScript script = be5Project.getApplication().getFreemarkerScripts().optScript(scriptName);
                    if (script != null) {
                        scripts.add(script);
                    }
                    continue;
                } else {
                    Module module = be5Project.getModule(moduleName);
                    if (module == null) {
                        if (ignoreMissing) {
                            System.err.println("Warning: module '" + moduleName + "' not found");
                            continue;
                        } else
                            throw new MojoFailureException("Module '" + moduleName + "' not found");
                    }
                    scriptsCatalog = module.getFreemarkerScripts();
                }
            }
            FreemarkerScript freemarkerScript = scriptsCatalog == null ? null : scriptsCatalog.optScript(scriptName);
            if (freemarkerScript == null) {
                if (ignoreMissing) {
                    System.err.println("Warning: FTL script " + scriptName + " not found");
                    continue;
                } else
                    throw new MojoFailureException("FTL script " + scriptName + " not found");
            }
            scripts.add(freemarkerScript);
        }
        SqlExecutor sqlExecutor = new BeSqlExecutor(connector, ps);
        for (FreemarkerScript freemarkerScript : scripts) {
            executeScript(sqlExecutor, freemarkerScript);
        }
        DatabaseUtils.clearAllCache(sqlExecutor);
    } catch (ProjectElementException | FreemarkerSqlException e) {
        throw new MojoFailureException(e.getMessage(), e);
    } catch (Exception e) {
        throw new MojoFailureException(e.getMessage(), e);
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}
Also used : FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) PrintStream(java.io.PrintStream) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) FreemarkerCatalog(com.developmentontheedge.be5.metadata.model.FreemarkerCatalog) FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) IOException(java.io.IOException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Module(com.developmentontheedge.be5.metadata.model.Module) File(java.io.File)

Example 2 with SqlExecutor

use of com.developmentontheedge.dbms.SqlExecutor in project be5 by DevelopmentOnTheEdge.

the class TableDef method isSafeTypeUpdate.

private boolean isSafeTypeUpdate(ColumnDef oldColumn, ColumnDef column, DbmsTypeManager typeManager, SqlExecutor sql) throws ExtendedSqlException {
    SqlColumnType oldType = oldColumn.getType();
    SqlColumnType type = column.getType();
    if (typeManager.getTypeClause(oldType).equals(typeManager.getTypeClause(type)))
        return true;
    // Enlarging VARCHAR column
    if (oldType.getTypeName().equals(TYPE_VARCHAR) && type.getTypeName().equals(TYPE_VARCHAR)) {
        if (type.getSize() >= oldType.getSize())
            return true;
        return sql != null && !sql.hasResult("sql.select.longer", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), type.getSize());
    }
    // Enlarging DECIMAL column
    if (oldType.getTypeName().equals(TYPE_DECIMAL) && type.getTypeName().equals(TYPE_DECIMAL)) {
        if (type.getSize() >= oldType.getSize() && type.getPrecision() >= oldType.getPrecision())
            return true;
    }
    // Adding new variants for ENUM column
    if ((oldType.getTypeName().equals(TYPE_ENUM) || oldType.getTypeName().equals(TYPE_BOOL) || oldType.getTypeName().equals(TYPE_VARCHAR)) && (type.getTypeName().equals(TYPE_ENUM) || type.getTypeName().equals(TYPE_BOOL))) {
        List<String> newValues = Arrays.asList(type.getEnumValues());
        if (!oldType.getTypeName().equals(TYPE_VARCHAR) && newValues.containsAll(Arrays.asList(oldType.getEnumValues())))
            return true;
        return sql != null && !sql.hasResult("sql.select.not.in.range", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), MetadataUtils.toInClause(newValues));
    }
    // Changing ENUM to varchar
    if ((oldType.getTypeName().equals(TYPE_ENUM) || oldType.getTypeName().equals(TYPE_BOOL)) && type.getTypeName().equals(TYPE_VARCHAR)) {
        int len = 0;
        for (String value : oldType.getEnumValues()) {
            len = Math.max(len, value.length());
        }
        if (type.getSize() >= len)
            return true;
        return sql != null && !sql.hasResult("sql.select.longer", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), type.getSize());
    }
    // Changing ENUM to char
    if (oldType.getTypeName().equals(TYPE_ENUM) && type.getTypeName().equals(TYPE_CHAR)) {
        return StreamEx.of(oldType.getEnumValues()).map(String::length).distinct().collect(MoreCollectors.onlyOne()).filter(len -> type.getSize() == len).isPresent();
    }
    return false;
}
Also used : DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager) Arrays(java.util.Arrays) BeCaseInsensitiveCollection(com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) Iterator(java.util.Iterator) BeVectorCollection(com.developmentontheedge.be5.metadata.model.base.BeVectorCollection) MoreCollectors(one.util.streamex.MoreCollectors) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) Set(java.util.Set) MetadataUtils(com.developmentontheedge.be5.metadata.MetadataUtils) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ArrayList(java.util.ArrayList) BeModelElement(com.developmentontheedge.be5.metadata.model.base.BeModelElement) List(java.util.List) Strings2(com.developmentontheedge.be5.metadata.util.Strings2) PropertyName(com.developmentontheedge.beans.annot.PropertyName) StreamEx(one.util.streamex.StreamEx) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType) Map(java.util.Map) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType)

Example 3 with SqlExecutor

use of com.developmentontheedge.dbms.SqlExecutor in project be5 by DevelopmentOnTheEdge.

the class FreemarkerSqlHandlerTest method testHandler.

@Test
public void testHandler() throws UnsupportedEncodingException, IOException, FreemarkerSqlException, ProjectElementException {
    ByteArrayOutputStream log = new ByteArrayOutputStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(log, true, "UTF-8");
    PrintStream psOut = new PrintStream(out, true, "UTF-8");
    SqlExecutor executor = new SqlExecutor(new NullConnector(), ps, SqlExecutor.class.getResource("basesql.properties"));
    FreemarkerSqlHandler handler = new FreemarkerSqlHandler(executor, true, new WriterLogger(psOut));
    Project proj = new Project("test");
    proj.setDatabaseSystem(Rdbms.POSTGRESQL);
    FreemarkerScript sql = new FreemarkerScript("sql", proj.getApplication().getFreemarkerScripts());
    DataElementUtils.save(sql);
    FreemarkerScript sql2 = new FreemarkerScript("sql2", proj.getApplication().getFreemarkerScripts());
    DataElementUtils.save(sql2);
    sql2.setSource("UPDATE test SET b = 'c';");
    sql.setSource("delete from test;-- hehehe\nINSERT INTO \"test\" VALUES('a','b','c');\nBEGIN update test SET a='a''b END;';END;\nDELETE FROM test;<#include 'sql2'/>");
    handler.execute(sql);
    String result = new String(log.toByteArray(), StandardCharsets.UTF_8);
    String expected = "\n" + "-- Execute test/application/Scripts/sql\n" + "-- At test/application/Scripts/sql[1,1]-[1,17]\n" + "delete from test;\n" + "-- At test/application/Scripts/sql[2,1]-[2,39]\n" + "INSERT INTO \"test\" VALUES('a','b','c');\n" + "-- At test/application/Scripts/sql[3,1]-[3,40]\n" + "BEGIN update test SET a='a''b END;';END;\n" + "-- At test/application/Scripts/sql[4,1]-[4,17]\n" + "DELETE FROM test;\n" + "-- At test/application/Scripts/sql[4,18]-[4,35]\n" + "-- At test/application/Scripts/sql2[1,1]-[1,24]\n" + "\n" + "-- Start of included test/application/Scripts/sql2\n" + "UPDATE test SET b = 'c';\n" + "-- End of included test/application/Scripts/sql2\n";
    assertEquals(expected, result.replace("\r", ""));
    String outResult = new String(out.toByteArray(), StandardCharsets.UTF_8);
    String outExpected = "xx:xx:xx: [>] test/application/Scripts/sql\n" + "xx:xx:xx: [>]   test/application/Scripts/sql2\n";
    assertEquals(outExpected, outResult.replace("\r", "").replaceAll("\\d\\d", "xx"));
}
Also used : PrintStream(java.io.PrintStream) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) WriterLogger(com.developmentontheedge.be5.metadata.util.WriterLogger) Project(com.developmentontheedge.be5.metadata.model.Project) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SqlExecutor (com.developmentontheedge.dbms.SqlExecutor)3 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)2 FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 MetadataUtils (com.developmentontheedge.be5.metadata.MetadataUtils)1 FreemarkerSqlException (com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException)1 FreemarkerCatalog (com.developmentontheedge.be5.metadata.model.FreemarkerCatalog)1 Module (com.developmentontheedge.be5.metadata.model.Module)1 Project (com.developmentontheedge.be5.metadata.model.Project)1 SqlColumnType (com.developmentontheedge.be5.metadata.model.SqlColumnType)1 BeCaseInsensitiveCollection (com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection)1 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)1 BeVectorCollection (com.developmentontheedge.be5.metadata.model.base.BeVectorCollection)1 BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)1 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)1 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)1 Strings2 (com.developmentontheedge.be5.metadata.util.Strings2)1 WriterLogger (com.developmentontheedge.be5.metadata.util.WriterLogger)1 PropertyName (com.developmentontheedge.beans.annot.PropertyName)1