예제 #1
0
파일: example.c 프로젝트: GNOME/libgda
/*
 * Open a connection to the example.db file
 */
GdaConnection *
open_connection ()
{
        GdaConnection *cnc;
        GError *error = NULL;

	/* open connection */
        cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
					    GDA_CONNECTION_OPTIONS_NONE,
					    &error);
        if (!cnc) {
                g_print ("Could not open connection: %s\n",
                         error && error->message ? error->message : "No detail");
                exit (1);
        }

	/* create an SQL parser */
	parser = gda_connection_create_parser (cnc);
	if (!parser) /* @cnc does not provide its own parser => use default one */
		parser = gda_sql_parser_new ();
	/* attach the parser object to the connection */
	g_object_set_data_full (G_OBJECT (cnc), "parser", parser, g_object_unref);

        return cnc;
}
예제 #2
0
GdaConnection *
open_connection (void)
{
        GdaConnection *cnc;
        GError *error = NULL;
        cnc = gda_connection_open_from_dsn ("SalesTest", NULL, 
					    GDA_CONNECTION_OPTIONS_NONE,
					    &error);
        if (!cnc) {
                g_print ("Could not open connection to DSN 'SalesTest': %s\n",
                         error && error->message ? error->message : "No detail");
                exit (1);
        }
        return cnc;
}
예제 #3
0
파일: example.c 프로젝트: GNOME/libgda
int
main (int argc, char** argv)
{
	GdaConnection *cnc;
	guint id1, id2, id3;
	GError *error = NULL;

	gda_init ();

	/* REM: the GDA_CONNECTION_OPTIONS_THREAD_ISOLATED flag is necessary
	 * to make sure the connection is opened in a separate thread from the main thread in order
	 * for the asynchronous method to work. Failing to do so will generally (depending on the
	 * database provider's implementation) lead to asynchronous execution failure.
	 */
	cnc = gda_connection_open_from_dsn ("SalesTest", NULL, GDA_CONNECTION_OPTIONS_THREAD_ISOLATED, NULL);
	if (!cnc) {
		g_print ("Can't open connection: %s\n", error && error->message ? error->message : "No detail");
		return 1;
	}

	/* execute */
	id1 = request_execution (cnc, "SELECT * from customers");
	id2 = request_execution (cnc, "SELECT * from products LIMIT 2");
	id3 = request_execution_with_params (cnc, "SELECT * from customers WHERE id>=##theid::int", "theid", "6", NULL);

	/* fetch result */
	fetch_execute_result (cnc, id2);
	fetch_execute_result (cnc, 10);
	fetch_execute_result (cnc, id1);
	fetch_execute_result (cnc, id2);
	fetch_execute_result (cnc, id3);

	/*gda_statement_get_parameters (stmt, &parameters, NULL);*/
  
	g_object_unref (cnc);

	return 0;
}
예제 #4
0
파일: background.c 프로젝트: UIKit0/libgda
int
main (int argc, char *argv[])
{
	GdaConnection* connection;
	GdaDataModel* data;
	GError* error = NULL;
	GValue *value;

	gda_init();
	error = NULL;

	/* open connection to the SalesTest data source, using the GDA_CONNECTION_OPTIONS_THREAD_SAFE flag
	 * to make sure we can use the same connection from multiple threads at once */
	connection = gda_connection_open_from_dsn ("SalesTest", NULL, GDA_CONNECTION_OPTIONS_THREAD_SAFE, &error);
	if (!connection) {
		fprintf (stderr, "%s\n", error->message);
		return -1;
	}

	/* update meta data */
	GdaThreadWrapper *wrapper;
	guint job_id;

	g_print ("Requesting a meta data update in the background.\n");
	wrapper = gda_thread_wrapper_new ();
	job_id = gda_thread_wrapper_execute (wrapper, (GdaThreadWrapperFunc) sub_thread_update_meta_store,
					     connection, NULL, &error);
	if (job_id == 0) {
		fprintf (stderr, "Can't use thread wrapper: %s\n", error->message);
		return -1;
	}

	while (1) {
		gboolean *result;
		g_print ("Doing some business here...\n");
		g_usleep (100000);
		result = (gboolean *) gda_thread_wrapper_fetch_result (wrapper, FALSE, job_id, &error);
		if (result) {
			gboolean meta_ok;

			g_object_unref (wrapper);
			meta_ok = *result;
			g_free (result);
			if (!meta_ok) {
				fprintf (stderr, "Could not update meta data: %s\n", error->message);
				return -1;
			}
			g_print ("Meta data has been updated!\n");
			break;
		}
	}

	/*
	 * Get columns of the 'products' table
	 */
	g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), "products");
	data = gda_connection_get_meta_store_data (connection, GDA_CONNECTION_META_FIELDS, &error, 1,
						   "name", value);
	if (!data)
		return -1;
	list_table_columns (data);
	g_object_unref (data);

	return 0;
}
예제 #5
0
static void
dsn_test_cb (G_GNUC_UNUSED GSimpleAction *action, GVariant *state, gpointer data)
{
	GdauiDsnEditor *editor;
	editor = GDAUI_DSN_EDITOR (data);

	GtkWidget *test_dialog = NULL;
	GdauiLogin* login = NULL;
	GdaConnection *cnc = NULL;
	gboolean auth_needed = gdaui_dsn_editor_need_authentication (editor);

	GtkWindow *parent = NULL;
	parent = (GtkWindow*) gtk_widget_get_ancestor (GTK_WIDGET (editor), GTK_TYPE_WINDOW);

	const gchar *dsn;
	dsn = editor->priv->name;
	if (auth_needed) {
		gchar *title;
		title = g_strdup_printf (_("Login for %s"), dsn);
		test_dialog = gdaui_login_dialog_new (title, parent);
		g_free (title);
		login = gdaui_login_dialog_get_login_widget (GDAUI_LOGIN_DIALOG (test_dialog));
		g_object_set (G_OBJECT (login), "dsn", dsn, NULL);
	}

	if (!auth_needed || gdaui_login_dialog_run (GDAUI_LOGIN_DIALOG (test_dialog))) {
		if (test_dialog)
			parent = GTK_WINDOW (test_dialog);

		GtkWidget *msgdialog;
		GError *error = NULL;
		const GdaDsnInfo *cinfo = NULL;

		if (login)
			cinfo = gdaui_login_get_connection_information (login);
		cnc = gda_connection_open_from_dsn (dsn,
						    cinfo ? cinfo->auth_string : NULL,
						    GDA_CONNECTION_OPTIONS_NONE, &error);
		if (cnc) {
			msgdialog = gtk_message_dialog_new_with_markup (parent,
									GTK_DIALOG_MODAL,
									GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
									"<b>%s</b>",
									_("Connection successfully opened!"));
			gda_connection_close (cnc, NULL);
		}
		else {
			msgdialog = gtk_message_dialog_new_with_markup (parent,
									GTK_DIALOG_MODAL,
									GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
									"<b>%s:</b>\n%s",
									_("Could not open connection"),
									error->message ? error->message : _("No detail"));
			if (error)
				g_error_free (error);
		}

		gtk_dialog_run (GTK_DIALOG (msgdialog));
		gtk_widget_destroy (msgdialog);
	}
	if (test_dialog)
		gtk_widget_destroy (test_dialog);
}
예제 #6
0
/*
 * Open a connection
 */
static GdaConnection*
open_connection (const gchar *cnc_string, GError **error)
{
	GdaConnection *cnc = NULL;

	GdaDsnInfo *info;
	gchar *user, *pass, *real_cnc, *real_provider, *real_auth_string = NULL;
	gda_connection_string_split (cnc_string, &real_cnc, &real_provider, &user, &pass);
	if (!real_cnc) {
		g_free (user);
		g_free (pass);
		g_free (real_provider);
		g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_DSN_NOT_FOUND_ERROR, 
			     "Malformed connection string '%s'", cnc_string);
		return NULL;
	}

	if (ask_pass) {
		if (user && !*user) {
			gchar buf[80];
			g_print ("\tUsername for '%s': ", cnc_string);
			if (scanf ("%80s", buf) == -1) {
				g_free (real_cnc);
				g_free (user);
				g_free (pass);
				g_free (real_provider);
				g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_DSN_NOT_FOUND_ERROR, 
					     "No username for '%s'", cnc_string);
				return NULL;
			}
			g_free (user);
			user = g_strdup (buf);
		}
		if (pass && !*pass) {
			gchar buf[80];
			g_print ("\tPassword for '%s': ", cnc_string);
			if (scanf ("%80s", buf) == -1) {
				g_free (real_cnc);
				g_free (user);
				g_free (pass);
				g_free (real_provider);
				g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_DSN_NOT_FOUND_ERROR, 
					     "No password for '%s'", cnc_string);
				return NULL;
			}
			g_free (pass);
			pass = g_strdup (buf);
		}
		if (user || pass) {
			gchar *s1;
			s1 = gda_rfc1738_encode (user);
			if (pass) {
				gchar *s2;
				s2 = gda_rfc1738_encode (pass);
				real_auth_string = g_strdup_printf ("USERNAME=%s;PASSWORD=%s", s1, s2);
				g_free (s2);
			}
			else
				real_auth_string = g_strdup_printf ("USERNAME=%s", s1);
			g_free (s1);
		}
	}
	
	info = gda_config_get_dsn_info (real_cnc);
	if (info && !real_provider)
		cnc = gda_connection_open_from_dsn (cnc_string, real_auth_string, 0, error);
	else 
		cnc = gda_connection_open_from_string (NULL, cnc_string, real_auth_string, 0, error);
	
	g_free (real_cnc);
	g_free (user);
	g_free (pass);
	g_free (real_provider);
	g_free (real_auth_string);

	return cnc;
}
예제 #7
0
int 
main (int argc, char **argv)
{
	GError *error = NULL;	
	GOptionContext *context;

	GdaConnection *cnc;
	gchar *auth_string = NULL;
	gchar *blob_data;

	/* command line parsing */
	context = g_option_context_new ("Tests opening a connection");
	g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
	if (!g_option_context_parse (context, &argc, &argv, &error)) {
		g_warning ("Can't parse arguments: %s", error->message);
		exit (1);
	}
	g_option_context_free (context);
	
	if (direct && dsn) {
		g_print ("DSN and connection string are exclusive\n");
		exit (1);
	}

	if (!direct && !dsn) {
		g_print ("You must specify a connection to open either as a DSN or a connection string\n");
		exit (1);
	}

	if (direct && !prov) {
		g_print ("You must specify a provider when using a connection string\n");
		exit (1);
	}

	gda_init ();

	/* open connection */
	if (user) {
		if (pass)
			auth_string = g_strdup_printf ("USERNAME=%s;PASSWORD=%s", user, pass);
		else
			auth_string = g_strdup_printf ("USERNAME=%s", user);
	}
	if (dsn) {
		GdaDsnInfo *info = NULL;
		info = gda_config_get_dsn_info (dsn);
		if (!info)
			g_error (_("DSN '%s' is not declared"), dsn);
		else {
			cnc = gda_connection_open_from_dsn (info->name, auth_string ? auth_string : info->auth_string,
							    0, &error);
			if (!cnc) {
				g_warning (_("Can't open connection to DSN %s: %s\n"), info->name,
				   error && error->message ? error->message : "???");
				exit (1);
			}
			prov = info->provider;
		}
	}
	else {
		
		cnc = gda_connection_open_from_string (prov, direct, auth_string, 0, &error);
		if (!cnc) {
			g_warning (_("Can't open specified connection: %s\n"),
				   error && error->message ? error->message : "???");
			exit (1);
		}
	}
	g_free (auth_string);

	g_print (_("Connection successfully opened!\n"));

	parser = gda_connection_create_parser (cnc);
	gda_connection_begin_transaction (cnc, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN, NULL);

	/* 
	 * clear all blobs 
	 */
	if (!clear_blobs (cnc, &error))
		g_error ("Blobs clear error: %s", error && error->message ? error->message : "No detail");

	/* insert a blob */
	blob_data = "Blob Data 1";
	if (!insert_blob (cnc, 1, blob_data, strlen (blob_data), &error)) 
		g_error ("Blob insert error: %s", error && error->message ? error->message : "No detail");
	else if (error) {
		g_print ("Msg: %s\n", error->message);
		g_error_free (error);
		error = NULL;
	}

	/* insert a blob */
	blob_data = "Blob Data 2";
	if (!insert_blob (cnc, 2, blob_data, strlen (blob_data), &error)) 
		g_error ("Blob insert error: %s", error && error->message ? error->message : "No detail");
	else if (error) {
		g_print ("Msg: %s\n", error->message);
		g_error_free (error);
		error = NULL;
	}
	if (!display_blobs (cnc, &error))
		g_error ("Blobs display error: %s", error && error->message ? error->message : "No detail");


	/* update blob */
	blob_data = "New blob 1 contents is now this one...";
	if (!update_blob (cnc, 1, blob_data, strlen (blob_data), &error)) 
		g_error ("Blob update error: %s", error && error->message ? error->message : "No detail");
	else if (error) {
		g_print ("Msg: %s\n", error->message);
		g_error_free (error);
		error = NULL;
	}
	if (!display_blobs (cnc, &error))
		g_error ("Blobs display error: %s", error && error->message ? error->message : "No detail");

	/* update blob */
	blob_data = "After several blobs updated";
	if (!update_multiple_blobs (cnc, blob_data, strlen (blob_data), &error)) 
		g_error ("Multiple blob update error: %s", error && error->message ? error->message : "No detail");
	else if (error) {
		g_print ("Msg: %s\n", error->message);
		g_error_free (error);
		error = NULL;
	}
	if (!display_blobs (cnc, &error))
		g_error ("Blobs display error: %s", error && error->message ? error->message : "No detail");


	/* SQL Postgres:
	   create table blobs (id serial not null primary key, name varchar (50), data oid);
	   SQL Oracle:
	   CREATE TABLE blobs (id number primary key, name varchar2 (50), data BLOB);
	*/

	gda_connection_commit_transaction (cnc, NULL, NULL);
	if (! gda_connection_close (cnc, &error))
		g_error ("Can't close connection: %s", error && error->message ? error->message : "No detail");

	return 0;
}