| 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.BigDecimal; |
| 20 | |
import java.sql.Connection; |
| 21 | |
import java.sql.PreparedStatement; |
| 22 | |
import java.sql.ResultSet; |
| 23 | |
import java.sql.SQLException; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.regex.Pattern; |
| 26 | |
|
| 27 | |
import javax.persistence.EntityManager; |
| 28 | |
|
| 29 | |
import org.apache.ojb.broker.PersistenceBroker; |
| 30 | |
import org.apache.ojb.broker.accesslayer.LookupException; |
| 31 | |
import org.apache.ojb.broker.query.Criteria; |
| 32 | |
import org.kuali.rice.core.config.ConfigContext; |
| 33 | |
import org.kuali.rice.core.util.RiceConstants; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class OracleDatabasePlatform extends ANSISqlDatabasePlatform { |
| 40 | |
|
| 41 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OracleDatabasePlatform.class); |
| 42 | |
private static final long DEFAULT_TIMEOUT_SECONDS = 60 * 60; |
| 43 | |
public static final long WAIT_FOREVER = -1; |
| 44 | |
|
| 45 | 0 | private static final Pattern APOS_PAT = Pattern.compile("'"); |
| 46 | |
|
| 47 | |
public void applyLimit(Integer limit, Criteria criteria) { |
| 48 | 0 | if (limit != null) { |
| 49 | 0 | criteria.addSql("rownum <= " + limit.intValue()); |
| 50 | |
} |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
public String getStrToDateFunction() { |
| 54 | 0 | return "TO_DATE"; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public String getCurTimeFunction() { |
| 58 | 0 | return "sysdate"; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public String getDateFormatString(String dateFormatString) { |
| 62 | 0 | return "'" + dateFormatString + "'"; |
| 63 | |
} |
| 64 | |
|
| 65 | |
@SuppressWarnings("unchecked") |
| 66 | |
public Long getNextValSQL(String sequenceName, EntityManager entityManager) { |
| 67 | 0 | List resultList = entityManager.createNativeQuery("select " + sequenceName + ".nextval from dual").getResultList(); |
| 68 | 0 | return new Long(((BigDecimal)resultList.get(0)).longValue()); |
| 69 | |
|
| 70 | |
} |
| 71 | |
|
| 72 | |
public Long getNextValSQL(String sequenceName, PersistenceBroker persistenceBroker) { |
| 73 | 0 | PreparedStatement statement = null; |
| 74 | 0 | ResultSet resultSet = null; |
| 75 | |
try { |
| 76 | 0 | Connection connection = persistenceBroker.serviceConnectionManager().getConnection(); |
| 77 | 0 | statement = connection.prepareStatement("select " + sequenceName + ".nextval from dual"); |
| 78 | 0 | resultSet = statement.executeQuery(); |
| 79 | |
|
| 80 | 0 | if (!resultSet.next()) { |
| 81 | 0 | throw new RuntimeException("Error retrieving next option id for action list from sequence."); |
| 82 | |
} |
| 83 | 0 | return new Long(resultSet.getLong(1)); |
| 84 | 0 | } catch (SQLException e) { |
| 85 | 0 | throw new RuntimeException("Error retrieving next option id for action list from sequence.", e); |
| 86 | 0 | } catch (LookupException e) { |
| 87 | 0 | throw new RuntimeException("Error retrieving next option id for action list from sequence.", e); |
| 88 | |
} finally { |
| 89 | 0 | if (statement != null) { |
| 90 | |
try { |
| 91 | 0 | statement.close(); |
| 92 | 0 | } catch (SQLException e) { |
| 93 | 0 | } |
| 94 | |
} |
| 95 | 0 | if (resultSet != null) { |
| 96 | |
try { |
| 97 | 0 | resultSet.close(); |
| 98 | 0 | } catch (SQLException e) { |
| 99 | 0 | } |
| 100 | |
} |
| 101 | |
} |
| 102 | |
} |
| 103 | |
|
| 104 | |
public String getLockRouteHeaderQuerySQL(Long routeHeaderId, boolean wait) { |
| 105 | 0 | long timeoutSeconds = getTimeoutSeconds(); |
| 106 | 0 | String waitClause = ""; |
| 107 | 0 | if (!wait) { |
| 108 | 0 | waitClause = " NOWAIT"; |
| 109 | 0 | } else if (wait && timeoutSeconds > 0) { |
| 110 | 0 | waitClause = " WAIT " + timeoutSeconds; |
| 111 | |
} |
| 112 | 0 | return "SELECT DOC_HDR_ID FROM KREW_DOC_HDR_T WHERE DOC_HDR_ID=? FOR UPDATE" + waitClause; |
| 113 | |
} |
| 114 | |
|
| 115 | |
public String toString() { |
| 116 | 0 | return "[OracleDatabasePlatform]"; |
| 117 | |
} |
| 118 | |
|
| 119 | |
protected long getTimeoutSeconds() { |
| 120 | 0 | String timeoutValue = ConfigContext.getCurrentContextConfig().getDocumentLockTimeout(); |
| 121 | 0 | if (timeoutValue != null) { |
| 122 | |
try { |
| 123 | 0 | return Long.parseLong(timeoutValue); |
| 124 | 0 | } catch (NumberFormatException e) { |
| 125 | 0 | LOG.warn("Failed to parse document lock timeout as it was not a valid number: " + timeoutValue); |
| 126 | |
} |
| 127 | |
} |
| 128 | 0 | return DEFAULT_TIMEOUT_SECONDS; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public String getSelectForUpdateSuffix(long waitMillis) { |
| 135 | 0 | String sql = "for update"; |
| 136 | 0 | if (WAIT_FOREVER == waitMillis) { |
| 137 | |
|
| 138 | 0 | LOG.warn("Selecting for update and waiting forever..."); |
| 139 | 0 | } else if (RiceConstants.NO_WAIT == waitMillis) { |
| 140 | 0 | sql += " nowait"; |
| 141 | |
} else { |
| 142 | |
|
| 143 | 0 | long seconds = waitMillis / 1000; |
| 144 | 0 | if (seconds == 0) seconds = 1; |
| 145 | 0 | sql += " wait " + seconds; |
| 146 | |
} |
| 147 | 0 | return sql; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public String escapeString(String sqlString) { |
| 156 | 0 | return (sqlString != null) ? APOS_PAT.matcher(sqlString).replaceAll("''") : null; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
@Override |
| 166 | |
public String getDateSQL(String date, String time) { |
| 167 | 0 | String d = date.replace('/', '-'); |
| 168 | 0 | if (time == null) { |
| 169 | 0 | return new StringBuilder("{d '").append(d).append("'}").toString(); |
| 170 | |
} else { |
| 171 | 0 | return new StringBuilder(getStrToDateFunction()).append("('").append(d).append(" ").append( |
| 172 | |
time).append("', 'YYYY-MM-DD HH24:MI:SS')").toString(); |
| 173 | |
} |
| 174 | |
} |
| 175 | |
} |