static gboolean __mysql_reconnect (MidgardConnection *mgd) { g_assert (mgd != NULL); guint i = 0; gboolean opened; gda_connection_close_no_warning (mgd->priv->connection); g_signal_emit (mgd, MIDGARD_CONNECTION_GET_CLASS (mgd)->signal_id_disconnected, 0); /* Try to reconnect 3 times */ do { g_usleep (5000000); /* 5 seconds interval */ g_debug ("Trying to reopen connection"); opened = gda_connection_open (mgd->priv->connection, NULL); if (opened) { g_signal_emit (mgd, MIDGARD_CONNECTION_GET_CLASS (mgd)->signal_id_connected, 0); g_debug ("Successfully reopened connection"); return TRUE; } i++; } while (i < 3); return FALSE; }
/* * 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; cnc = gda_connection_new_from_string ("SQLite", "DB_NAME=test-cnc-opendb", NULL, GDA_CONNECTION_OPTIONS_AUTO_META_DATA, &error); if (!cnc) { g_print ("gda_connection_new_from_string() failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } guint counter = 0; setup_main_context (cnc, &counter); if (! gda_connection_open (cnc, &error)) { g_print ("gda_connection_open() failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } 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_object_unref (cnc); return 0; }
static guint test2 (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; } /* 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); #define NB_THREADS 3 GThread *ths[NB_THREADS]; guint i; for (i = 0; i < NB_THREADS; i++) { gchar *tmp; tmp = g_strdup_printf ("th%u", i); ths[i] = g_thread_new (tmp, (GThreadFunc) test2_th, cnc); g_free (tmp); g_print ("Thread %u is %p\n", i, ths[i]); } for (i = 0; i < NB_THREADS; i++) { guint *counter; counter = g_thread_join (ths[i]); if (*counter == 0 && !connected) { g_print ("Thread %u: gda_connection_lock() failed: did not make GMainContext 'run'\n", i); return 1; } else g_print ("Thread %u: Counter incremented to %u\n", i, *counter); g_free (counter); } g_object_unref (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, &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; }
/* * Test 5: open, close and open again, also tests signal's emission */ static guint test5 (void) { g_print ("============= %s started =============\n", __FUNCTION__); GdaConnection *cnc; GError *error = NULL; cnc = gda_connection_new_from_string ("SQLite", "DB_NAME=test-cnc-opendb", NULL, GDA_CONNECTION_OPTIONS_AUTO_META_DATA, &error); if (!cnc) { g_print ("gda_connection_new_from_string() failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } guint nsignals = 0; g_signal_connect (cnc, "status-changed", G_CALLBACK (t5_status_changed_cb), &nsignals); guint counter = 0; setup_main_context (cnc, &counter); /* open */ if (! gda_connection_open (cnc, &error)) { g_print ("gda_connection_open() 1 failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } if (counter == 0) { g_print ("gda_connection_open() 1 failed: did not make GMainContext 'run'\n"); return 1; } else g_print ("Counter incremented to %u\n", counter); /* open */ counter = 0; if (! gda_connection_close (cnc, &error)) { g_print ("gda_connection_close() failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } if (counter == 0) { g_print ("gda_connection_close() failed: did not make GMainContext 'run'\n"); return 1; } else g_print ("Counter incremented to %u\n", counter); /* open */ counter = 0; if (! gda_connection_open (cnc, &error)) { g_print ("gda_connection_open() 2 failed: %s\n", error && error->message ? error->message : "No detail"); return 1; } if (counter == 0) { g_print ("gda_connection_open() 2 failed: did not make GMainContext 'run'\n"); return 1; } else g_print ("Counter incremented to %u\n", counter); g_object_unref (cnc); if (nsignals != 6) { g_print ("Expected %d signals and got %d\n", 6, nsignals); return 1; } 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; }