001: /*
002: * AbstractConstraintReader.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.sql.Connection;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Map;
021: import workbench.util.ExceptionUtil;
022:
023: import workbench.log.LogMgr;
024: import workbench.util.SqlUtil;
025:
026: /**
027: * A class to read table level constraints from the database.
028: * @author support@sql-workbench.net
029: */
030: public abstract class AbstractConstraintReader implements
031: ConstraintReader {
032: public AbstractConstraintReader() {
033: }
034:
035: public abstract String getColumnConstraintSql();
036:
037: public abstract String getTableConstraintSql();
038:
039: public String getPrefixTableConstraintKeyword() {
040: return "";
041: }
042:
043: public String getSuffixTableConstraintKeyword() {
044: return "";
045: }
046:
047: public boolean isColumnConstraintNameIncluded() {
048: return false;
049: }
050:
051: public boolean isTableConstraintNameIncluded() {
052: return false;
053: }
054:
055: public int getIndexForSchemaParameter() {
056: return -1;
057: }
058:
059: public int getIndexForCatalogParameter() {
060: return -1;
061: }
062:
063: public int getIndexForTableNameParameter() {
064: return 1;
065: }
066:
067: /**
068: * Returns the column constraints for the given table. The key to the Map is
069: * the column name, the value is the full expression which can be appended
070: * to the column definition inside a CREATE TABLE statement.
071: */
072: public Map<String, String> getColumnConstraints(
073: Connection dbConnection, TableIdentifier aTable) {
074: String sql = this .getColumnConstraintSql();
075: if (sql == null)
076: return Collections.emptyMap();
077:
078: HashMap<String, String> result = new HashMap<String, String>();
079:
080: ResultSet rs = null;
081: PreparedStatement stmt = null;
082: try {
083: stmt = dbConnection.prepareStatement(sql,
084: ResultSet.TYPE_FORWARD_ONLY,
085: ResultSet.CONCUR_READ_ONLY);
086: int index = this .getIndexForSchemaParameter();
087: if (index > 0)
088: stmt.setString(index, aTable.getSchema());
089:
090: index = this .getIndexForCatalogParameter();
091: if (index > 0)
092: stmt.setString(index, aTable.getCatalog());
093:
094: index = this .getIndexForTableNameParameter();
095: if (index > 0)
096: stmt.setString(index, aTable.getTableName());
097:
098: rs = stmt.executeQuery();
099: while (rs.next()) {
100: String column = rs.getString(1);
101: String constraint = rs.getString(2);
102: if (column != null && constraint != null) {
103: result.put(column.trim(), constraint.trim());
104: }
105: }
106: } catch (Exception e) {
107: LogMgr.logError("AbstractConstraintReader",
108: "Error when reading column constraints", e);
109: } finally {
110: SqlUtil.closeResult(rs);
111: }
112: return result;
113: }
114:
115: /**
116: * Returns the SQL Statement that should be appended to a CREATE table
117: * in order to create the constraints defined on the table
118: */
119: public String getTableConstraints(Connection dbConnection,
120: TableIdentifier aTable, String indent) throws SQLException {
121: String sql = this .getTableConstraintSql();
122: if (sql == null)
123: return null;
124: StringBuilder result = new StringBuilder(100);
125: String prefix = this .getPrefixTableConstraintKeyword();
126: String suffix = this .getSuffixTableConstraintKeyword();
127: PreparedStatement stmt = null;
128:
129: ResultSet rs = null;
130: try {
131: stmt = dbConnection.prepareStatement(sql,
132: ResultSet.TYPE_FORWARD_ONLY,
133: ResultSet.CONCUR_READ_ONLY);
134:
135: int index = this .getIndexForSchemaParameter();
136: if (index > 0)
137: stmt.setString(index, aTable.getSchema());
138:
139: index = this .getIndexForCatalogParameter();
140: if (index > 0)
141: stmt.setString(index, aTable.getCatalog());
142:
143: index = this .getIndexForTableNameParameter();
144: if (index > 0)
145: stmt.setString(index, aTable.getTableName());
146:
147: rs = stmt.executeQuery();
148: int count = 0;
149: while (rs.next()) {
150: String constraint = rs.getString(1);
151: if (constraint != null) {
152: if (count > 0) {
153: result.append("\n");
154: result.append(indent);
155: result.append(',');
156: }
157: result.append(prefix);
158: result.append(constraint);
159: result.append(suffix);
160: count++;
161: }
162: }
163: } catch (SQLException e) {
164: LogMgr.logError("AbstractConstraintReader",
165: "Error when reading column constraints "
166: + ExceptionUtil.getDisplay(e), null);
167: throw e;
168: } finally {
169: SqlUtil.closeAll(rs, stmt);
170: }
171: return result.toString();
172: }
173:
174: }
|