Commit 31fc2d69 authored by Stefan Huehner's avatar Stefan Huehner
Browse files

ISSUE-47047: Speedup 'Reading tables...' on postgresql

Speed up reading the comment of columns (which can contain i.e. our
NVARCHAR) marker.
Do that by:
- Adding the col_description pl-function call directly into the SQL to
  read all other column definition (_stmt_listcolumns)
- That allows to move the commentCol processing into the readColumn part
  For that overload the readColumn with a small postgres specific
version
- That allows to remove the loop in the readTable which before ran +1
  SQL per column to retrieve the commentCol one by one
parent fd928183
......@@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.ForeignKey;
import org.apache.ddlutils.model.Function;
......@@ -62,7 +63,6 @@ public class PostgreSqlModelLoader extends ModelLoaderBase {
protected PreparedStatement _stmt_oids_funcs;
protected PreparedStatement _stmt_comments_funcs;
protected PreparedStatement _stmt_oids_tables;
protected PreparedStatement _stmt_comments_tables;
protected PreparedStatement _stmt_column_indexes;
protected Translation _checkTranslation = new PostgreSqlCheckTranslation();
......@@ -199,7 +199,8 @@ public class PostgreSqlModelLoader extends ModelLoaderBase {
sql += " WHERE pg_attrdef.adrelid = c.oid ";
sql += " AND pg_attrdef.adnum = a.attnum)";
sql += " ELSE NULL::text";
sql += " END ";
sql += " END, ";
sql += " col_description(c.oid,isc.ordinal_position)";
sql += " FROM pg_class c JOIN pg_attribute a ON c.oid = a.attrelid ";
sql += " JOIN pg_type t ON a.atttypid = t.oid ";
sql += " JOIN pg_namespace n ON c.relnamespace = n.oid ";
......@@ -374,8 +375,6 @@ public class PostgreSqlModelLoader extends ModelLoaderBase {
_stmt_oids_tables = _connection
.prepareStatement("SELECT oid, relname FROM pg_class WHERE upper(relname) = ?");
_stmt_comments_tables = _connection.prepareStatement("SELECT col_description(?,?)");
_stmt_column_indexes = _connection.prepareStatement(
"SELECT ordinal_position FROM information_schema.columns WHERE table_name = ? order by ordinal_position;");
......@@ -616,7 +615,6 @@ public class PostgreSqlModelLoader extends ModelLoaderBase {
int oidTable;
// tablename from readTable with the exact case like in the database
String tableRealName;
String commentCol;
ArrayList<Integer> columnIndexes;
......@@ -642,38 +640,37 @@ public class PostgreSqlModelLoader extends ModelLoaderBase {
}
});
// We'll change the types of NVarchar columns (which should have a
// comment in the database)
for (int i = 0; i < t.getColumnCount(); i++) {
_stmt_comments_tables.setInt(1, oidTable);
_stmt_comments_tables.setInt(2, columnIndexes.get(i));
fillList(_stmt_comments_tables, new RowFiller() {
@Override
public void fillRow(ResultSet r) throws SQLException {
commentCol = r.getString(1);
}
});
if (commentCol != null && !commentCol.equals("")) {
return t;
}
// We'll change the types of NVarchar columns (which should have a
// comment in the database)
@Override
protected Column readColumn(ResultSet rs) throws SQLException {
Column c = super.readColumn(rs);
// 9th column of _stmt_listcolumns contains the column comment
String commentCol = rs.getString(9);
if (commentCol != null && !commentCol.equals("")) {
Pattern pat = Pattern.compile("--OBTG:NVARCHAR--");
Matcher match = pat.matcher(commentCol);
if (match.find()) {
t.getColumn(i).setTypeCode(ExtTypes.NVARCHAR);
c.setTypeCode(ExtTypes.NVARCHAR);
}
Pattern pat2 = Pattern.compile("--OBTG:NCHAR--");
Matcher match2 = pat2.matcher(commentCol);
if (match2.find()) {
t.getColumn(i).setTypeCode(ExtTypes.NCHAR);
c.setTypeCode(ExtTypes.NCHAR);
}
Pattern pat3 = Pattern.compile("--OBTG:ONCREATEDEFAULT:(.*)--");
Matcher match3 = pat3.matcher(commentCol);
if (match3.find()) {
t.getColumn(i).setOnCreateDefault(match3.group(1));
c.setOnCreateDefault(match3.group(1));
}
}
}
return t;
return c;
}
@SuppressWarnings("rawtypes")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment