package database.crud.denormalised;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import database.Crud;
import database.dataview.view_facture;
import database.dataview.view_groupe;
import database.tools.DbConnection;

public class ViewGroupeCrud implements Crud<view_groupe> {
	private Connection connection;
	private Statement statement;

	/**
	 * @param connection
	 * @throws SQLException 
	 */
	public ViewGroupeCrud(Connection connection) throws SQLException {
		super();
		this.connection = connection;
		this.statement = this.connection.createStatement();
	}

	@Override
	public int create(view_groupe dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public view_groupe readById(int id) throws SQLException {
		String query = "SELECT * FROM view_groupe WHERE groupe_p_id='"+id+"'";
		ResultSet rs = statement.executeQuery(query);
		rs.next();
		
		//Construire le dataset � partir du resultset
		return new view_groupe(rs.getInt("client_p_id"), 
								rs.getString("client_nom"), 
								rs.getString("client_prenom"), 
								rs.getString("client_commentaire"), 
								rs.getInt("adresse_p_id"), 
								rs.getString("adresse_no_rue_app"), 
								rs.getString("adresse_codepostal"), 
								rs.getInt("ville_p_id"), 
								rs.getString("ville_nom"), 
								rs.getString("province"), 
								rs.getString("pays"), 
								rs.getInt("groupe_p_id"), 
								rs.getString("groupe_nom")); 
	}

	@Override
	public List<view_groupe> readAll() throws SQLException {
		List<view_groupe> lst = new ArrayList<>();
		
		String query = "SELECT * FROM view_groupe LIMIT 0, 100";
		ResultSet rs = statement.executeQuery(query);
		
		while(rs.next()){
		    //Construire le dataset � partir du resultset
			view_groupe tmp = new view_groupe(rs.getInt("client_p_id"), 
					rs.getString("client_nom"), 
					rs.getString("client_prenom"), 
					rs.getString("client_commentaire"), 
					rs.getInt("adresse_p_id"), 
					rs.getString("adresse_no_rue_app"), 
					rs.getString("adresse_codepostal"), 
					rs.getInt("ville_p_id"), 
					rs.getString("ville_nom"), 
					rs.getString("province"), 
					rs.getString("pays"), 
					rs.getInt("groupe_p_id"), 
					rs.getString("groupe_nom"));
			lst.add(tmp);
		}
		
		return lst;
	}

	@Override
	public boolean update(view_groupe dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean destroy(view_groupe dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public int getNewSequenceId() throws SQLException {
		throw new UnsupportedOperationException();
	}
	public static void main(String ... arg) throws ClassNotFoundException, IOException, SQLException{
		Connection connection = DbConnection.createConnection();
		ViewGroupeCrud crud = new ViewGroupeCrud(connection);
		for (view_groupe v : crud.readAll())
			System.out.println(v.toString());
	}
}
