Search in sources :

Example 1 with BeSqlExecutor

use of com.developmentontheedge.be5.metadata.sql.BeSqlExecutor 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 BeSqlExecutor

use of com.developmentontheedge.be5.metadata.sql.BeSqlExecutor in project be5 by DevelopmentOnTheEdge.

the class AppDb method execute.

@Override
public void execute() throws MojoFailureException {
    init();
    try {
        if (logPath != null) {
            logPath.mkdirs();
            ps = new PrintStream(new File(logPath, (moduleName == null ? be5Project.getName() : moduleName) + "_db.sql"), "UTF-8");
        }
        sql = new BeSqlExecutor(connector, ps);
        if (moduleName != null) {
            Module module = be5Project.getModule(moduleName);
            if (module == null) {
                throw new MojoFailureException("Module '" + moduleName + "' not found!");
            }
            createDb(module);
        } else {
            for (Module module : be5Project.getModules()) {
                if (ModuleLoader2.containsModule(module.getName()))
                    createDb(module);
            }
            createDb(be5Project.getApplication());
        }
        getLog().info("Created tables: " + createdTables + ", created views: " + createdViews);
    } catch (MojoFailureException e) {
        throw e;
    } catch (ProjectElementException | FreemarkerSqlException e) {
        if (debug) {
            e.printStackTrace();
            throw new MojoFailureException("Setup db error", e);
        }
        throw new MojoFailureException(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        throw new MojoFailureException("Setup db error", e);
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}
Also used : BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) PrintStream(java.io.PrintStream) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Module(com.developmentontheedge.be5.metadata.model.Module) File(java.io.File) FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 3 with BeSqlExecutor

use of com.developmentontheedge.be5.metadata.sql.BeSqlExecutor in project be5 by DevelopmentOnTheEdge.

the class AppSync 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() + "_sync_ddl.sql"), "UTF-8");
        }
        sqlExecutor = new BeSqlExecutor(connector, ps);
        if (be5Project.getDebugStream() != null) {
            be5Project.getDebugStream().println("Modules and extras for " + be5Project.getName() + ":");
            be5Project.allModules().map(m -> "- " + m.getName() + ": " + (m.getExtras() == null ? "" : String.join(", ", m.getExtras()))).forEach(be5Project.getDebugStream()::println);
        }
        readSchema();
        createEntities();
        String ddlString = getDdlStatements(false);
        ddlString = MultiSqlParser.normalize(be5Project.getDatabaseSystem().getType(), ddlString);
        if (ddlString.isEmpty()) {
            getLog().info("Database scheme is up-to-date");
            return;
        }
        if (forceUpdate) {
            sqlExecutor.startSection("Sync schema");
            logger.setOperationName("[>] Schema");
            sqlExecutor.executeMultiple(ddlString);
            sqlExecutor.startSection(null);
        } else {
            System.err.println("The following statements should be executed to update database scheme:");
            System.err.println(ddlString);
            System.err.println("Use -DBE5_FORCE_UPDATE=true, for apply");
        }
        checkSynchronizationStatus();
        logger.setOperationName("Finished");
    } catch (// ReadException | ProjectLoadException | SQLException e )
    FreemarkerSqlException | ExtendedSqlException | SQLException e) {
        if (debug)
            throw new MojoFailureException("Synchronisation error: " + e.getMessage(), e);
        throw new MojoFailureException("Synchronisation error: " + e.getMessage());
    } catch (IOException | ProcessInterruptedException e) {
        throw new MojoFailureException("Synchronisation error: " + e.getMessage(), e);
    } catch (Throwable t) {
        t.printStackTrace();
        throw new MojoFailureException("Synchronisation error: " + t.getMessage(), t);
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}
Also used : FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) Arrays(java.util.Arrays) ViewDef(com.developmentontheedge.be5.metadata.model.ViewDef) NullLogger(com.developmentontheedge.be5.metadata.util.NullLogger) HashMap(java.util.HashMap) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) Parameter(org.apache.maven.plugins.annotations.Parameter) MultiSqlParser(com.developmentontheedge.dbms.MultiSqlParser) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ArrayList(java.util.ArrayList) Entity(com.developmentontheedge.be5.metadata.model.Entity) DefaultTypeManager(com.developmentontheedge.be5.metadata.sql.type.DefaultTypeManager) Mojo(org.apache.maven.plugins.annotations.Mojo) SQLException(java.sql.SQLException) IndexDef(com.developmentontheedge.be5.metadata.model.IndexDef) DatabaseUtils(com.developmentontheedge.be5.metadata.sql.DatabaseUtils) ResultSet(java.sql.ResultSet) Locale(java.util.Locale) Map(java.util.Map) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) DdlElement(com.developmentontheedge.be5.metadata.model.DdlElement) Module(com.developmentontheedge.be5.metadata.model.Module) TableDef(com.developmentontheedge.be5.metadata.model.TableDef) DataElementUtils(com.developmentontheedge.be5.metadata.model.DataElementUtils) ProcessController(com.developmentontheedge.be5.metadata.util.ProcessController) PrintStream(java.io.PrintStream) DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager) ProcessInterruptedException(com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) IOException(java.io.IOException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo) List(java.util.List) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType) DbmsType(com.developmentontheedge.dbms.DbmsType) IntStreamEx(one.util.streamex.IntStreamEx) Pattern(java.util.regex.Pattern) Project(com.developmentontheedge.be5.metadata.model.Project) Collections(java.util.Collections) EntityType(com.developmentontheedge.be5.metadata.model.EntityType) DbmsSchemaReader(com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader) FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) PrintStream(java.io.PrintStream) SQLException(java.sql.SQLException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) File(java.io.File) ProcessInterruptedException(com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException)

Example 4 with BeSqlExecutor

use of com.developmentontheedge.be5.metadata.sql.BeSqlExecutor in project be5 by DevelopmentOnTheEdge.

the class AppTools method execute.

@Override
public void execute() throws MojoFailureException {
    init();
    BeConnectionProfile prof = be5Project.getConnectionProfile();
    if (prof == null) {
        throw new MojoFailureException("Connection profile is required for SQL console");
    }
    try {
        getLog().info("Welcome to FTL/SQL console!");
        BeSqlExecutor sql = new BeSqlExecutor(connector) {

            @Override
            public void executeSingle(String statement) throws ExtendedSqlException {
                ResultSet rs = null;
                try {
                    rs = connector.executeQuery(statement);
                    format(rs, System.err, 20);
                } catch (SQLException e) {
                    throw new ExtendedSqlException(connector, statement, e);
                } finally {
                    connector.close(rs);
                }
            }
        };
        FreemarkerScript fs = new FreemarkerScript("SQL", be5Project.getApplication().getFreemarkerScripts());
        DataElementUtils.save(fs);
        ProcessController log = new NullLogger();
        while (true) {
            getLog().info("Enter FTL/SQL (use 'quit' to exit):");
            String line = new BufferedReader(new InputStreamReader(inputStream)).readLine();
            if (line == null) {
                break;
            }
            line = line.trim();
            if (line.equals("quit")) {
                break;
            }
            fs.setSource(line);
            ParseResult result = fs.getResult();
            if (result.getResult() != null) {
                getLog().info("SQL> " + result.getResult());
            } else {
                getLog().info("ERROR> " + result.getError());
                continue;
            }
            try {
                sql.executeScript(fs, log);
            } catch (Exception e) {
                getLog().info("ERROR> " + e.getMessage());
            }
        }
    } catch (Exception e) {
        throw new MojoFailureException("Console error: " + e.getMessage(), e);
    }
}
Also used : ParseResult(com.developmentontheedge.be5.metadata.model.ParseResult) SQLException(java.sql.SQLException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SQLException(java.sql.SQLException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) NullLogger(com.developmentontheedge.be5.metadata.util.NullLogger) BeConnectionProfile(com.developmentontheedge.be5.metadata.model.BeConnectionProfile) ProcessController(com.developmentontheedge.be5.metadata.util.ProcessController) ResultSet(java.sql.ResultSet) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript)

Aggregations

BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)4 MojoFailureException (org.apache.maven.plugin.MojoFailureException)4 FreemarkerSqlException (com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException)3 Module (com.developmentontheedge.be5.metadata.model.Module)3 File (java.io.File)3 PrintStream (java.io.PrintStream)3 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)2 FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)2 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)2 ProcessController (com.developmentontheedge.be5.metadata.util.ProcessController)2 ExtendedSqlException (com.developmentontheedge.dbms.ExtendedSqlException)2 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 ProcessInterruptedException (com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException)1 BeConnectionProfile (com.developmentontheedge.be5.metadata.model.BeConnectionProfile)1 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)1 DataElementUtils (com.developmentontheedge.be5.metadata.model.DataElementUtils)1 DdlElement (com.developmentontheedge.be5.metadata.model.DdlElement)1