Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / driver / mysql / MySQLDriver.java @ 466

History | View | Annotate | Download (5.87 KB)

1
/*
2
 * Created on 16-oct-2004
3
 */
4
package com.hardcode.gdbms.driver.mysql;
5

    
6
import com.hardcode.driverManager.Driver;
7

    
8
import com.hardcode.gdbms.engine.data.DBDriver;
9
import com.hardcode.gdbms.engine.data.DriverException;
10
import com.hardcode.gdbms.engine.values.Value;
11
import com.hardcode.gdbms.engine.values.ValueFactory;
12

    
13
import java.sql.Connection;
14
import java.sql.DriverManager;
15
import java.sql.ResultSet;
16
import java.sql.SQLException;
17
import java.sql.Statement;
18
import java.sql.Types;
19

    
20

    
21
/**
22
 * DOCUMENT ME!
23
 *
24
 * @author Fernando Gonz?lez Cort?s
25
 */
26
public class MySQLDriver implements DBDriver, Driver {
27
    private static Exception driverException;
28

    
29
    static {
30
        try {
31
            Class.forName("com.mysql.jdbc.Driver").newInstance();
32
        } catch (Exception ex) {
33
            driverException = ex;
34
        }
35
    }
36

    
37
    private Connection conn;
38
    private Statement st;
39
    private ResultSet res;
40
    private long tableLength;
41

    
42
    /**
43
     * DOCUMENT ME!
44
     *
45
     * @param host DOCUMENT ME!
46
     * @param port DOCUMENT ME!
47
     * @param dbName DOCUMENT ME!
48
     * @param user DOCUMENT ME!
49
     * @param password DOCUMENT ME!
50
     *
51
     * @throws SQLException
52
     * @throws RuntimeException DOCUMENT ME!
53
     *
54
     * @see com.hardcode.gdbms.engine.data.DBDriver#connect(java.lang.String)
55
     */
56
    public void connect(String host, int port, String dbName, String user,
57
        String password) throws SQLException {
58
        if (driverException != null) {
59
            throw new RuntimeException(driverException);
60
        }
61

    
62
        String connectionString = "jdbc:mysql://" + host;
63
        if (port != -1){
64
                connectionString += ":" + port;
65
        }
66
        
67
        connectionString += "/" + dbName;
68
        
69
        if (user != null){
70
                connectionString += "?user=" + user + "&password=" + password;
71
        }
72
        
73
        conn = DriverManager.getConnection(connectionString);
74
    }
75

    
76
    /**
77
     * DOCUMENT ME!
78
     *
79
     * @throws SQLException
80
     *
81
     * @see com.hardcode.gdbms.engine.data.DBDriver#closeConnection()
82
     */
83
    public void closeConnection() throws SQLException {
84
        conn.close();
85
    }
86

    
87
    /**
88
     * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
89
     */
90
    public Value getFieldValue(long rowIndex, int fieldId)
91
        throws DriverException {
92
        try {
93
            fieldId++;
94
            res.absolute((int) rowIndex + 1);
95

    
96
            int type = res.getMetaData().getColumnType(fieldId);
97

    
98
            switch (type) {
99
                case Types.BIGINT:
100
                    return ValueFactory.createValue(res.getLong(fieldId));
101

    
102
                case Types.BIT:
103
                    return ValueFactory.createValue(res.getBoolean(fieldId));
104

    
105
                case Types.CHAR:
106
                    return ValueFactory.createValue(res.getString(fieldId));
107

    
108
                case Types.DATE:
109
                    return ValueFactory.createValue(res.getDate(fieldId));
110

    
111
                case Types.FLOAT:
112
                case Types.DOUBLE:
113
                    return ValueFactory.createValue(res.getDouble(fieldId));
114

    
115
                case Types.INTEGER:
116
                    return ValueFactory.createValue(res.getInt(fieldId));
117

    
118
                case Types.REAL:
119
                    return ValueFactory.createValue(res.getFloat(fieldId));
120

    
121
                case Types.VARCHAR:
122
                    return ValueFactory.createValue(res.getString(fieldId));
123

    
124
                case Types.VARBINARY:
125
                case Types.TINYINT:
126
                case Types.TIMESTAMP:
127
                case Types.TIME:
128
                case Types.SMALLINT:
129
                case Types.OTHER:
130
                case Types.NUMERIC:
131
                case Types.LONGVARCHAR:
132
                case Types.LONGVARBINARY:
133
                case Types.DECIMAL:
134
                case Types.BINARY:default:
135
                    throw new DriverException("Type not recognized: " + type);
136
            }
137
        } catch (SQLException e) {
138
            throw new DriverException(e);
139
        }
140
    }
141

    
142
    /**
143
     * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
144
     */
145
    public int getFieldCount() throws DriverException {
146
        try {
147
            return res.getMetaData().getColumnCount();
148
        } catch (SQLException e) {
149
            throw new DriverException(e);
150
        }
151
    }
152

    
153
    /**
154
     * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
155
     */
156
    public String getFieldName(int fieldId) throws DriverException {
157
        try {
158
                fieldId++;
159
            return res.getMetaData().getColumnName(fieldId);
160
        } catch (SQLException e) {
161
            throw new DriverException(e);
162
        }
163
    }
164

    
165
    /**
166
     * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
167
     */
168
    public long getRowCount() throws DriverException {
169
        return tableLength;
170
    }
171

    
172
    /**
173
     * @see com.hardcode.driverManager.Driver#getName()
174
     */
175
    public String getName() {
176
        return "mysql";
177
    }
178

    
179
    /**
180
     * @see com.hardcode.gdbms.engine.data.DBDriver#openTable(java.lang.String)
181
     */
182
    public void openTable(String table) throws SQLException {
183
        st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
184
                ResultSet.CONCUR_READ_ONLY);
185
        res = st.executeQuery("select * from " + table);
186
        res.last();
187
        tableLength = res.getRow();
188
        res.beforeFirst();
189
    }
190

    
191
    /**
192
     * @see com.hardcode.gdbms.engine.data.DBDriver#closeTable()
193
     */
194
    public void closeTable() throws SQLException {
195
        res.close();
196
        st.close();
197
    }
198

    
199
    /**
200
     * @see com.hardcode.gdbms.engine.data.Delegable#executeSQL(java.lang.String)
201
     */
202
    public void executeSQL(String sql) throws SQLException {
203
        st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
204
                ResultSet.CONCUR_READ_ONLY);
205
        res = st.executeQuery(sql);
206
        res.last();
207
        tableLength = res.getRow();
208
        res.beforeFirst();
209
    }
210
}