/*
 *
 *  Copyright 2018 FJN Corp.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  Author                        Date                       Issue
 *  fs1194361820@163.com          2015-01-01                 Initial Version
 *
 */

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package com.fjn.helper.common.sql.dialect.dialect;

import java.sql.Types;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * An SQL dialect for Oracle, compatible with Oracle 8.
 *
 * @deprecated Use Oracle8iDialect instead.
 * @author Gavin King
 */
@SuppressWarnings("unused")
public class OracleDialect extends Oracle9Dialect {

    private static final Logger LOG = LoggerFactory.getLogger(
            OracleDialect.class
    );

    /**
     * Constructs a (DEPRECATED) Oracle9Dialect
     */
    public OracleDialect() {
        super();
    }


    @Override
    public String getLimitString(String sql, boolean hasOffset) {

        sql = sql.trim();
        boolean isForUpdate = false;
        if ( sql.toLowerCase(Locale.ROOT).endsWith( " for update" ) ) {
            sql = sql.substring( 0, sql.length()-11 );
            isForUpdate = true;
        }

        final StringBuilder pagingSelect = new StringBuilder( sql.length()+100 );
        if (hasOffset) {
            pagingSelect.append( "select * from ( select row_.*, rownum rownum_ from ( " );
        } else {
            pagingSelect.append( "select * from ( " );
        }
        pagingSelect.append( sql );
        if (hasOffset) {
            pagingSelect.append( " ) row_ ) where rownum_ <= ? and rownum_ > ?" );
        } else {
            pagingSelect.append( " ) where rownum <= ?" );
        }

        if ( isForUpdate ) {
            pagingSelect.append( " for update" );
        }

        return pagingSelect.toString();
    }

    @Override
    public String getSelectClauseNullString(int sqlType) {
        switch(sqlType) {
            case Types.VARCHAR:
            case Types.CHAR:
                return "to_char(null)";
            case Types.DATE:
            case Types.TIMESTAMP:
            case Types.TIME:
                return "to_date(null)";
            default:
                return "to_number(null)";
        }
    }

    @Override
    public String getCurrentTimestampSelectString() {
        return "select sysdate from dual";
    }

    @Override
    public String getCurrentTimestampSQLFunctionName() {
        return "sysdate";
    }
}
