static void fetch_execute_result (GdaConnection *cnc, guint task_id) { GObject *result; GError *error = NULL; gint i; for (i = 0; i < 10; i++) { g_print ("=========== Waiting for task %u\n", task_id); result = gda_connection_async_fetch_result (cnc, task_id, NULL, &error); if (result) { if (GDA_IS_DATA_MODEL (result)) gda_data_model_dump (GDA_DATA_MODEL (result), NULL); else g_print ("Unknown result: %s\n", G_OBJECT_TYPE_NAME (result)); g_object_unref (result); return; } else if (error) { if (error && (error->domain == GDA_CONNECTION_ERROR) && (error->code == GDA_CONNECTION_TASK_NOT_FOUND_ERROR)) g_print ("Task not found error: %s\n", error->message); else g_print ("Execution failed: %s\n", error->message ? error->message : "No detail"); return; } else { g_print ("Not yet executed...\n"); g_usleep (100000); } } }
static gboolean exec_statement (GdaConnection *cnc, GdaStatement *stmt, GdaSet *plist, GError **error) { GObject *exec_res; exec_res = gda_connection_statement_execute (cnc, stmt, plist, GDA_STATEMENT_MODEL_RANDOM_ACCESS, NULL, error); if (!exec_res) return FALSE; if (GDA_IS_DATA_MODEL (exec_res)) { g_print ("Query returned a GdaDataModel...\n"); gda_data_model_dump ((GdaDataModel*) exec_res, stdout); } else { if (GDA_IS_SET (exec_res)) { GSList *list; g_print ("Query returned a GdaSet:\n"); for (list = GDA_SET (exec_res)->holders; list; list = list->next) { gchar *str; str = gda_holder_get_value_str (GDA_HOLDER (list->data), NULL); g_print (" %s => %s\n", gda_holder_get_id (GDA_HOLDER (list->data)), str); g_free (str); } } else g_print ("Query returned a %s object\n", G_OBJECT_TYPE_NAME (exec_res)); } return TRUE; }
int main (int argc, char *argv []) { GdaDataModel *model; gint i, nrows; GError *error = NULL; gda_init (); model = g_object_new (TYPE_CUSTOM_DATA_MODEL, "filename", DATABASE, NULL); gda_data_model_dump (model, stdout); nrows = gda_data_model_get_n_rows (model); i = gda_data_model_append_row (model, &error); if (i < 0) { g_print ("Could not append row: %s\n", error && error->message ? error->message : "no detail"); exit (1); } else { GValue *value; GdaBinary bin; Key m_key; Value m_value; strncpy (m_key.color, "black", COLORSIZE); m_key.type = 100; bin.data = (gpointer) &m_key; bin.binary_length = sizeof (Key); value = gda_value_new (GDA_TYPE_BINARY); gda_value_set_binary (value, &bin); if (!gda_data_model_set_value_at (model, 0, i, value, &error)) { g_print ("Could not set key: %s\n", error && error->message ? error->message : "no detail"); exit (1); } gda_value_free (value); m_value.size = 100.1; strncpy (m_value.name, "blackhole", NAMESIZE); bin.data = (gpointer) &m_value; bin.binary_length = sizeof (Value); value = gda_value_new (GDA_TYPE_BINARY); gda_value_set_binary (value, &bin); if (!gda_data_model_set_value_at (model, 1, i, value, &error)) { g_print ("Could not set key: %s\n", error && error->message ? error->message : "no detail"); exit (1); } gda_value_free (value); } g_object_unref (model); return 0; }
/* * display the contents of the 'products' table */ void display_customers (GdaConnection *cnc) { GdaDataModel *data_model; GdaSqlParser *parser; GdaStatement *stmt; gchar *sql = "SELECT id, name FROM customers ORDER BY id"; GError *error = NULL; parser = g_object_get_data (G_OBJECT (cnc), "parser"); stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL); data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error); g_object_unref (stmt); if (!data_model) g_error ("Could not get the contents of the 'products' table: %s\n", error && error->message ? error->message : "No detail"); gda_data_model_dump (data_model, stdout); g_object_unref (data_model); }
static gboolean display_blobs (GdaConnection *cnc, GError **error) { GdaStatement *stmt; gboolean retval; GdaDataModel *model; stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM blobs", NULL, error); if (!stmt) return FALSE; model = gda_connection_statement_execute_select (cnc, stmt, NULL, error); retval = model ? TRUE : FALSE; if (model) { gda_data_model_dump (model, stdout); g_object_unref (model); } return retval; }
static const GValue *_select_guid_at_offset(const gchar *table, guint offset) { GdaCommand *command; GdaDataModel *model; GString *sql = g_string_new("SELECT guid FROM "); g_string_append_printf(sql, "%s LIMIT 1 OFFSET %d", table, offset); command = gda_command_new (sql->str, GDA_COMMAND_TYPE_SQL, 0); model = gda_connection_execute_select_command (mgd->priv->connection, command, NULL, NULL); gda_command_free (command); g_string_free(sql, TRUE); if (!model) return NULL; gda_data_model_dump (model, stdout); return gda_data_model_get_value_at_col_name(model, "guid", 0); }
/* * display the contents of the 'products' table */ void display_products_contents (GdaConnection *cnc) { GdaDataModel *data_model; GdaStatement *stmt; gchar *sql = "SELECT * FROM products"; GError *error = NULL; GdaSqlParser *parser; parser = gda_connection_create_parser (cnc); if (!parser) /* @cnc doe snot provide its own parser => use default one */ parser = gda_sql_parser_new (); stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL); data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error); g_object_unref (stmt); if (!data_model) g_error ("Could not get the contents of the 'products' table: %s\n", error && error->message ? error->message : "No detail"); gda_data_model_dump (data_model, stdout); g_object_unref (data_model); }
/* * Test 1: gda_connection_open() when there is no error */ static guint test1 (void) { g_print ("============= %s started =============\n", __FUNCTION__); GdaConnection *cnc; GError *error = NULL; gchar *cnc_string, *fname; fname = g_build_filename (ROOT_DIR, "data", NULL); cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=sales_test", fname); g_free (fname); cnc = gda_connection_new_from_string ("SQLite", cnc_string, NULL, GDA_CONNECTION_OPTIONS_READ_ONLY, NULL); if (!cnc) { g_print ("gda_connection_new_from_string([%s]) failed: %s\n", cnc_string, error && error->message ? error->message : "No detail"); g_free (cnc_string); return 1; } guint counter = 0; setup_main_context (cnc, &counter); /* connection open */ if (! gda_connection_open (cnc, &error)) { g_print ("gda_connection_open([%s]) failed: %s\n", cnc_string, error && error->message ? error->message : "No detail"); g_free (cnc_string); return 1; } g_free (cnc_string); if (counter == 0) { g_print ("gda_connection_open() failed: did not make GMainContext 'run'\n"); return 1; } else g_print ("Counter incremented to %u\n", counter); g_print ("Connection %p Opened\n", cnc); /* Update meta store */ counter = 0; if (! gda_connection_update_meta_store (cnc, NULL, &error)) { g_print ("gda_connection_update_meta_store () failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } if (counter == 0) { g_print ("gda_connection_update_meta_store() failed: did not make GMainContext 'run'\n"); g_object_unref (cnc); return 1; } else g_print ("Counter incremented to %u\n", counter); /* get meta data */ counter = 0; GdaDataModel *model; model = gda_connection_get_meta_store_data (cnc, GDA_CONNECTION_META_TABLES, &error, 0); if (!model) { g_print ("gda_connection_get_meta_store_data () failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } if (counter == 0) { g_print ("gda_connection_get_meta_store_data() failed: did not make GMainContext 'run'\n"); g_object_unref (cnc); return 1; } else g_print ("Counter incremented to %u\n", counter); gda_data_model_dump (model, NULL); g_object_unref (model); g_object_unref (cnc); return 0; }
static void _run_binary_test() { GdaParameterList *plist; GdaParameter *param; //_create_binary_guid(); const GValue *guid_value = _select_guid_at_offset(BINARY_TABLE, 14); const unsigned char *bguid = (const unsigned char *)g_value_get_string(guid_value); gchar *b = NULL; guint length = strlen(bguid); guint i; gchar *_gstr, *gstr; gstr = _gstr = g_new(gchar , 34); for (i = 0; i < length; i++, bguid++) { unsigned char c = *bguid; *(gstr++) = hexdigits[(c>>4)&0xf]; *(gstr++) = hexdigits[c&0xf]; } *gstr = '\0'; return; if (!guid_value) g_error ("Returned NULL guid value"); GdaDict *dict = gda_dict_new(); gda_dict_set_connection (dict , mgd->priv->connection); GString *sql = g_string_new ("SELECT guid FROM "); g_string_append_printf (sql, "%s WHERE guid= ##/*name:'guid' type:GdaBinary*/", BINARY_TABLE); GdaQuery *query = gda_query_new_from_sql (dict, sql->str, NULL); if (!query) g_error("Failed to create new query from string"); plist = gda_query_get_parameter_list (query); param = gda_parameter_list_find_param (plist, "guid"); if (!param) g_warning ("Failed to find guid param"); GValue binval = {0, }; g_value_init(&binval, GDA_TYPE_BINARY); g_value_transform(guid_value, &binval); gda_parameter_set_value (param, &binval); if (!gda_parameter_is_valid (param)) g_error ("Guid parameter (with value) is invalid"); gda_connection_clear_events_list (mgd->priv->connection); GError *error = NULL; GdaDataModel *model = GDA_DATA_MODEL(gda_query_execute (query, plist, FALSE, &error)); if (!model) g_error("Query returned NULL model"); if (error) g_error ("%s", error->message); gda_data_model_dump (model, stdout); const GValue *rval = gda_data_model_get_value_at_col_name (model, "guid", 0); }
int main (int argc, char *argv[]) { gtk_init (&argc, &argv); gdaui_init (); /* create data model */ GdaDataModel *model; model = data_model_errors_new (); gda_data_model_dump (model, NULL); /* create UI */ GtkWidget *window, *vbox, *button, *form, *grid; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW(window), 400, 200); g_signal_connect_swapped (window, "destroy", G_CALLBACK (destroy), window); gtk_container_set_border_width (GTK_CONTAINER (window), 10); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); gtk_container_add (GTK_CONTAINER (window), vbox); /* main form to list customers */ form = gdaui_form_new (model); gtk_box_pack_start (GTK_BOX (vbox), form, FALSE, FALSE, 0); GtkWidget *raw; g_object_get (form, "raw-form", &raw, NULL); g_timeout_add (80, (GSourceFunc) change_unknow_color, raw); g_object_unref (raw); g_object_set (G_OBJECT (form), "info-flags", GDAUI_DATA_PROXY_INFO_CURRENT_ROW | GDAUI_DATA_PROXY_INFO_ROW_MOVE_BUTTONS | GDAUI_DATA_PROXY_INFO_ROW_MODIFY_BUTTONS, NULL ); /* main grid to list customers */ grid = gdaui_grid_new (model); g_object_unref (model); gtk_box_pack_start (GTK_BOX (vbox), grid, TRUE, TRUE, 0); g_object_set (G_OBJECT (grid), "info-flags", GDAUI_DATA_PROXY_INFO_CURRENT_ROW | GDAUI_DATA_PROXY_INFO_ROW_MODIFY_BUTTONS, NULL ); /* button to quit */ button = gtk_button_new_with_label ("Quit"); gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window); gtk_widget_show_all (window); gtk_main (); return 0; }
int main (int argc, char *argv []) { GdaDataModel *model; gint i, nrows; GError *error = NULL; gda_init (); if (! g_file_test (DATABASE, G_FILE_TEST_EXISTS)) { g_print ("File '%s' does not exist\n", DATABASE); exit (1); } model = gda_data_model_bdb_new (DATABASE, NULL); gda_data_model_dump (model, stdout); nrows = gda_data_model_get_n_rows (model); for (i = 0; i < nrows; i++) { Key *key; Value *value; const GValue *c_value; const GdaBinary *bin; g_print ("=============== ROW %d\n", i); c_value= gda_data_model_get_value_at (model, 0, i, &error); if (!c_value) { g_print ("Could not get value from data model: %s\n", error && error->message ? error->message : "No detail"); exit (1); } bin = gda_value_get_binary (c_value); key = (Key *)bin->data; g_print ("color/type = %s/%d\n", key->color, key->type); c_value= gda_data_model_get_value_at (model, 1, i, &error); if (!c_value) { g_print ("Could not get value from data model: %s\n", error && error->message ? error->message : "No detail"); exit (1); } bin = gda_value_get_binary (c_value); value = (Value *)bin->data; g_print ("size/name = %f/%s\n", value->size, value->name); } i = gda_data_model_append_row (model, &error); if (i < 0) { g_print ("Could not append row: %s\n", error && error->message ? error->message : "no detail"); exit (1); } else { { /* EXTRA tests */ gda_data_model_dump (model, stdout); if (!gda_data_model_remove_row (model, i, &error)) { g_print ("Could not remove row: %s\n", error && error->message ? error->message : "no detail"); exit (1); } gda_data_model_dump (model, stdout); gchar *str = "AAA"; GValue *value = gda_value_new_binary ((guchar*) str, 4); if (!gda_data_model_set_value_at (model, 1, 2, value, &error)) { g_print ("Could not set value: %s\n", error && error->message ? error->message : "no detail"); exit (1); } gda_data_model_dump (model, stdout); exit (0); } GValue *value; GdaBinary bin; Key m_key; Value m_value; strncpy (m_key.color, "black", COLORSIZE); m_key.type = 100; bin.data = (gpointer) &m_key; bin.binary_length = sizeof (Key); value = gda_value_new (GDA_TYPE_BINARY); gda_value_set_binary (value, &bin); if (!gda_data_model_set_value_at (model, 0, i, value, &error)) { g_print ("Could not set key: %s\n", error && error->message ? error->message : "no detail"); exit (1); } gda_value_free (value); m_value.size = 100.1; strncpy (m_value.name, "blackhole", NAMESIZE); bin.data = (gpointer) &m_value; bin.binary_length = sizeof (Value); value = gda_value_new (GDA_TYPE_BINARY); gda_value_set_binary (value, &bin); if (!gda_data_model_set_value_at (model, 1, i, value, &error)) { g_print ("Could not set key: %s\n", error && error->message ? error->message : "no detail"); exit (1); } gda_value_free (value); } g_object_unref (model); return 0; }
int main (int argc, char *argv[]) { gda_init (); GdaConnection *cnc; GError *error = NULL; GdaStatement *stmt; GdaDataModel *model; gchar *str; GValue *name; /* open connection */ cnc = open_connection (); /* begin transaction */ if (! gda_connection_begin_transaction (cnc, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN, &error)) { g_print ("Could not begin transaction: %s\n", error && error->message ? error->message : "No detail"); exit (1); } /* execute SELECT */ stmt = gda_sql_parser_parse_string (parser, "SELECT id, name FROM customers ORDER BY id", NULL, NULL); g_assert (stmt); model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error); g_object_unref (stmt); if (!model) { g_print ("Could not execute SELECT statement: %s\n", error && error->message ? error->message : "No detail"); exit (1); } g_print ("** Data model is:\n"); gda_data_model_dump (model, stdout); /* * make sure the mete data is up to date */ g_print ("Computing meta data, this may take a while; in real applications, this should be cached to avoid waiting\n"); if (! gda_connection_update_meta_store (cnc, NULL, &error)) { g_print ("Could not fetch meta data: %s\n", error && error->message ? error->message : "No detail"); exit (1); } /* * Make the data model compute the modification statements which * will actually be executed when the data model is modified */ if (! gda_data_select_compute_modification_statements (GDA_DATA_SELECT (model), &error)) { g_print ("Could not compute modification statements: %s\n", error && error->message ? error->message : "No detail"); exit (1); } g_object_get (G_OBJECT (model), "update-stmt", &stmt, NULL); str = gda_statement_to_sql (stmt, NULL, NULL); g_print ("Computed UPDATE: %s\n", str); g_free (str); g_object_unref (stmt); g_object_get (G_OBJECT (model), "delete-stmt", &stmt, NULL); str = gda_statement_to_sql (stmt, NULL, NULL); g_print ("Computed DELETE: %s\n", str); g_free (str); g_object_unref (stmt); g_object_get (G_OBJECT (model), "insert-stmt", &stmt, NULL); str = gda_statement_to_sql (stmt, NULL, NULL); g_print ("Computed INSERT: %s\n", str); g_free (str); g_object_unref (stmt); /* * remove row 0 (1st row) */ g_print ("\n\n** Removing row 0\n"); if (! gda_data_model_remove_row (model, 0, &error)) { g_print ("Could not remove row 0: %s\n", error && error->message ? error->message : "No detail"); exit (1); } g_print ("** Data model is now:\n"); gda_data_model_dump (model, stdout); g_print ("** Table's contents is now:\n"); display_customers (cnc); /* * add a row: the row's values is a list of GValue pointers * (or NULL pointers where the default value should be inserted */ GList *list; g_print ("\n\n** Adding a row\n"); list = g_list_append (NULL, NULL); g_value_set_string ((name = gda_value_new (G_TYPE_STRING)), "Hiro"); list = g_list_append (list, name); if (gda_data_model_append_values (model, list, &error) == -1) { g_print ("Could not add a row: %s\n", error && error->message ? error->message : "No detail"); exit (1); } gda_value_free (name); g_list_free (list); g_print ("** Data model is now:\n"); gda_data_model_dump (model, stdout); g_print ("** Table's contents is now:\n"); display_customers (cnc); /* * alter row 2 */ g_print ("\n\n** Modifying row 2\n"); g_value_set_string ((name = gda_value_new (G_TYPE_STRING)), "Tom"); if (! gda_data_model_set_value_at (model, 1, 2, name, &error)) { g_print ("Could not modify row 2: %s\n", error && error->message ? error->message : "No detail"); exit (1); } gda_value_free (name); g_print ("** Data model is now:\n"); gda_data_model_dump (model, stdout); g_print ("** Table's contents is now:\n"); display_customers (cnc); /* rollback transaction */ gda_connection_rollback_transaction (cnc, NULL, NULL); gda_connection_close (cnc); return 0; }
/* * Test 1: gda_connection_open() when there is no error */ static guint test1 (void) { g_print ("============= %s started =============\n", __FUNCTION__); GdaConnection *cnc; GError *error = NULL; gchar *cnc_string, *fname; fname = g_build_filename (ROOT_DIR, "data", NULL); cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=sales_test", fname); g_free (fname); cnc = gda_connection_new_from_string ("SQLite", cnc_string, NULL, GDA_CONNECTION_OPTIONS_READ_ONLY, NULL); if (!cnc) { g_print ("gda_connection_new_from_string([%s]) failed: %s\n", cnc_string, error && error->message ? error->message : "No detail"); g_free (cnc_string); return 1; } guint counter = 0; setup_main_context (cnc, 0, &counter); /* connection open */ if (! gda_connection_open (cnc, &error)) { g_print ("gda_connection_open([%s]) failed: %s\n", cnc_string, error && error->message ? error->message : "No detail"); g_free (cnc_string); return 1; } g_free (cnc_string); if (counter == 0 && !connected) { g_print ("gda_connection_open() failed: did not make GMainContext 'run'\n"); return 1; } else g_print ("Counter incremented to %u\n", counter); g_print ("Connection %p Opened\n", cnc); /* SELECT */ counter = 0; GdaDataModel *model; model = gda_connection_execute_select_command (cnc, "SELECT * FROM customers", &error); if (model) { gda_data_model_dump (model, NULL); gint expnrows = 5; if (gda_data_model_get_n_rows (model) != expnrows) { g_print ("SELECT Exec() failed: expected %d and got %d\n", expnrows, gda_data_model_get_n_rows (model)); g_object_unref (model); g_object_unref (cnc); return 1; } g_object_unref (model); } else { g_print ("gda_connection_execute_select_command() failed: %s\n", error && error->message ? error->message : "No detail"); g_object_unref (cnc); return 1; } if (counter == 0 && !connected) { g_print ("gda_connection_open() failed: did not make GMainContext 'run'\n"); g_object_unref (cnc); return 1; } else g_print ("Counter incremented to %u\n", counter); g_object_unref (cnc); return 0; }