PostgresDatabasePolicyImpl.java :  » Database-ORM » ODAL » com » completex » objective » components » persistency » core » impl » Java Open Source

Java Open Source » Database ORM » ODAL 
ODAL » com » completex » objective » components » persistency » core » impl » PostgresDatabasePolicyImpl.java
/**
 *  Objective Database Abstraction Layer (ODAL)
 *  Copyright (c) 2004, The ODAL Development Group
 *  All rights reserved.
 *  For definition of the ODAL Development Group please refer to LICENCE.txt file
 *
 *  Distributable under LGPL license.
 *  See terms of license at gnu.org.
 */
package com.completex.objective.components.persistency.core.impl;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Gennady Krizhevsky
 */
public class PostgresDatabasePolicyImpl extends AbstractDatabasePolicy {

    public static final String DB_POSTGRES = "postgresql";
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public PostgresDatabasePolicyImpl() {
        setName(DB_POSTGRES);
    }

    public boolean supportsSubqueries() {
        return true;
    }

    public boolean supportsLoHiLimit() {
        return true;
    }

    public String getLimitSql(int offset, int rowCount) {
        return " LIMIT " + rowCount + ", " + offset + " ";
    }

    public String getLimitSql(int max) {
        return getLimitSql(max, 0);
    }

    public int getLimitLocation() {
        return LIMIT_IN_THE_END;
    }

    public String nextSequenceSql(final String objectName) {
        return new StringBuffer("SELECT nextval ('")
                .append(objectName).append("')").toString();
    }
    
    public boolean supportsSequences() {
        return true;
    }

    public String getLockString() {
        return getLockString(0, null);
    }

    public String getLockString(final long timeout, String forUpdateOf) {
        forUpdateOf = elaborateForUpdateOf(forUpdateOf);
        return " FOR UPDATE " + forUpdateOf;
    }

    public String getNoWaitLockString(String forUpdateOf) {
        return new StringBuffer(" FOR UPDATE ").append(elaborateForUpdateOf(forUpdateOf))
                .append(" NOWAIT").toString();
    }

    private String elaborateForUpdateOf(String forUpdateOf) {
        return forUpdateOf == null ? "" : " OF " + forUpdateOf;
    }

    public boolean isLocked(Exception e) {
        if (e == null || e.getMessage() == null) {
            return false;
        } else {
            return e.getMessage().indexOf("could not obtain lock") >= 0;
        }
    }

    public String connectionActiveSql() {
        return "SELECT 1 ";
    }

    public boolean useAnsiJoin() {
        return true;
    }

    public String dateSql(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        String formatted = format.format(date);
        return " timestamp '" + formatted + "' ";
    }

    public String nowSql() {
        return " now() ";
    }

    public String charLengthSql(String expression) {
        return " character_length(" + expression + ") ";
    }

    protected String rlike(String expression, String pattern, boolean matches) {
        String negation = matches ? "" : " NOT";
        return new StringBuffer(expression).append(" ")
                .append(negation).append(" SIMILAR TO ").append(pattern).toString();
    }

    protected String getPaginatedWrapperSql(String innerSql) {
        return new StringBuffer("SELECT a.* FROM (")
                .append(innerSql)
                .append(") a LIMIT ?, ?").toString();
    }

    protected Long firstArgForPaginatedWrapper(long offset, long pageSize) {
        return new Long(offset + pageSize);
    }

    protected Long secondArgForPaginatedWrapper(long offset, long pageSize) {
        return new Long(offset);
    }

    /**
     * @param e
     * @return true is this is duplicate record exception
     */
    public boolean isDuplicate(Exception e) {
        if (e == null || e.getMessage() == null) {
            return false;
        } else {
            return e.getMessage().indexOf("duplicate key") >= 0;
        }
    }

    public static final Character MULTI_SYMBOL_WILD_CARD = new Character('%');
    public static final Character SINGLE_SYMBOL_WILD_CARD = new Character('_');

    public Character getMultiSymbolWildCard() {
        return PostgresDatabasePolicyImpl.MULTI_SYMBOL_WILD_CARD;
    }

    public Character getSingleSymbolWildCard() {
        return PostgresDatabasePolicyImpl.SINGLE_SYMBOL_WILD_CARD;
    }

    public int getRefCursorJdbcType() {
        return Types.OTHER;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.