Skip to content

Commit

Permalink
Wasn't a great idea to do view sql query once vs. once per view.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncurrier committed Aug 7, 2008
1 parent d80fd6b commit 76eff94
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/net/sourceforge/schemaspy/dbTypes/db2.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ driverPath=c:/Program Files/IBM/SQLLIB/java/db2java.zip
# all of these selects are optional:
# these named parameters are supported: :schema, :owner, :table and :view

# return the sql used to create each view
selectViewSql=select viewname view_name, text view_text from syscat.views where viewschema=:schema
# return text that represents a specific :view / :schema
selectViewSql=select text from syscat.views where viewname=:view and viewschema=:schema

# return table_name, constraint_name and text for a specific :schema
selectCheckConstraintsSql=select constname constraint_name, tabname table_name, text from syscat.checks where tabschema=:schema
Expand Down
4 changes: 2 additions & 2 deletions src/net/sourceforge/schemaspy/dbTypes/mssql.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ driverPath=C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/msbase
# all of these selects are optional:
# these named parameters are supported: :schema, :owner, :table and :view

# return the sql used to create each view
selectViewSql=select so.name as view_name, text as view_text from syscomments sc, sysobjects so where sc.id=so.id
# return text that represents a specific :view / :schema
selectViewSql=select text from syscomments sc, sysobjects so where sc.id=so.id and so.name=:table

# return table_name, constraint_name and text for a specific :schema
#selectCheckConstraintsSql=select distinct a.name constraint_name, text from sysobjects a, syscomments b, sysobjects c where a.id = b.id and a.type = 'C'
Expand Down
4 changes: 2 additions & 2 deletions src/net/sourceforge/schemaspy/dbTypes/ora.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ dbThreads=1
# all of these selects are optional:
# these named parameters are supported: :schema, :owner, :table and :view

# return the sql used to create each view
selectViewSql=select view_name, text as view_text from all_views where owner=:owner
# return text that represents a specific :view / :schema
selectViewSql=select text from all_views where view_name=:view and owner=:owner

# return table_name, constraint_name and text for a specific :schema
selectCheckConstraintsSql=select table_name, constraint_name, search_condition text from all_constraints where constraint_type = 'C' and constraint_name not like 'SYS%' and owner = :owner
Expand Down
32 changes: 1 addition & 31 deletions src/net/sourceforge/schemaspy/model/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ private void initViews(String schema, DatabaseMetaData metadata, Properties prop
System.out.print('.');

try {
View view = new View(this, rs);
View view = new View(this, rs, properties.getProperty("selectViewSql"));

if (include.matcher(view.getName()).matches())
views.put(view.getName(), view);
Expand All @@ -558,36 +558,6 @@ private void initViews(String schema, DatabaseMetaData metadata, Properties prop
} finally {
if (rs != null)
rs.close();
rs = null;
}

String selectViewSql = properties.getProperty("selectViewSql");
if (selectViewSql != null) {
PreparedStatement stmt = null;

try {
stmt = prepareStatement(selectViewSql, null);
rs = stmt.executeQuery();
while (rs.next()) {
String viewName = rs.getString("view_name");
String viewSql = rs.getString("view_text");

View view = views.get(viewName);
if (view != null) {
view.setViewSql(viewSql);
}
}
} catch (SQLException sqlException) {
System.out.flush();
System.err.println();
System.err.println("Failed to retrieve view details: " + sqlException);
System.err.println(selectViewSql);
} finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
}
}
}

Expand Down
35 changes: 29 additions & 6 deletions src/net/sourceforge/schemaspy/model/View.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package net.sourceforge.schemaspy.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class View extends Table {
private String viewSql;
private final String viewSql;

/**
* @param db
* @param rs
* @param selectViewSql
* @throws java.sql.SQLException
*/
public View(Database db, ResultSet rs) throws SQLException {
public View(Database db, ResultSet rs, String selectViewSql) throws SQLException {
super(db, rs.getString("TABLE_SCHEM"), rs.getString("TABLE_NAME"), db.getOptionalString(rs, "REMARKS"), null);
viewSql = getViewSql(db, selectViewSql);
}

/**
Expand All @@ -24,10 +27,6 @@ public boolean isView() {
return true;
}

public void setViewSql(String viewSql) {
this.viewSql = viewSql;
}

@Override
public String getViewSql() {
return viewSql;
Expand All @@ -37,4 +36,28 @@ public String getViewSql() {
protected int fetchNumRows(Database database, Properties properties) {
return 0;
}

private String getViewSql(Database db, String selectViewSql) throws SQLException {
if (selectViewSql == null)
return null;

PreparedStatement stmt = null;
ResultSet rs = null;

try {
stmt = db.prepareStatement(selectViewSql, getName());
rs = stmt.executeQuery();
while (rs.next())
return rs.getString("text");
return null;
} catch (SQLException sqlException) {
System.err.println(selectViewSql);
throw sqlException;
} finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
}
}
}

0 comments on commit 76eff94

Please sign in to comment.