/
PlatformFactory.java
113 lines (93 loc) · 4.19 KB
/
PlatformFactory.java
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
package org.jumpmind.symmetric.ddlutils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.ddl.Platform;
import org.jumpmind.symmetric.ddl.platform.mssql.MSSqlPlatform;
import org.jumpmind.symmetric.ddlutils.oracle.OraclePlatform;
import org.jumpmind.symmetric.ddlutils.sqlite.SqLitePlatform;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
public class PlatformFactory {
final static ILog log = LogFactory.getLog(PlatformFactory.class);
static private boolean initialized = false;
public static Platform getPlatform(DataSource dataSource) {
initPlatforms();
String productName = getDbProductName(dataSource);
int majorVersion = getDbMajorVersion(dataSource);
// Try to use latest version of platform, then fallback on default
// platform
String productString = productName;
if (majorVersion > 0) {
productString += majorVersion;
}
if (productName.startsWith("DB2")) {
productString = "DB2v8";
}
Platform pf = org.jumpmind.symmetric.ddl.PlatformFactory.createNewPlatformInstance(productString);
if (pf == null) {
pf = org.jumpmind.symmetric.ddl.PlatformFactory.createNewPlatformInstance(dataSource);
} else {
pf.setDataSource(dataSource);
}
if (pf instanceof MSSqlPlatform && !pf.isDelimitedIdentifierModeOn()) {
log.info("PlatformTurningOnDelimitedIdentifierMode");
pf.setDelimitedIdentifierModeOn(true);
}
log.info("PlatformInUse", pf.getClass().getName());
return pf;
}
public static String getDbProductName(DataSource dataSource) {
return new JdbcTemplate(dataSource).execute(new ConnectionCallback<String>() {
public String doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData metaData = c.getMetaData();
return metaData.getDatabaseProductName();
}
});
}
public static String getDatabaseProductVersion(DataSource dataSource) {
return new JdbcTemplate(dataSource).execute(new ConnectionCallback<String>() {
public String doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData metaData = c.getMetaData();
return metaData.getDatabaseProductVersion();
}
});
}
public static int getDbMajorVersion(DataSource dataSource) {
return new JdbcTemplate(dataSource).execute(new ConnectionCallback<Integer>() {
public Integer doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData metaData = c.getMetaData();
try {
return metaData.getDatabaseMajorVersion();
} catch (UnsupportedOperationException e) {
return 0;
}
}
});
}
public static int getDbMinorVersion(DataSource dataSource) {
return new JdbcTemplate(dataSource).execute(new ConnectionCallback<Integer>() {
public Integer doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData metaData = c.getMetaData();
try {
return metaData.getDatabaseMinorVersion();
} catch (UnsupportedOperationException e) {
return 0;
}
}
});
}
private synchronized static void initPlatforms() {
if (!initialized) {
org.jumpmind.symmetric.ddl.PlatformFactory.registerPlatform(SqLitePlatform.DATABASENAME,
SqLitePlatform.class);
org.jumpmind.symmetric.ddl.PlatformFactory.registerPlatform(OraclePlatform.DATABASENAME,
OraclePlatform.class);
initialized = true;
}
}
}