svn-gvsig-desktop / tags / v1_1_Build_1001 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / driver / AbstractJDBCDriver.java @ 11984
History | View | Annotate | Download (8.77 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.hardcode.gdbms.engine.data.driver; |
42 |
|
43 |
import java.sql.Connection; |
44 |
import java.sql.Date; |
45 |
import java.sql.ResultSet; |
46 |
import java.sql.SQLException; |
47 |
import java.sql.Statement; |
48 |
import java.sql.Time; |
49 |
import java.sql.Timestamp; |
50 |
import java.text.DateFormat; |
51 |
import java.text.SimpleDateFormat; |
52 |
import java.util.HashMap; |
53 |
|
54 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
55 |
import com.hardcode.gdbms.engine.data.db.JDBCSupport; |
56 |
import com.hardcode.gdbms.engine.values.Value; |
57 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
58 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
59 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
60 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
61 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
62 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
63 |
import com.iver.cit.gvsig.fmap.edition.writers.JdbcWriter; |
64 |
import com.iver.utiles.NumberUtilities; |
65 |
|
66 |
public abstract class AbstractJDBCDriver implements AlphanumericDBDriver, |
67 |
IWriteable { |
68 |
|
69 |
protected JDBCSupport jdbcSupport;
|
70 |
|
71 |
protected JdbcWriter jdbcWriter = new JdbcWriter(); |
72 |
|
73 |
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
74 |
|
75 |
private static DateFormat timeFormat = new SimpleDateFormat( |
76 |
"yyyy-MM-dd HH:mm:ss");
|
77 |
|
78 |
private ValueWriter vWriter = ValueWriter.internalValueWriter;
|
79 |
|
80 |
/**
|
81 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#executeSQL(java.sql.Connection,
|
82 |
* java.lang.String)
|
83 |
*/
|
84 |
public void open(Connection con, String sql) throws SQLException { |
85 |
jdbcSupport = JDBCSupport.newJDBCSupport(con, sql); |
86 |
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
87 |
ResultSet.CONCUR_UPDATABLE);
|
88 |
ResultSet res = st.executeQuery(sql);
|
89 |
if (res.getConcurrency() != ResultSet.CONCUR_UPDATABLE) { |
90 |
System.err.println("Error: No se puede editar la tabla " + sql); |
91 |
jdbcWriter = null;
|
92 |
} else
|
93 |
{ |
94 |
jdbcWriter.initialize(con, res); |
95 |
jdbcWriter.setCreateTable(false);
|
96 |
jdbcWriter.setWriteAll(false);
|
97 |
} |
98 |
} |
99 |
|
100 |
/**
|
101 |
* DOCUMENT ME!
|
102 |
*
|
103 |
* @return DOCUMENT ME!
|
104 |
*
|
105 |
* @throws DriverException
|
106 |
* DOCUMENT ME!
|
107 |
*/
|
108 |
public int getFieldCount() throws DriverException { |
109 |
return jdbcSupport.getFieldCount();
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* DOCUMENT ME!
|
114 |
*
|
115 |
* @param fieldId
|
116 |
* DOCUMENT ME!
|
117 |
*
|
118 |
* @return DOCUMENT ME!
|
119 |
*
|
120 |
* @throws DriverException
|
121 |
* DOCUMENT ME!
|
122 |
*/
|
123 |
public String getFieldName(int fieldId) throws DriverException { |
124 |
return jdbcSupport.getFieldName(fieldId);
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* DOCUMENT ME!
|
129 |
*
|
130 |
* @param i
|
131 |
* DOCUMENT ME!
|
132 |
*
|
133 |
* @return DOCUMENT ME!
|
134 |
*
|
135 |
* @throws DriverException
|
136 |
* DOCUMENT ME!
|
137 |
*/
|
138 |
public int getFieldType(int i) throws DriverException { |
139 |
return jdbcSupport.getFieldType(i);
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* DOCUMENT ME!
|
144 |
*
|
145 |
* @param rowIndex
|
146 |
* DOCUMENT ME!
|
147 |
* @param fieldId
|
148 |
* DOCUMENT ME!
|
149 |
*
|
150 |
* @return DOCUMENT ME!
|
151 |
*
|
152 |
* @throws DriverException
|
153 |
* DOCUMENT ME!
|
154 |
*/
|
155 |
public Value getFieldValue(long rowIndex, int fieldId) |
156 |
throws DriverException {
|
157 |
return jdbcSupport.getFieldValue(rowIndex, fieldId);
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* DOCUMENT ME!
|
162 |
*
|
163 |
* @return DOCUMENT ME!
|
164 |
*
|
165 |
* @throws DriverException
|
166 |
* DOCUMENT ME!
|
167 |
*/
|
168 |
public long getRowCount() throws DriverException { |
169 |
return jdbcSupport.getRowCount();
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#close()
|
174 |
*/
|
175 |
public void close() throws SQLException { |
176 |
jdbcSupport.close(); |
177 |
jdbcWriter.close(); |
178 |
} |
179 |
|
180 |
/**
|
181 |
* @see com.hardcode.gdbms.engine.data.driver.DriverCommons#getDriverProperties()
|
182 |
*/
|
183 |
public HashMap getDriverProperties() { |
184 |
return null; |
185 |
} |
186 |
|
187 |
/**
|
188 |
* @see com.hardcode.gdbms.engine.data.driver.DriverCommons#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
189 |
*/
|
190 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
191 |
} |
192 |
|
193 |
/**
|
194 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#execute(java.sql.Connection,
|
195 |
* java.lang.String, com.hardcode.gdbms.engine.data.HasProperties)
|
196 |
*/
|
197 |
public void execute(Connection con, String sql) throws SQLException { |
198 |
JDBCSupport.execute(con, sql); |
199 |
} |
200 |
|
201 |
/**
|
202 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getInternalTableName(java.lang.String)
|
203 |
*/
|
204 |
public String getInternalTableName(String tableName) { |
205 |
return tableName;
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(long)
|
210 |
*/
|
211 |
public String getStatementString(long i) { |
212 |
return vWriter.getStatementString(i);
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(int,
|
217 |
* int)
|
218 |
*/
|
219 |
public String getStatementString(int i, int sqlType) { |
220 |
return vWriter.getStatementString(i, sqlType);
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(double,
|
225 |
* int)
|
226 |
*/
|
227 |
public String getStatementString(double d, int sqlType) { |
228 |
return vWriter.getStatementString(d, sqlType);
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.lang.String,
|
233 |
* int)
|
234 |
*/
|
235 |
public String getStatementString(String str, int sqlType) { |
236 |
return vWriter.getStatementString(str, sqlType);
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.sql.Date)
|
241 |
*/
|
242 |
public String getStatementString(Date d) { |
243 |
return dateFormat.format(d);
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.sql.Time)
|
248 |
*/
|
249 |
public String getStatementString(Time t) { |
250 |
return timeFormat.format(t);
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.sql.Timestamp)
|
255 |
*/
|
256 |
public String getStatementString(Timestamp ts) { |
257 |
return timeFormat.format(ts);
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(byte[])
|
262 |
*/
|
263 |
public String getStatementString(byte[] binary) { |
264 |
return "x" + vWriter.getStatementString(binary); |
265 |
} |
266 |
|
267 |
/**
|
268 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(boolean)
|
269 |
*/
|
270 |
public String getStatementString(boolean b) { |
271 |
return vWriter.getStatementString(b);
|
272 |
} |
273 |
|
274 |
/**
|
275 |
* @see com.hardcode.gdbms.engine.values.ValueWriter#getNullStatementString()
|
276 |
*/
|
277 |
public String getNullStatementString() { |
278 |
return "null"; |
279 |
} |
280 |
|
281 |
public int getFieldWidth(int i) throws DriverException { |
282 |
return jdbcSupport.getFieldWidth(i);
|
283 |
} |
284 |
|
285 |
public IWriter getWriter() {
|
286 |
return jdbcWriter;
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* Return the default tabledefinition (only
|
291 |
* has field description values).
|
292 |
* Each driver that inheret this class must overwrite
|
293 |
* this.
|
294 |
*
|
295 |
* */
|
296 |
// public ITableDefinition getTableDefinition(){
|
297 |
// TableDefinition tableDef = new TableDefinition();
|
298 |
// try {
|
299 |
// tableDef.setFieldsDesc(getFieldsDescription());
|
300 |
// } catch (DriverException e) {
|
301 |
// throw new RuntimeException(e);//excepcion sin chequear
|
302 |
// }
|
303 |
// return tableDef;
|
304 |
// }
|
305 |
|
306 |
|
307 |
/*azabala
|
308 |
TODO Copypasteado de SelectableDataSource.
|
309 |
?No estar?a mejor aqu??
|
310 |
*
|
311 |
*/
|
312 |
private FieldDescription[] getFieldsDescription() throws DriverException |
313 |
{ |
314 |
int numFields = getFieldCount();
|
315 |
FieldDescription[] fieldsDescrip = new FieldDescription[numFields]; |
316 |
for (int i = 0; i < numFields; i++) { |
317 |
fieldsDescrip[i] = new FieldDescription();
|
318 |
int type = getFieldType(i);
|
319 |
fieldsDescrip[i].setFieldType(type); |
320 |
fieldsDescrip[i].setFieldName(getFieldName(i)); |
321 |
fieldsDescrip[i].setFieldLength(getFieldWidth(i)); |
322 |
if (NumberUtilities.isNumeric(type))
|
323 |
{ |
324 |
if (!NumberUtilities.isNumericInteger(type))
|
325 |
// TODO: If there is a lost in precision, this should be changed.
|
326 |
fieldsDescrip[i].setFieldDecimalCount(6);
|
327 |
} |
328 |
else
|
329 |
fieldsDescrip[i].setFieldDecimalCount(0);
|
330 |
// TODO: ?DEFAULTVALUE?
|
331 |
// fieldsDescrip[i].setDefaultValue(get)
|
332 |
} |
333 |
return fieldsDescrip;
|
334 |
} |
335 |
} |