001: /*
002: * HsqlSequenceReader.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.hsqldb;
013:
014: import java.sql.Connection;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.LinkedList;
020: import java.util.List;
021: import workbench.db.SequenceDefinition;
022: import workbench.db.SequenceReader;
023: import workbench.log.LogMgr;
024: import workbench.resource.Settings;
025: import workbench.storage.DataStore;
026: import workbench.util.SqlUtil;
027: import static workbench.util.StringUtil.isEmptyString;
028:
029: /**
030: * @author support@sql-workbench.net
031: */
032: public class HsqlSequenceReader implements SequenceReader {
033: private Connection dbConn;
034: private String sequenceTable;
035:
036: public HsqlSequenceReader(Connection conn) {
037: this .dbConn = conn;
038: if (HsqlMetadata.supportsInformationSchema(conn)) {
039: sequenceTable = "information_schema.system_sequences";
040: } else {
041: sequenceTable = "system_sequences";
042: }
043: }
044:
045: public void readSequenceSource(SequenceDefinition def) {
046: if (def == null)
047: return;
048: CharSequence s = getSequenceSource(def.getSequenceOwner(), def
049: .getSequenceName());
050: def.setSource(s);
051: }
052:
053: public DataStore getRawSequenceDefinition(String owner,
054: String sequence) {
055: String query = "SELECT sequence_schema, " + "sequence_name, "
056: + "dtd_identifier, " + "maximum_value, "
057: + "minimum_value, " + "increment, " + "cycle_option, "
058: + "start_with " + "FROM " + sequenceTable;
059:
060: if (!isEmptyString(sequence)) {
061: query += " WHERE sequence_name = ?";
062: }
063:
064: if (Settings.getInstance().getDebugMetadataSql()) {
065: LogMgr.logInfo(
066: "HsqlSequenceReader.getRawSequenceDefinition()",
067: "Using query=" + query.toString());
068: }
069:
070: PreparedStatement stmt = null;
071: ResultSet rs = null;
072: DataStore result = null;
073: try {
074: stmt = this .dbConn.prepareStatement(query.toString());
075: if (!isEmptyString(sequence))
076: stmt.setString(1, sequence.trim());
077: rs = stmt.executeQuery();
078: result = new DataStore(rs, true);
079: } catch (Throwable e) {
080: LogMgr.logError(
081: "HsqlSequenceReader.getSequenceDefinition()",
082: "Error when retrieving sequence definition", e);
083: } finally {
084: SqlUtil.closeAll(rs, stmt);
085: }
086:
087: return result;
088: }
089:
090: public List<SequenceDefinition> getSequences(String owner) {
091: DataStore ds = getRawSequenceDefinition(owner, null);
092: if (ds == null)
093: return Collections.emptyList();
094:
095: List<SequenceDefinition> result = new ArrayList<SequenceDefinition>();
096:
097: for (int row = 0; row < ds.getRowCount(); row++) {
098: result.add(createSequenceDefinition(ds, row));
099: }
100: return result;
101: }
102:
103: private SequenceDefinition createSequenceDefinition(DataStore ds,
104: int row) {
105: SequenceDefinition result = null;
106:
107: if (ds == null || ds.getRowCount() == 0)
108: return null;
109:
110: String name = ds.getValueAsString(row, "SEQUENCE_NAME");
111: String schema = ds.getValueAsString(row, "SEQUENCE_SCHEMA");
112: result = new SequenceDefinition(schema, name);
113:
114: result.setSequenceProperty("START_WITH", ds.getValue(row,
115: "START_WITH"));
116: result.setSequenceProperty("INCREMENT", ds.getValue(row,
117: "INCREMENT"));
118: result.setSequenceProperty("DTD_IDENTIFIER", ds.getValue(row,
119: "DTD_IDENTIFIER"));
120:
121: return result;
122: }
123:
124: public SequenceDefinition getSequenceDefinition(String owner,
125: String sequence) {
126: DataStore ds = getRawSequenceDefinition(owner, sequence);
127: if (ds == null)
128: return null;
129: return createSequenceDefinition(ds, 0);
130: }
131:
132: public List<String> getSequenceList(String owner) {
133: DataStore ds = getRawSequenceDefinition(owner, null);
134: if (ds == null)
135: return Collections.emptyList();
136:
137: List<String> result = new LinkedList<String>();
138:
139: for (int row = 0; row < ds.getRowCount(); row++) {
140: result.add(ds.getValueAsString(row, "SEQUENCE_NAME"));
141: }
142: return result;
143: }
144:
145: public CharSequence getSequenceSource(String owner, String sequence) {
146: SequenceDefinition def = getSequenceDefinition(owner, sequence);
147:
148: StringBuilder result = new StringBuilder(100);
149: result.append("CREATE SEQUENCE ");
150: String nl = Settings.getInstance()
151: .getInternalEditorLineEnding();
152: result.append(def.getSequenceName());
153: String type = (String) def
154: .getSequenceProperty("DTD_IDENTIFIER");
155:
156: if (!"INTEGER".equals(type)) {
157: result.append(" AS " + type);
158: }
159:
160: // For some reason HSQLDB returns all properties as String objects, even the numeric ones!
161: String start = (String) def.getSequenceProperty("START_WITH");
162: result.append(nl + " START WITH ");
163: result.append(start);
164:
165: String inc = (String) def.getSequenceProperty("INCREMENT");
166: result.append(nl + " INCREMENT BY ");
167: result.append(inc);
168: result.append(';');
169: result.append(nl);
170:
171: return result;
172: }
173: }
|