001: /*
002: * SequenceDefinition.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.SQLException;
015: import java.util.Iterator;
016: import java.util.LinkedHashMap;
017: import java.util.Map;
018:
019: /**
020: *
021: * @author support@sql-workbench.net
022: */
023: public class SequenceDefinition implements DbObject {
024: private String sequenceName;
025: private String schema;
026: private CharSequence source;
027:
028: private Map<String, Object> properties = new LinkedHashMap<String, Object>();
029:
030: public SequenceDefinition(String seqSchema, String seqName) {
031: sequenceName = seqName;
032: schema = seqSchema;
033: }
034:
035: public CharSequence getSource(WbConnection con) throws SQLException {
036: if (con == null)
037: return null;
038: return con.getMetadata().getSequenceSource(null, schema,
039: sequenceName);
040: }
041:
042: public String getSchema() {
043: return schema;
044: }
045:
046: public String getCatalog() {
047: return null;
048: }
049:
050: public String getObjectName(WbConnection conn) {
051: return conn.getMetadata().quoteObjectname(this .sequenceName);
052: }
053:
054: public String getObjectExpression(WbConnection conn) {
055: StringBuilder expr = new StringBuilder(30);
056: if (schema != null) {
057: expr.append(conn.getMetadata().quoteObjectname(schema));
058: expr.append('.');
059: }
060: expr.append(conn.getMetadata().quoteObjectname(sequenceName));
061: return expr.toString();
062: }
063:
064: public String getObjectType() {
065: return "SEQUENCE";
066: }
067:
068: public String getObjectName() {
069: return getSequenceName();
070: }
071:
072: public String getSequenceName() {
073: return this .sequenceName;
074: }
075:
076: public String getSequenceOwner() {
077: return this .schema;
078: }
079:
080: /**
081: * As each Database has a different set of attributes for a sequence
082: * these attributes are stored as key/value pairs to be as generic
083: * as possible
084: */
085: public void setSequenceProperty(String property, Object value) {
086: this .properties.put(property, value);
087: }
088:
089: public Object getSequenceProperty(String property) {
090: return properties.get(property);
091: }
092:
093: public CharSequence getSource() {
094: return source;
095: }
096:
097: public void setSource(CharSequence src) {
098: source = src;
099: }
100:
101: public Iterator<String> getProperties() {
102: return this .properties.keySet().iterator();
103: }
104:
105: @Override
106: public boolean equals(Object other) {
107: if (other instanceof SequenceDefinition) {
108: SequenceDefinition od = (SequenceDefinition) other;
109: if (od.properties.size() != this .properties.size())
110: return false;
111: Iterator<Map.Entry<String, Object>> entries = this .properties
112: .entrySet().iterator();
113:
114: while (entries.hasNext()) {
115: Map.Entry entry = entries.next();
116: Object ov = od.properties.get(entry.getKey());
117: if (ov == null)
118: return false;
119: if (!ov.equals(entry.getValue()))
120: return false;
121: }
122: return true;
123: }
124: return false;
125: }
126:
127: @Override
128: public int hashCode() {
129: int hash = 3;
130: hash = 67
131: * hash
132: + (this .properties != null ? this .properties.hashCode()
133: : 0);
134: return hash;
135: }
136:
137: }
|