001: /*
002: * PostgresConstraintReader.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.postgres;
013:
014: import java.sql.Connection;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Savepoint;
019: import workbench.db.AbstractConstraintReader;
020: import workbench.db.TableIdentifier;
021: import workbench.util.ExceptionUtil;
022: import workbench.log.LogMgr;
023: import workbench.resource.Settings;
024: import workbench.util.SqlUtil;
025:
026: /**
027: * Read table level constraints for Postgres
028: * (column constraints are stored on table level...)
029: * @author support@sql-workbench.net
030: */
031: public class PostgresConstraintReader extends AbstractConstraintReader {
032: private static final String TABLE_SQL = "select rel.consrc, rel.conname \n"
033: + "from pg_class t, pg_constraint rel \n"
034: + "where t.relname = ? \n"
035: + "and t.oid = rel.conrelid "
036: + "and rel.contype = 'c'";
037:
038: public PostgresConstraintReader() {
039: }
040:
041: public String getPrefixTableConstraintKeyword() {
042: return "check";
043: }
044:
045: public String getColumnConstraintSql() {
046: return null;
047: }
048:
049: public String getTableConstraintSql() {
050: return TABLE_SQL;
051: }
052:
053: public String getTableConstraints(Connection dbConnection,
054: TableIdentifier aTable, String indent) throws SQLException {
055: String sql = this .getTableConstraintSql();
056: if (sql == null)
057: return null;
058: StringBuilder result = new StringBuilder(100);
059:
060: String nl = Settings.getInstance()
061: .getInternalEditorLineEnding();
062:
063: ResultSet rs = null;
064: PreparedStatement stmt = null;
065: Savepoint sp = null;
066: try {
067: sp = dbConnection.setSavepoint();
068: stmt = dbConnection.prepareStatement(sql);
069: stmt.setString(1, aTable.getTableName());
070: rs = stmt.executeQuery();
071: int count = 0;
072: while (rs.next()) {
073: String constraint = rs.getString(1);
074: String name = rs.getString(2);
075: if (constraint != null) {
076: if (count > 0) {
077: result.append(nl);
078: result.append(indent);
079: result.append(',');
080: }
081: if (name != null) {
082: result.append("CONSTRAINT ");
083: result.append(name);
084: result.append(' ');
085: }
086: result.append("CHECK ");
087: result.append(constraint);
088: count++;
089: }
090: }
091: dbConnection.releaseSavepoint(sp);
092: } catch (SQLException e) {
093: try {
094: dbConnection.rollback(sp);
095: } catch (Throwable th) {
096: }
097: LogMgr.logError("AbstractConstraintReader",
098: "Error when reading column constraints "
099: + ExceptionUtil.getDisplay(e), null);
100: throw e;
101: } finally {
102: SqlUtil.closeAll(rs, stmt);
103: }
104: return result.toString();
105: }
106:
107: }
|