001: /*
002: * PostgresIndexReader.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.ResultSet;
015: import java.sql.Savepoint;
016: import java.sql.Statement;
017: import workbench.db.DbMetadata;
018: import workbench.db.JdbcIndexReader;
019: import workbench.db.TableIdentifier;
020: import workbench.db.WbConnection;
021: import workbench.log.LogMgr;
022: import workbench.resource.Settings;
023: import workbench.storage.DataStore;
024: import workbench.util.ExceptionUtil;
025: import workbench.util.SqlUtil;
026: import workbench.util.StringUtil;
027:
028: /**
029: * @author support@sql-workbench.net
030: */
031: public class PostgresIndexReader extends JdbcIndexReader {
032: public PostgresIndexReader(DbMetadata meta) {
033: super (meta);
034: }
035:
036: public StringBuilder getIndexSource(TableIdentifier table,
037: DataStore indexDefinition, String tableNameToUse) {
038: WbConnection con = this .metaData.getWbConnection();
039: Statement stmt = null;
040: ResultSet rs = null;
041:
042: // The full CREATE INDEX Statement is stored in pg_indexes for each
043: // index. So all we need to do, is retrieve the indexdef value
044: // from that table for all indexes defined for this table.
045:
046: StringBuilder sql = new StringBuilder(50 + indexDefinition
047: .getRowCount() * 20);
048: sql
049: .append("SELECT indexdef FROM pg_indexes WHERE indexname in (");
050:
051: String nl = Settings.getInstance()
052: .getInternalEditorLineEnding();
053: int count = indexDefinition.getRowCount();
054: if (count == 0)
055: return StringUtil.emptyBuffer();
056:
057: StringBuilder source = new StringBuilder(count * 50);
058: Savepoint sp = null;
059: try {
060: sp = con.setSavepoint();
061: stmt = con.createStatement();
062:
063: for (int i = 0; i < count; i++) {
064: String idxName = indexDefinition
065: .getValueAsString(
066: i,
067: DbMetadata.COLUMN_IDX_TABLE_INDEXLIST_INDEX_NAME);
068: String pk = indexDefinition.getValueAsString(i,
069: DbMetadata.COLUMN_IDX_TABLE_INDEXLIST_PK_FLAG);
070: if ("YES".equalsIgnoreCase(pk))
071: continue;
072: if (i > 0)
073: sql.append(',');
074: sql.append('\'');
075: sql.append(idxName);
076: sql.append('\'');
077: }
078: sql.append(')');
079:
080: rs = stmt.executeQuery(sql.toString());
081: while (rs.next()) {
082: source.append(rs.getString(1));
083: source.append(';');
084: source.append(nl);
085: }
086: SqlUtil.closeResult(rs);
087:
088: con.releaseSavepoint(sp);
089: } catch (Exception e) {
090: con.rollback(sp);
091: LogMgr.logError("PostgresIndexReader.getIndexSource()",
092: "Error retrieving source", e);
093: source = new StringBuilder(ExceptionUtil.getDisplay(e));
094: } finally {
095: SqlUtil.closeAll(rs, stmt);
096: }
097:
098: if (source.length() > 0)
099: source.append(nl);
100:
101: return source;
102: }
103:
104: }
|