001: /*
002: * H2SequenceReader.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.h2database;
013:
014: import java.sql.Connection;
015: import java.sql.ResultSet;
016: import java.sql.Statement;
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 workbench.util.StringUtil;
028:
029: /**
030: * SequenceReqder for H2 Database engine
031: * @author support@sql-workbench.net
032: */
033: public class H2SequenceReader implements SequenceReader {
034: private Connection dbConnection;
035:
036: public H2SequenceReader(Connection conn) {
037: this .dbConnection = conn;
038: }
039:
040: /**
041: * Return the source SQL for a H2 sequence definition.
042: *
043: * @return The SQL to recreate the given sequence
044: */
045: public CharSequence getSequenceSource(String owner, String aSequence) {
046: SequenceDefinition def = getSequenceDefinition(owner, aSequence);
047: if (def == null)
048: return "";
049: return def.getSource();
050: }
051:
052: public List<String> getSequenceList(String owner) {
053: List<String> result = new LinkedList<String>();
054: ResultSet rs = null;
055: Statement stmt = null;
056: try {
057: stmt = this .dbConnection.createStatement();
058: rs = stmt
059: .executeQuery("SELECT sequence_name FROM information_schema.sequences ORDER BY 1");
060: while (rs.next()) {
061: result.add(rs.getString(1));
062: }
063: } catch (Exception e) {
064: LogMgr.logError("H2SequenceReader.getSequenceList()",
065: "Error reading sequences", e);
066: } finally {
067: SqlUtil.closeAll(rs, stmt);
068: }
069: return result;
070: }
071:
072: public List<SequenceDefinition> getSequences(String owner) {
073: DataStore ds = getRawSequenceDefinition(owner, null);
074: if (ds == null)
075: return Collections.emptyList();
076: List<SequenceDefinition> result = new ArrayList<SequenceDefinition>();
077:
078: for (int row = 0; row < ds.getRowCount(); row++) {
079: result.add(createSequenceDefinition(ds, row));
080: }
081: return result;
082: }
083:
084: public SequenceDefinition getSequenceDefinition(String owner,
085: String sequence) {
086: DataStore ds = getRawSequenceDefinition(owner, sequence);
087: if (ds == null || ds.getRowCount() == 0)
088: return null;
089:
090: return createSequenceDefinition(ds, 0);
091: }
092:
093: private SequenceDefinition createSequenceDefinition(DataStore ds,
094: int row) {
095: SequenceDefinition result = null;
096:
097: if (ds == null || ds.getRowCount() == 0)
098: return null;
099:
100: String name = ds.getValueAsString(row, "SEQUENCE_NAME");
101: String schema = ds.getValueAsString(row, "SEQUENCE_SCHEMA");
102: result = new SequenceDefinition(schema, name);
103:
104: result.setSequenceProperty("INCREMENT", ds.getValue(row,
105: "INCREMENT"));
106: readSequenceSource(result);
107:
108: return result;
109: }
110:
111: public void readSequenceSource(SequenceDefinition def) {
112: if (def == null)
113: return;
114:
115: StringBuilder result = new StringBuilder(100);
116: String nl = Settings.getInstance()
117: .getInternalEditorLineEnding();
118:
119: result.append("CREATE SEQUENCE ");
120: result.append(def.getSequenceName());
121:
122: Long inc = (Long) def.getSequenceProperty("INCREMENT");
123: if (inc != null && inc != 1) {
124: result.append(" INCREMENT BY ");
125: result.append(inc);
126: }
127: result.append(';');
128: result.append(nl);
129:
130: def.setSource(result);
131: return;
132: }
133:
134: public DataStore getRawSequenceDefinition(String owner,
135: String sequence) {
136: Statement stmt = null;
137: ResultSet rs = null;
138: DataStore ds = null;
139: try {
140: String sql = "SELECT SEQUENCE_CATALOG, "
141: + "SEQUENCE_SCHEMA, SEQUENCE_NAME, "
142: + "CURRENT_VALUE, " + "INCREMENT, "
143: + "IS_GENERATED, " + "REMARKS, " + "ID "
144: + "FROM information_schema.sequences ";
145:
146: boolean whereAdded = false;
147:
148: if (!StringUtil.isEmptyString(owner)) {
149: if (!whereAdded) {
150: sql += " WHERE ";
151: whereAdded = true;
152: } else {
153: sql += " AND ";
154: }
155: sql += " sequence_schema = '" + owner + "'";
156: }
157:
158: if (!StringUtil.isEmptyString(sequence)) {
159: if (!whereAdded) {
160: sql += " WHERE ";
161: whereAdded = true;
162: } else {
163: sql += " AND ";
164: }
165: sql += " sequence_name = '" + sequence + "'";
166: }
167: stmt = this .dbConnection.createStatement();
168: rs = stmt.executeQuery(sql);
169: ds = new DataStore(rs, true);
170: } catch (Exception e) {
171: LogMgr.logError("H2SequenceReader.getSequenceDefinition()",
172: "Error reading sequence definition", e);
173: ds = null;
174: } finally {
175: SqlUtil.closeAll(rs, stmt);
176: }
177: return ds;
178: }
179: }
|