summaryrefslogtreecommitdiff
path: root/src/connectors/src/data/io/sql/SQLConnectionWrapper.java
blob: de9b2cc5a5787bbb2330d056d0e517bcdbce902b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes
 * Copyright (C) 2014  Ludovic Pouzenc <ludovic@pouzenc.fr>
 *  
 * This file is part of SSSync.
 *
 *  SSSync is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SSSync is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SSSync.  If not, see <http://www.gnu.org/licenses/>
 */

package data.io.sql;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

import data.io.MVDataReader;

/**
 * TODO javadoc
 * 
 * @author lpouzenc
 */
public class SQLConnectionWrapper implements Closeable {

	/**
	 * Enumeration of supported DBMS. Each use a particular JDBC driver.
	 */
	public enum DBMSType { ORACLE, MYSQL }

	private final Connection conn;
	
	/**
	 * TODO javadoc
	 * @param dbms
	 * @param host
	 * @param port
	 * @param ress
	 * @param user
	 * @param pass
	 * @param db
	 */
	public SQLConnectionWrapper(DBMSType dbms, String host, int port, String ress, String user, String pass, String db) {
		
		String driverClassName=null;
		String url;
		
		switch ( dbms ) {
		case ORACLE:
			driverClassName="oracle.jdbc.driver.OracleDriver";
			url="jdbc:oracle:thin:@" + host + ":" + port + ":" + ress + "/" + db;
			break;
		case MYSQL:
			driverClassName="com.mysql.jdbc.Driver";
			url="jdbc:mysql://" + host + ":" + port + "/" + db;
			break;
		/*	Could be useful with JUnit tests
			case derby:
			driverClassName="org.apache.derby.jdbc.EmbeddedDriver";
			url="jdbc:derby:" + db;
			break;
		*/
		default:
			throw new IllegalArgumentException("Unsupported DBMSType : " + dbms); 
		}
		
		try {
			@SuppressWarnings("unchecked")
			Class<? extends Driver> clazz = (Class<? extends Driver>) Class.forName(driverClassName);
			DriverManager.registerDriver(clazz.newInstance());
		} catch (Exception e) {
			throw new RuntimeException("Can't load or register JDBC driver for " + dbms + " (" + driverClassName + ")", e);
		}
		
		try {
			conn = DriverManager.getConnection(url, user, pass);
		} catch (SQLException e) {
			throw new RuntimeException("Can't establish database connection (" + url + ")");
		}
	}

	/**
	 * Builds a new reader against current connection and a File containing a SELECT statement.
	 * @param name
	 * @param queryFile
	 * @return
	 * @throws IOException 
	 */
	public MVDataReader newReader(String name, File queryFile) throws IOException {
		return new SQLRelDataReader(name, conn, queryFile);
	}

	/**
	 * Builds a new reader against current connection and a String containing a SELECT statement.
	 * @param name
	 * @param query
	 * @return
	 * @throws IOException 
	 */
	public MVDataReader newReader(String name, String query) {
		return new SQLRelDataReader(name, conn, query);
	}
	
	/**
	 * Close the current database connection.
	 */
	@Override
	public void close() throws IOException {
		try {
			conn.close();
		} catch (SQLException e) {
			throw new IOException("Exception occured while trying to close the SQL connection", e);
		}
	}
	
	/**
	 * @return the current database connection (useful for JUnit tests)
	 */
	public Connection getConn() {
		return conn;
	}
}