001: /*
002: * IndexDefinition.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.util.ArrayList;
015: import java.util.Collections;
016: import java.util.List;
017: import workbench.util.SqlUtil;
018:
019: /**
020: * A class to store the defintion of a database index.
021: * @author support@sql-workbench.net
022: */
023: public class IndexDefinition implements DbObject {
024: private String expression;
025: private boolean isPK = false;
026: private boolean isUnique = false;
027: private String indexName;
028: private String indexType;
029: private String indexSchema;
030: private TableIdentifier baseTable;
031: private List<IndexColumn> columns = new ArrayList<IndexColumn>();
032:
033: public IndexDefinition(TableIdentifier table, String schema,
034: String name, String exp) {
035: this .indexSchema = schema;
036: this .indexName = name;
037: this .expression = exp;
038: this .baseTable = table;
039: }
040:
041: public String getSchema() {
042: return indexSchema;
043: }
044:
045: public String getCatalog() {
046: return null;
047: }
048:
049: public IndexDefinition(TableIdentifier table, String name,
050: String exp) {
051: this (table, null, name, exp);
052: }
053:
054: public void addColumn(String column, String direction) {
055: this .columns.add(new IndexColumn(column, direction));
056: }
057:
058: public void setIndexType(String type) {
059: if (type == null) {
060: this .indexType = "NORMAL";
061: } else {
062: this .indexType = type;
063: }
064: }
065:
066: public void setBaseTable(TableIdentifier table) {
067: this .baseTable = table;
068: }
069:
070: public String getObjectExpression(WbConnection conn) {
071: return SqlUtil.buildExpression(conn, null, indexSchema,
072: indexName);
073: }
074:
075: public String getObjectName(WbConnection conn) {
076: return conn.getMetadata().quoteObjectname(indexName);
077: }
078:
079: public String getObjectType() {
080: return "INDEX";
081: }
082:
083: public String getObjectName() {
084: return getName();
085: }
086:
087: public List<IndexColumn> getColumns() {
088: return Collections.unmodifiableList(columns);
089: }
090:
091: public String getIndexType() {
092: return this .indexType;
093: }
094:
095: public void setExpression(String exp) {
096: this .expression = exp;
097: }
098:
099: public String getExpression() {
100: if (this .expression == null) {
101: return buildExpression();
102: }
103: return this .expression;
104: }
105:
106: private String buildExpression() {
107: StringBuilder result = new StringBuilder(
108: this .columns.size() * 10);
109: for (int i = 0; i < this .columns.size(); i++) {
110: if (i > 0)
111: result.append(", ");
112: result.append(columns.get(i).getExpression());
113: }
114: return result.toString();
115: }
116:
117: public String getName() {
118: return this .indexName;
119: }
120:
121: public void setPrimaryKeyIndex(boolean flag) {
122: this .isPK = flag;
123: }
124:
125: public boolean isPrimaryKeyIndex() {
126: return this .isPK;
127: }
128:
129: public void setUnique(boolean flag) {
130: this .isUnique = flag;
131: }
132:
133: public boolean isUnique() {
134: return this .isUnique;
135: }
136:
137: public int hashCode() {
138: int hash = 71 * 7 + (this .indexName != null ? this .indexName
139: .hashCode() : 0);
140: return hash;
141: }
142:
143: public boolean equals(Object o) {
144: if (o instanceof IndexDefinition) {
145: IndexDefinition other = (IndexDefinition) o;
146: boolean equal = this .getExpression().equals(
147: other.getExpression());
148: if (equal) {
149: equal = (this .isPK == other.isPK)
150: && (this .isUnique == other.isUnique);
151: }
152: return equal;
153: } else if (o instanceof String) {
154: return this .getExpression().equals((String) o);
155: }
156: return false;
157: }
158:
159: public CharSequence getSource(WbConnection con) {
160: if (con == null)
161: return null;
162: String[] cols = new String[this .columns.size()];
163:
164: for (int i = 0; i < columns.size(); i++) {
165: cols[i] = columns.get(i).getColumn();
166: }
167:
168: return con.getMetadata().buildIndexSource(baseTable, indexName,
169: isUnique, cols);
170: }
171: }
|