package cz.hsrs.db.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import cz.hsrs.db.DBObject;
import cz.hsrs.db.pool.SQLExecutor;

public class DBUtil {

	protected SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
	protected SQLExecutor stmt = SQLExecutor.getInstance();

	protected String toDateString(Date date){	
		return "TIMESTAMP '"+format.format(date)+"'";
	}
	
	protected List<? extends DBObject> generateObjectList(DBObject element, ResultSet res) {
		try {
			List<DBObject> result = new LinkedList<>();
			while (res.next()) {
				DBObject dbob = (element.getClass().newInstance()).getDBObject(res);		
				result.add(dbob);
			}
			return result;
		} catch (SQLException | InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
}