Exemplo n.º 1
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;
	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;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
static guint
test4 (void)
{
	g_print ("============= %s started =============\n", __FUNCTION__);
	GdaConnection *cnc;
	GError *error = NULL;
	cnc = gda_connection_new_from_string ("SQLite", "DB_NAMEEEE=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);

	GMainLoop *loop;
	loop = g_main_loop_new (gda_connection_get_main_context (cnc, NULL), FALSE);

	guint job_id;
	job_id = gda_connection_open_async (cnc, (GdaConnectionOpenFunc) test4_open_func, loop, &error);

	if (!job_id) {
		g_print ("gda_connection_open_async() failed: %s\n", error && error->message ? error->message : "No detail");
		return 1;
	}
	else {
		g_print ("Connection opening job is %u\n", job_id);
	}

	g_main_loop_run (loop);

	gboolean opened;
	opened = gda_connection_is_opened (cnc);
	g_object_unref (cnc);

	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);

	return opened ? 1 : 0;
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
/*
 * @prov may be NULL
 */
static guint
do_a_test (GdaServerProvider *prov, GdaSqlParser *parser)
{
	guint nfailed = 0;
	GdaStatement *stmt;
	GdaSet *params;
	gchar *sql;
	GError *error = NULL;

	/* SQL parsed as an INSERT statement */
	sql = "INSERT INTO tstest VALUES (##ts::timestamp, ##time::time)";
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error);
	if (!stmt) {
		g_print ("Failed to parse [%s]: %s\n", sql, error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		nfailed ++;
		goto endtest;
	}

	g_assert (gda_statement_get_statement_type (stmt) == GDA_SQL_STATEMENT_INSERT);
	if (! gda_statement_get_parameters (stmt, &params, &error)) {
		g_print ("Failed to obtain parameters: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "ts", &ts)) {
		g_print ("Failed to bind 'ts' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "time", &gt)) {
		g_print ("Failed to bind 'time' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	GdaConnection *cnc = NULL;
	if (prov) {
		cnc = gda_connection_new_from_string (gda_server_provider_get_name (prov), "DB_NAME=dummy;HOST=dummy", NULL,
						      GDA_CONNECTION_OPTIONS_NONE, &error);
		if (!cnc) {
			g_print ("Failed to create GdaConnection object: %s\n",
				 error && error->message ? error->message : "No detail");
			g_clear_error (&error);
			g_object_unref (stmt);
			g_object_unref (params);
			nfailed ++;
			goto endtest;
		}
	}
	gchar *expected;
	expected = "('@@@@@@@@@@ 17:10:23+2', '16:09:22-3')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, 0, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, 0, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

	expected = "('@@@@@@@@@@ 15:10:23', '19:09:22')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL for GMT timezone: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

	/* SQL not parsed as a valid statement */
	sql = "AAAA (##ts::timestamp, ##time::time)";
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error);
	if (!stmt) {
		g_print ("Failed to parse [%s]: %s\n", sql, error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		nfailed ++;
		goto endtest;
	}

	g_assert (gda_statement_get_statement_type (stmt) == GDA_SQL_STATEMENT_UNKNOWN);
	if (! gda_statement_get_parameters (stmt, &params, &error)) {
		g_print ("Failed to obtain parameters: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "ts", &ts)) {
		g_print ("Failed to bind 'ts' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "time", &gt)) {
		g_print ("Failed to bind 'time' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	expected = "('@@@@@@@@@@ 17:10:23+2', '16:09:22-3')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, 0, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, 0, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

	expected = "('@@@@@@@@@@ 15:10:23', '19:09:22')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL for GMT timezone: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

 endtest:
	return nfailed;
}
Exemplo n.º 7
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;
}