/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */
package com.haulmont.cuba.core.global;

/**
 * PostgreSQL dialect.
 *
 * <p>$Id$</p>
 *
 * @author krivopustov
 */
public class PostgresDbDialect extends DbDialect implements SequenceSupport {

    public String sequenceExistsSql(String sequenceName) {
        return "select relname from pg_class where relkind = 'S' and relname = '"
                + sequenceName.toLowerCase() + "'";
    }

    public String createSequenceSql(String sequenceName, long startValue, long increment) {
        return "create sequence " + sequenceName.toLowerCase()
                + " increment by " + increment + " start with " + startValue;
    }

    public String modifySequenceSql(String sequenceName, long startWith) {
        return "select setval('" + sequenceName.toLowerCase() + "', " + startWith + ")";
    }

    public String getNextValueSql(String sequenceName) {
        return "select nextval('" + sequenceName.toLowerCase() + "')";
    }

    public String getCurrentValueSql(String sequenceName) {
        return "select currval('" + sequenceName.toLowerCase() + "')";
    }

    @Override
    public String getName() {
        return "postgres";
    }

    @Override
    public String getIdColumn() {
        return "id";
    }

    @Override
    public String getDeleteTsColumn() {
        return "delete_ts";
    }

    @Override
    public String getUniqueConstraintViolationMarker() {
        return "ERROR: duplicate key value violates unique constraint";
    }

    @Override
    public String getUniqueConstraintViolationPattern() {
        return "ERROR: duplicate key value violates unique constraint \"(.+)\"";
    }

    @Override
    public String getScriptSeparator() {
        return "^";
    }
}

@@ -700,16 +700,40 @@
 ence %22 +
+ (sequenceName != null ?
  sequenc
@@ -751,16 +751,32 @@
 erCase()
+ : sequenceName)
 %0D%0A      
@@ -959,32 +959,56 @@
 lect setval('%22 +
+ (sequenceName != null ?
  sequenceName.to
@@ -1014,24 +1014,40 @@
 oLowerCase()
+ : sequenceName)
  + %22', %22 + s
@@ -1157,32 +1157,56 @@
 ect nextval('%22 +
+ (sequenceName != null ?
  sequenceName.to
@@ -1208,32 +1208,48 @@
 me.toLowerCase()
+ : sequenceName)
  + %22')%22;%0D%0A    %7D%0D
@@ -1347,16 +1347,40 @@
 val('%22 +
+ (sequenceName != null ?
  sequenc
@@ -1394,24 +1394,40 @@
 oLowerCase()
+ : sequenceName)
  + %22')%22;%0D%0A  

check on null in sequence