Statistics
| Revision:

root / branches / v10 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / mysql / MySQLDriver.java @ 10051

History | View | Annotate | Download (4.81 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.DataSourceFactory;
12
import com.hardcode.gdbms.engine.data.db.DBDataSourceAdapter;
13
import com.hardcode.gdbms.engine.data.db.DBDataSourceFactory;
14
import com.hardcode.gdbms.engine.data.db.DBDataWare;
15
import com.hardcode.gdbms.engine.data.db.DBTableSourceInfo;
16
import com.hardcode.gdbms.engine.data.db.JDBCSupport;
17
import com.hardcode.gdbms.engine.data.driver.AbstractJDBCDriver;
18
import com.hardcode.gdbms.engine.data.driver.DBDriver;
19
import com.hardcode.gdbms.engine.data.driver.DBTransactionalDriver;
20
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver;
21
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
22
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
23
import com.iver.cit.gvsig.fmap.edition.IWriter;
24

    
25

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

    
49
    
50
    public IWriter getWriter() {
51
                return writer;
52
        }
53
    
54
    public void open(Connection con, String sql) throws SQLException {
55
                jdbcSupport = JDBCSupport.newJDBCSupport(con, sql);
56
        writer.initialize(con);
57
                writer.setCreateTable(false);
58
            writer.setWriteAll(false);
59
                DBDataWare dw = DBDataSourceFactory.newDataWareInstance(this, DataSourceFactory.DATA_WARE_DIRECT_MODE);
60
        dw.setDriver(this);
61
//            ITableDefinition schema = super.getTableDefinition();
62
                ResultSetMetaData metadata = jdbcSupport.getResultSet().
63
                                                getMetaData();
64
        
65
        DBTableSourceInfo sourceInfo = new DBTableSourceInfo();
66
        
67
        sourceInfo.connection = con;        
68
        sourceInfo.dbName = con.getCatalog();
69
        sourceInfo.tableName = metadata.getTableName(1);
70
                
71
        dw.setSourceInfo(sourceInfo);            
72
            
73
            writer.setDirectDataWare(dw);
74
            
75
        }
76
    
77
    public void close() throws SQLException {
78
            //commented to avoid problems with automatic datasource
79
//                jdbcSupport.close();
80
//                writer.close();
81
        }
82
    
83
    /**
84
     * DOCUMENT ME!
85
     *
86
     * @param host DOCUMENT ME!
87
     * @param port DOCUMENT ME!
88
     * @param dbName DOCUMENT ME!
89
     * @param user DOCUMENT ME!
90
     * @param password DOCUMENT ME!
91
     *
92
     * @return DOCUMENT ME!
93
     *
94
     * @throws SQLException
95
     * @throws RuntimeException DOCUMENT ME!
96
     *
97
     * @see com.hardcode.gdbms.engine.data.driver.DBDriver#connect(java.lang.String)
98
     */
99
    public Connection getConnection(String host, int port, String dbName,
100
        String user, String password) throws SQLException {
101
        if (driverException != null) {
102
            throw new RuntimeException(driverException);
103
        }
104

    
105
        String connectionString = "jdbc:mysql://" + host;
106

    
107
        if (port != -1) {
108
            connectionString += (":" + port);
109
        }
110

    
111
        connectionString += ("/" + dbName);
112

    
113
        if (user != null) {
114
            connectionString += ("?user=" + user + "&password=" + password);
115
        }
116

    
117
        return DriverManager.getConnection(connectionString);
118
    }
119

    
120
    /**
121
     * @see com.hardcode.driverManager.Driver#getName()
122
     */
123
    public String getName() {
124
        return "mysql";
125
    }
126
    
127
    /*
128
     *Sobreescribo el metodo de AbstractJDBCDriver porque
129
     *si para la gran mayoria de bbdd no hay que escribir querys
130
     *(se hace con updatableresultset) para MySQL no funciona esto.
131
     *Como se necesita construir la query al vuelo, as? se le proporciona
132
     *al ITableDefinition el nombre de la tabla 
133
     * */
134
//    public ITableDefinition getTableDefinition(){
135
//            DBLayerDefinition solution = new DBLayerDefinition();
136
//            ITableDefinition schema = super.getTableDefinition();
137
//            solution.setFieldsDesc(schema.getFieldsDesc());
138
//            solution.setName(schema.getName());
139
//            try {
140
//                        ResultSetMetaData metadata = jdbcSupport.getResultSet().
141
//                                                getMetaData();
142
//                        solution.setTableName(metadata.getTableName(1));
143
//                } catch (SQLException e) {
144
//                        throw new RuntimeException(e);
145
//                }
146
//            return solution;
147
//            
148
//            
149
//    }
150

    
151
        public void beginTrans(Connection con) throws SQLException {
152
                // TODO Auto-generated method stub
153
                
154
        }
155

    
156
        public void commitTrans(Connection con) throws SQLException {
157
                // TODO Auto-generated method stub
158
                
159
        }
160

    
161
        public void rollBackTrans(Connection con) throws SQLException {
162
                // TODO Auto-generated method stub
163
                
164
        }
165

    
166
}