/** * @brief * Retrieve the Datastore schema version (maj, min) * * @param[out] db_maj_ver - return the major schema version * @param[out] db_min_ver - return the minor schema version * * @return Error code * @retval -1 - Failure * @retval 0 - Success * */ int pbs_db_get_schema_version(pbs_db_conn_t *conn, int *db_maj_ver, int *db_min_ver) { PGresult *res; int rc; char ver_str[MAX_SCHEMA_VERSION_LEN+1]; char *token; if ((rc = pg_db_query(conn, STMT_SELECT_DBVER, 0, &res)) != 0) return rc; memset(ver_str, 0, sizeof(ver_str)); strncpy(ver_str, PQgetvalue(res, 0, PQfnumber(res, "pbs_schema_version")), MAX_SCHEMA_VERSION_LEN+1); if (ver_str[MAX_SCHEMA_VERSION_LEN] != '\0') return -1; token = strtok(ver_str, "."); if (token) *db_maj_ver = atol(token); else return -1; token = strtok(NULL, "."); if (token) *db_min_ver = atol(token); else return -1; return 0; }
/** * @brief * Load server data from the database * * @param[in] conn - Connection handle * @param[in] obj - Load server information into this object * * @return Error code * @retval -1 - Failure * @retval 0 - Success * @retval 1 - Success but no rows loaded * */ int pg_db_load_svr(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj) { PGresult *res; int rc; pbs_db_svr_info_t *ps = obj->pbs_db_un.pbs_db_svr; LOAD_STR(conn, ps->sv_name, 0); if ((rc = pg_db_query(conn, STMT_SELECT_SVR, 1, &res)) != 0) return rc; /* get the other fields */ strcpy(ps->sv_name, PQgetvalue(res, 0, PQfnumber(res, "sv_name"))); strcpy(ps->sv_hostname, PQgetvalue(res, 0, PQfnumber(res, "sv_hostname"))); ps->sv_numjobs = strtol(PQgetvalue(res, 0, PQfnumber(res, "sv_numjobs")), NULL, 10); ps->sv_numque = strtol(PQgetvalue(res, 0, PQfnumber(res, "sv_numque")), NULL, 10); ps->sv_jobidnumber = strtol(PQgetvalue(res, 0, PQfnumber(res, "sv_jobidnumber")), NULL, 10); ps->sv_savetm = strtoll(PQgetvalue(res, 0, PQfnumber(res, "sv_savetm")), NULL, 10); ps->sv_creattm = strtoll(PQgetvalue(res, 0, PQfnumber(res, "sv_creattm")), NULL, 10); PQclear(res); return 0; }
/** * @brief * load job script * * @param[in] conn - Connection handle * @param[in/out] obj - Job script is loaded into this object * * @return Error code * @retval -1 - Failure * @retval 0 - Success * */ int pg_db_load_jobscr(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj) { PGresult *res; pbs_db_jobscr_info_t *pscr = obj->pbs_db_un.pbs_db_jobscr; static int script_fnum = -1; SET_PARAM_STR(conn, pscr->ji_jobid, 0); /* * The data (script) we stored was a "encoded" binary. We "decode" it * back while reading, giving us "binary" data. Since we want the * result data to be returned in binary, we set conn_result_format * to 1 to indicate binary result. This setting is a one-time, * auto-reset switch which resets to 0 (TEXT) mode after each execution * of pg_db_query. * */ if (pg_db_query(conn, STMT_SELECT_JOBSCR, 1, &res) != 0) return -1; if (script_fnum == -1) script_fnum = PQfnumber(res, "script"); GET_PARAM_BIN(res, 0, pscr->script, script_fnum); return 0; }
/** * @brief * Find jobs * * @param[in] conn - Connection handle * @param[out] st - The cursor state variable updated by this query * @param[in] obj - Information of job 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_job(pbs_db_conn_t *conn, void *st, pbs_db_obj_info_t *obj, pbs_db_query_options_t *opts) { PGresult *res; pg_query_state_t *state = (pg_query_state_t *) st; pbs_db_job_info_t *pdjob = obj->pbs_db_un.pbs_db_job; int rc; int params; if (!state) return -1; if (opts != NULL && opts->flags == FIND_JOBS_BY_QUE) { SET_PARAM_STR(conn, pdjob->ji_queue, 0); params=1; strcpy(conn->conn_sql, STMT_FINDJOBS_BYQUE_ORDBY_QRANK); } else { strcpy(conn->conn_sql, STMT_FINDJOBS_ORDBY_QRANK); params=0; } 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 svrid sequence number from the database * * The svr hostname is associated with a sever_id (sv_name) column. This column * serves as the id for the server, used for all subsequent queries. These "id" * values are created from a sequence generator within the PBS schema called * "pbs.svr_id_seq", whenever a new server database is created (typically at new * installation). * This function reads the "next" value from the sequence "pbs.svr_id_seq" and * returns that number (in ascii format) to be used as the server-id for the * new server database being created. * * @see chk_and_update_db_svrhost * * @param[in] conn - The database connection handle * * @return Current sequence number in ascii format (to be freed by caller) * @retval -NULL - Failure * -!NULL - Success * */ char* pbs_db_get_unique_svrid(pbs_db_conn_t *conn) { PGresult *res; char *svrid; if (pg_db_query(conn, STMT_SELECT_NEXT_SEQID, 0, &res) != 0) return NULL; svrid = strdup(PQgetvalue(res, 0, 0)); PQclear(res); return svrid; }
/** * @brief * Get the svrid corresponding to the given svr hostname * * The svr hostname is associated with a sever_id (sv_name) column. This column * serves as the id for the server, used for all subsequent queries. For a * server database that has already been created, an id value (sv_name column) * is associated with the server's hostname. This id needs to be retrieved by * pbs_server to be used in subsequent queries for this server row. * * This function loads the server_id (sv_name column value) corresponding to the * given hostname (sv_hostname column). * * @see chk_and_update_db_svrhost * * @param[in] conn - The database connection handle * @param[in] hostname - The svr hostname * * @return The database svrid (to be freed by caller) * @retval -NULL - Failure * -!NULL - Success * */ char* pbs_db_get_svr_id(pbs_db_conn_t *conn, char *hostname) { PGresult *res; char *svrid; LOAD_STR(conn, hostname, 0); if (pg_db_query(conn, STMT_SELECT_SVRID, 1, &res) != 0) return NULL; svrid = strdup(PQgetvalue(res, 0, 0)); PQclear(res); return svrid; }
/** * @brief * Load job data from the database * * @param[in] conn - Connection handle * @param[in/out]obj - Load job information into this object where * jobid = obj->pbs_db_un.pbs_db_job->ji_jobid * * @return Error code * @retval -1 - Failure * @retval 0 - Success * @retval 1 - Success but no rows loaded * */ int pg_db_load_job(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj) { PGresult *res; int rc; pbs_db_job_info_t *pj = obj->pbs_db_un.pbs_db_job; SET_PARAM_STR(conn, pj->ji_jobid, 0); if ((rc = pg_db_query(conn, STMT_SELECT_JOB, 1, &res)) != 0) return rc; rc = load_job(res, pj, 0); PQclear(res); return rc; }
/** * @brief * Load scheduler data from the database * * @param[in] conn - Connection handle * @param[in] obj - Load scheduler information into this object * * @return Error code * @retval -1 - Failure * @retval 0 - Success * @retval 1 - Success but no rows loaded * */ int pg_db_load_sched(pbs_db_conn_t *conn, pbs_db_obj_info_t *obj) { PGresult *res; int rc; pbs_db_sched_info_t *psch = obj->pbs_db_un.pbs_db_sched; LOAD_STR(conn, psch->sched_name, 0); if ((rc = pg_db_query(conn, STMT_SELECT_SCHED, 1, &res)) != 0) return rc; /* get the other fields */ strcpy(psch->sched_name, PQgetvalue(res, 0, PQfnumber(res, "sched_name"))); strcpy(psch->sched_sv_name, PQgetvalue(res, 0, PQfnumber(res, "sched_sv_name"))); psch->sched_savetm = strtoll(PQgetvalue(res, 0, PQfnumber(res, "sched_savetm")), NULL, 10); psch->sched_creattm = strtoll(PQgetvalue(res, 0, PQfnumber(res, "sched_creattm")), NULL, 10); PQclear(res); return 0; }