root / tags / v1_1_Build_1005 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / odbc / ODBCDriver.java @ 12355
History | View | Annotate | Download (9.16 KB)
1 |
/*
|
---|---|
2 |
* Created on 16-oct-2004
|
3 |
*/
|
4 |
package com.hardcode.gdbms.driver.odbc; |
5 |
|
6 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
7 |
import com.hardcode.gdbms.engine.data.db.JDBCSupport; |
8 |
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver; |
9 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
10 |
import com.hardcode.gdbms.engine.values.Value; |
11 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
12 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
13 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
14 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
15 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
16 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
17 |
import com.iver.cit.gvsig.fmap.edition.writers.JdbcWriter; |
18 |
import com.iver.utiles.NumberUtilities; |
19 |
|
20 |
import java.sql.Connection; |
21 |
import java.sql.Date; |
22 |
import java.sql.DriverManager; |
23 |
import java.sql.ResultSet; |
24 |
import java.sql.SQLException; |
25 |
import java.sql.Statement; |
26 |
import java.sql.Time; |
27 |
import java.sql.Timestamp; |
28 |
|
29 |
import java.text.DateFormat; |
30 |
import java.text.SimpleDateFormat; |
31 |
|
32 |
import java.util.HashMap; |
33 |
|
34 |
|
35 |
/**
|
36 |
* MySQL driver
|
37 |
*
|
38 |
* @author Fernando Gonz?lez Cort?s
|
39 |
*/
|
40 |
public class ODBCDriver implements AlphanumericDBDriver, IWriteable { |
41 |
private static Exception driverException; |
42 |
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
43 |
private static DateFormat timeFormat = new SimpleDateFormat( |
44 |
"yyyy-MM-dd HH:mm:ss");
|
45 |
private static ValueWriter vWriter = ValueWriter.internalValueWriter; |
46 |
|
47 |
static {
|
48 |
try {
|
49 |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); |
50 |
} catch (Exception ex) { |
51 |
driverException = ex; |
52 |
} |
53 |
} |
54 |
|
55 |
private JDBCSupport jdbcSupport;
|
56 |
private JdbcWriter jdbcWriter = new JdbcWriter(); |
57 |
|
58 |
/**
|
59 |
* DOCUMENT ME!
|
60 |
*
|
61 |
* @param host DOCUMENT ME!
|
62 |
* @param port DOCUMENT ME!
|
63 |
* @param dbName DOCUMENT ME!
|
64 |
* @param user DOCUMENT ME!
|
65 |
* @param password DOCUMENT ME!
|
66 |
*
|
67 |
* @return DOCUMENT ME!
|
68 |
*
|
69 |
* @throws SQLException
|
70 |
* @throws RuntimeException DOCUMENT ME!
|
71 |
*
|
72 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#connect(java.lang.String)
|
73 |
*/
|
74 |
public Connection getConnection(String host, int port, String dbName, |
75 |
String user, String password) throws SQLException { |
76 |
if (driverException != null) { |
77 |
throw new RuntimeException(driverException); |
78 |
} |
79 |
|
80 |
String connectionString = "jdbc:odbc:" + dbName; |
81 |
|
82 |
if (user != null) { |
83 |
connectionString += (";UID=" + user + ";PWD=" + password); |
84 |
} |
85 |
|
86 |
return DriverManager.getConnection(connectionString); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* @see com.hardcode.driverManager.Driver#getName()
|
91 |
*/
|
92 |
public String getName() { |
93 |
return "odbc"; |
94 |
} |
95 |
|
96 |
/**
|
97 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#executeSQL(java.sql.Connection,
|
98 |
* java.lang.String)
|
99 |
*/
|
100 |
public void open(Connection con, String sql) throws SQLException { |
101 |
jdbcSupport = JDBCSupport.newJDBCSupport(con, sql); |
102 |
|
103 |
jdbcWriter.setCreateTable(false);
|
104 |
jdbcWriter.setWriteAll(false);
|
105 |
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
106 |
ResultSet.CONCUR_UPDATABLE);
|
107 |
ResultSet res = st.executeQuery(sql);
|
108 |
|
109 |
jdbcWriter.initialize(con, res); |
110 |
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* DOCUMENT ME!
|
115 |
*
|
116 |
* @return DOCUMENT ME!
|
117 |
*
|
118 |
* @throws DriverException DOCUMENT ME!
|
119 |
*/
|
120 |
public int getFieldCount() throws DriverException { |
121 |
return jdbcSupport.getFieldCount();
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* DOCUMENT ME!
|
126 |
*
|
127 |
* @param fieldId DOCUMENT ME!
|
128 |
*
|
129 |
* @return DOCUMENT ME!
|
130 |
*
|
131 |
* @throws DriverException DOCUMENT ME!
|
132 |
*/
|
133 |
public String getFieldName(int fieldId) throws DriverException { |
134 |
return jdbcSupport.getFieldName(fieldId);
|
135 |
} |
136 |
|
137 |
/**
|
138 |
* DOCUMENT ME!
|
139 |
*
|
140 |
* @param i DOCUMENT ME!
|
141 |
*
|
142 |
* @return DOCUMENT ME!
|
143 |
*
|
144 |
* @throws DriverException DOCUMENT ME!
|
145 |
*/
|
146 |
public int getFieldType(int i) throws DriverException { |
147 |
return jdbcSupport.getFieldType(i);
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* DOCUMENT ME!
|
152 |
*
|
153 |
* @param rowIndex DOCUMENT ME!
|
154 |
* @param fieldId DOCUMENT ME!
|
155 |
*
|
156 |
* @return DOCUMENT ME!
|
157 |
*
|
158 |
* @throws DriverException DOCUMENT ME!
|
159 |
*/
|
160 |
public Value getFieldValue(long rowIndex, int fieldId) |
161 |
throws DriverException {
|
162 |
return jdbcSupport.getFieldValue(rowIndex, fieldId);
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* DOCUMENT ME!
|
167 |
*
|
168 |
* @return DOCUMENT ME!
|
169 |
*
|
170 |
* @throws DriverException DOCUMENT ME!
|
171 |
*/
|
172 |
public long getRowCount() throws DriverException { |
173 |
return jdbcSupport.getRowCount();
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#close()
|
178 |
*/
|
179 |
public void close() throws SQLException { |
180 |
jdbcSupport.close(); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* @see com.hardcode.gdbms.engine.data.driver.DriverCommons#getDriverProperties()
|
185 |
*/
|
186 |
public HashMap getDriverProperties() { |
187 |
return null; |
188 |
} |
189 |
|
190 |
/**
|
191 |
* @see com.hardcode.gdbms.engine.data.driver.DriverCommons#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
192 |
*/
|
193 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
194 |
} |
195 |
|
196 |
/**
|
197 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#execute(java.sql.Connection,
|
198 |
* java.lang.String, com.hardcode.gdbms.engine.data.HasProperties)
|
199 |
*/
|
200 |
public void execute(Connection con, String sql) throws SQLException { |
201 |
JDBCSupport.execute(con, sql); |
202 |
} |
203 |
|
204 |
/**
|
205 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getInternalTableName(java.lang.String)
|
206 |
*/
|
207 |
public String getInternalTableName(String tableName) { |
208 |
return tableName;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(long)
|
213 |
*/
|
214 |
public String getStatementString(long i) { |
215 |
return vWriter.getStatementString(i);
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(int,
|
220 |
* int)
|
221 |
*/
|
222 |
public String getStatementString(int i, int sqlType) { |
223 |
return vWriter.getStatementString(i, sqlType);
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(double,
|
228 |
* int)
|
229 |
*/
|
230 |
public String getStatementString(double d, int sqlType) { |
231 |
return vWriter.getStatementString(d, sqlType);
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.lang.String,
|
236 |
* int)
|
237 |
*/
|
238 |
public String getStatementString(String str, int sqlType) { |
239 |
return vWriter.getStatementString(str, sqlType);
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.sql.Date)
|
244 |
*/
|
245 |
public String getStatementString(Date d) { |
246 |
return dateFormat.format(d);
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.sql.Time)
|
251 |
*/
|
252 |
public String getStatementString(Time t) { |
253 |
return timeFormat.format(t);
|
254 |
} |
255 |
|
256 |
/**
|
257 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(java.sql.Timestamp)
|
258 |
*/
|
259 |
public String getStatementString(Timestamp ts) { |
260 |
return timeFormat.format(ts);
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(byte[])
|
265 |
*/
|
266 |
public String getStatementString(byte[] binary) { |
267 |
return "x" + vWriter.getStatementString(binary); |
268 |
} |
269 |
|
270 |
/**
|
271 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getStatementString(boolean)
|
272 |
*/
|
273 |
public String getStatementString(boolean b) { |
274 |
return vWriter.getStatementString(b);
|
275 |
} |
276 |
|
277 |
/**
|
278 |
* @see com.hardcode.gdbms.engine.values.ValueWriter#getNullStatementString()
|
279 |
*/
|
280 |
public String getNullStatementString() { |
281 |
return "null"; |
282 |
} |
283 |
public int getFieldWidth(int i) throws DriverException { |
284 |
return jdbcSupport.getFieldWidth(i);
|
285 |
} |
286 |
|
287 |
public IWriter getWriter() {
|
288 |
return jdbcWriter;
|
289 |
} |
290 |
|
291 |
public ITableDefinition getTableDefinition() {
|
292 |
TableDefinition tableDef = new TableDefinition();
|
293 |
try {
|
294 |
tableDef.setFieldsDesc(getFieldsDescription()); |
295 |
} catch (DriverException e) {
|
296 |
throw new RuntimeException(e);//excepcion sin chequear |
297 |
} |
298 |
return tableDef;
|
299 |
} |
300 |
|
301 |
/*azabala
|
302 |
TODO Codigo repetido entre ODBCDriver y AbstractJDBCDriver.
|
303 |
Igual hay que ponerlo en una clase auxiliar comun
|
304 |
*
|
305 |
*/
|
306 |
private FieldDescription[] getFieldsDescription() throws DriverException |
307 |
{ |
308 |
int numFields = getFieldCount();
|
309 |
FieldDescription[] fieldsDescrip = new FieldDescription[numFields]; |
310 |
for (int i = 0; i < numFields; i++) { |
311 |
fieldsDescrip[i] = new FieldDescription();
|
312 |
int type = getFieldType(i);
|
313 |
fieldsDescrip[i].setFieldType(type); |
314 |
fieldsDescrip[i].setFieldName(getFieldName(i)); |
315 |
fieldsDescrip[i].setFieldLength(getFieldWidth(i)); |
316 |
if (NumberUtilities.isNumeric(type))
|
317 |
{ |
318 |
if (!NumberUtilities.isNumericInteger(type))
|
319 |
// TODO: If there is a lost in precision, this should be changed.
|
320 |
fieldsDescrip[i].setFieldDecimalCount(6);
|
321 |
} |
322 |
else
|
323 |
fieldsDescrip[i].setFieldDecimalCount(0);
|
324 |
// TODO: ?DEFAULTVALUE?
|
325 |
// fieldsDescrip[i].setDefaultValue(get)
|
326 |
} |
327 |
return fieldsDescrip;
|
328 |
} |
329 |
} |