Search in sources :

Example 6 with Table

use of henplus.sqlmodel.Table in project henplus by neurolabs.

the class SQLMetaDataBuilder method getTable.

public Table getTable(final SQLSession session, final String tableName) {
    Table table = null;
    ResultSet rset = null;
    try {
        final String catalog = session.getConnection().getCatalog();
        final DatabaseMetaData meta = session.getConnection().getMetaData();
        rset = meta.getColumns(catalog, null, tableName, null);
        table = buildTable(catalog, meta, tableName, rset);
    } catch (final Exception e) {
        if (VERBOSE) {
            e.printStackTrace();
        }
        HenPlus.msg().println("Database problem reading meta data: " + e.getMessage().trim());
    } finally {
        if (rset != null) {
            try {
                rset.close();
            } catch (final Exception e) {
            }
        }
    }
    return table;
}
Also used : Table(henplus.sqlmodel.Table) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with Table

use of henplus.sqlmodel.Table in project henplus by neurolabs.

the class DependencyResolver method resolveDep.

/**
     * @return
     * 
     */
/*
     * Martin: needed ? private Set restructureDeps() { Set deps = null; if (
     * cyclicDependencies != null ) { deps = new HashSet(); Iterator iter =
     * cyclicDependencies.iterator(); while ( iter.hasNext() ) deps.add(
     * ((ListMap)iter.next()).valuesList() ); } return deps; }
     */
/**
     * @param resolved
     * @param unresolved
     * @return A Map which contains all yet unresolved Tables mapped to their names.
     */
/*
     * Martin: needed ? private Map cleanUnresolved( Map resolved, Map
     * unresolved ) { Map result = null;
     * 
     * if ( unresolved != null ) { Iterator iter =
     * unresolved.keySet().iterator(); while ( iter.hasNext() ) { // type
     * element = (type) iter.next();
     * 
     * } }
     * 
     * return null; }
     */
/**
     * @param t
     * @param cyclePath
     *            The path of tables which have cyclic dependencies
     * @param resolved
     * @param unresolved
     */
private void resolveDep(final Table t, List<Table> cyclePath, final Map<String, Table> resolved, final Map<String, Table> unresolved) {
    Logger.debug("[resolveDep] >>> Starting for t: %s and cyclePath: %s", t, cyclePath);
    // if the current table is no more in the unresolved collection
    if (t == null || resolved.containsKey(t.getName())) {
        return;
    }
    boolean nodep = false;
    boolean firstrun = true;
    final Set<ColumnFkInfo> fks = t.getForeignKeys();
    final Iterator<ColumnFkInfo> iter = fks.iterator();
    while (iter.hasNext()) {
        final ColumnFkInfo fk = iter.next();
        Logger.debug("[resolveDep] FK -> %s: %s", fk.getPkTable(), resolved.containsKey(fk.getPkTable()));
        if (!resolved.containsKey(fk.getPkTable())) {
            final Table inner = unresolved.get(fk.getPkTable());
            // then proceed to the next FK and ignore this potential cycle
            if (duplicateCycle(t, inner)) {
                continue;
            }
            if (cyclePath != null && cyclePath.contains(inner)) {
                cyclePath.add(t);
                // create a new list for the detected cycle to add to the
                // cyclicDeps, the former one (cyclePath) is used further on
                final List<Table> cycle = new ArrayList<Table>(cyclePath);
                cycle.add(inner);
                if (_cyclicDependencies == null) {
                    _cyclicDependencies = new HashSet<List<Table>>();
                }
                Logger.debug("[resolveDep] +++ Putting cyclePath: %s", cycle);
                _cyclicDependencies.add(cycle);
                continue;
            } else {
                if (cyclePath == null) {
                    Logger.debug("[resolveDep] Starting cyclePath with: %s", t);
                    cyclePath = new ArrayList<Table>();
                }
                cyclePath.add(t);
            }
            resolveDep(inner, cyclePath, resolved, unresolved);
            if (resolved.containsKey(fk.getPkTable())) {
                nodep = (firstrun || nodep);
                firstrun = false;
            }
        } else {
            nodep = (firstrun || nodep);
            firstrun = false;
        }
    }
    if (nodep && !resolved.containsKey(t.getName())) {
        Logger.debug("[resolveDep] put %s to resolved.", t);
        resolved.put(t.getName(), t);
    }
}
Also used : Table(henplus.sqlmodel.Table) ColumnFkInfo(henplus.sqlmodel.ColumnFkInfo) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Table (henplus.sqlmodel.Table)7 ColumnFkInfo (henplus.sqlmodel.ColumnFkInfo)3 SQLException (java.sql.SQLException)3 DatabaseMetaData (java.sql.DatabaseMetaData)2 ResultSet (java.sql.ResultSet)2 NoSuchElementException (java.util.NoSuchElementException)2 SQLMetaData (henplus.SQLMetaData)1 SQLMetaDataBuilder (henplus.SQLMetaDataBuilder)1 Column (henplus.sqlmodel.Column)1 PrimaryKey (henplus.sqlmodel.PrimaryKey)1 DependencyResolver (henplus.util.DependencyResolver)1 ResolverResult (henplus.util.DependencyResolver.ResolverResult)1 NameCompleter (henplus.view.util.NameCompleter)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1