001/**
002 * Copyright 2005-2016 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;
019import org.apache.ojb.broker.accesslayer.LookupException;
020
021import java.sql.Connection;
022import java.sql.PreparedStatement;
023import java.sql.ResultSet;
024import java.sql.SQLException;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028public class MySQLDatabasePlatform extends ANSISqlDatabasePlatform {
029
030        private static final Pattern APOS_PAT = Pattern.compile("'");
031        private static final Pattern BSLASH_PAT = Pattern.compile(Matcher.quoteReplacement("\\"));
032        
033    public String getLockRouteHeaderQuerySQL(String documentId, boolean wait) {
034        return "SELECT DOC_HDR_ID FROM KREW_DOC_HDR_T WHERE DOC_HDR_ID=? FOR UPDATE";
035    }
036
037    public String getStrToDateFunction() {
038        return "STR_TO_DATE";
039    }
040
041    public String getCurTimeFunction() {
042        return "NOW()";
043    }
044
045    @Override
046    public String applyLimitSql(Integer limit) {
047        if (limit != null) {
048            return " 1 LIMIT 0," + limit.intValue(); // 1 has to be there because the criteria is ANDed
049        }
050        return null;
051    }
052
053    /**
054     * Generate next id value for the logical sequence given the JDBC Connection
055     * @param sequenceName the logical sequence name
056     * @param connection JDBC Connection to use (without closing)
057     * @return next id in sequence or RuntimeException on error
058     */
059    @Override
060    protected Long getNextValSqlJdbc(String sequenceName, Connection connection) {
061        PreparedStatement statement = null;
062        ResultSet resultSet = null;
063        try {
064            statement = connection.prepareStatement("INSERT INTO " + sequenceName + " VALUES (NULL);");
065            statement.executeUpdate();
066            statement = connection.prepareStatement("SELECT LAST_INSERT_ID()");
067            resultSet = statement.executeQuery();
068
069            if (!resultSet.next()) {
070                throw new RuntimeException("Error retrieving next option id for action list from sequence.");
071            }
072            return new Long(resultSet.getLong(1));
073        } catch (SQLException e) {
074            throw new RuntimeException("Error retrieving next option id for action list from sequence.", e);
075        } finally {
076            if (statement != null) {
077                try {
078                    statement.close();
079                } catch (SQLException e) {
080                }
081            }
082            if (resultSet != null) {
083                try {
084                    resultSet.close();
085                } catch (SQLException e) {
086                }
087            }
088        }
089    }
090
091    @Override
092    protected Long getNextValSqlOjb(String sequenceName, PersistenceBroker persistenceBroker) {
093                try {
094                        Connection connection = persistenceBroker.serviceConnectionManager().getConnection();
095            return getNextValSqlJdbc(sequenceName, connection);
096                } catch (LookupException e) {
097                        throw new RuntimeException("Error retrieving next option id for action list from sequence.", e);
098                }
099        }
100
101    public boolean isSITCacheSupported() {
102        return false;
103    }
104
105    public String toString() {
106        return "[MySQLDatabasePlatform]";
107    }
108    
109    public String getSelectForUpdateSuffix(long waitMillis) {
110        return "for update";
111    }
112    
113    public String getDateFormatString(String dateFormatString) {
114        String newString = "";
115        if ("yyyy-mm-dd".equalsIgnoreCase(dateFormatString)) {
116            newString = "'%Y-%m-%d'";
117        }
118        else if ("DD/MM/YYYY HH12:MI:SS PM".equalsIgnoreCase(dateFormatString)) {
119            newString = "'%d/%m/%Y %r'";
120        }
121        return newString;
122    }
123
124    /**
125     * Performs MySQL-specific escaping of String parameters.
126     * 
127     * @see DatabasePlatform#escapeString(java.lang.String)
128     */
129    public String escapeString(String sqlString) {
130        return (sqlString != null) ? BSLASH_PAT.matcher(APOS_PAT.matcher(sqlString).replaceAll("''")).replaceAll(Matcher.quoteReplacement("\\\\")) : null;
131    } 
132}