Search in sources :

Example 1 with DbTableOrView

use of org.h2.bnf.context.DbTableOrView in project h2database by h2database.

the class WebApp method addTablesAndViews.

private int addTablesAndViews(DbSchema schema, boolean mainSchema, StringBuilder buff, int treeIndex) throws SQLException {
    if (schema == null) {
        return treeIndex;
    }
    Connection conn = session.getConnection();
    DatabaseMetaData meta = session.getMetaData();
    int level = mainSchema ? 0 : 1;
    boolean showColumns = mainSchema || !schema.isSystem;
    String indentation = ", " + level + ", " + (showColumns ? "1" : "2") + ", ";
    String indentNode = ", " + (level + 1) + ", 2, ";
    DbTableOrView[] tables = schema.getTables();
    if (tables == null) {
        return treeIndex;
    }
    boolean isOracle = schema.getContents().isOracle();
    boolean notManyTables = tables.length < SysProperties.CONSOLE_MAX_TABLES_LIST_INDEXES;
    for (DbTableOrView table : tables) {
        if (table.isView()) {
            continue;
        }
        int tableId = treeIndex;
        String tab = table.getQuotedName();
        if (!mainSchema) {
            tab = schema.quotedName + "." + tab;
        }
        tab = escapeIdentifier(tab);
        buff.append("setNode(").append(treeIndex).append(indentation).append(" 'table', '").append(PageParser.escapeJavaScript(table.getName())).append("', 'javascript:ins(\\'").append(tab).append("\\',true)');\n");
        treeIndex++;
        if (mainSchema || showColumns) {
            StringBuilder columnsBuffer = new StringBuilder();
            treeIndex = addColumns(mainSchema, table, buff, treeIndex, notManyTables, columnsBuffer);
            if (!isOracle && notManyTables) {
                treeIndex = addIndexes(mainSchema, meta, table.getName(), schema.name, buff, treeIndex);
            }
            buff.append("addTable('").append(PageParser.escapeJavaScript(table.getName())).append("', '").append(PageParser.escapeJavaScript(columnsBuffer.toString())).append("', ").append(tableId).append(");\n");
        }
    }
    tables = schema.getTables();
    for (DbTableOrView view : tables) {
        if (!view.isView()) {
            continue;
        }
        int tableId = treeIndex;
        String tab = view.getQuotedName();
        if (!mainSchema) {
            tab = view.getSchema().quotedName + "." + tab;
        }
        tab = escapeIdentifier(tab);
        buff.append("setNode(").append(treeIndex).append(indentation).append(" 'view', '").append(PageParser.escapeJavaScript(view.getName())).append("', 'javascript:ins(\\'").append(tab).append("\\',true)');\n");
        treeIndex++;
        if (mainSchema) {
            StringBuilder columnsBuffer = new StringBuilder();
            treeIndex = addColumns(mainSchema, view, buff, treeIndex, notManyTables, columnsBuffer);
            if (schema.getContents().isH2()) {
                try (PreparedStatement prep = conn.prepareStatement("SELECT * FROM " + "INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=?")) {
                    prep.setString(1, view.getName());
                    ResultSet rs = prep.executeQuery();
                    if (rs.next()) {
                        String sql = rs.getString("SQL");
                        buff.append("setNode(").append(treeIndex).append(indentNode).append(" 'type', '").append(PageParser.escapeJavaScript(sql)).append("', null);\n");
                        treeIndex++;
                    }
                    rs.close();
                }
            }
            buff.append("addTable('").append(PageParser.escapeJavaScript(view.getName())).append("', '").append(PageParser.escapeJavaScript(columnsBuffer.toString())).append("', ").append(tableId).append(");\n");
        }
    }
    return treeIndex;
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) DbTableOrView(org.h2.bnf.context.DbTableOrView) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 2 with DbTableOrView

use of org.h2.bnf.context.DbTableOrView in project h2database by h2database.

the class WebApp method addColumns.

private static int addColumns(boolean mainSchema, DbTableOrView table, StringBuilder buff, int treeIndex, boolean showColumnTypes, StringBuilder columnsBuffer) {
    DbColumn[] columns = table.getColumns();
    for (int i = 0; columns != null && i < columns.length; i++) {
        DbColumn column = columns[i];
        if (columnsBuffer.length() > 0) {
            columnsBuffer.append(' ');
        }
        columnsBuffer.append(column.getName());
        String col = escapeIdentifier(column.getName());
        String level = mainSchema ? ", 1, 1" : ", 2, 2";
        buff.append("setNode(").append(treeIndex).append(level).append(", 'column', '").append(PageParser.escapeJavaScript(column.getName())).append("', 'javascript:ins(\\'").append(col).append("\\')');\n");
        treeIndex++;
        if (mainSchema && showColumnTypes) {
            buff.append("setNode(").append(treeIndex).append(", 2, 2, 'type', '").append(PageParser.escapeJavaScript(column.getDataType())).append("', null);\n");
            treeIndex++;
        }
    }
    return treeIndex;
}
Also used : DbColumn(org.h2.bnf.context.DbColumn)

Aggregations

Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 DbColumn (org.h2.bnf.context.DbColumn)1 DbTableOrView (org.h2.bnf.context.DbTableOrView)1 SimpleResultSet (org.h2.tools.SimpleResultSet)1