package kfs.kfsDbi;

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 ";
        }
        if (serverType == kfsDbServerType.kfsDbiSqlite) {
            return getColumnName() + " BLOB ";
        }
        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(d), r);
    }

    public void setData(kfsBlobData d, kfsRowData r) {
        super.setObject(d, r);
    }

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

    @Override
    public void setParam(int inx, PreparedStatement ps, kfsRowData row) throws SQLException {
        kfsBlobData data = getData(row);
        if (!data.isNull()) {
            if (serverType == kfsDbServerType.kfsDbiSqlite) {
                ps.setBytes(inx, data.getBytes());   
            } else if (serverType == kfsDbServerType.kfsDbiPostgre) {
                ps.setBinaryStream(inx, data.getInputStream(), data.getLength());
            } else {
                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)), row);
        } else if (serverType == kfsDbServerType.kfsDbiPostgre) {
            super.setObject(new kfsBlobData(ps.getBytes(inx)), row);
        } else {
            Blob bl = ps.getBlob(inx);
            super.setObject(new kfsBlobData(bl.getBinaryStream()), row);
        }
    }

}

@@ -2688,16 +2688,50 @@
 b(inx);%0A
+            if (bl != null) %7B%0A    
         
@@ -2787,32 +2787,135 @@
 tream()), row);%0A
+            %7D else %7B%0A                super.setObject(new kfsBlobData(new byte%5B0%5D), row);%0A            %7D%0A
         %7D%0A    %7D%0A

load empty blob