/*------------------------------------------------------------------------- * Copyright (c) 1999-2002 Kenneth W. Sodemann (stuffle@charter.net) *------------------------------------------------------------------------- * prq_utils * * Synopsis: * Helper routines for modules dealing with the pr_query table. * * $Id: prq_utils.c,v 1.1 2003/03/09 20:59:11 stuffle Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA *------------------------------------------------------------------------- */ #include #include #include #include #define ARRAY_SEL "SELECT pr_query.%s[%d] \ FROM pr_query \ WHERE query_num = %d" GList *create_num_list (PGconn *conn, gint query_pk, const gchar *column) { GList *the_list = NULL; GString *sql_buffer; PGresult *res; gint idx = 1; /* * Process the first result, and get the next result. * Repeat until no more results. */ sql_buffer = g_string_new(""); g_string_sprintf (sql_buffer, ARRAY_SEL, column, idx, query_pk); res = PQexec (conn, sql_buffer->str); /* * We should get either 1 or 0 tuples. If we get zero the first * time, we are done. If we get 1 the first time, we will continue * to get 1 tuple each time. In this case, loop through the array * until we get a NULL back (this will happen when we go past the * last value in the array. */ if (PQntuples (res) > 0) { while (!PQgetisnull(res, 0, 0)) { the_list = g_list_append (the_list, GINT_TO_POINTER (atoi (PQgetvalue (res, 0, 0)))); idx++; g_string_sprintf (sql_buffer, ARRAY_SEL, column, idx, query_pk); PQclear (res); res = PQexec (conn, sql_buffer->str); } } PQclear (res); g_string_free (sql_buffer, TRUE); return the_list; } GList *create_txt_list (PGconn *conn, gint query_pk, const gchar* column) { GList *the_list = NULL; GString *sql_buffer; gchar *curr; PGresult *res; gint idx = 1; /* * Process the first result, and get the next result. * Repeat until no more results. */ sql_buffer = g_string_new(""); g_string_sprintf (sql_buffer, ARRAY_SEL, column, idx, query_pk); res = PQexec (conn, sql_buffer->str); /* * See note in create_num_list(): same idea here... */ if (PQntuples (res) > 0) { while (!PQgetisnull (res, 0, 0)) { curr = (gchar *)g_malloc (strlen (PQgetvalue (res, 0, 0)) + 1); strcpy (curr, PQgetvalue (res, 0, 0)); the_list = g_list_append (the_list, curr); idx++; g_string_sprintf (sql_buffer, ARRAY_SEL, column, idx, query_pk); PQclear (res); res = PQexec (conn, sql_buffer->str); } } PQclear (res); g_string_free (sql_buffer, TRUE); return the_list; } GString *create_int_arr_str (GList *list) { GList *iter; GString *str; GString *tmp; /* * The extra space gives us a spot to put the closing brace if * the list is empty, and it does no harm if the list is not. */ str = g_string_new ("{ "); tmp = g_string_new (""); iter = g_list_first (list); while (iter) { g_string_sprintf (tmp, "%d,", GPOINTER_TO_INT (iter->data)); str = g_string_append (str, tmp->str); iter = iter->next; } str->str[str->len - 1] = '}'; g_string_free (tmp, TRUE); return str; } GString *create_txt_arr_str (GList *list) { GList *iter; GString *str; GString *tmp; /* * The extra space gives us a spot to put the closing brace if * the list is empty, and it does no harm if the list is not. */ str = g_string_new ("{ "); tmp = g_string_new (""); iter = g_list_first (list); while (iter) { g_string_sprintf (tmp, "\"%s\",", (gchar *)iter->data); str = g_string_append (str, tmp->str); iter = iter->next; } str->str[str->len - 1] = '}'; g_string_free (tmp, TRUE); return str; }