Search in sources :

Example 11 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController in project be5 by DevelopmentOnTheEdge.

the class H2SchemaReader method readColumns.

@Override
public Map<String, List<SqlColumnInfo>> readColumns(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<SqlColumnInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT " + "c.table_name, " + "c.column_name, " + "c.column_default, " + "c.TYPE_NAME, " + "c.character_maximum_length, " + "c.numeric_precision, " + "c.numeric_scale, " + "c.is_nullable, " + "c.CHECK_CONSTRAINT " + "FROM INFORMATION_SCHEMA.COLUMNS c " + "WHERE c.TABLE_SCHEMA <> 'INFORMATION_SCHEMA'");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            if (!tableName.equals(tableName.toLowerCase()))
                continue;
            List<SqlColumnInfo> list = result.get(tableName);
            if (list == null) {
                list = new ArrayList<>();
                result.put(tableName, list);
            }
            SqlColumnInfo info = new SqlColumnInfo();
            list.add(info);
            info.setName(rs.getString(2));
            info.setType(rs.getString(4));
            info.setCanBeNull("YES".equals(rs.getString(8)));
            String defaultValue = rs.getString(3);
            if (defaultValue != null) {
                for (String suffix : SUFFICES) {
                    if (defaultValue.endsWith(suffix))
                        defaultValue = defaultValue.substring(0, defaultValue.length() - suffix.length());
                }
                defaultValue = DEFAULT_DATE_PATTERN.matcher(defaultValue).replaceFirst("'$1'");
            }
            info.setDefaultValue(defaultValue);
            info.setSize(rs.getInt(5));
            if (rs.wasNull()) {
                info.setSize(rs.getInt(6));
            }
            info.setPrecision(rs.getInt(7));
            info.setAutoIncrement(defaultValue != null && defaultValue.startsWith("nextval"));
            String check = rs.getString(9);
            if (check == null)
                continue;
            Matcher matcher = ENUM_VALUES_PATTERN.matcher(check);
            List<String> vals = new ArrayList<>();
            while (matcher.find()) {
                vals.add(matcher.group(1));
            }
            info.setEnumValues(vals.toArray(new String[vals.size()]));
            // Just to check for interrupts
            controller.setProgress(0);
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)

Example 12 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController in project be5 by DevelopmentOnTheEdge.

the class MySqlSchemaReader method readIndices.

@Override
public Map<String, List<IndexInfo>> readIndices(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<IndexInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT table_name,index_name,column_name,non_unique FROM information_schema.statistics " + "WHERE table_schema='" + defSchema + "' ORDER BY table_name,index_name,seq_in_index");
    try {
        IndexInfo curIndex = null;
        String lastTable = null;
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            String indexName = rs.getString(2);
            if (!tableName.equals(lastTable) || curIndex == null || !curIndex.getName().equals(indexName)) {
                List<IndexInfo> list = result.get(tableName);
                if (list == null) {
                    list = new ArrayList<>();
                    result.put(tableName, list);
                }
                curIndex = new IndexInfo();
                lastTable = tableName;
                list.add(curIndex);
                curIndex.setName(indexName);
                int nonUnique = rs.getInt(4);
                curIndex.setUnique(nonUnique == 0);
            }
            String column = rs.getString(3);
            curIndex.addColumn(column);
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)

Example 13 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController in project be5 by DevelopmentOnTheEdge.

the class AppSync method readSchema.

private void readSchema() throws ExtendedSqlException, SQLException, ProcessInterruptedException {
    getLog().info("Read database scheme ...");
    long time = System.currentTimeMillis();
    ProcessController controller = new NullLogger();
    Rdbms rdbms = DatabaseUtils.getRdbms(connector);
    DbmsSchemaReader schemaReader = rdbms.getSchemaReader();
    defSchema = schemaReader.getDefaultSchema(sqlExecutor);
    tableTypes = schemaReader.readTableNames(sqlExecutor, defSchema, controller);
    columns = schemaReader.readColumns(sqlExecutor, defSchema, controller);
    indices = schemaReader.readIndices(sqlExecutor, defSchema, controller);
    if (debug) {
        if (!warnings.isEmpty()) {
            System.err.println(warnings.size() + " warning(s) during loading the project from " + sqlExecutor.getConnector().getConnectString());
            Collections.sort(warnings);
            for (String warning : warnings) {
                System.err.println(warning);
            }
        }
    }
    getLog().info("comleted, " + (System.currentTimeMillis() - time) + "ms.");
}
Also used : NullLogger(com.developmentontheedge.be5.metadata.util.NullLogger) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ProcessController(com.developmentontheedge.be5.metadata.util.ProcessController) DbmsSchemaReader(com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader)

Example 14 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController 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)

Example 15 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController in project be5 by DevelopmentOnTheEdge.

the class ModuleLoader2 method mergeModules.

public static void mergeModules(Project be5Project, ProcessController logger) throws ProjectLoadException {
    long startTime = System.nanoTime();
    LoadContext loadContext = new LoadContext();
    try {
        ModuleLoader2.mergeAllModules(be5Project, logger, loadContext);
    } catch (ProjectLoadException e) {
        throw new ProjectLoadException("Merge modules", e);
    }
    loadContext.check();
    log.info(ModuleLoader2.logLoadedProject(be5Project, startTime));
}
Also used : ProjectLoadException(com.developmentontheedge.be5.metadata.exception.ProjectLoadException)

Aggregations

ResultSet (java.sql.ResultSet)12 ArrayList (java.util.ArrayList)12 DbmsConnector (com.developmentontheedge.dbms.DbmsConnector)11 HashMap (java.util.HashMap)11 List (java.util.List)11 SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)6 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)5 Matcher (java.util.regex.Matcher)5 ColumnFunction (com.developmentontheedge.be5.metadata.model.ColumnFunction)3 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)2 ProcessController (com.developmentontheedge.be5.metadata.util.ProcessController)2 ProjectLoadException (com.developmentontheedge.be5.metadata.exception.ProjectLoadException)1 BeConnectionProfile (com.developmentontheedge.be5.metadata.model.BeConnectionProfile)1 FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)1 Module (com.developmentontheedge.be5.metadata.model.Module)1 ParseResult (com.developmentontheedge.be5.metadata.model.ParseResult)1 Project (com.developmentontheedge.be5.metadata.model.Project)1 BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)1 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)1 DbmsSchemaReader (com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader)1