/*
 * 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
                + " increment by " + increment + " start with " + startValue;
    }

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

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

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

    @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 "^";
    }
}

@@ -713,16 +713,30 @@
 enceName
+.toLowerCase()
 %0D%0A      
@@ -936,24 +936,38 @@
 sequenceName
+.toLowerCase()
  + %22', %22 + s
@@ -1090,32 +1090,46 @@
 %22 + sequenceName
+.toLowerCase()
  + %22')%22;%0D%0A    %7D%0D
@@ -1240,16 +1240,30 @@
 enceName
+.toLowerCase()
  + %22')%22;

set editable runtime properties frame