package kfs.kfsDbi;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author pavedrim
 */
public class kfsBlob extends kfsColObject {

    private final kfsDbServerType serverType;

    public kfsBlob(final String name, final String label, final int position, final kfsDbServerType serverType) {
        super(name, label, position, true);
        this.serverType = serverType;
    }

    @Override
    public String getColumnCreateTable(kfsDbServerType serverType) {
        if (serverType == kfsDbServerType.kfsDbiMysql) {
            return getColumnName() + " MEDIUMBLOB";
        }
        if (serverType == kfsDbServerType.kfsDbiPostgre) {
            return getColumnName() + " bytea ";
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Class<?> getColumnJavaClass() {
        return kfsBlobData.class;
    }

    @Override
    public Object getObject(kfsRowData row) {
        return (kfsBlobData) super.getObject(row);
    }

    public kfsBlobData getData(kfsRowData row) {
        return (kfsBlobData) super.getObject(row);
    }

    public void setData(byte[] d, kfsRowData r) {
        super.setObject(new kfsBlobData(new ByteArrayInputStream(d), d.length), r);
    }

    public void setData(kfsBlobData d, kfsRowData r) {
        super.setObject(d, r);
    }
    
    public void setData(InputStream value, int size, kfsRowData r) { 
        this.setObject(new kfsBlobData(value, size), r);
    }

    @Override
    public void setParam(int inx, PreparedStatement ps, kfsRowData row) throws SQLException {
        kfsBlobData data = getData(row);
        if (data.getInputStream() != null) {
            ps.setBinaryStream(inx, data.getInputStream());
        } else {
            ps.setNull(inx, java.sql.Types.BLOB);
        }
    }

    @Override
    public void getParam(int inx, ResultSet ps, kfsRowData row) throws SQLException {
        if (serverType == kfsDbServerType.kfsDbiOracle) {
            super.setObject(new kfsBlobData(ps.getBinaryStream(inx), -1), row);
        } else {
            Blob bl = ps.getBlob(inx);
            super.setObject(new kfsBlobData(bl.getBinaryStream(), bl.length()), row);
        }
    }


    /*
     * public void setData(Object value) { if (value instanceof String) { setData((String) value); }
     * else if (value instanceof InputStream) { setData((InputStream) value); } else { throw new
     * UnsupportedOperationException("Not supported yet. SetData(" + // value.getClass().getName() +
     * ")"); } }
     *
     * public void setData(String value) { this.ind = new ByteArrayInputStream(value.getBytes());
     * this.length = value.length(); }
     *
     * public String getStreamAsString(String encoding) throws IOException { if (this.ind != null) {
     * StringWriter writer = new StringWriter(); IOUtils.copy(this.ind, writer, encoding); return
     * writer.toString(); } return ""; }
     *
     * public byte[] getStreamAsByteArr() throws IOException { if (this.ind != null) { return
     * IOUtils.toByteArray(this.ind); } return new byte[0]; }
     *
     * public void toOutputStream(OutputStream out) throws IOException { if (this.ind != null) {
     * IOUtils.copy(this.ind, out); } }
     */
}

@@ -1473,20 +1473,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -1542,17 +1542,16 @@
 ata r) %7B
- 
 %0A       
@@ -2346,1036 +2346,6 @@
  %7D%0A%0A
-%0A    /*%0A     * public void setData(Object value) %7B if (value instanceof String) %7B setData((String) value); %7D%0A     * else if (value instanceof InputStream) %7B setData((InputStream) value); %7D else %7B throw new%0A     * UnsupportedOperationException(%22Not supported yet. SetData(%22 + // value.getClass().getName() +%0A     * %22)%22); %7D %7D%0A     *%0A     * public void setData(String value) %7B this.ind = new ByteArrayInputStream(value.getBytes());%0A     * this.length = value.length(); %7D%0A     *%0A     * public String getStreamAsString(String encoding) throws IOException %7B if (this.ind != null) %7B%0A     * StringWriter writer = new StringWriter(); IOUtils.copy(this.ind, writer, encoding); return%0A     * writer.toString(); %7D return %22%22; %7D%0A     *%0A     * public byte%5B%5D getStreamAsByteArr() throws IOException %7B if (this.ind != null) %7B return%0A     * IOUtils.toByteArray(this.ind); %7D return new byte%5B0%5D; %7D%0A     *%0A     * public void toOutputStream(OutputStream out) throws IOException %7B if (this.ind != null) %7B%0A     * IOUtils.copy(this.ind, out); %7D %7D%0A     */%0A
 %7D%0A

remove old code