Re: [gnome-db] libgda cursor support



On Wed, 2007-09-26 at 14:22 +0200, Vivien Malerba wrote:
> > > Anyway, adding such methods is very easy and I can do it if you
> want.
> >
> > That would be nice. Thanks. That would help to push that Parameter
> to
> > being "internal API" that I don't have to use.
> 
> Ok, I'll do it.

gda_data_model_iter_set_at_row() doesn't seem to work either. Should it?
The attached test case iterates over the rows  with
gda_data_model_iter_move_next() and gets values. That works.

But then it tries to get the values by using
gda_data_model_iter_set_at_row(). But it displays the same value for
each row (the value for the last row). This is the output for my test
table:

murrayc murrayc-desktop:~$ ./a.out 
row=0
  value=0
row=1
  value=1
row=2
  value=2
row=3
  value=3
Number of columns: 5, Number of rows=3
row=0
  value=3
row=1
  value=3
row=2
  value=3

-- 
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 */, "murraypw" /* 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_data_model_iter_get_param_for_column(iter, 0);

  /* Iterate over the rows: */
  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);

  /* Access rows by number, using the iter: */
  int row_index = 0;
  while(row_index < count_rows)
  {
    printf("row=%d\n", row_index);

    GdaDataModelIter *iter = gda_data_model_create_iter(model);
    gda_data_model_iter_set_at_row(iter, row_index);
   
    GdaParameter *param = gda_data_model_iter_get_param_for_column(iter, 0);
    const GValue *value = gda_parameter_get_value (param);
    gchar *text = gda_value_stringify(value);
    printf ("  value=%s\n", text);
    g_free(text);

    ++row_index;
  }


  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]