don't insert a line break when formatting an empty array declaration
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / sql / SQLResultSetResults.java
1 package com.quantum.sql;
2
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;
15
16 import com.quantum.model.Bookmark;
17 import com.quantum.model.Entity;
18
19 /**
20  * @author Tom Schneider
21  * @author BC Holmes
22  */
23 public abstract class SQLResultSetResults extends SQLResults {
24         
25         
26         class ColumnArrayComparator implements Comparator {
27
28                 public int compare(Object arg0, Object arg1) {
29                         return compare((Column[]) arg0, (Column[]) arg1);
30                 }
31                 public int compare(Column[] columns0, Column[] columns1) {
32                         
33                         if (columns0 == null && columns1 == null) {
34                                 return 0;
35                         } else if (columns0 == null) {
36                                 return -1;
37                         } else if (columns1 == null) {
38                                 return 1;
39                         } else if (columns0.length < columns1.length) {
40                                 return -1;
41                         } else if (columns0.length > columns1.length) {
42                                 return 1;
43                         } else {
44                                 int result = 0;
45                                 for (int i = 0, length = columns1 == null ? 0 : columns1.length; 
46                                                 result == 0 && i < length; i++) {
47                                         result = compare(columns0[i], columns1[i]);
48                                 }
49                                 return result;
50                         }
51                 }
52                 /**
53                  * @param columns0
54                  * @param columns1
55                  * @param result
56                  * @param i
57                  * @return
58                  */
59                 private int compare(Column column0, Column column1) {
60
61                         if (column0 == null && column1 == null) {
62                                 return 0;
63                         } else if (column0 == null) {
64                                 return -1;
65                         } else if (column1 == null) {
66                                 return 1;
67                         } else if (column0.getName() == null) {
68                                 return -1;
69                         } else if (column1.getName() == null) {
70                                 return 1;
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) {
75                                 return -1;
76                         } else if (column1.getType() == null) {
77                                 return 1;
78                         } else if (column0.getType() != null && column1.getType() != null
79                                         && column0.getType().compareTo(column1.getType()) != 0) {
80                                 return column0.getType().compareTo(column1.getType());
81                         } else {
82                                 return column0.getSize() - column1.getSize();
83                         }
84                 }
85         }
86         
87         public class Row {
88                 private final List elements;
89
90                 Row(List elements) {
91                         this.elements = elements;
92                 }
93                 
94                 public Object get(int columnNumber) {
95                         return (columnNumber > this.elements.size() || columnNumber <= 0) 
96                                         ? null 
97                                         : this.elements.get(columnNumber - 1);
98                 }
99                 
100                 public SQLResultSetResults getResultSet() {
101                         return SQLResultSetResults.this;
102                 }
103         }
104         
105         public class Column {
106                 private final String name;
107                 private final String type;
108                 private final int size;
109
110                 Column(String name, String type, int size) {
111                         this.name = name;
112                         this.type = type;
113                         this.size = size;
114                 }
115                 public String getName() {
116                         return this.name;
117                 }
118                 public int getSize() {
119                         return this.size;
120                 }
121                 public String getType() {
122                         return this.type;
123                 }
124         }
125         
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 = "";
132         
133         private FilterSort filterSort = null;
134         
135         /**
136          * @param entity
137          * @param query2
138          */
139         public SQLResultSetResults(String query, Bookmark bookmark, Entity entity) {
140                 this.query = query;
141                 this.bookmark = bookmark;
142                 this.entity = entity;
143         }
144         public String getColumnName(int columnNumber) {
145                 Column column = getColumn(columnNumber);
146                 return column == null ? "" : column.getName();
147                         
148         }
149         /**
150          * @param columnNumber
151          * @return
152          */
153         protected Column getColumn(int columnNumber) {
154                 return columnNumber <= this.columns.size() 
155                                         ? (Column) this.columns.get(columnNumber-1)
156                                         : null;
157         }
158         public Column[] getColumns() {
159                 return (Column[]) this.columns.toArray(new Column[this.columns.size()]);
160         }
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);
167                 }
168         }
169         public Object getElement(int column, int row) {
170                 return ((Row) rows.get(row - 1)).get(column);
171         }
172         public int getColumnCount() {
173                 if (columns.size() > 0) {
174                         return columns.size();
175                 }
176                 return 0;
177         }
178         public int getRowCount() {
179                 return rows.size();
180         }
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());
186                 }
187                 return (String[]) names.toArray(new String[names.size()]);
188         }
189         /**
190          * Returns the resultSet.
191          * @return boolean
192          */
193         public boolean isResultSet() {
194                 return true;
195         }
196         public Row[] getRows() {
197                 return (Row[]) rows.toArray(new Row[this.rows.size()]);
198         }
199         /**
200          * Returns the query.
201          * @return String
202          */
203         public String getQuery() {
204                 return this.query;
205         }
206         public String getFilteredQuery() {
207                 if (this.filterSort == null) {
208                         return this.query;
209                 } else {
210                         return this.query + this.filterSort.toString();
211                 }
212         }
213         public Bookmark getBookmark() {
214                 return this.bookmark;
215         }
216
217         public Entity getEntity() {
218                 return this.entity;
219         }
220         /**
221          * Returns the resultSet.
222          * @return boolean
223          */
224         public boolean isMetaData() {
225                 return false;
226         }
227         private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
228
229         public void addPropertyChangeListener(PropertyChangeListener listener) {
230                 this.propertyChangeSupport.addPropertyChangeListener(listener);
231         }
232         public void removePropertyChangeListener(PropertyChangeListener listener) {
233                 this.propertyChangeSupport.removePropertyChangeListener(listener);
234         }
235         protected abstract void parseResultSet(ResultSet set) throws SQLException;
236         
237         public void refresh(Connection connection) throws SQLException {
238                 Statement statement = connection.createStatement();
239                 try {
240                         ResultSet resultSet = statement.executeQuery(getFilteredQuery());
241                         try {
242                                 parseResultSet(resultSet);
243                         } finally {
244                                 resultSet.close();
245                         }
246                 } finally {
247                         statement.close();
248                 }
249         }
250         
251         protected void setRows(Row[] rows) {
252                 Row[] original = getRows();
253                 this.rows.clear();
254                 if (rows != null) {
255                         this.rows.addAll(Arrays.asList(rows));
256                 }
257                 this.propertyChangeSupport.firePropertyChange("rows", original, getRows());
258         }
259         public String getEncoding() {
260                 return this.encoding;
261         }
262         public void setEncoding(String encoding) {
263                 this.encoding = encoding;
264         }
265         public FilterSort getFilterSort() {
266                 return this.filterSort;
267         }
268         public void setFilterSort(FilterSort filterSort) {
269                 this.filterSort = filterSort;
270         }
271 }