Implemeted action for uploading Wikipedia articles (thanks to D.Wunsch)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / sql / SQLVirtualResultSet.java
1 /*
2  * Created on 20.08.2004
3  *
4  *      A Virtual Result Set. It's virtual because it doesn't has cache,
5  *      but rather is tightly coupled with its RecordSet object,
6  *      and mainly gives some added functionality, for use in
7  *      virtual tables. It has SQLResults as a superclass mainly
8  *      for conceptual reasons, it could be standalone.
9  */
10 package com.quantum.sql;
11
12 import java.sql.Connection;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16
17 import com.quantum.model.Column;
18 import com.quantum.model.Entity;
19 import com.quantum.model.NotConnectedException;
20 import com.quantum.util.StringMatrix;
21 import com.quantum.util.sql.SQLInstructionBuilder;
22
23 /**
24  * @author Julen
25  *
26  */
27 public class SQLVirtualResultSet extends SQLResults {
28         // The resultSet will be based on a query, that can be changed
29         private String query = "";
30         private ResultSet resultSet = null;
31         private int knownNumberOfRows = 0;
32         
33         
34         /* (non-Javadoc)
35          * @see com.quantum.sql.SQLResults#isResultSet()
36          */
37         public SQLVirtualResultSet(Entity entity) {
38                 try {
39                         // Cannot use "SELECT *" because that is not updatable, at least in some JDBC drivers
40                         Initialize(SQLInstructionBuilder.buildSelect(entity, entity.getColumns(), null), 
41                                                 entity.getBookmark().getConnection(), 
42                                                 ResultSet.TYPE_SCROLL_SENSITIVE, 
43                                                 ResultSet.CONCUR_UPDATABLE);
44
45                 } catch (NotConnectedException e) {
46                         e.printStackTrace();
47                 } catch (SQLException e) {
48                         e.printStackTrace();
49                 }
50         }
51         public SQLVirtualResultSet(Entity entity, int type, int concurrency) {
52                 try {
53                         // Cannot use "SELECT *" because that is not updatable, at least in some JDBC drivers
54                         Initialize(SQLInstructionBuilder.buildSelect(entity, entity.getColumns(), null), 
55                                                 entity.getBookmark().getConnection(), 
56                                                 type, 
57                                                 concurrency);
58
59                 } catch (NotConnectedException e) {
60                         e.printStackTrace();
61                 } catch (SQLException e) {
62                         e.printStackTrace();
63                 }
64         }
65         /**
66          * @param string
67          * @param connection
68          */
69         private void Initialize(String query, Connection connection, int type, int concurrency) {
70                 if (connection == null) return;
71                 this.query = query;
72                 try {
73                         Statement statement = connection.createStatement(type, concurrency);
74                         resultSet = statement.executeQuery(query);
75                         // If the recordset is not empty, we know there is at least one row. This call may fall also
76                         // because the recordset is not of the correct type to run isLast, so the resultSet may be valid.
77                         if (!resultSet.isLast())
78                                 knownNumberOfRows = 1; 
79                 } catch (SQLException e) {
80                         return;
81                 }
82                 
83                 
84         }
85         
86         public boolean isResultSet() {
87                 return true;
88         }
89         
90         /**
91          * @return If the resultSet is open, that is, connected to a data source
92          */
93         public boolean isOpen() {
94                 return (resultSet != null);
95         }
96         
97
98         /**
99          * @return Returns the knowNumberOfRows.
100          */
101         public int getKnownNumberOfRows() {
102                 return knownNumberOfRows;
103         }
104         /**
105          * @param index
106          * @param i
107          * @return
108          */
109         public String getStringValue(int row, int column) {
110                 if (resultSet == null) return null;
111                 try {
112                         //      Rows and columns in resultSets are 1-based
113                         resultSet.absolute(row+1);
114                         actKnownNumberOfRows();
115                         return resultSet.getString(column+1);
116                 } catch (SQLException e) {
117                         e.printStackTrace();
118                         return null;
119                 }
120         }
121         /**
122          * 
123          */
124         private void actKnownNumberOfRows() {
125                 if (resultSet == null) return;
126                 int nRowsNow;
127                 try {
128                         nRowsNow = resultSet.getRow() + (resultSet.isLast() ? 0 : 1);
129                         knownNumberOfRows = Math.max( nRowsNow, knownNumberOfRows);
130                 } catch (SQLException e) {
131                         // TODO Auto-generated catch block
132                         e.printStackTrace();
133                 }
134         }
135         /**
136          * @param index
137          * @return
138          */
139         public String[] getRowAsStringArray(int row) {
140                 if (resultSet == null) return null;
141                 try {
142                         resultSet.absolute(row+1);
143                         actKnownNumberOfRows();
144                         return getRowAsStringArray();
145                 } catch (SQLException e) {
146                         e.printStackTrace();
147                         return null;
148                 }
149         }
150         
151         /**
152          * @return
153          * 
154          */
155         public String[] getRowAsStringArray() {
156                 String[] resultArray = null;
157                 int numColumns;
158                 try {
159                         numColumns = resultSet.getMetaData().getColumnCount();
160                         resultArray = new String[numColumns];
161                         for (int i = 0; i <  numColumns; i++) {
162                                 //      Rows and columns in resultSets are 1-based
163                                 resultArray[i] =  resultSet.getString(i+1);
164                         }
165                 } catch (SQLException e) {
166                         // TODO Auto-generated catch block
167                         e.printStackTrace();
168                 }
169                 return resultArray;
170         }
171         
172         /**
173          * 
174          */
175         public boolean next(){
176                 try {
177                         return resultSet.next();
178                 } catch (SQLException e) {
179                         // TODO Auto-generated catch block
180                         e.printStackTrace();
181                         return false;
182                 }
183         }
184         public boolean isLast() {
185                 try {
186                         return resultSet.isLast();
187                 } catch (SQLException e) {
188                         e.printStackTrace();
189                         return true;
190                 }
191         }
192         /**
193          * @return      Names of the Columns of the ResultSet, in the order given
194          */
195         public String[] getColumnNames() {
196                 if (resultSet == null) return null;
197                 String resultArray[] = null;
198                 try {
199                         int numColumns = resultSet.getMetaData().getColumnCount();
200                         resultArray = new String[numColumns];
201                         for (int i = 0; i <  numColumns; i++) {
202                                 //      Rows and columns in resultSets are 1-based
203                                 resultArray[i] =  resultSet.getMetaData().getColumnName(i+1);
204                         }
205                 } catch (SQLException e) {
206                         e.printStackTrace();
207                 }
208                 return resultArray;
209         }
210         /**
211          * @param columnIndex
212          * @param valueStr
213          */
214         public boolean update(int row, int columnIndex, String valueStr) {
215                 if (row < 0 || columnIndex < 0 ) return false;
216                 try {
217                         resultSet.absolute(row+1);
218                         resultSet.updateString(columnIndex+1, valueStr);
219                         resultSet.updateRow();
220                         return true;
221                 } catch (SQLException e) {
222                         e.printStackTrace();
223                         return false;
224                 }
225                 
226         }
227         public void close(){
228                 try {
229                         resultSet.close();
230                 } catch (SQLException e) {
231                         e.printStackTrace();
232                 }
233         }
234 }