Exemplo n.º 1
0
static int _pgsql_jobcomp_check_tables(char *user)
{

	int i = 0, job_found = 0;
	PGresult *result = NULL;
	char *query = xstrdup_printf("select tablename from pg_tables "
				     "where tableowner='%s' "
				     "and tablename !~ '^pg_+'", user);

	if(!(result =
	     pgsql_db_query_ret(jobcomp_pgsql_db, query))) {
		xfree(query);
		return SLURM_ERROR;
	}
	xfree(query);

	for (i = 0; i < PQntuples(result); i++) {
		if(!job_found
		   && !strcmp(jobcomp_table, PQgetvalue(result, i, 0)))
			job_found = 1;
	}
	PQclear(result);

	if(!job_found)
		if(pgsql_db_create_table(jobcomp_pgsql_db, "public", jobcomp_table,
					 jobcomp_table_fields,
					 ")") == SLURM_ERROR)
			return SLURM_ERROR;

	return SLURM_SUCCESS;
}
Exemplo n.º 2
0
/*
 * check_table - check account tables
 * IN db_conn: database connection
 * IN table: table name
 * IN fields: fields of the table
 * IN constraint: additional constraint of the table
 * RET: error code
 */
extern int
check_table(PGconn *db_conn, char *schema, char *table,
	    storage_field_t *fields, char *constraint)
{
	DEF_VARS;
	char **tables = NULL;
	int i, num, rc = SLURM_SUCCESS;

	query = xstrdup_printf(
		"SELECT tablename FROM pg_tables WHERE schemaname='%s' AND "
		"tableowner='%s' AND tablename !~ '^pg_+' "
		"AND tablename !~ '^sql_+'", schema, PQuser(db_conn));
	result = pgsql_db_query_ret(db_conn, query);
	xfree(query);
	if (!result)
		return SLURM_ERROR;

	num = PQntuples(result);
	tables = xmalloc(sizeof(char *) * (num + 1));
	for (i = 0; i < num; i ++)
		tables[i] = xstrdup(PQgetvalue(result, i, 0));
	tables[num] = NULL;
	PQclear(result);

	i = 0;
	while (tables[i] && strcmp(tables[i], table))
		i ++;

	if (!tables[i]) {
		debug("as/pg: table %s.%s not found, create it", schema, table);
		rc = pgsql_db_create_table(db_conn, schema, table, fields,
					   constraint);
	} else {
		rc = pgsql_db_make_table_current(
			db_conn, schema, table, fields);
	}
	for (i = 0; i < num; i ++)
		xfree(tables[i]);
	xfree(tables);
	return rc;
}