Coverage Report - org.kuali.rice.core.database.platform.OracleDatabasePlatform
 
Classes in this File Line Coverage Branch Coverage Complexity
OracleDatabasePlatform
0%
0/59
0%
0/26
3
 
 1  
 /*
 2  
  * Copyright 2005-2009 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * DatabasePlatform implementation that generates Oracle-compliant SQL
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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; // default to 1 hour
 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  
 //        return new Long(((BigDecimal) entityManager.createNativeQuery("select " + sequenceName + ".nextval from dual").getSingleResult()).longValue());
 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  
      * @see org.kuali.rice.ken.DatabasePlatform.Platform#getSelectForUpdateSuffix(long)
 133  
      */
 134  
     public String getSelectForUpdateSuffix(long waitMillis) {
 135  0
         String sql = "for update";
 136  0
         if (WAIT_FOREVER == waitMillis) {
 137  
             // do nothing
 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  
             // Oracle only supports wait time in seconds...
 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  
      * Performs Oracle-specific escaping of String parameters.
 152  
      * 
 153  
      * @see org.kuali.rice.core.database.platform.DatabasePlatform#escapeString(java.lang.String)
 154  
      */
 155  
     public String escapeString(String sqlString) {
 156  0
             return (sqlString != null) ? APOS_PAT.matcher(sqlString).replaceAll("''") : null;
 157  
     }
 158  
 
 159  
     /**
 160  
      * Converts date-only values into JDBC d{...} date literals, but converts date-and-time values into timestamp
 161  
      * strings that are then converted into date values via the strToDateFunction.
 162  
      * 
 163  
      * @see org.kuali.rice.core.database.platform.ANSISqlDatabasePlatform#getDateSQL(java.lang.String, java.lang.String)
 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  
 }