/*
 * Copyright (C) 2004-2006  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope;


import java.sql.SQLException;

import org.postgresql.Driver;

import com.exedio.dsmf.PostgresqlDriver;


/**
 * Still does not work.
 */
final class PostgresqlDatabase extends Database
{
	static
	{
		try
		{
			Class.forName(Driver.class.getName());
		}
		catch(ClassNotFoundException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	protected PostgresqlDatabase(final Properties properties)
	{
		super(new PostgresqlDriver(), properties);
	}
	
	@Override
	public String getIntegerType(final long minimum, final long maximum)
	{
		return "INTEGER";
	}

	@Override
	public String getDoubleType()
	{
		return "DOUBLE PRECISION";
	}

	@Override
	public String getStringType(final int maxLength)
	{
		return "VARCHAR("+maxLength+')';
	}
	
	@Override
	public String getDayType()
	{
		return "DATE";
	}
	
	// TODO check if there is a suitable column type
	@Override
	public String getDateTimestampType()
	{
		return null;
	}
	
	// TODO check if there is a suitable column type
	@Override
	public String getBlobType(final long maximumLength)
	{
		return null;
	}
	
	@Override
	LimitSupport getLimitSupport()
	{
		// TODO support limit clause
		return LimitSupport.NONE;
	}

	@Override
	void appendLimitClause(final Statement bf, final int start, final int count)
	{
		throw new RuntimeException();
	}
	
	@Override
	void appendLimitClause2(final Statement bf, final int start, final int count)
	{
		throw new RuntimeException();
	}
	
	@Override
	protected void appendMatchClauseFullTextIndex(final Statement bf, final StringFunction function, final String value)
	{
		// TODO check for full text indexes
		appendMatchClauseByLike(bf, function, value);
	}
	
	private String extractConstraintName(final SQLException e, final int vendorCode, final String start, final String end)
	{
		if(e.getErrorCode()!=vendorCode)
			return null;
		
		final String m = e.getMessage();
		if(m.startsWith(start) && m.endsWith(end))
		{
			final int pos = m.indexOf('.', start.length());
			return m.substring(pos+1, m.length()-end.length());
		}
		else
			return null;
	}
	
	@Override
	protected String extractUniqueConstraintName(final SQLException e)
	{
		return extractConstraintName(e, 1, "ORA-00001: unique constraint (", ") violated\n");
	}

}

@@ -1368,18 +1368,108 @@
 urn 
-%22INTEGER%22;
+(minimum%3E=Integer.MIN_VALUE && maximum%3C=Integer.MAX_VALUE) ? %22INTEGER%22 : %22BIGINT%22; // TODO: smallint
 %0A%09%7D%0A
@@ -1624,33 +1624,105 @@
 urn 
-%22VARCHAR(%22+maxLength+')';
+(maxLength%3E10485760) ? %22TEXT%22 : %22VARCHAR(%22+maxLength+')'; // in postgres varchar cannot be longer
 %0A%09%7D%0A
@@ -2900,88 +2900,42 @@
 )%0A%09%09
-%7B%0A%09%09%09final int pos = m.indexOf('.', start.length());%0A%09%09%09return m.substring(pos+1
+%09return m.substring(start.length()
 , m.
@@ -2958,20 +2958,16 @@
 gth());%0A
-%09%09%7D%0A
 %09%09else%0A%09
@@ -3106,31 +3106,65 @@
 (e, 
-1, %22ORA-00001: u
+0, %22FEHLER: duplizierter Schl%5Cu00fcssel verletzt U
 nique
- c
+-C
 onst
@@ -3173,28 +3173,64 @@
 int 
-(
 %22, %22
-) violated%5Cn%22);
+%5Cn%22); // TODO checks against localized error message
 %0A%09%7D%0A

fix a bit for postgres, still not working