Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / mysql / MySQLDriver.java @ 9767

History | View | Annotate | Download (3.43 KB)

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

    
6
import java.sql.Connection;
7
import java.sql.DriverManager;
8
import java.sql.ResultSetMetaData;
9
import java.sql.SQLException;
10

    
11
import com.hardcode.gdbms.engine.data.db.JDBCSupport;
12
import com.hardcode.gdbms.engine.data.driver.AbstractJDBCDriver;
13
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
14
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
15
import com.iver.cit.gvsig.fmap.drivers.TableDefinition;
16
import com.iver.cit.gvsig.fmap.edition.IWriter;
17

    
18

    
19
/**
20
 * MySQL driver
21
 *
22
 * @author Fernando Gonz?lez Cort?s
23
 * @author azabala
24
 */
25
public class MySQLDriver extends AbstractJDBCDriver {
26
    private static Exception driverException;
27
    
28
    static {
29
        try {
30
            Class.forName("com.mysql.jdbc.Driver").newInstance();
31
        } catch (Exception ex) {
32
            driverException = ex;
33
        }
34
    }
35
    
36
    /**
37
     * IWriter implementation for MySQL DB.
38
     * It does editing operations
39
     * */
40
    MySQLWriter writer = new MySQLWriter();
41

    
42
    
43
    public IWriter getWriter() {
44
                return writer;
45
        }
46
    
47
    public void open(Connection con, String sql) throws SQLException {
48
                jdbcSupport = JDBCSupport.newJDBCSupport(con, sql);
49
        writer.initialize(con);
50
                writer.setCreateTable(false);
51
            writer.setWriteAll(false);
52
        }
53
    
54
    public void close() throws SQLException {
55
            //commented to avoid problems with automatic datasource
56
//                jdbcSupport.close();
57
//                writer.close();
58
        }
59
    
60
    /**
61
     * DOCUMENT ME!
62
     *
63
     * @param host DOCUMENT ME!
64
     * @param port DOCUMENT ME!
65
     * @param dbName DOCUMENT ME!
66
     * @param user DOCUMENT ME!
67
     * @param password DOCUMENT ME!
68
     *
69
     * @return DOCUMENT ME!
70
     *
71
     * @throws SQLException
72
     * @throws RuntimeException DOCUMENT ME!
73
     *
74
     * @see com.hardcode.gdbms.engine.data.driver.DBDriver#connect(java.lang.String)
75
     */
76
    public Connection getConnection(String host, int port, String dbName,
77
        String user, String password) throws SQLException {
78
        if (driverException != null) {
79
            throw new RuntimeException(driverException);
80
        }
81

    
82
        String connectionString = "jdbc:mysql://" + host;
83

    
84
        if (port != -1) {
85
            connectionString += (":" + port);
86
        }
87

    
88
        connectionString += ("/" + dbName);
89

    
90
        if (user != null) {
91
            connectionString += ("?user=" + user + "&password=" + password);
92
        }
93

    
94
        return DriverManager.getConnection(connectionString);
95
    }
96

    
97
    /**
98
     * @see com.hardcode.driverManager.Driver#getName()
99
     */
100
    public String getName() {
101
        return "mysql";
102
    }
103
    
104
    /*
105
     *Sobreescribo el metodo de AbstractJDBCDriver porque
106
     *si para la gran mayoria de bbdd no hay que escribir querys
107
     *(se hace con updatableresultset) para MySQL no funciona esto.
108
     *Como se necesita construir la query al vuelo, as? se le proporciona
109
     *al ITableDefinition el nombre de la tabla 
110
     * */
111
    public ITableDefinition getTableDefinition(){
112
            DBLayerDefinition solution = new DBLayerDefinition();
113
            ITableDefinition schema = super.getTableDefinition();
114
            solution.setFieldsDesc(schema.getFieldsDesc());
115
            solution.setName(schema.getName());
116
            try {
117
                        ResultSetMetaData metadata = jdbcSupport.getResultSet().
118
                                                getMetaData();
119
                        solution.setTableName(metadata.getTableName(1));
120
                } catch (SQLException e) {
121
                        throw new RuntimeException(e);
122
                }
123
            return solution;
124
            
125
            
126
    }
127

    
128
}