1 package com.quantum.sql;
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.sql.Connection;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.Iterator;
14 import java.util.List;
16 import com.quantum.model.Bookmark;
17 import com.quantum.model.Entity;
20 * @author Tom Schneider
23 public abstract class SQLResultSetResults extends SQLResults {
26 class ColumnArrayComparator implements Comparator {
28 public int compare(Object arg0, Object arg1) {
29 return compare((Column[]) arg0, (Column[]) arg1);
31 public int compare(Column[] columns0, Column[] columns1) {
33 if (columns0 == null && columns1 == null) {
35 } else if (columns0 == null) {
37 } else if (columns1 == null) {
39 } else if (columns0.length < columns1.length) {
41 } else if (columns0.length > columns1.length) {
45 for (int i = 0, length = columns1 == null ? 0 : columns1.length;
46 result == 0 && i < length; i++) {
47 result = compare(columns0[i], columns1[i]);
59 private int compare(Column column0, Column column1) {
61 if (column0 == null && column1 == null) {
63 } else if (column0 == null) {
65 } else if (column1 == null) {
67 } else if (column0.getName() == null) {
69 } else if (column1.getName() == null) {
71 } else if (column0.getName() != null && column1.getName() != null
72 && column0.getName().compareTo(column1.getName()) != 0) {
73 return column0.getName().compareTo(column1.getName());
74 } else if (column0.getType() == null) {
76 } else if (column1.getType() == null) {
78 } else if (column0.getType() != null && column1.getType() != null
79 && column0.getType().compareTo(column1.getType()) != 0) {
80 return column0.getType().compareTo(column1.getType());
82 return column0.getSize() - column1.getSize();
88 private final List elements;
91 this.elements = elements;
94 public Object get(int columnNumber) {
95 return (columnNumber > this.elements.size() || columnNumber <= 0)
97 : this.elements.get(columnNumber - 1);
100 public SQLResultSetResults getResultSet() {
101 return SQLResultSetResults.this;
105 public class Column {
106 private final String name;
107 private final String type;
108 private final int size;
110 Column(String name, String type, int size) {
115 public String getName() {
118 public int getSize() {
121 public String getType() {
126 private List rows = Collections.synchronizedList(new ArrayList());
127 private List columns = Collections.synchronizedList(new ArrayList());
128 private String query;
129 private Bookmark bookmark;
130 private final Entity entity;
131 private String encoding = "";
133 private FilterSort filterSort = null;
139 public SQLResultSetResults(String query, Bookmark bookmark, Entity entity) {
141 this.bookmark = bookmark;
142 this.entity = entity;
144 public String getColumnName(int columnNumber) {
145 Column column = getColumn(columnNumber);
146 return column == null ? "" : column.getName();
150 * @param columnNumber
153 protected Column getColumn(int columnNumber) {
154 return columnNumber <= this.columns.size()
155 ? (Column) this.columns.get(columnNumber-1)
158 public Column[] getColumns() {
159 return (Column[]) this.columns.toArray(new Column[this.columns.size()]);
161 protected void setColumns(Column[] columns) {
162 Column[] original = getColumns();
163 if (new ColumnArrayComparator().compare(original, columns) != 0) {
164 this.columns.clear();
165 this.columns.addAll(Arrays.asList(columns));
166 this.propertyChangeSupport.firePropertyChange("columns", original, columns);
169 public Object getElement(int column, int row) {
170 return ((Row) rows.get(row - 1)).get(column);
172 public int getColumnCount() {
173 if (columns.size() > 0) {
174 return columns.size();
178 public int getRowCount() {
181 public String[] getColumnNames() {
182 List names = new ArrayList();
183 for (Iterator i = this.columns.iterator(); i.hasNext();) {
184 Column column = (Column) i.next();
185 names.add(column.getName());
187 return (String[]) names.toArray(new String[names.size()]);
190 * Returns the resultSet.
193 public boolean isResultSet() {
196 public Row[] getRows() {
197 return (Row[]) rows.toArray(new Row[this.rows.size()]);
203 public String getQuery() {
206 public String getFilteredQuery() {
207 if (this.filterSort == null) {
210 return this.query + this.filterSort.toString();
213 public Bookmark getBookmark() {
214 return this.bookmark;
217 public Entity getEntity() {
221 * Returns the resultSet.
224 public boolean isMetaData() {
227 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
229 public void addPropertyChangeListener(PropertyChangeListener listener) {
230 this.propertyChangeSupport.addPropertyChangeListener(listener);
232 public void removePropertyChangeListener(PropertyChangeListener listener) {
233 this.propertyChangeSupport.removePropertyChangeListener(listener);
235 protected abstract void parseResultSet(ResultSet set) throws SQLException;
237 public void refresh(Connection connection) throws SQLException {
238 Statement statement = connection.createStatement();
240 ResultSet resultSet = statement.executeQuery(getFilteredQuery());
242 parseResultSet(resultSet);
251 protected void setRows(Row[] rows) {
252 Row[] original = getRows();
255 this.rows.addAll(Arrays.asList(rows));
257 this.propertyChangeSupport.firePropertyChange("rows", original, getRows());
259 public String getEncoding() {
260 return this.encoding;
262 public void setEncoding(String encoding) {
263 this.encoding = encoding;
265 public FilterSort getFilterSort() {
266 return this.filterSort;
268 public void setFilterSort(FilterSort filterSort) {
269 this.filterSort = filterSort;