Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / db / DBDataSourceAdapter.java @ 10627

History | View | Annotate | Download (8.07 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.OpenDriverException;
11
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
12
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
13
import com.hardcode.gdbms.engine.data.AbstractDataSource;
14
import com.hardcode.gdbms.engine.data.SourceInfo;
15
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver;
16
import com.hardcode.gdbms.engine.data.driver.DBDriver;
17
import com.hardcode.gdbms.engine.data.driver.DriverException;
18
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver;
19
import com.hardcode.gdbms.engine.data.edition.DataWare;
20
import com.hardcode.gdbms.engine.values.Value;
21

    
22

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

    
40
    //driver de base de datos
41
    protected DBDriver driver;
42

    
43
    //objetos jdbc
44
    protected Connection con;
45

    
46
    //Instrucci?n sql completa que representa el dataSource
47
    protected String sql;
48

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

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

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

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

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

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

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

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

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

    
118
        this.sql = "SELECT * FROM " + tableName;
119
    }
120

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

    
132
    protected String tableName;
133
    protected int sem = 0;
134
    private int[] cachedPKIndices = null;
135

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

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

    
162
            }
163

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

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

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

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

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

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

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

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

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

    
232
        return cachedPKIndices;
233
    }
234

    
235
    /**
236
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
237
     */
238
    public DataWare getDataWare(int mode) throws ReadDriverException {
239
                try {
240
                        DBDataWare dw = DBDataSourceFactory.newDataWareInstance(((DBDriver)getDriver()), mode);
241
                        DBDriver driver;
242
                        driver = (DBDriver) getDataSourceFactory().getDriverManager().getDriver(getDriver().getName());
243
                ((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory());
244
                dw.setDriver(driver);
245
                dw.setDataSourceFactory(dsf);
246
                dw.setSourceInfo(getSourceInfo());
247
                return dw;
248
                } catch (DriverLoadException e) {
249
                        throw new ReadDriverException(getName(),e);
250
                }
251

    
252
    }
253

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

    
258
        public void reload() throws ReloadDriverException {
259
        try {
260
            sem = 0;
261

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

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

    
276
        }
277
}