Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / data / db / JDBCSupport.java @ 1956

History | View | Annotate | Download (6.38 KB)

1
package com.hardcode.gdbms.engine.data.db;
2

    
3
import com.hardcode.gdbms.engine.data.driver.DriverException;
4
import com.hardcode.gdbms.engine.data.driver.ReadAccess;
5
import com.hardcode.gdbms.engine.values.Value;
6
import com.hardcode.gdbms.engine.values.ValueFactory;
7

    
8
import java.sql.Connection;
9
import java.sql.ResultSet;
10
import java.sql.SQLException;
11
import java.sql.Statement;
12
import java.sql.Types;
13

    
14

    
15
/**
16
 *
17
 */
18
public class JDBCSupport implements ReadAccess {
19
    private ResultSet resultSet;
20
    private int rowCount = -1;
21

    
22
    /**
23
     * Creates a new JDBCSupport object.
24
     *
25
     * @param r DOCUMENT ME!
26
     */
27
    public JDBCSupport(ResultSet r) {
28
        resultSet = r;
29
    }
30

    
31
    /**
32
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
33
     *      int)
34
     */
35
    public Value getFieldValue(long rowIndex, int fieldId)
36
        throws DriverException {
37
        Value value = null;
38

    
39
        try {
40
            fieldId += 1;
41
            resultSet.absolute((int) rowIndex + 1);
42

    
43
            int type = resultSet.getMetaData().getColumnType(fieldId);
44

    
45
            switch (type) {
46
            case Types.BIGINT:
47
                value = ValueFactory.createValue(resultSet.getLong(fieldId));
48

    
49
                break;
50

    
51
            case Types.BIT:
52
            case Types.BOOLEAN:
53
                value = ValueFactory.createValue(resultSet.getBoolean(fieldId));
54

    
55
                break;
56

    
57
            case Types.CHAR:
58
            case Types.VARCHAR:
59
            case Types.LONGVARCHAR:
60
                value = ValueFactory.createValue(resultSet.getString(fieldId));
61

    
62
                break;
63

    
64
            case Types.DATE:
65
                value = ValueFactory.createValue(resultSet.getDate(fieldId));
66

    
67
                break;
68

    
69
            case Types.DECIMAL:
70
            case Types.NUMERIC:
71
            case Types.FLOAT:
72
            case Types.DOUBLE:
73
                value = ValueFactory.createValue(resultSet.getDouble(fieldId));
74

    
75
                break;
76

    
77
            case Types.INTEGER:
78
                value = ValueFactory.createValue(resultSet.getInt(fieldId));
79

    
80
                break;
81

    
82
            case Types.REAL:
83
                value = ValueFactory.createValue(resultSet.getFloat(fieldId));
84

    
85
                break;
86

    
87
            case Types.SMALLINT:
88
                value = ValueFactory.createValue(resultSet.getShort(fieldId));
89

    
90
                break;
91

    
92
            case Types.TINYINT:
93
                value = ValueFactory.createValue(resultSet.getByte(fieldId));
94

    
95
                break;
96

    
97
            case Types.BINARY:
98
            case Types.VARBINARY:
99
            case Types.LONGVARBINARY:
100
                value = ValueFactory.createValue(resultSet.getBytes(fieldId));
101

    
102
                break;
103

    
104
            case Types.TIMESTAMP:
105
                value = ValueFactory.createValue(resultSet.getTimestamp(fieldId));
106

    
107
                break;
108

    
109
            case Types.TIME:
110
                value = ValueFactory.createValue(resultSet.getTime(fieldId));
111

    
112
                break;
113

    
114
            default:
115
                value = ValueFactory.createValue(resultSet.getString(fieldId));
116
            }
117
        } catch (SQLException e) {
118
            throw new DriverException(e);
119
        }
120

    
121
                return (value == null)?ValueFactory.createNullValue():value;
122
    }
123

    
124
    /**
125
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
126
     */
127
    public int getFieldCount() throws DriverException {
128
        try {
129
            return resultSet.getMetaData().getColumnCount();
130
        } catch (SQLException e) {
131
            throw new DriverException(e);
132
        }
133
    }
134

    
135
    /**
136
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
137
     */
138
    public String getFieldName(int fieldId) throws DriverException {
139
        try {
140
            return resultSet.getMetaData().getColumnName(fieldId + 1);
141
        } catch (SQLException e) {
142
            throw new DriverException(e);
143
        }
144
    }
145

    
146
    /**
147
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
148
     */
149
    public long getRowCount() throws DriverException {
150
        try {
151
            if (rowCount == -1) {
152
                resultSet.last();
153
                rowCount = resultSet.getRow();
154
            }
155

    
156
            return rowCount;
157
        } catch (SQLException e) {
158
            throw new RuntimeException(e);
159
        }
160
    }
161

    
162
    /**
163
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
164
     */
165
    public int getFieldType(int i) throws DriverException {
166
        try {
167
            return resultSet.getMetaData().getColumnType(i);
168
        } catch (SQLException e) {
169
            throw new DriverException(e);
170
        }
171
    }
172

    
173
    /**
174
     * DOCUMENT ME!
175
     *
176
     * @throws SQLException
177
     */
178
    public void close() throws SQLException {
179
        resultSet.close();
180
    }
181

    
182
    /**
183
     * DOCUMENT ME!
184
     *
185
     * @return DOCUMENT ME!
186
     */
187
    public ResultSet getResultSet() {
188
        return resultSet;
189
    }
190

    
191
    /**
192
     * DOCUMENT ME!
193
     *
194
     * @param con DOCUMENT ME!
195
     * @param sql DOCUMENT ME!
196
     *
197
     * @return DOCUMENT ME!
198
     *
199
     * @throws SQLException DOCUMENT ME!
200
     */
201
    public static JDBCSupport open(Connection con, String sql)
202
        throws SQLException {
203
        Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
204
                ResultSet.CONCUR_READ_ONLY);
205
        ResultSet res = st.executeQuery(sql);
206

    
207
        return new JDBCSupport(res);
208
    }
209

    
210
    /**
211
     * DOCUMENT ME!
212
     *
213
     * @param fields DOCUMENT ME!
214
     * @param tableName DOCUMENT ME!
215
     * @param where DOCUMENT ME!
216
     *
217
     * @return DOCUMENT ME!
218
     */
219
    private static String buildQuery(String[] fields, String tableName,
220
        String where) {
221
        String res = "select ";
222

    
223
        if (fields.length > 0) {
224
            res += fields[0];
225
        }
226

    
227
        for (int i = 1; i < fields.length; i++) {
228
            res += ("," + fields[i]);
229
        }
230

    
231
        res += (" from " + tableName);
232

    
233
        if (where != null) {
234
            res += (" where " + where);
235
        }
236

    
237
        return res;
238
    }
239

    
240
    /**
241
     * Executes a query with the 'con' connection
242
     *
243
     * @param con connection
244
     * @param sql instruction to execute
245
     *
246
     * @throws SQLException if execution fails
247
     */
248
    public static void execute(Connection con, String sql)
249
        throws SQLException {
250
        Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
251
                ResultSet.CONCUR_UPDATABLE);
252

    
253
        st.execute(sql);
254
    }
255
}