001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.core.framework.persistence.platform;
017
018import org.apache.ojb.broker.PersistenceBroker;
019
020import java.sql.Connection;
021
022public abstract class ANSISqlDatabasePlatform implements DatabasePlatform
023{
024    /**
025     * @see DatabasePlatform#getTruncateTableSql(String)
026     */
027    public String getTruncateTableSql(String tableName) 
028    {
029        return "truncate table " + tableName;
030    }
031    
032    /**
033     * @see DatabasePlatform#getCreateTableFromTableSql(String, String)
034     */
035    public String getCreateTableFromTableSql(String tableToCreate, String fromTable) 
036     {
037        return new StringBuffer("create table ").append(tableToCreate)
038                .append(" as select * from ").append(fromTable).toString();
039    }
040    
041    /**
042     * @see DatabasePlatform#getInsertDataFromTableSql(String, String)
043     */
044    public String getInsertDataFromTableSql(String restoreTableName, String fromTableName) 
045    {
046        return new StringBuffer("insert into ").append(restoreTableName)
047                .append(" select * from ").append(fromTableName).toString();
048    }
049    
050    /**
051     * @see DatabasePlatform#getDropTableSql(String)
052     */
053    public String getDropTableSql(String tableName) {
054        return new StringBuffer("drop table ").append(tableName).toString();
055    }
056    
057    /**
058     * Returns an expression equivalent to oracle's NVL statement using the CASE and IS NULL expressions, which should
059     * be supported by many database systems
060     * 
061     * @see DatabasePlatform#getIsNullFunction(java.lang.String, java.lang.String)
062     */
063    public String getIsNullFunction(String exprToTest, String exprToReplaceIfTestExprNull) {
064        return new StringBuilder(" case when ").append(exprToTest).append(" is null then ").append(exprToReplaceIfTestExprNull)
065        .append(" else ").append(exprToTest).append(" end ").toString();
066    }
067    
068    public String getDateSQL(String date, String time) {
069        // SQL 92 date literal syntax:
070        // http://www.stanford.du/dept/itss/docs/oracle/9i/java.920/a96654/ref.htm#1005145
071        String d = date.replace('/', '-');
072        if (time == null) {
073            return new StringBuilder("{d '").append(d).append("'}").toString();    
074        } else {
075            return new StringBuilder("{ts '").append(d).append(" ").append(time).append("'}").toString(); 
076        }
077    }
078    
079    /**
080     * @see DatabasePlatform#getUpperCaseFunction()
081     * @return the String "UPPER"
082     */
083    //chb: this was copied over from the legacy code, but is it really necessary?
084    public String getUpperCaseFunction() {
085        return "UPPER";
086    }
087
088    public String toString() {
089        return "[ANSISqlDatabasePlatform]";
090    }
091
092    @Override
093    public Long getNextValSQL(String sequenceName, Object nextValSource) {
094        if (nextValSource instanceof Connection) {
095            return getNextValSqlJdbc(sequenceName, (Connection)nextValSource);
096        } else if (nextValSource instanceof PersistenceBroker) {
097            return getNextValSqlOjb(sequenceName, (PersistenceBroker)nextValSource);
098        }
099        throw new IllegalArgumentException("No next value strategy found for given nextValSource: " + nextValSource);
100    }
101
102    @Deprecated
103    protected abstract Long getNextValSqlOjb(String sequenceName, PersistenceBroker persistenceBroker);
104
105    @Deprecated
106    protected abstract Long getNextValSqlJdbc(String sequenceName, Connection connection);
107
108}