Re: [gnome-db] libgda cursor support



On Wed, 2007-09-26 at 10:29 +0200, Vivien Malerba wrote:
> You can't use gda_data_model_get_value_at () when you are using a
> cursor, that API call is for random access (I should make that clear
> in the doc), here is my small correction:
> 
> [...]
> GdaDataModelIter *iter = gda_data_model_create_iter(model);
> 
> /* fetch the GdaParameter in iter for the first column */
> GdaParameter *param = GDA_PARAMETER (GDA_PARAMETER_LIST
> (iter)->parameters->data);
> 
> while(gda_data_model_iter_move_next(iter))
> 	{
> 		const int row = gda_data_model_iter_get_row(iter);
> 		printf("row=%d\n", row);
> 		
> 		/*const GValue *value = gda_data_model_get_value_at(model, 0, row);*/
> 		const GValue *value = gda_parameter_get_value (param);

That's a really horrible API. How do we get a value for a different
column?

I have attached the updated test case.

-- 
murrayc murrayc com
www.murrayc.com
www.openismus.com
//Compile with 
//g++ libgda_cursor_test.c `pkg-config libgda-3.0 --libs --cflags`

#include <libgda/libgda.h>

int
main (int argc, char **argv)
{
  gda_init ("test", "0.1", argc, argv);

  GdaClient* client = gda_client_new();
  GError* error = NULL;
  GdaConnection* connection = gda_client_open_connection_from_string (client, "PostgreSQL", "HOST=localhost;DB_NAME=glom_musiccollection21", "murrayc" /* username */, "something" /* password */, (GdaConnectionOptions)0, &error);
  if(error)
  {
    printf("GError from gda_client_open_connection_from_string(): %s\n", error->message);
    g_error_free(error);
    return EXIT_FAILURE;
  }

  GdaCommand* command = gda_command_new("SELECT * FROM artists", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
   
  GdaParameterList *params = gda_parameter_list_new(NULL);
  GValue value = { 0, };
  g_value_init(&value, G_TYPE_BOOLEAN);
  g_value_set_boolean(&value, TRUE);
  gda_parameter_list_add_param_from_value (params, "ITER_MODEL_ONLY", &value);
    
  GdaDataModel* model = gda_connection_execute_select_command(connection, command, params, &error);
  if(error)
  {
    printf("GError from gda_connection_execute_select_command(): %s\n", error->message);
    g_error_free(error);
    return EXIT_FAILURE;
  }

  if(!model)
  {
     printf("gda_connection_execute_select_command(): returned NULL but did not set the GError.\n");
     return EXIT_FAILURE;
  }

  GdaDataModelIter *iter = gda_data_model_create_iter(model);
  GdaParameter *param = GDA_PARAMETER (GDA_PARAMETER_LIST(iter)->parameters->data);

  int row = 0;
  while(gda_data_model_iter_move_next(iter))
  {
    row = gda_data_model_iter_get_row(iter);    
    printf("row=%d\n", row);

    //Doesn't work with ITER_MODEL_ONLY: const GValue *value = gda_data_model_get_value_at(model, 0, row);
    const GValue *value = gda_parameter_get_value (param);
    gchar *text = gda_value_stringify(value);
    printf ("  value=%s\n", text);
    g_free(text);
  }

  //Doesn't work with ITER_MODEL_ONLY:  const gint count_rows = gda_data_model_get_n_rows(model);
  const gint count_rows = row;
  if(count_rows < 0)
  {
     //This happens when we use the ITER_MODEL_ONLY parameter:
     printf("gda_data_model_get_n_rows()=%d after iterating.\n", count_rows);
     return EXIT_FAILURE;
  }

  printf ("Number of columns: %d, Number of rows=%d\n", gda_data_model_get_n_columns(model), count_rows);

  gda_command_free (command);
  g_object_unref (params);
  g_object_unref (connection);
  g_object_unref (client);

  return EXIT_SUCCESS;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]