List of usage examples for org.apache.commons.dbutils BasicRowProcessor instance
BasicRowProcessor instance
To view the source code for org.apache.commons.dbutils BasicRowProcessor instance.
Click Source Link
From source file:org.openconcerto.sql.model.JDBCStructureSource.java
protected void _fillTables(final Set<String> newSchemas, final Connection conn) throws SQLException { // for new tables, add ; for existing, refresh final DatabaseMetaData metaData = conn.getMetaData(); // getColumns() only supports pattern (eg LIKE) so we must make multiple calls for (final String s : newSchemas) { final ResultSet rs = metaData.getColumns(this.getBase().getMDName(), s, "%", null); // handle tables becoming empty (possible in pg) final Set<SQLName> tablesWithColumns = new HashSet<SQLName>(); boolean hasNext = rs.next(); while (hasNext) { final String schemaName = rs.getString("TABLE_SCHEM"); final String tableName = rs.getString("TABLE_NAME"); tablesWithColumns.add(new SQLName(schemaName, tableName)); final Boolean moved; if (newSchemas.contains(schemaName)) { final SQLSchema schema = getNewSchema(schemaName); moved = schema.refreshTable(metaData, rs); } else { moved = null;/* w w w . ja v a 2 s. c om*/ } hasNext = moved == null ? rs.next() : moved; } // tables with no column = all tables - tables with column final SQLSchema schema = getNewSchema(s); for (final String t : schema.getTableNames()) { if (!tablesWithColumns.contains(new SQLName(s, t))) { // empty table with no db access schema.getTable(t).emptyFields(); } } } // type & comment for (final SQLName tName : getTablesNames()) { final SQLTable t = getNewSchema(tName.getItemLenient(-2)).getTable(tName.getName()); final List<String> l = (List<String>) this.tableNames.getNonNull(tName); t.setType(l.get(0)); t.setComment(l.get(1)); } final SQLSystem system = getBase().getServer().getSQLSystem(); // procedures final CollectionMap<String, String> proceduresBySchema = new CollectionMap<String, String>(); for (final String s : newSchemas) { final ResultSet rsProc = metaData.getProcedures(this.getBase().getMDName(), s, "%"); while (rsProc.next()) { // to ignore case : pg.AbstractJdbc2DatabaseMetaData doesn't quote aliases final Map map = BasicRowProcessor.instance().toMap(rsProc); final String schemaName = (String) map.get("PROCEDURE_SCHEM"); if (newSchemas.contains(schemaName)) { final String procName = (String) map.get("PROCEDURE_NAME"); proceduresBySchema.put(schemaName, procName); getNewSchema(schemaName).addProcedure(procName); } } } // try to find out more about those procedures if (proceduresBySchema.size() > 0) { final String sel = system.getSyntax().getFunctionQuery(getBase(), proceduresBySchema.keySet()); if (sel != null) { // don't cache since we don't listen on system tables for (final Object o : (List) getBase().getDataSource().execute(sel, new IResultSetHandler(SQLDataSource.MAP_LIST_HANDLER, false))) { final Map m = (Map) o; final SQLSchema newSchema = getNewSchema((String) m.get("schema")); if (newSchema != null) newSchema.setProcedureSource((String) m.get("name"), (String) m.get("src")); } } } // if no schemas exist, there can be no triggers // (avoid a query and a special case since "in ()" is not valid) if (newSchemas.size() > 0) { final ITransformer<Tuple2<String, String>, SQLTable> tableFinder = new ITransformer<Tuple2<String, String>, SQLTable>() { @Override public SQLTable transformChecked(Tuple2<String, String> input) { return getNewTable(input.get0(), input.get1()); } }; new JDBCStructureSource.TriggerQueryExecutor(tableFinder).apply(getBase(), newSchemas); new JDBCStructureSource.ColumnsQueryExecutor(tableFinder).apply(getBase(), newSchemas); try { new JDBCStructureSource.ConstraintsExecutor(tableFinder).apply(getBase(), newSchemas); } catch (QueryExn e1) { // constraints are not essentials, continue e1.printStackTrace(); for (final SQLName tName : getTablesNames()) { final SQLTable t = getNewSchema(tName.getItemLenient(-2)).getTable(tName.getName()); t.addConstraint(null); } } } }