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 package org.utgenome.gwt.utgb.server;
26
27 import java.io.File;
28
29 import org.utgenome.UTGBErrorCode;
30 import org.utgenome.UTGBException;
31 import org.utgenome.config.UTGBConfig;
32 import org.utgenome.config.UTGBConfig.Database;
33 import org.xerial.db.DBException;
34 import org.xerial.db.sql.ConnectionPoolImpl;
35 import org.xerial.db.sql.DatabaseAccess;
36 import org.xerial.db.sql.DatabaseAccessBase;
37 import org.xerial.db.sql.h2.H2Access;
38 import org.xerial.db.sql.mysql.MySQLAccess;
39 import org.xerial.db.sql.postgres.PostgresAccess;
40 import org.xerial.db.sql.sqlite.SQLiteAccess;
41
42
43
44
45
46
47
48 public class JDBCService {
49 public static DatabaseAccess getDatabaseAccess(UTGBConfig.Database db) throws UTGBException {
50 return getDatabaseAccess(new File(".").getAbsolutePath(), db);
51 }
52
53 public static DatabaseAccess getDatabaseAccess(String projectRootFolder, UTGBConfig.Database db) throws UTGBException {
54 try {
55 if (db.dbms.equals("sqlite")) {
56 if (db.address.startsWith("/"))
57 return new SQLiteAccess(db.address);
58 else if (db.address.startsWith("file://"))
59 return new SQLiteAccess(db.address.substring("file://".length()));
60 else
61 return new SQLiteAccess(projectRootFolder + "/" + db.address);
62 }
63 else if (db.dbms.equals("postgres")) {
64 return new PostgresAccess(db.address, db.user, db.pass);
65 }
66 else if (db.dbms.equals("mysql")) {
67 return new MySQLAccess(db.address, db.user, db.pass);
68 }
69 else if (db.dbms.equals("h2")) {
70 return new H2Access(db.address);
71 }
72 else if (db.equals("hsqldb")) {
73 return new HSQLDBAccess(db.address, db.user, db.pass);
74 }
75 else
76 return new GenericDBAccess(db);
77
78 }
79 catch (DBException e) {
80 throw new UTGBException(UTGBErrorCode.DatabaseError, e);
81 }
82
83 }
84
85 public static class GenericDBAccess extends DatabaseAccessBase {
86 public GenericDBAccess(Database db) throws DBException {
87 super(new ConnectionPoolImpl(db.driver, db.jdbcPrefix + db.address, db.user, db.pass));
88 }
89 }
90
91 public static class HSQLDBAccess extends DatabaseAccessBase {
92 public static final String DRIVER_NAME = "org.hsqldb.jdbcDriver";
93 public static final String ADDRESS_PREFIX = "jdbc:hsqldb:";
94
95
96
97
98
99
100 public HSQLDBAccess() throws DBException {
101 super(new ConnectionPoolImpl(DRIVER_NAME, ADDRESS_PREFIX + "."));
102 }
103
104 public HSQLDBAccess(String address) throws DBException {
105 super(new ConnectionPoolImpl(DRIVER_NAME, ADDRESS_PREFIX + address));
106 }
107
108 public HSQLDBAccess(String address, String user, String pass) throws DBException {
109 super(new ConnectionPoolImpl(DRIVER_NAME, ADDRESS_PREFIX + address, user, pass));
110 }
111
112 }
113
114 }