// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package javadb;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.RowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

public class RowSetDAL extends Demo implements DAL {
    private String constr;
    private String usr;
    private String pwd;
    public RowSetDAL(String constr, String usr, String pwd) {
        this.constr = constr;
        this.usr = usr;
        this.pwd = pwd;
    }
    private RowSet getRowSet(String sql) throws SQLException {
        RowSetFactory rsf = RowSetProvider.newFactory();
        RowSet rs = rsf.createJdbcRowSet();
        rs.setUrl(constr);
        rs.setUsername(usr);
        rs.setPassword(pwd);
        rs.setCommand(sql);
        return rs;
    }
    @Override
    public T1 getOneByPK(int f1) throws SQLException {
        T1 res;
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1 WHERE f1 = ?")) {
            rs.setInt(1, f1);
            rs.execute();
            if(rs.next()) {
                res = new T1(rs.getInt("f1"), rs.getString("f2"));
            } else {
                res = null;
            }
        }
        return res;
    }
    @Override
    public List<T1> getAll() throws SQLException {
        List<T1> res = new ArrayList<T1>();
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1")) {
            rs.execute();
            while(rs.next()) {
                res.add(new T1(rs.getInt("f1"), rs.getString("f2")));
            }
        }
        return res;
    }
    @Override
    public List<T1> getRangeOfPK(int f1_start, int f1_end) throws SQLException {
        List<T1> res = new ArrayList<T1>();
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1 WHERE f1 BETWEEN ? AND ?")) {
            rs.setInt(1, f1_start);
            rs.setInt(2, f1_end);
            rs.execute();
            while(rs.next()) {
                res.add(new T1(rs.getInt(1), rs.getString(2)));
            }
        }
        return res;
    }
    @Override
    public List<T1> getContainingF2(String f2) throws SQLException {
        List<T1> res = new ArrayList<T1>();
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1 WHERE f2 LIKE ?")) {
            rs.setString(1, "%" + f2 + "%");
            rs.execute();
            while(rs.next()) {
                res.add(new T1(rs.getInt(1), rs.getString(2)));
            }
        }
        return res;
    }
    @Override
    public void saveChanges(T1 o) throws SQLException {
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1 WHERE f1 = ?")) {
            rs.setInt(1, o.getF1());
            rs.execute();
            rs.last();
            rs.updateInt("f1", o.getF1());
            rs.updateString("f2", o.getF2());
            rs.updateRow();
        }
    }
    @Override
    public void saveNew(T1 o) throws SQLException {
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1")) {
            rs.execute();
            rs.moveToInsertRow();
            rs.updateInt("f1", o.getF1());
            rs.updateString("f2", o.getF2());
            rs.insertRow();;
        }
    }
    @Override
    public void remove(int f1) throws SQLException {
        try(RowSet rs = getRowSet("SELECT f1,f2 FROM t1 WHERE f1 = ?")) {
            rs.setInt(1, f1);
            rs.execute();
            rs.last();
            rs.deleteRow();
        }
    }
    public static void main(String[] args) throws SQLException {
        test("JDBC RowSet - MySQL", new RowSetDAL("jdbc:mysql://localhost/Test", "root", ""));
        test("JDBC RowSet - Oracle", new RowSetDAL("jdbc:oracle:thin:@localhost:1521:xe", "arne", "xxxxxx"));
        test("JDBC RowSet - DB2", new RowSetDAL("jdbc:db2://localhost:50000/Test", "arne", "xxxxxx"));
    }
}
