Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / db / DBDataSourceAdapter.java @ 37927

History | View | Annotate | Download (7.94 KB)

1
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
import com.hardcode.driverManager.Driver;
9
import com.hardcode.driverManager.DriverLoadException;
10
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
11
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
12
import com.hardcode.gdbms.engine.data.AbstractDataSource;
13
import com.hardcode.gdbms.engine.data.SourceInfo;
14
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver;
15
import com.hardcode.gdbms.engine.data.driver.DBDriver;
16
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver;
17
import com.hardcode.gdbms.engine.data.edition.DataWare;
18
import com.hardcode.gdbms.engine.values.Value;
19

    
20

    
21
/**
22
 * Adaptador de la interfaz DBDriver a la interfaz DataSource. Adapta las
23
 * interfaces de los drivers de base de datos a la interfaz DataSource.
24
 *
25
 * @author Fernando Gonz?lez Cort?s
26
 */
27
public class DBDataSourceAdapter extends AbstractDataSource
28
    implements DBDataSource {
29
    /*
30
     * datos de la conexi?n
31
     */
32
    protected String host;
33
    protected int port;
34
    protected String dbName;
35
    protected String user;
36
    protected String password;
37

    
38
    //driver de base de datos
39
    protected DBDriver driver;
40

    
41
    //objetos jdbc
42
    protected Connection con;
43

    
44
    //Instrucci?n sql completa que representa el dataSource
45
    protected String sql;
46

    
47
    /**
48
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getRowCount()
49
     */
50
    public long getRowCount() throws ReadDriverException {
51
        return ((DBDriver)getDriver()).getRowCount();
52
    }
53

    
54
    /**
55
     * Establece el driver
56
     *
57
     * @param driver The driver to set.
58
     */
59
    public void setDriver(DBDriver driver) {
60
        this.driver = driver;
61
    }
62

    
63
    /**
64
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
65
     */
66
    public int getFieldCount() throws ReadDriverException {
67
        return ((DBDriver)getDriver()).getFieldCount();
68
    }
69

    
70
    /**
71
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
72
     */
73
    public String getFieldName(int fieldId) throws ReadDriverException {
74
        return ((DBDriver)getDriver()).getFieldName(fieldId);
75
    }
76

    
77
    /**
78
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
79
     */
80
    public int getFieldType(int i) throws ReadDriverException {
81
        return ((DBDriver)getDriver()).getFieldType(i);
82
    }
83

    
84
    /**
85
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
86
     *      int)
87
     */
88
    public Value getFieldValue(long rowIndex, int fieldId)
89
        throws ReadDriverException {
90
        return ((DBDriver)getDriver()).getFieldValue(rowIndex, fieldId);
91
    }
92

    
93
    /**
94
     * DOCUMENT ME!
95
     *
96
     * @return
97
     */
98
    public Driver getDriver() {
99
        return driver;
100
    }
101

    
102
    /**
103
     * @see com.hardcode.gdbms.engine.data.DataSource#setSourceInfo(com.hardcode.gdbms.engine.data.driver.DriverInfo)
104
     */
105
    public void setSourceInfo(SourceInfo sourceInfo) {
106
        super.setSourceInfo(sourceInfo);
107

    
108
        this.host = ((DBSourceInfo) sourceInfo).host;
109
        this.port = ((DBSourceInfo) sourceInfo).port;
110
        this.dbName = ((DBSourceInfo) sourceInfo).dbName;
111
        this.user = ((DBSourceInfo) sourceInfo).user;
112
        this.password = ((DBSourceInfo) sourceInfo).password;
113
        this.tableName = ((DBTableSourceInfo) sourceInfo).tableName;
114
        this.con =((DBTableSourceInfo) sourceInfo).connection;
115

    
116
        this.sql = "SELECT * FROM " + tableName;
117
    }
118

    
119
    /**
120
     * @see com.hardcode.gdbms.engine.data.db.DBDataSource#execute(java.lang.String)
121
     */
122
    public void execute(String sql) throws ReadDriverException {
123
        try {
124
                ((DBDriver)getDriver()).execute(this.getConnection(), sql);
125
        } catch (SQLException e) {
126
            throw new ReadDriverException(getName(),e);
127
        }
128
    }
129

    
130
    protected String tableName;
131
    protected int sem = 0;
132
    private int[] cachedPKIndices = null;
133

    
134
    /**
135
     * Get's a connection to the driver
136
     *
137
     * @return Connection
138
     *
139
     * @throws SQLException if the connection cannot be established
140
     */
141
    public Connection getConnection() throws SQLException {
142
            if (this.con == null) {
143
                    return driver.getConnection(host, port, dbName, user, password);
144
            }
145
            else {
146
                    return this.con;
147
            }
148
    }
149

    
150
    /**
151
     * @see com.hardcode.gdbms.engine.data.DataSource#start()
152
     */
153
    public void start() throws ReadDriverException {
154
        try {
155
            if (sem == 0) {
156
                    con = getConnection();
157
                    ((AlphanumericDBDriver) driver).open(con, sql);
158
                    // driver.setSourceInfo(this.getSourceInfo());
159

    
160
            }
161

    
162
            sem++;
163
        } catch (SQLException e) {
164
            throw new ReadDriverException(getName(),e);
165
        }
166
    }
167

    
168
    /**
169
     * @see com.hardcode.gdbms.engine.data.DataSource#stop()
170
     */
171
    public void stop() throws ReadDriverException {
172
        try {
173
            sem--;
174

    
175
            if (sem == 0) {
176
                    driver.close();
177
                    if (this.con != null) {
178
                        con.close();
179
                        con = null;
180
                    }
181
            } else if (sem < 0) {
182
                throw new RuntimeException("DataSource closed too many times");
183
            }
184
        } catch (SQLException e) {
185
            throw new ReadDriverException(getName(),e);
186
        }
187
    }
188

    
189
    /**
190
     * @see com.hardcode.gdbms.engine.data.DataSource#getDBMS()
191
     */
192
    public String getDBMS() {
193
        return ((DBSourceInfo)sourceInfo).dbms;
194
    }
195

    
196
    /**
197
     * @see com.hardcode.gdbms.engine.data.DataSource#getName()
198
     */
199
    public String getName() {
200
        return sourceInfo.name;
201
    }
202

    
203
    /**
204
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
205
     */
206
    public int[] getPrimaryKeys() throws ReadDriverException {
207
        if (cachedPKIndices == null) {
208
            try {
209
                //Gets the pk column names
210
                Connection c = getConnection();
211
                ResultSet rs = c.getMetaData().getPrimaryKeys(null, null,
212
                        driver.getInternalTableName(tableName));
213
                ArrayList pks = new ArrayList();
214

    
215
                while (rs.next()) {
216
                    pks.add(rs.getString("COLUMN_NAME"));
217
                }
218

    
219
                //create the index array
220
                cachedPKIndices = new int[pks.size()];
221

    
222
                for (int i = 0; i < cachedPKIndices.length; i++) {
223
                    cachedPKIndices[i] = getFieldIndexByName((String) pks.get(i));
224
                }
225
            } catch (SQLException e) {
226
                throw new ReadDriverException(getName(),e);
227
            }
228
        }
229

    
230
        return cachedPKIndices;
231
    }
232

    
233
    /**
234
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
235
     */
236
    public DataWare getDataWare(int mode) throws ReadDriverException {
237
                try {
238
                        DBDataWare dw = DBDataSourceFactory.newDataWareInstance(((DBDriver)getDriver()), mode);
239
                        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 ReadDriverException(getName(),e);
248
                }
249

    
250
    }
251

    
252
        public int getFieldWidth(int i) throws ReadDriverException {
253
        return ((DBDriver)getDriver()).getFieldWidth(i);
254
        }
255

    
256
        public void reload() throws ReloadDriverException {
257
        try {
258
            sem = 0;
259

    
260
            driver.close();
261
            if (this.con == null) {
262
                    con.close();
263
                    con = null;
264
            }
265

    
266
            this.start();
267
        } catch (SQLException e) {
268
            throw new ReloadDriverException(getName(),e);
269
        } catch (ReadDriverException e) {
270
                 throw new ReloadDriverException(getName(),e);
271
                }
272
        this.raiseEventReloaded();
273

    
274
        }
275
}