/
AbstractJdbcDatabasePlatform.java
81 lines (66 loc) · 2.63 KB
/
AbstractJdbcDatabasePlatform.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
package org.jumpmind.db.platform;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.jumpmind.db.AbstractDatabasePlatform;
import org.jumpmind.db.sql.ISqlTemplate;
import org.jumpmind.db.sql.jdbc.JdbcSqlTemplate;
import org.jumpmind.log.Log;
abstract public class AbstractJdbcDatabasePlatform extends AbstractDatabasePlatform {
protected DataSource dataSource;
protected ISqlTemplate sqlTemplate;
protected DatabasePlatformSettings settings;
public AbstractJdbcDatabasePlatform(DataSource dataSource, DatabasePlatformSettings settings, Log log) {
super(log);
this.dataSource = dataSource;
this.settings = settings;
createSqlTemplate();
}
protected void createSqlTemplate() {
this.sqlTemplate = new JdbcSqlTemplate(dataSource, settings, null);
}
@Override
public ISqlTemplate getSqlTemplate() {
return sqlTemplate;
}
public boolean isPrimaryKeyViolation(Exception ex) {
boolean primaryKeyViolation = false;
if (primaryKeyViolationCodes != null || primaryKeyViolationSqlStates != null) {
SQLException sqlEx = findSQLException(ex);
if (sqlEx != null) {
if (primaryKeyViolationCodes != null) {
int errorCode = sqlEx.getErrorCode();
for (int primaryKeyViolationCode : primaryKeyViolationCodes) {
if (primaryKeyViolationCode == errorCode) {
primaryKeyViolation = true;
break;
}
}
}
if (primaryKeyViolationSqlStates != null) {
String sqlState = sqlEx.getSQLState();
if (sqlState != null) {
for (String primaryKeyViolationSqlState : primaryKeyViolationSqlStates) {
if (primaryKeyViolationSqlState != null
&& primaryKeyViolationSqlState.equals(sqlState)) {
primaryKeyViolation = true;
break;
}
}
}
}
}
}
return primaryKeyViolation;
}
protected SQLException findSQLException(Throwable ex) {
if (ex instanceof SQLException) {
return (SQLException) ex;
} else {
Throwable cause = ex.getCause();
if (cause != null && !cause.equals(ex)) {
return findSQLException(cause);
}
}
return null;
}
}