private Table _getTable(String catalog, String schema, String tableName) throws SQLException { if (tableName == null || tableName.trim().length() == 0) throw new IllegalArgumentException("tableName must be not empty"); catalog = StringHelper.defaultIfEmpty(catalog, null); schema = StringHelper.defaultIfEmpty(schema, null); Connection conn = getConnection(); DatabaseMetaData dbMetaData = conn.getMetaData(); ResultSet rs = dbMetaData.getTables(catalog, schema, tableName, null); while (rs.next()) { Table table = createTable(conn, rs); return table; } return null; }
private Table createTable(Connection conn, ResultSet rs) throws SQLException { String realTableName = null; try { ResultSetMetaData rsMetaData = rs.getMetaData(); String schemaName = rs.getString("TABLE_SCHEM") == null ? "" : rs.getString("TABLE_SCHEM"); realTableName = rs.getString("TABLE_NAME"); String tableType = rs.getString("TABLE_TYPE"); String remarks = rs.getString("REMARKS"); if (remarks == null && dbHelper.isOracleDataBase()) { remarks = getOracleTableComments(realTableName); } Table table = new Table(); table.setSqlName(realTableName); table.setRemarks(remarks); if ("SYNONYM".equals(tableType) && dbHelper.isOracleDataBase()) { table.setOwnerSynonymName(getSynonymOwner(realTableName)); } retriveTableColumns(table); table.initExportedKeys(conn.getMetaData()); table.initImportedKeys(conn.getMetaData()); BeanHelper.copyProperties( table, TableOverrideValuesProvider.getTableOverrideValues(table.getSqlName())); return table; } catch (SQLException e) { throw new RuntimeException("create table object error,tableName:" + realTableName, e); } }
private List getAllTables(Connection conn) throws SQLException { DatabaseMetaData dbMetaData = conn.getMetaData(); ResultSet rs = dbMetaData.getTables(getCatalog(), getSchema(), null, null); List tables = new ArrayList(); while (rs.next()) { tables.add(createTable(conn, rs)); } return tables; }
public Connection getConnection() { try { if (connection == null || connection.isClosed()) { loadJdbcDriver(); connection = DriverManager.getConnection( GeneratorProperties.getRequiredProperty("jdbc.url"), GeneratorProperties.getRequiredProperty("jdbc.username"), GeneratorProperties.getProperty("jdbc.password")); } return connection; } catch (SQLException e) { throw new RuntimeException(e); } }