001: /*
002: * McKoiMetadata.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.mckoi;
013:
014: import java.math.BigInteger;
015: import java.sql.Connection;
016: import java.sql.PreparedStatement;
017: import java.sql.ResultSet;
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.LinkedList;
021: import java.util.List;
022: import workbench.db.SequenceDefinition;
023: import workbench.db.SequenceReader;
024: import workbench.log.LogMgr;
025: import workbench.resource.Settings;
026: import workbench.storage.DataStore;
027: import workbench.util.SqlUtil;
028: import workbench.util.StringUtil;
029:
030: /**
031: * @author support@sql-workbench.net
032: */
033: public class McKoiMetadata implements SequenceReader {
034: private Connection connection;
035:
036: public McKoiMetadata(Connection con) {
037: this .connection = con;
038: }
039:
040: // This is a dirty hack, as McKoi does not store the real SQL
041: // but some kind of Object in the database. But for now this seems
042: // to work.
043: public String getViewSourceFromBlob(byte[] content) {
044: String s = new String(content, 2, content.length - 6);
045: return s;
046: }
047:
048: public List<SequenceDefinition> getSequences(String owner) {
049: DataStore ds = getRawSequenceDefinition(owner, null);
050: if (ds == null)
051: return Collections.emptyList();
052: List<SequenceDefinition> result = new ArrayList<SequenceDefinition>();
053: for (int row = 0; row < ds.getRowCount(); row++) {
054: SequenceDefinition def = createDefinition(ds, row);
055: result.add(def);
056: }
057: return result;
058: }
059:
060: public SequenceDefinition getSequenceDefinition(String owner,
061: String sequence) {
062: DataStore ds = getRawSequenceDefinition(owner, sequence);
063: if (ds == null || ds.getRowCount() != 1)
064: return null;
065: SequenceDefinition def = createDefinition(ds, 0);
066: return def;
067: }
068:
069: public DataStore getRawSequenceDefinition(String owner,
070: String sequence) {
071: String sql = "SELECT si.name, " + " sd.minvalue, "
072: + " sd.maxvalue, " + " sd.increment, "
073: + " sd.cycle, " + " sd.start, "
074: + " sd.cache "
075: + " FROM SYS_INFO.sUSRSequence sd, "
076: + " SYS_INFO.sUSRSequenceInfo si "
077: + "WHERE sd.seq_id = si.id " + "AND si.schema = ? ";
078:
079: if (!StringUtil.isEmptyString(sequence)) {
080: sql += "AND si.name = ? ";
081: }
082:
083: if (Settings.getInstance().getDebugMetadataSql()) {
084: LogMgr.logInfo("McKoiMetadata.getRawSequenceDefinition()",
085: "Using query=" + sql);
086: }
087:
088: PreparedStatement stmt = null;
089: ResultSet rs = null;
090: DataStore result = null;
091: try {
092: stmt = this .connection.prepareStatement(sql.toString());
093: stmt.setString(1, owner);
094: if (!StringUtil.isEmptyString(sequence))
095: stmt.setString(2, sequence);
096: rs = stmt.executeQuery();
097: result = new DataStore(rs, true);
098: } catch (Exception e) {
099: LogMgr.logError("McKoiMetadata.getSequenceDefinition()",
100: "Error when retrieving sequence definition", e);
101: } finally {
102: SqlUtil.closeAll(rs, stmt);
103: }
104:
105: return result;
106: }
107:
108: private SequenceDefinition createDefinition(DataStore ds, int row) {
109: if (ds == null || row >= ds.getRowCount())
110: return null;
111: String name = ds.getValueAsString(row, "name");
112: SequenceDefinition result = new SequenceDefinition(null, name);
113: result.setSequenceProperty("minvalue", ds.getValue(row,
114: "minvalue"));
115: result.setSequenceProperty("maxvalue", ds.getValue(row,
116: "maxvalue"));
117: result.setSequenceProperty("increment", ds.getValue(row,
118: "increment"));
119: result.setSequenceProperty("cycle", ds.getValue(row, "cycle"));
120: result.setSequenceProperty("cache", ds.getValue(row, "cache"));
121: result.setSequenceProperty("start", ds.getValue(row, "start"));
122: readSequenceSource(result);
123: return result;
124: }
125:
126: public List<String> getSequenceList(String owner) {
127: DataStore ds = getRawSequenceDefinition(owner, null);
128: if (ds == null)
129: return Collections.emptyList();
130: List<String> result = new LinkedList<String>();
131: for (int row = 0; row < ds.getRowCount(); row++) {
132: result.add(ds.getValueAsString(row, "NAME"));
133: }
134: return result;
135: }
136:
137: public CharSequence getSequenceSource(String owner, String sequence) {
138: SequenceDefinition def = getSequenceDefinition(owner, sequence);
139: if (def == null)
140: return null;
141: return def.getSource();
142: }
143:
144: public void readSequenceSource(SequenceDefinition def) {
145: StringBuilder result = new StringBuilder(200);
146:
147: result.append("CREATE SEQUENCE ");
148: result.append(def.getSequenceName());
149:
150: Number minvalue = (Number) def.getSequenceProperty("minvalue");
151: Number maxvalue = (Number) def.getSequenceProperty("maxvalue");
152: Number increment = (Number) def
153: .getSequenceProperty("increment");
154: Boolean cycle = (Boolean) def.getSequenceProperty("cycle");
155: Number start = (Number) def.getSequenceProperty("start");
156: Number cache = (Number) def.getSequenceProperty("cache");
157:
158: if (start.longValue() > 0) {
159: result.append("\n START ");
160: result.append(start);
161: }
162:
163: if (increment.longValue() != 1) {
164: result.append("\n INCREMENT ");
165: result.append(increment);
166: }
167:
168: BigInteger max = new BigInteger(Long.toString(Long.MAX_VALUE));
169:
170: if (minvalue.longValue() != 0) {
171: result.append("\n MINVALUE ");
172: result.append(minvalue);
173: }
174:
175: if (maxvalue.longValue() != Long.MAX_VALUE) {
176: result.append("\n MAXVALUE ");
177: result.append(maxvalue);
178: }
179:
180: if (cache.longValue() > 0) {
181: result.append("\n CACHE ");
182: result.append(cache);
183: }
184: if (cycle) {
185: result.append("\n CYCLE");
186: }
187: result.append(";\n");
188:
189: def.setSource(result);
190: }
191:
192: }
|