/*
 * Copyright (C) 1994-2018 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * PBS Pro is free software. You can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * For a copy of the commercial license terms and conditions,
 * go to: (http://www.pbspro.com/UserArea/agreement.html)
 * or contact the Altair Legal Department.
 *
 * Altair’s dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of PBS Pro and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair’s trademarks, including but not limited to "PBS™",
 * "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
 * trademark licensing policies.
 *
 */


/**
 * @file    db_postgres_resv.c
 *
 * @brief
 *      Implementation of the resv data access functions for postgres
 */
#include <pbs_config.h>   /* the master config generated by configure */
#include "pbs_db.h"
#include "db_postgres.h"

/**
 * @brief
 *	Prepare all the reservation related sqls. Typically called after connect
 *	and before any other sql exeuction
 *
 * @param[in]	conn - Database connection handle
 *
 * @return      Error code
 * @retval	-1 - Failure
 * @retval	 0 - Success
 *
 */
int
pg_db_prepare_resv_sqls(pbs_db_conn_t *conn)
{
	snprintf(conn->conn_sql, MAX_SQL_LENGTH, "insert into pbs.resv ("
		"ri_resvID, "
		"ri_queue, "
		"ri_state, "
		"ri_substate, "
		"ri_type, "
		"ri_stime, "
		"ri_etime, "
		"ri_duration, "
		"ri_tactive, "
		"ri_svrflags, "
		"ri_numattr, "
		"ri_resvTag, "
		"ri_un_type, "
		"ri_fromsock, "
		"ri_fromaddr, "
		"ri_savetm, "
		"ri_creattm, "
		"attributes "
		") "
		"values "
		"($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, "
		"$14, $15, localtimestamp, localtimestamp, hstore($16::text[]))");
	if (pg_prepare_stmt(conn, STMT_INSERT_RESV, conn->conn_sql, 16) != 0)
		return -1;

	snprintf(conn->conn_sql, MAX_SQL_LENGTH, "update pbs.resv set "
		"ri_queue = $2, "
		"ri_state = $3, "
		"ri_substate = $4, "
		"ri_type = $5, "
		"ri_stime = $6, "
		"ri_etime = $7, "
		"ri_duration = $8, "
		"ri_tactive = $9, "
		"ri_svrflags = $10, "
		"ri_numattr = $11, "
		"ri_resvTag = $12, "
		"ri_un_type = $13, "
		"ri_fromsock = $14, "
		"ri_fromaddr = $15, "
		"ri_savetm = localtimestamp, "
		"attributes = attributes || hstore($16::text[]) "
		"where ri_resvID = $1");
	if (pg_prepare_stmt(conn, STMT_UPDATE_RESV, conn->conn_sql, 16) != 0)
		return -1;

	snprintf(conn->conn_sql, MAX_SQL_LENGTH, "update pbs.resv set "
		"ri_savetm = localtimestamp,"
		"attributes = attributes - hstore($2::text[]) "
		"where ri_resvID = $1");
	if (pg_prepare_stmt(conn, STMT_REMOVE_RESVATTRS, conn->conn_sql, 2) != 0)
		return -1;

	snprintf(conn->conn_sql, MAX_SQL_LENGTH, "select "
		"ri_resvID, "
		"ri_queue, "
		"ri_state, "
		"ri_substate, "
		"ri_type, "
		"ri_stime, "
		"ri_etime, "
		"ri_duration, "
		"ri_tactive, "
		"ri_svrflags, "
		"ri_numattr, "
		"ri_resvTag, "
		"ri_un_type, "
		"ri_fromsock, "
		"ri_fromaddr, "
		"extract(epoch from ri_savetm)::bigint as ri_savetm, "
		"extract(epoch from ri_creattm)::bigint as ri_creattm, "
		"hstore_to_array(attributes) as attributes "
		"from pbs.resv where ri_resvid = $1");
	if (pg_prepare_stmt(conn, STMT_SELECT_RESV, conn->conn_sql, 1) != 0)
		return -1;

	snprintf(conn->conn_sql, MAX_SQL_LENGTH, "select "
		"ri_resvID, "
		"ri_queue, "
		"ri_state, "
		"ri_substate, "
		"ri_type, "
		"ri_stime, "
		"ri_etime, "
		"ri_duration, "
		"ri_tactive, "
		"ri_svrflags, "
		"ri_numattr, "
		"ri_resvTag, "
		"ri_un_type, "
		"ri_fromsock, "
		"ri_fromaddr, "
		"extract(epoch from ri_savetm)::bigint as ri_savetm, "
		"extract(epoch from ri_creattm)::bigint as ri_creattm, "
		"hstore_to_array(attributes) as attributes "
		"from pbs.resv "
		"order by ri_creattm");
	if (pg_prepare_stmt(conn, STMT_FINDRESVS_ORDBY_CREATTM,
		conn->conn_sql, 0) != 0)
		return -1;

	snprintf(conn->conn_sql, MAX_SQL_LENGTH, "delete from pbs.resv where ri_resvid = $1");
	if (pg_prepare_stmt(conn, STMT_DELETE_RESV, conn->conn_sql, 1) != 0)
		return -1;

	return 0;
}

/**
 * @brief
 *	Load resv data from the row into the resv object
 *
 * @param[in]	res - Resultset from a earlier query
 * @param[in]	presv  - resv object to load data into
 * @param[in]	row - The current row to load within the resultset
 *
 * @return      Error code
 * @retval	-1 - On Error
 * @retval	 0 - On Success
 * @retval	>1 - Number of attributes
 */
static int
load_resv(PGresult *res, pbs_db_resv_info_t *presv, int row)
{
	char *raw_array;
	static int ri_resvid_fnum, ri_queue_fnum, ri_state_fnum, ri_substate_fnum, ri_type_fnum, ri_stime_fnum,
	ri_etime_fnum, ri_duration_fnum, ri_tactive_fnum, ri_svrflags_fnum, ri_numattr_fnum, ri_resvTag_fnum,
	ri_un_type_fnum, ri_fromsock_fnum, ri_fromaddr_fnum, ri_savetm_fnum, ri_creattm_fnum,
	attributes_fnum;

	static int fnums_inited = 0;

	if (fnums_inited == 0) {
		ri_resvid_fnum = PQfnumber(res, "ri_resvID");
		ri_queue_fnum = PQfnumber(res, "ri_queue");
		ri_state_fnum = PQfnumber(res, "ri_state");
		ri_substate_fnum = PQfnumber(res, "ri_substate");
		ri_type_fnum = PQfnumber(res, "ri_type");
		ri_stime_fnum = PQfnumber(res, "ri_stime");
		ri_etime_fnum = PQfnumber(res, "ri_etime");
		ri_duration_fnum = PQfnumber(res, "ri_duration");
		ri_tactive_fnum = PQfnumber(res, "ri_tactive");
		ri_svrflags_fnum = PQfnumber(res, "ri_svrflags");
		ri_numattr_fnum = PQfnumber(res, "ri_numattr");
		ri_resvTag_fnum = PQfnumber(res, "ri_resvTag");
		ri_un_type_fnum = PQfnumber(res, "ri_un_type");
		ri_fromsock_fnum = PQfnumber(res, "ri_fromsock");
		ri_fromaddr_fnum = PQfnumber(res, "ri_fromaddr");
		ri_savetm_fnum = PQfnumber(res, "ri_savetm");
		ri_creattm_fnum = PQfnumber(res, "ri_creattm");
		attributes_fnum = PQfnumber(res, "attributes");
		fnums_inited = 1;
	}

	GET_PARAM_STR(res, row, presv->ri_resvid, ri_resvid_fnum);
	GET_PARAM_STR(res, row, presv->ri_queue, ri_queue_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_state, ri_state_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_substate, ri_substate_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_type, ri_type_fnum);
	GET_PARAM_BIGINT(res, row, presv->ri_stime, ri_stime_fnum);
	GET_PARAM_BIGINT(res, row, presv->ri_etime, ri_etime_fnum);
	GET_PARAM_BIGINT(res, row, presv->ri_duration, ri_duration_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_tactive, ri_tactive_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_svrflags, ri_svrflags_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_numattr, ri_numattr_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_resvTag, ri_resvTag_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_un_type, ri_un_type_fnum);
	GET_PARAM_INTEGER(res, row, presv->ri_fromsock, ri_fromsock_fnum);
	GET_PARAM_BIGINT(res, row, presv->ri_fromaddr, ri_fromaddr_fnum);
	GET_PARAM_BIGINT(res, row, presv->ri_savetm, ri_savetm_fnum);
	GET_PARAM_BIGINT(res, row, presv->ri_creattm, ri_creattm_fnum);
	GET_PARAM_BIN(res, row, raw_array, attributes_fnum);

	/* convert attributes from postgres raw array format */
	return (convert_array_to_db_attr_list(raw_array, &presv->attr_list));
}

/**
 * @brief
 *	Insert resv data into the database
 *
 * @param[in]	conn - Connection handle
 * @param[in]	obj  - Information of resv to be inserted
 *
 * @return      Error code
 * @retval	-1 - Failure
 * @retval	 0 - Success
 *
 */
int
pg_db_save_resv(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj, int savetype)
{
	pbs_db_resv_info_t *presv = obj->pbs_db_un.pbs_db_resv;
	char *stmt;
	int params;
	char *raw_array = NULL;

	SET_PARAM_STR(conn, presv->ri_resvid, 0);
	SET_PARAM_STR(conn, presv->ri_queue, 1);
	SET_PARAM_INTEGER(conn, presv->ri_state, 2);
	SET_PARAM_INTEGER(conn, presv->ri_substate, 3);
	SET_PARAM_INTEGER(conn, presv->ri_type, 4);
	SET_PARAM_BIGINT(conn, presv->ri_stime, 5);
	SET_PARAM_BIGINT(conn, presv->ri_etime, 6);
	SET_PARAM_BIGINT(conn, presv->ri_duration, 7);
	SET_PARAM_INTEGER(conn, presv->ri_tactive, 8);
	SET_PARAM_INTEGER(conn, presv->ri_svrflags, 9);
	SET_PARAM_INTEGER(conn, presv->ri_numattr, 10);
	SET_PARAM_INTEGER(conn, presv->ri_resvTag, 11);
	SET_PARAM_INTEGER(conn, presv->ri_un_type, 12);
	SET_PARAM_INTEGER(conn, presv->ri_fromsock, 13);
	SET_PARAM_BIGINT(conn, presv->ri_fromaddr, 14);

	if (savetype == PBS_UPDATE_DB_QUICK) {
		params = 15;
	} else {
		int len = 0;
		/* convert attributes to postgres raw array format */
		if ((len = convert_db_attr_list_to_array(&raw_array, &presv->attr_list)) <= 0)
			return -1;

		SET_PARAM_BIN(conn, raw_array, len, 15);
		params = 16;
	}

	if (savetype == PBS_UPDATE_DB_FULL)
		stmt = STMT_UPDATE_RESV;
	else
		stmt = STMT_INSERT_RESV;

	if (pg_db_cmd(conn, stmt, params) != 0) {
		free(raw_array);
		return -1;
	}

	free(raw_array);

	return 0;
}

/**
 * @brief
 *	Load resv data from the database
 *
 * @param[in]	conn - Connection handle
 * @param[in]	obj  - Load resv information into this object
 *
 * @return      Error code
 * @retval	-1 - Failure
 * @retval	 0 - Success
 * @retval	 1 -  Success but no rows loaded
 *
 */
int
pg_db_load_resv(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj)
{
	pbs_db_resv_info_t *presv = obj->pbs_db_un.pbs_db_resv;
	PGresult *res;
	int rc;

	SET_PARAM_STR(conn, presv->ri_resvid, 0);

	if ((rc = pg_db_query(conn, STMT_SELECT_RESV, 1, &res)) != 0)
		return rc;

	rc = load_resv(res, presv, 0);

	PQclear(res);
	return rc;
}

/**
 * @brief
 *	Find resv
 *
 * @param[in]	conn - Connection handle
 * @param[in]	st   - The cursor state variable updated by this query
 * @param[in]	obj  - Information of resv to be found
 * @param[in]	opts - Any other options (like flags, timestamp)
 *
 * @return      Error code
 * @retval	-1 - Failure
 * @retval	 0 - Success
 * @retval	 1 - Success, but no rows found
 *
 */
int
pg_db_find_resv(pbs_db_conn_t *conn, void *st, pbs_db_obj_info_t *obj,
	pbs_db_query_options_t *opts)
{
	PGresult *res;
	int rc;
	int params;

	pg_query_state_t *state = (pg_query_state_t *) st;

	if (!state)
		return -1;

	params=0;
	strcpy(conn->conn_sql, STMT_FINDRESVS_ORDBY_CREATTM);

	if ((rc = pg_db_query(conn, conn->conn_sql, params, &res)) != 0)
		return rc;

	state->row = 0;
	state->res = res;
	state->count = PQntuples(res);
	return 0;
}

/**
 * @brief
 *	Get the next resv from the cursor
 *
 * @param[in]	conn - Connection handle
 * @param[in]	st   - The cursor state
 * @param[in]	obj  - Resv information is loaded into this object
 *
 * @return      Error code
 *		(Even though this returns only 0 now, keeping it as int
 *			to support future change to return a failure)
 * @retval	 0 - Success
 *
 */
int
pg_db_next_resv(pbs_db_conn_t* conn, void* st, pbs_db_obj_info_t* obj)
{
	pg_query_state_t *state = (pg_query_state_t *) st;

	return (load_resv(state->res, obj->pbs_db_un.pbs_db_resv, state->row));
}

/**
 * @brief
 *	Delete the resv from the database
 *
 * @param[in]	conn - Connection handle
 * @param[in]	obj  - Resv information
 *
 * @return      Error code
 * @retval	-1 - Failure
 * @retval	 0 - Success
 * @retval	 1 - Success but no rows deleted
 *
 */
int
pg_db_delete_resv(pbs_db_conn_t *conn, pbs_db_obj_info_t* obj)
{
	pbs_db_resv_info_t *presv = obj->pbs_db_un.pbs_db_resv;
	SET_PARAM_STR(conn, presv->ri_resvid, 0);
	return (pg_db_cmd(conn, STMT_DELETE_RESV, 1));
}


/**
 * @brief
 *	Deletes attributes of a Resv
 *
 * @param[in]	conn - Connection handle
 * @param[in]	obj  - Resv information
 * @param[in]	obj_id  - Resv id
 * @param[in]	attr_list - List of attributes
 *
 * @return      Error code
 * @retval	 0 - Success
 * @retval	-1 - On Failure
 *
 */
int
pg_db_del_attr_resv(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj, void *obj_id, pbs_db_attr_list_t *attr_list)
{
	char *raw_array = NULL;
	int len = 0;

	if ((len = convert_db_attr_list_to_array(&raw_array, attr_list)) <= 0)
		return -1;
	SET_PARAM_STR(conn, obj_id, 0);

	SET_PARAM_BIN(conn, raw_array, len, 1);

	if (pg_db_cmd(conn, STMT_REMOVE_RESVATTRS, 2) != 0)
		return -1;

	free(raw_array);

	return 0;
}


/**
 * @brief
 *	Frees allocate memory of an Object
 *
 * @param[in]	obj - pbs_db_obj_info_t containing the DB object
 *
 * @return None
 *
 */
void
pg_db_reset_resv(pbs_db_obj_info_t *obj)
{
	free_db_attr_list(&(obj->pbs_db_un.pbs_db_resv->attr_list));
}
