package com.quantum.adapters;

import java.util.HashMap;
import java.util.Map;

import com.quantum.util.QuantumUtil;
import com.quantum.util.sql.TypesHelper;


public class PostgresAdapter extends DatabaseAdapter {
	protected PostgresAdapter() {
		super(AdapterFactory.POSTGRES);
	}
	public String getShowTableQuery(String qualifier) {
        return "SELECT SCHEMANAME, TABLENAME FROM PG_TABLES WHERE SCHEMANAME = '" 
            + qualifier + "'";
    }
    public String getShowViewQuery(String qualifier) {
        return "SELECT SCHEMANAME, VIEWNAME FROM PG_VIEWS WHERE SCHEMANAME = '" 
            + qualifier + "'";
    }
    public String getShowSequenceQuery(String qualifier) {
    	return "select pg_namespace.nspname, relname " +
    			"from pg_class, pg_namespace where relkind = 'S' " +
    			"and relnamespace = pg_namespace.oid " +
    			"and pg_namespace.nspname = '" + qualifier + "'";
    }
	public String getNextValue(String sequence, String owner) {
		return "SELECT NEXTVAL('" + getQualifiedName(owner, sequence) + "')";
	}
	public String getPrevValue(String sequence, String owner) {
		return "SELECT * FROM " + getQualifiedName(owner, sequence);
	}

	/**
	 * Quotes a string according to the type of the column 
	 * @param string to be quoted
	 * @param type according to java.sql.Types
	 * @return
	 */
	public String quote(String string, int type, String typeString) {
		// Booleans in PostgreSQL are queried "t" or "f", but require "true" or "false" when inputed.
		if (type == TypesHelper.BIT || type == TypesHelper.BOOLEAN ) // Postgresql seems to identify booleans as BITs
		{
			if (string.indexOf('t') >= 0 || string.indexOf('T') >= 0 )
				return "true";
			else if (string.indexOf('f') >= 0 || string.indexOf('F') >= 0 )
				return "false";
			else 
				return string;
		}
		// use the default (upper type)
		return super.quote(string, type, typeString);
	}

	public String getTableQuery(String table) {
		return "SELECT * FROM " + filterTableName(table); //$NON-NLS-1$
	}
    /**
     * @see com.quantum.adapters.DatabaseAdapter#filterTableName(java.lang.String)
     */
    public String filterTableName(String tableName) {
        // If there is no mixed case, better not quote, it's prettier on display
    	// Postgresql defaults to lower case identifiers
        if (tableName.equals(tableName.toLowerCase())) return tableName;
        // We quote the table name because it has mixed case
        if (QuantumUtil.getSchemaName(tableName).equals("")) {
            return "\"" + tableName + "\""; //$NON-NLS-1$
        } else {
            return "\"" + QuantumUtil.getSchemaName(tableName) + "\".\"" + 
                    QuantumUtil.getTableName(tableName) + "\"";
        }
    }
    /**
     * @see com.quantum.adapters.DatabaseAdapter#getDefaultSchema(java.lang.String)
     */
    public String getDefaultSchema(String userid) {
        return "public";
    }

	public Map getDefaultConnectionParameters() {
		Map map = new HashMap();
		map.put("port", "5432");
		map.put("hostname", "localhost");
		return map;
	}
}