svn-gvsig-desktop / tags / v1_0_2_Build_905 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / db / DBDataSourceAdapter.java @ 10767
History | View | Annotate | Download (7.78 KB)
1 | 3199 | fjp | package com.hardcode.gdbms.engine.data.db; |
---|---|---|---|
2 | |||
3 | import java.sql.Connection; |
||
4 | import java.sql.ResultSet; |
||
5 | import java.sql.SQLException; |
||
6 | import java.util.ArrayList; |
||
7 | |||
8 | 5568 | jmvivo | import com.hardcode.driverManager.Driver; |
9 | 4050 | fjp | import com.hardcode.driverManager.DriverLoadException; |
10 | 3199 | fjp | import com.hardcode.gdbms.engine.data.AbstractDataSource; |
11 | import com.hardcode.gdbms.engine.data.SourceInfo; |
||
12 | import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver; |
||
13 | import com.hardcode.gdbms.engine.data.driver.DBDriver; |
||
14 | import com.hardcode.gdbms.engine.data.driver.DriverException; |
||
15 | import com.hardcode.gdbms.engine.data.driver.GDBMSDriver; |
||
16 | import com.hardcode.gdbms.engine.data.edition.DataWare; |
||
17 | import com.hardcode.gdbms.engine.values.Value; |
||
18 | |||
19 | |||
20 | /**
|
||
21 | * Adaptador de la interfaz DBDriver a la interfaz DataSource. Adapta las
|
||
22 | * interfaces de los drivers de base de datos a la interfaz DataSource.
|
||
23 | *
|
||
24 | * @author Fernando Gonz?lez Cort?s
|
||
25 | */
|
||
26 | public class DBDataSourceAdapter extends AbstractDataSource |
||
27 | implements DBDataSource {
|
||
28 | /*
|
||
29 | * datos de la conexi?n
|
||
30 | */
|
||
31 | protected String host; |
||
32 | protected int port; |
||
33 | protected String dbName; |
||
34 | protected String user; |
||
35 | protected String password; |
||
36 | |||
37 | //driver de base de datos
|
||
38 | protected DBDriver driver;
|
||
39 | |||
40 | //objetos jdbc
|
||
41 | 4919 | jmvivo | protected Connection con; |
42 | 3199 | fjp | |
43 | //Instrucci?n sql completa que representa el dataSource
|
||
44 | protected String sql; |
||
45 | |||
46 | /**
|
||
47 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getRowCount()
|
||
48 | */
|
||
49 | public long getRowCount() throws DriverException { |
||
50 | 5568 | jmvivo | return ((DBDriver)getDriver()).getRowCount();
|
51 | 3199 | fjp | } |
52 | |||
53 | /**
|
||
54 | * Establece el driver
|
||
55 | *
|
||
56 | * @param driver The driver to set.
|
||
57 | */
|
||
58 | public void setDriver(DBDriver driver) { |
||
59 | this.driver = driver;
|
||
60 | } |
||
61 | |||
62 | /**
|
||
63 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
|
||
64 | */
|
||
65 | public int getFieldCount() throws DriverException { |
||
66 | 5568 | jmvivo | return ((DBDriver)getDriver()).getFieldCount();
|
67 | 3199 | fjp | } |
68 | |||
69 | /**
|
||
70 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
|
||
71 | */
|
||
72 | public String getFieldName(int fieldId) throws DriverException { |
||
73 | 5568 | jmvivo | return ((DBDriver)getDriver()).getFieldName(fieldId);
|
74 | 3199 | fjp | } |
75 | |||
76 | /**
|
||
77 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
|
||
78 | */
|
||
79 | public int getFieldType(int i) throws DriverException { |
||
80 | 5568 | jmvivo | return ((DBDriver)getDriver()).getFieldType(i);
|
81 | 3199 | fjp | } |
82 | |||
83 | /**
|
||
84 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
|
||
85 | * int)
|
||
86 | */
|
||
87 | public Value getFieldValue(long rowIndex, int fieldId) |
||
88 | throws DriverException {
|
||
89 | 5568 | jmvivo | return ((DBDriver)getDriver()).getFieldValue(rowIndex, fieldId);
|
90 | 3199 | fjp | } |
91 | |||
92 | /**
|
||
93 | * DOCUMENT ME!
|
||
94 | *
|
||
95 | * @return
|
||
96 | */
|
||
97 | 5568 | jmvivo | public Driver getDriver() { |
98 | 3199 | fjp | return driver;
|
99 | } |
||
100 | |||
101 | /**
|
||
102 | * @see com.hardcode.gdbms.engine.data.DataSource#setSourceInfo(com.hardcode.gdbms.engine.data.driver.DriverInfo)
|
||
103 | */
|
||
104 | public void setSourceInfo(SourceInfo sourceInfo) { |
||
105 | super.setSourceInfo(sourceInfo);
|
||
106 | |||
107 | this.host = ((DBSourceInfo) sourceInfo).host;
|
||
108 | this.port = ((DBSourceInfo) sourceInfo).port;
|
||
109 | this.dbName = ((DBSourceInfo) sourceInfo).dbName;
|
||
110 | this.user = ((DBSourceInfo) sourceInfo).user;
|
||
111 | this.password = ((DBSourceInfo) sourceInfo).password;
|
||
112 | this.tableName = ((DBTableSourceInfo) sourceInfo).tableName;
|
||
113 | 4919 | jmvivo | this.con =((DBTableSourceInfo) sourceInfo).connection;
|
114 | 3199 | fjp | |
115 | this.sql = "SELECT * FROM " + tableName; |
||
116 | } |
||
117 | |||
118 | /**
|
||
119 | * @see com.hardcode.gdbms.engine.data.db.DBDataSource#execute(java.lang.String)
|
||
120 | */
|
||
121 | public void execute(String sql) throws DriverException { |
||
122 | try {
|
||
123 | 5568 | jmvivo | ((DBDriver)getDriver()).execute(this.getConnection(), sql);
|
124 | 3199 | fjp | } catch (SQLException e) { |
125 | throw new DriverException(e); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | protected String tableName; |
||
130 | protected int sem = 0; |
||
131 | private int[] cachedPKIndices = null; |
||
132 | |||
133 | /**
|
||
134 | * Get's a connection to the driver
|
||
135 | *
|
||
136 | * @return Connection
|
||
137 | *
|
||
138 | * @throws SQLException if the connection cannot be established
|
||
139 | */
|
||
140 | 4436 | jorpiell | public Connection getConnection() throws SQLException { |
141 | 4919 | jmvivo | if (this.con == null) { |
142 | return driver.getConnection(host, port, dbName, user, password);
|
||
143 | } |
||
144 | else {
|
||
145 | return this.con; |
||
146 | } |
||
147 | 3199 | fjp | } |
148 | |||
149 | /**
|
||
150 | * @see com.hardcode.gdbms.engine.data.DataSource#start()
|
||
151 | */
|
||
152 | public void start() throws DriverException { |
||
153 | try {
|
||
154 | if (sem == 0) { |
||
155 | 4919 | jmvivo | con = getConnection(); |
156 | ((AlphanumericDBDriver) driver).open(con, sql); |
||
157 | 9887 | fjp | // driver.setSourceInfo(this.getSourceInfo());
|
158 | 4919 | jmvivo | |
159 | 3199 | fjp | } |
160 | |||
161 | sem++; |
||
162 | } catch (SQLException e) { |
||
163 | throw new DriverException(e); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * @see com.hardcode.gdbms.engine.data.DataSource#stop()
|
||
169 | */
|
||
170 | public void stop() throws DriverException { |
||
171 | try {
|
||
172 | sem--; |
||
173 | |||
174 | if (sem == 0) { |
||
175 | 4919 | jmvivo | driver.close(); |
176 | if (this.con == null) { |
||
177 | con.close(); |
||
178 | con = null;
|
||
179 | } |
||
180 | 3199 | fjp | } else if (sem < 0) { |
181 | throw new RuntimeException("DataSource closed too many times"); |
||
182 | } |
||
183 | } catch (SQLException e) { |
||
184 | throw new DriverException(e); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /**
|
||
189 | * @see com.hardcode.gdbms.engine.data.DataSource#getDBMS()
|
||
190 | */
|
||
191 | public String getDBMS() { |
||
192 | return ((DBSourceInfo)sourceInfo).dbms;
|
||
193 | } |
||
194 | |||
195 | /**
|
||
196 | * @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
||
197 | */
|
||
198 | public String getName() { |
||
199 | return sourceInfo.name;
|
||
200 | } |
||
201 | |||
202 | /**
|
||
203 | * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
|
||
204 | */
|
||
205 | public int[] getPrimaryKeys() throws DriverException { |
||
206 | if (cachedPKIndices == null) { |
||
207 | try {
|
||
208 | //Gets the pk column names
|
||
209 | Connection c = getConnection();
|
||
210 | ResultSet rs = c.getMetaData().getPrimaryKeys(null, null, |
||
211 | driver.getInternalTableName(tableName)); |
||
212 | ArrayList pks = new ArrayList(); |
||
213 | |||
214 | while (rs.next()) {
|
||
215 | pks.add(rs.getString("COLUMN_NAME"));
|
||
216 | } |
||
217 | |||
218 | //create the index array
|
||
219 | cachedPKIndices = new int[pks.size()]; |
||
220 | |||
221 | for (int i = 0; i < cachedPKIndices.length; i++) { |
||
222 | cachedPKIndices[i] = getFieldIndexByName((String) pks.get(i));
|
||
223 | } |
||
224 | } catch (SQLException e) { |
||
225 | throw new DriverException(e); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return cachedPKIndices;
|
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | 4050 | fjp | * @throws DriverException
|
234 | 3199 | fjp | * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
|
235 | */
|
||
236 | 4050 | fjp | public DataWare getDataWare(int mode) throws DriverException { |
237 | try {
|
||
238 | 5568 | jmvivo | DBDataWare dw = DBDataSourceFactory.newDataWareInstance(((DBDriver)getDriver()), mode); |
239 | 4050 | fjp | DBDriver driver; |
240 | driver = (DBDriver) getDataSourceFactory().getDriverManager().getDriver(getDriver().getName()); |
||
241 | ((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory()); |
||
242 | dw.setDriver(driver); |
||
243 | dw.setDataSourceFactory(dsf); |
||
244 | dw.setSourceInfo(getSourceInfo()); |
||
245 | return dw;
|
||
246 | } catch (DriverLoadException e) {
|
||
247 | throw new DriverException(e); |
||
248 | } |
||
249 | |||
250 | 3199 | fjp | } |
251 | 4851 | jmvivo | |
252 | public int getFieldWidth(int i) throws DriverException { |
||
253 | 5568 | jmvivo | return ((DBDriver)getDriver()).getFieldWidth(i);
|
254 | 4851 | jmvivo | } |
255 | 6314 | jmvivo | |
256 | public void reload() throws DriverException { |
||
257 | try {
|
||
258 | sem = 0;
|
||
259 | |||
260 | driver.close(); |
||
261 | if (this.con == null) { |
||
262 | con.close(); |
||
263 | con = null;
|
||
264 | } |
||
265 | } catch (SQLException e) { |
||
266 | throw new DriverException(e); |
||
267 | } |
||
268 | this.start();
|
||
269 | this.raiseEventReloaded();
|
||
270 | |||
271 | } |
||
272 | 3199 | fjp | } |