1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.database.platform; |
18 | |
|
19 | |
import java.math.BigInteger; |
20 | |
import java.sql.Connection; |
21 | |
import java.sql.PreparedStatement; |
22 | |
import java.sql.ResultSet; |
23 | |
import java.sql.SQLException; |
24 | |
import java.util.regex.Matcher; |
25 | |
import java.util.regex.Pattern; |
26 | |
|
27 | |
import javax.persistence.EntityManager; |
28 | |
import javax.persistence.EntityTransaction; |
29 | |
|
30 | |
import org.apache.ojb.broker.PersistenceBroker; |
31 | |
import org.apache.ojb.broker.accesslayer.LookupException; |
32 | |
import org.apache.ojb.broker.query.Criteria; |
33 | |
|
34 | 0 | public class MySQLDatabasePlatform extends ANSISqlDatabasePlatform { |
35 | |
|
36 | 0 | private static final Pattern APOS_PAT = Pattern.compile("'"); |
37 | 0 | private static final Pattern BSLASH_PAT = Pattern.compile(Matcher.quoteReplacement("\\")); |
38 | |
|
39 | |
public String getLockRouteHeaderQuerySQL(Long routeHeaderId, boolean wait) { |
40 | 0 | return "SELECT DOC_HDR_ID FROM KREW_DOC_HDR_T WHERE DOC_HDR_ID=? FOR UPDATE"; |
41 | |
} |
42 | |
|
43 | |
public String getStrToDateFunction() { |
44 | 0 | return "STR_TO_DATE"; |
45 | |
} |
46 | |
|
47 | |
public String getCurTimeFunction() { |
48 | 0 | return "NOW()"; |
49 | |
} |
50 | |
|
51 | |
public void applyLimit(Integer limit, Criteria criteria) { |
52 | 0 | if (limit != null) { |
53 | 0 | criteria.addSql(" 1 LIMIT 0," + limit.intValue()); |
54 | |
} |
55 | 0 | } |
56 | |
|
57 | |
public Long getNextValSQL(String sequenceName, PersistenceBroker persistenceBroker) { |
58 | 0 | PreparedStatement statement = null; |
59 | 0 | ResultSet resultSet = null; |
60 | |
try { |
61 | 0 | Connection connection = persistenceBroker.serviceConnectionManager().getConnection(); |
62 | 0 | statement = connection.prepareStatement("INSERT INTO " + sequenceName + " VALUES (NULL);"); |
63 | 0 | statement.executeUpdate(); |
64 | 0 | statement = connection.prepareStatement("SELECT LAST_INSERT_ID()"); |
65 | 0 | resultSet = statement.executeQuery(); |
66 | |
|
67 | 0 | if (!resultSet.next()) { |
68 | 0 | throw new RuntimeException("Error retrieving next option id for action list from sequence."); |
69 | |
} |
70 | 0 | return new Long(resultSet.getLong(1)); |
71 | 0 | } catch (SQLException e) { |
72 | 0 | throw new RuntimeException("Error retrieving next option id for action list from sequence.", e); |
73 | 0 | } catch (LookupException e) { |
74 | 0 | throw new RuntimeException("Error retrieving next option id for action list from sequence.", e); |
75 | |
} finally { |
76 | 0 | if (statement != null) { |
77 | |
try { |
78 | 0 | statement.close(); |
79 | 0 | } catch (SQLException e) { |
80 | 0 | } |
81 | |
} |
82 | 0 | if (resultSet != null) { |
83 | |
try { |
84 | 0 | resultSet.close(); |
85 | 0 | } catch (SQLException e) { |
86 | 0 | } |
87 | |
} |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
public Long getNextValSQL(String sequenceName, EntityManager entityManager) { |
92 | 0 | EntityTransaction tx = entityManager.getTransaction(); |
93 | |
try { |
94 | 0 | tx.begin(); |
95 | 0 | entityManager.createNativeQuery("INSERT INTO " + sequenceName + " VALUES (NULL);").executeUpdate(); |
96 | 0 | Long result = new Long(((BigInteger) entityManager.createNativeQuery("SELECT LAST_INSERT_ID()").getSingleResult()).longValue()); |
97 | 0 | tx.commit(); |
98 | 0 | return result; |
99 | |
} |
100 | |
finally { |
101 | 0 | if ( tx.isActive() ) { |
102 | 0 | tx.rollback(); |
103 | |
} |
104 | |
} |
105 | |
} |
106 | |
|
107 | |
public boolean isSITCacheSupported() { |
108 | 0 | return false; |
109 | |
} |
110 | |
|
111 | |
public String toString() { |
112 | 0 | return "[MySQLDatabasePlatform]"; |
113 | |
} |
114 | |
|
115 | |
public String getSelectForUpdateSuffix(long waitMillis) { |
116 | 0 | return "for update"; |
117 | |
} |
118 | |
|
119 | |
public String getDateFormatString(String dateFormatString) { |
120 | 0 | String newString = ""; |
121 | 0 | if ("yyyy-mm-dd".equalsIgnoreCase(dateFormatString)) { |
122 | 0 | newString = "'%Y-%m-%d'"; |
123 | |
} |
124 | 0 | else if ("DD/MM/YYYY HH12:MI:SS PM".equalsIgnoreCase(dateFormatString)) { |
125 | 0 | newString = "'%d/%m/%Y %r'"; |
126 | |
} |
127 | 0 | return newString; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public String escapeString(String sqlString) { |
136 | 0 | return (sqlString != null) ? BSLASH_PAT.matcher(APOS_PAT.matcher(sqlString).replaceAll("''")).replaceAll(Matcher.quoteReplacement("\\\\")) : null; |
137 | |
} |
138 | |
} |