/*
 * AbstractTextBinaryTypeDriver.java
 * 
 * (C) 28.02.2005 Markus Schaber, Logi-Track ag, CH 8001 Zuerich
 * 
 * $Id: $
 */
package org.postgresql.types;

import org.postgresql.PGConnection;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

/** Should serve as a convenience base class for most TypeDrivers */
public abstract class AbstractTextTypeDriver implements TypeTextReader, TypeTextWriter, TypeDriver {
    public static final Class[] EMPTY_CLASSES = new Class[0];
    public static final String[] EMPTY_TYPES = new String[0];
    
    protected Class[] supportedClasses = EMPTY_CLASSES;
    protected String[] supportedTypes = EMPTY_TYPES;
    
    // *** TypeDriver methods ***
    public Class[] getSupportedClasses() {
        return supportedClasses;
    }

    public String[] getSupportedTypes() {
        return supportedTypes;
    }

    public boolean supports(String type) {
        for (int i = 0; i < supportedTypes.length; i++) {
            if (supportedTypes[i].equals(type)) {
                return true;
            }
        }
        return false;
    }

    public boolean supports(Class klass) {
        for (int i = 0; i < supportedClasses.length; i++) {
            if (supportedClasses[i].isAssignableFrom(klass)) {
                return true;
            }
        }
        return false;
    }

    public abstract String sqlType(Object data) throws SQLException;

    public boolean supportsBinaryWrite() {
        return false;
    }

    public boolean supportsTextWrite() {
        return true;
    }

    public boolean supportsBinaryReading() {
        return false;
    }

    public boolean supportsTextReading() {
        return true;
    }

    public TypeDriver forConnection(PGConnection conn) {
        return this;
    }

    // *** TypeTextReader Methods

    public String getString(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public boolean getBoolean(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public byte getByte(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public short getShort(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public int getInt(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public long getLong(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public float getFloat(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public double getDouble(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public BigDecimal getBigDecimal(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public byte[] getBytes(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Date getDate(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Date getDate(String data, Calendar cal) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Time getTime(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Time getTime(String data, Calendar cal) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Timestamp getTimestamp(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Timestamp getTimestamp(String data, Calendar cal) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public InputStream getAsciiStream(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public InputStream getUnicodeStream(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public InputStream getBinaryStream(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Object getObject(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public Reader getCharacterStream(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    public URL getURL(String data) throws SQLException {
        throw new SQLException("Unknown type!");
    }

    // *** TypeTextWriter Methods ***
    public abstract int objectCharLength(Object data) throws SQLException;

    public abstract String renderObject(Object data);

    public void renderObject(Object data, StringBuffer sb) {
        sb.append(renderObject(data));
    }

    public void renderObject(Object data, char[] target, int startindex) {
        String rep = renderObject(data);
        for (int i = 0; i < rep.length(); i++) {
            target[i + startindex] = rep.charAt(i);
        }
    }

    public void renderObject(Object data, Writer target) throws IOException {
        target.write(renderObject(data));
    }
}
