package com.quantum.adapters;

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

import com.quantum.Messages;
import com.quantum.util.QuantumUtil;



public class OracleAdapter extends DatabaseAdapter {
	protected OracleAdapter() {
		super(AdapterFactory.ORACLE);
	}
	public String getShowSequenceQuery(String qualifier) {
        return "SELECT SEQUENCE_OWNER, SEQUENCE_NAME FROM ALL_SEQUENCES WHERE SEQUENCE_OWNER = '" + qualifier + "'"; //$NON-NLS-1$
	}
	public String getPrevValue(String sequence, String owner) {
    	return "SELECT " + getQualifiedName(owner, sequence) + ".CURRVAL FROM DUAL";
	}
	public String getNextValue(String sequence, String owner) {
		return "SELECT " + getQualifiedName(owner, sequence) + ".NEXTVAL FROM DUAL";
	}
	public String getCommentsQuery(String tableName, String column) {
		String query = "SELECT COMMENTS FROM ALL_COL_COMMENTS WHERE TABLE_NAME = '"; 
		query += QuantumUtil.getTableName(tableName) + "' AND COLUMN_NAME = '" + column + "'" ;
		if (!(QuantumUtil.getSchemaName(tableName).equals("")))
			query += " AND OWNER = '" + QuantumUtil.getSchemaName(tableName) + "'";
		return query;
	}
	/**
	 * 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) {
		if (type == java.sql.Types.DATE || type == java.sql.Types.TIMESTAMP) {
			string = string.trim();
			// Eliminate the fractions of seconds, if present
			if (string.length() > 1) {
				// If the third character from the end is a dot, it means it has fractions
				String sub = string.substring(string.length()-2, string.length()-1);
				if ( sub.equals(Messages.getString("."))) //$NON-NLS-1$
					string = string.substring(0,string.length()-2);
			}
			return "TO_DATE('" + string + "','yyyy-mm-dd hh24:mi:ss')"; //$NON-NLS-1$ //$NON-NLS-2$
		}
		// use the default (upper type)
		return super.quote(string, type, typeString);
	}

	/* (non-Javadoc)
	 * @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
		if (tableName.equals(tableName.toUpperCase())) return tableName;
		// We quote the table name (and only 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) + "\"";
}

    /**
     * The default schema for Oracle is the upper-case userid.
     * @see com.quantum.adapters.DatabaseAdapter#getDefaultSchema(java.lang.String)
     */
    public String getDefaultSchema(String userid) {
        return super.getDefaultSchema(userid).toUpperCase();
    }
    
    public Map getDefaultConnectionParameters() {
    	Map map = new HashMap();
    	map.put("port", "1521");
    	map.put("hostname", "localhost");
    	return map;
    }
    

	/* (non-Javadoc)
	 * @see com.quantum.adapters.DatabaseAdapter#getShowSynonymsQuery(java.lang.String, java.lang.String)
	 */
	public String getShowSynonymsQuery(String schema, String type) {
		// The type string is the same as the one needed by Oracle. If it changes a switch would be needed.
		return "select SYNONYM_NAME from ALL_SYNONYMS, ALL_OBJECTS where " +
				" ALL_SYNONYMS.OWNER = '" + schema + "'" +
				" and ALL_SYNONYMS.TABLE_OWNER = ALL_OBJECTS.OWNER" +
				" and ALL_SYNONYMS.TABLE_NAME = ALL_OBJECTS.OBJECT_NAME" + 
				" and ALL_OBJECTS.OBJECT_TYPE = '" + type + "'" ;
		}
}