extern int pgsql_db_create_table(PGconn *pgsql_db, char *schema, char *table_name, storage_field_t *fields, char *ending) { char *query = NULL; char *tmp = NULL; char *next = NULL; int i = 0; query = xstrdup_printf("create table %s.%s (", schema, table_name); i=0; while (fields && fields->name) { next = xstrdup_printf(" %s %s", fields->name, fields->options); if (i) xstrcat(tmp, ","); xstrcat(tmp, next); xfree(next); fields++; i++; } xstrcat(query, tmp); xfree(tmp); xstrcat(query, ending); if (pgsql_db_query(pgsql_db, query) == SLURM_ERROR) { xfree(query); return SLURM_ERROR; } xfree(query); return SLURM_SUCCESS; }
/* * create_function_xfree - perform the create function query and * xfree the query string * IN db_conn: database connection * IN query: create function query * RET: error code */ extern int create_function_xfree(PGconn *db_conn, char *query) { int rc = pgsql_db_query(db_conn, query); xfree(query); return rc; }
extern int pgsql_insert_ret_id(PGconn *pgsql_db, char *sequence_name, char *query) { int new_id = 0; PGresult *result = NULL; slurm_mutex_lock(&pgsql_lock); if (pgsql_db_query(pgsql_db, query) != SLURM_ERROR) { char *new_query = xstrdup_printf( "select last_value from %s", sequence_name); if ((result = pgsql_db_query_ret(pgsql_db, new_query))) { new_id = atoi(PQgetvalue(result, 0, 0)); PQclear(result); } xfree(new_query); if (!new_id) { /* should have new id */ error("We should have gotten a new id: %s", PQerrorMessage(pgsql_db)); } } slurm_mutex_unlock(&pgsql_lock); return new_id; }
int pgsql_db_begin(PGconn * pgsql) { PGresult *result; ExecStatusType status; result = pgsql_db_query("BEGIN", pgsql); /* check the result */ status = PQresultStatus(result); if (status != PGRES_COMMAND_OK) { if (set->verbose >= LOW) { fprintf(stderr, "** Postgres Transaction Begin Error: %s\n", PQresultErrorMessage(result)); } return(FALSE); } else { return(TRUE); } }
/* wrapper for inserts */ int pgsql_db_insert(char *query, PGconn * pgsql) { PGresult *result; ExecStatusType status; result = pgsql_db_query(query, pgsql); /* check the result */ status = PQresultStatus(result); if (status != PGRES_COMMAND_OK) { if (set->verbose >= LOW) { fprintf(stderr, "** Postgres Insert Error: %s\n", PQresultErrorMessage(result)); /* clear the result */ PQclear(result); } return (FALSE); } else /* clear the result */ PQclear(result); return (TRUE); }
/* wrapper for selects */ PGresult * pgsql_db_select(char *query, PGconn * pgsql) { PGresult *result; ExecStatusType status; result = pgsql_db_query(query, pgsql); /* check the result */ status = PQresultStatus(result); if (status != PGRES_TUPLES_OK) { if (set->verbose >= LOW) { fprintf(stderr, "** Postgres Select Error: %s\n", PQresultErrorMessage(result)); /* clear the result */ PQclear(result); /* make an empty error result */ result = PQmakeEmptyPGresult(pgsql, PGRES_FATAL_ERROR); } return (result); } else /* return the result */ return (result); }
/* * as_pg_add_accts - add accounts * * IN pg_conn: database connection * IN uid: user performing the add operation * IN acct_list: accounts to add * RET: error code */ extern int as_pg_add_accts(pgsql_conn_t *pg_conn, uint32_t uid, List acct_list) { ListIterator itr = NULL; slurmdb_account_rec_t *object = NULL; List assoc_list = NULL; int rc = SLURM_SUCCESS; char *user_name = NULL, *query = NULL, *txn_query = NULL; char *rec = NULL, *info = NULL; time_t now = time(NULL); if (check_db_connection(pg_conn) != SLURM_SUCCESS) return ESLURM_DB_CONNECTION; assoc_list = list_create(slurmdb_destroy_association_rec); user_name = uid_to_string((uid_t) uid); itr = list_iterator_create(acct_list); while((object = list_next(itr))) { if(!object->name || !object->description || !object->organization) { error("as/pg: add_accts: We need an account name, " "description, and organization to add. %s %s %s", object->name, object->description, object->organization); rc = SLURM_ERROR; continue; } /* order of vals must match structure of acct_table */ rec = xstrdup_printf("(%ld, %ld, 0, '%s', '%s', '%s')", now, now, object->name, object->description, object->organization); query = xstrdup_printf("SELECT public.add_acct(%s);", rec); xfree(rec); rc = DEF_QUERY_RET_RC; if(rc != SLURM_SUCCESS) { error("as/pg: couldn't add acct %s", object->name); continue; } info = xstrdup_printf("description='%s', organization='%s'", object->description, object->organization); if(txn_query) xstrfmtcat(txn_query, ", (%ld, %u, '%s', '%s', $$%s$$)", now, DBD_ADD_ACCOUNTS, object->name, user_name, info); else xstrfmtcat(txn_query, "INSERT INTO %s " "(timestamp, action, name, actor, info) " "VALUES (%ld, %u, '%s', '%s', $$%s$$)", txn_table, now, DBD_ADD_ACCOUNTS, object->name, user_name, info); xfree(info); if(!object->assoc_list) continue; list_transfer(assoc_list, object->assoc_list); } list_iterator_destroy(itr); xfree(user_name); if(rc == SLURM_SUCCESS) { if(txn_query) { xstrcat(txn_query, ";"); rc = pgsql_db_query(pg_conn->db_conn, txn_query); xfree(txn_query); if(rc != SLURM_SUCCESS) { error("as/pg: add_accts: couldn't add txn"); rc = SLURM_SUCCESS; } } } else xfree(txn_query); if(rc == SLURM_SUCCESS && list_count(assoc_list)) { if(acct_storage_p_add_associations(pg_conn, uid, assoc_list) != SLURM_SUCCESS) { error("as/pg: add_accts: problem adding account " "associations"); rc = SLURM_ERROR; } } list_destroy(assoc_list); return rc; }
extern int slurm_jobcomp_log_record(struct job_record *job_ptr) { int rc = SLURM_SUCCESS; char *usr_str = NULL, *grp_str = NULL, lim_str[32]; char *connect_type = NULL, *reboot = NULL, *rotate = NULL, *geometry = NULL, *start = NULL, *blockid = NULL; enum job_states job_state; char *query = NULL; uint32_t time_limit, start_time, end_time; if(!jobcomp_pgsql_db || PQstatus(jobcomp_pgsql_db) != CONNECTION_OK) { char *loc = slurm_get_jobcomp_loc(); if(slurm_jobcomp_set_location(loc) == SLURM_ERROR) { xfree(loc); return SLURM_ERROR; } xfree(loc); } usr_str = _get_user_name(job_ptr->user_id); grp_str = _get_group_name(job_ptr->group_id); if ((job_ptr->time_limit == NO_VAL) && job_ptr->part_ptr) time_limit = job_ptr->part_ptr->max_time; else time_limit = job_ptr->time_limit; if (time_limit == INFINITE) strcpy(lim_str, "UNLIMITED"); else { snprintf(lim_str, sizeof(lim_str), "%lu", (unsigned long) time_limit); } /* Job will typically be COMPLETING when this is called. * We remove the flags to get the eventual completion state: * JOB_FAILED, JOB_TIMEOUT, etc. */ if (IS_JOB_RESIZING(job_ptr)) { job_state = JOB_RESIZING; if (job_ptr->resize_time) start_time = job_ptr->resize_time; else start_time = job_ptr->start_time; end_time = time(NULL); } else { job_state = job_ptr->job_state & JOB_STATE_BASE; if (job_ptr->resize_time) start_time = job_ptr->resize_time; else if (job_ptr->start_time > job_ptr->end_time) { /* Job cancelled while pending and * expected start time is in the future. */ start_time = 0; } else start_time = job_ptr->start_time; end_time = job_ptr->end_time; } connect_type = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_CONNECTION); reboot = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_REBOOT); rotate = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_ROTATE); geometry = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_GEOMETRY); start = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_START); #ifdef HAVE_BG blockid = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_BG_ID); #else blockid = select_g_select_jobinfo_xstrdup(job_ptr->select_jobinfo, SELECT_PRINT_RESV_ID); #endif query = xstrdup_printf( "insert into %s (jobid, uid, user_name, gid, group_name, " "name, state, proc_cnt, partition, timelimit, " "starttime, endtime, nodecnt", jobcomp_table); if(job_ptr->nodes) xstrcat(query, ", nodelist"); if(connect_type) xstrcat(query, ", connect_type"); if(reboot) xstrcat(query, ", reboot"); if(rotate) xstrcat(query, ", rotate"); if(job_ptr->details && (job_ptr->details->max_cpus != NO_VAL)) xstrcat(query, ", maxprocs"); if(geometry) xstrcat(query, ", geometry"); if(start) xstrcat(query, ", start"); if(blockid) xstrcat(query, ", blockid"); xstrfmtcat(query, ") values (%u, %u, '%s', %u, '%s', \"%s\", %d, %u, " "'%s', \"%s\", %u, %u, %u", job_ptr->job_id, job_ptr->user_id, usr_str, job_ptr->group_id, grp_str, job_ptr->name, job_state, job_ptr->total_cpus, job_ptr->partition, lim_str, start_time, end_time, job_ptr->node_cnt); if(job_ptr->nodes) xstrfmtcat(query, ", '%s'", job_ptr->nodes); if(connect_type) { xstrfmtcat(query, ", '%s'", connect_type); xfree(connect_type); } if(reboot) { xstrfmtcat(query, ", '%s'", reboot); xfree(reboot); } if(rotate) { xstrfmtcat(query, ", '%s'", rotate); xfree(rotate); } if(job_ptr->details && (job_ptr->details->max_cpus != NO_VAL)) xstrfmtcat(query, ", '%u'", job_ptr->details->max_cpus); if(geometry) { xstrfmtcat(query, ", '%s'", geometry); xfree(geometry); } if(start) { xstrfmtcat(query, ", '%s'", start); xfree(start); } if(blockid) { xstrfmtcat(query, ", '%s'", blockid); xfree(blockid); } xstrcat(query, ")"); //info("here is the query %s", query); rc = pgsql_db_query(jobcomp_pgsql_db, query); xfree(usr_str); return rc; }
extern int pgsql_db_make_table_current(PGconn *pgsql_db, char *schema, char *table_name, storage_field_t *fields) { char *query = NULL, *opt_part = NULL, *temp_char = NULL; char *type = NULL; int not_null = 0; char *default_str = NULL; char* original_ptr = NULL; int i = 0; PGresult *result = NULL; List columns = NULL; ListIterator itr = NULL; char *col = NULL; DEF_TIMERS; query = xstrdup_printf("select column_name from " "information_schema.columns where " "table_name='%s' and table_schema='%s' ", table_name, schema); if (!(result = pgsql_db_query_ret(pgsql_db, query))) { xfree(query); return SLURM_ERROR; } xfree(query); columns = list_create(slurm_destroy_char); for (i = 0; i < PQntuples(result); i++) { col = xstrdup(PQgetvalue(result, i, 0)); //column_name list_append(columns, col); } PQclear(result); itr = list_iterator_create(columns); query = xstrdup_printf("alter table %s.%s", schema, table_name); START_TIMER; i=0; while (fields[i].name) { int found = 0; not_null = 0; if (!strcasecmp("serial", fields[i].options)) { i++; continue; } opt_part = xstrdup(fields[i].options); original_ptr = opt_part; opt_part = strtok_r(opt_part, " ", &temp_char); if (opt_part) { /* XXX: only one identifier supported */ type = xstrdup(opt_part); opt_part = strtok_r(NULL, " ", &temp_char); while (opt_part) { if (!strcasecmp("not", opt_part)) { opt_part = strtok_r(NULL, " ", &temp_char); if (!strcasecmp("null", opt_part)) { not_null = 1; } } else if (!strcasecmp("default", opt_part)){ opt_part = strtok_r(NULL, " ", &temp_char); default_str = xstrdup(opt_part); } opt_part = strtok_r(NULL, " ", &temp_char); } } else { type = xstrdup(fields[i].options); } xfree(original_ptr); list_iterator_reset(itr); while ((col = list_next(itr))) { if (!strcmp(col, fields[i].name)) { list_delete_item(itr); found = 1; break; } } temp_char = NULL; if (!found) { info("adding column %s", fields[i].name); if (default_str) xstrfmtcat(temp_char, " default %s", default_str); if (not_null) xstrcat(temp_char, " not null"); xstrfmtcat(query, " add %s %s", fields[i].name, type); if (temp_char) xstrcat(query, temp_char); xstrcat(query, ","); } else { if (default_str) xstrfmtcat(temp_char, " alter %s set default %s,", fields[i].name, default_str); else xstrfmtcat(temp_char, " alter %s drop default,", fields[i].name); if (not_null) xstrfmtcat(temp_char, " alter %s set not null,", fields[i].name); else xstrfmtcat(temp_char, " alter %s drop not null,", fields[i].name); xstrfmtcat(query, " alter %s type %s,%s", fields[i].name, type, temp_char); } xfree(temp_char); xfree(default_str); xfree(type); i++; } list_iterator_destroy(itr); list_destroy(columns); query[strlen(query)-1] = ';'; //debug4("pgsql db create/alter table:\n %s", query); if (pgsql_db_query(pgsql_db, query)) { xfree(query); return SLURM_ERROR; } xfree(query); END_TIMER2("make table current"); return SLURM_SUCCESS; }
extern int pgsql_db_rollback(PGconn *pgsql_db) { return pgsql_db_query(pgsql_db, "ROLLBACK WORK"); }
extern int pgsql_db_commit(PGconn *pgsql_db) { return pgsql_db_query(pgsql_db, "COMMIT WORK"); }
extern int pgsql_db_start_transaction(PGconn *pgsql_db) { return pgsql_db_query(pgsql_db, "BEGIN WORK"); }