Пример #1
0
int 
main (int argc, char** argv)
{
#if GLIB_CHECK_VERSION(2,36,0)
#else
	g_type_init ();
#endif
	gda_init ();

	guint failures = 0;
	guint i, ntests = 0;
  
	data = tests_common_load_data ("stmt.data");
	for (i = 0; i < sizeof (tests) / sizeof (TestFunc); i++) {
		GError *error = NULL;
		if (! tests[i] (&error)) {
			g_print ("Test %d failed: %s\n", i+1, 
				 error && error->message ? error->message : "No detail");
			if (error)
				g_error_free (error);
			failures ++;
		}
		ntests ++;
	}

	g_print ("TESTS COUNT: %d\n", ntests);
	g_print ("FAILURES: %d\n", failures);

	return failures != 0 ? 1 : 0;
}
Пример #2
0
int
main(int argc, char *argv[])
{
    const gchar* connection_string = "HOST=localhost;USER=murrayc;PASSWORD=yourpasswordhere;DATABASE=template1";
    GdaClient     *client = 0;
    GdaConnection *con = 0;

    gda_init ("glom-gda-test", NULL, argc, argv);

    /* 3. Create a gda client */
    client = gda_client_new ();

    /* 4. Open the connection */
    con = gda_client_open_connection_from_string (client, "PostgreSQL", connection_string, 0);
    if (!GDA_IS_CONNECTION (con)) {
        g_print ("** ERROR: could not open connection.\n");
        /* This cannot work because it needs a working connection: get_errors (con); */
        return 0;
    }

    gboolean created = gda_connection_create_database(con, "glomtest");
    if(!created) {
        g_print("** Error: gda_connection_create_database failed.\n");
        get_errors(con);
    }

    gda_connection_close (con);

    g_object_unref (G_OBJECT (client));

    g_print ("** Connection successfully opened, database created, and connection closed.\n");

    return 0;
}
Пример #3
0
/**
 * wmud_db_init:
 * @err: a GError to put error messages in it
 *
 * Initializes the wMUD database system. Checks and opens database files.
 */
gboolean
wmud_db_init(GError **err)
{
    GError *local_err = NULL;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Initializing database");

    gda_init();

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
          "Database DSN is \"%s\"",
          active_config->database_dsn);
    dbh = gda_connection_open_from_string(NULL,
                                          active_config->database_dsn,
                                          NULL,
                                          GDA_CONNECTION_OPTIONS_THREAD_SAFE,
                                          &local_err);

    if (dbh == NULL) {
        g_set_error(err,
                    WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN,
                    "Can not open databsae (%s): %s",
                    active_config->database_dsn,
                    local_err->message);

        return FALSE;
    }

    parser = gda_sql_parser_new();

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Database initialization finished.");

    return TRUE;
}
Пример #4
0
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;
}
Пример #5
0
int
main (int argc, char** argv)
{
	gchar *file;
	GError *error = NULL;
	GdaDataModel *model;
	guint i, nrows;
	guint nfailed = 0;
	gda_init ();

	/* generic parser */
	GdaSqlParser *parser;
	parser = gda_sql_parser_new ();
	nfailed += do_a_test (NULL, parser);
	g_object_unref (parser);

	/* test other parsers only if generic one is Ok */
	if (nfailed == 0) {
		model = gda_config_list_providers ();
		nrows = gda_data_model_get_n_rows (model);
		for (i = 0; i < nrows; i++) {
			const GValue *cvalue;
			cvalue = gda_data_model_get_value_at (model, 0, i, NULL);
			g_assert (cvalue && (G_VALUE_TYPE (cvalue) == G_TYPE_STRING));
			if (!g_ascii_strcasecmp (g_value_get_string (cvalue), "Oracle"))
				continue; /* ignore Oracle for now */
			g_print ("Testing database provider '%s'\n", g_value_get_string (cvalue));

			GdaServerProvider *prov;
			prov = gda_config_get_provider (g_value_get_string (cvalue), NULL);
			g_assert (prov);

			GdaSqlParser *parser;
			parser = gda_server_provider_create_parser (prov, NULL);
			if (!parser)
				parser = gda_sql_parser_new ();

			nfailed += do_a_test (prov, parser);
			g_object_unref (parser);
		}
		g_object_unref (model);
	}

	if (nfailed == 0) {
		g_print ("Ok\n");
		return EXIT_SUCCESS;
	}
	else {
		g_print ("%u failed\n", nfailed);
		return EXIT_FAILURE;
	}
}
Пример #6
0
int main(int argc, const char *argv[])
{
	gda_init ();
	guint nfailed = 0;

	nfailed += test1 ();

	if (nfailed > 0)
		g_print ("Test failed %u times\n", nfailed);
	else
		g_print ("Test Ok\n");
	return nfailed ? 1 : 0;
}
Пример #7
0
int
main (int argc, char *argv[])
{
        gda_init ();

        GdaConnection *cnc;

	/* open connections */
	cnc = open_connection ();
	create_table (cnc);
	display_products_contents (cnc);
        gda_connection_close (cnc);

        return 0;
}
Пример #8
0
int
main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
{
	int number_failed = 0;
	gchar **env;
	const gchar *cnc_string;
	GError *error = NULL;

	env = g_get_environ ();
	cnc_string = g_environ_getenv (env, "MYSQL_CNC_PARAMS");
	if (cnc_string == NULL) {
		g_message ("No enviroment variable MYSQL_CNC_PARAMS was set. No PostgreSQL provider tests will be performed."
		          "Set this environment variable in order to get access to your server. Example: export MYSQL_CNC_PARAMS=\"DB_NAME=$MYSQL_DB;HOST=$MYSQL_HOST;USERNAME=$MYSQL_USER;PASSWORD=$MYSQL_PASSWORD\"");
		g_strfreev (env);
		return EXIT_SUCCESS;
	}

	gda_init ();

	pinfo = gda_config_get_provider_info (PROVIDER);
	if (!pinfo) {
		g_warning ("Could not find provider information for %s", PROVIDER);
		return EXIT_SUCCESS;
	}
	g_print ("Provider now tested: %s\n", pinfo->id);

	cnc = gda_connection_open_from_string (pinfo->id, cnc_string, NULL, GDA_CONNECTION_OPTIONS_NONE, &error);
	if (cnc == NULL) {
		g_warning ("Error opening connection: %s",
			         error && error->message ? error->message : "No error was set");
		return EXIT_FAILURE;
	}
	if (cnc) {
		number_failed += prov_test_common_check_timestamp ();
		number_failed += prov_test_common_check_meta_full ();
		number_failed += prov_test_common_check_meta_partial ();
		number_failed += prov_test_common_check_meta_partial2 ();
		number_failed += prov_test_common_check_meta_partial3 ();
		number_failed += prov_test_common_clean ();
	}

	if (! params_provided)
		return EXIT_SUCCESS;
	else {
		g_print ("Test %s\n", (number_failed == 0) ? "Ok" : "failed");
		return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
	}
}
Пример #9
0
/**
 * @brief The entry point for the program.
 * @param argc The number of arguments given.
 * @param argv The arguments vector.
 */
int main (int argc, char * argv[])
{
    BrokeUIMain  *main_window;
    GnomeDbLogin *login;

    /* libgda, GTK+ initialization */
    gtk_init (&argc, &argv);
    gda_init(BROKE, VERSION, argc, argv);

    main_window = BROKE_UI_MAIN;
    login = main_window->login;

    gtk_window_present (main_window->window);
    gtk_main ();

    return 0;
}
Пример #10
0
int main()
{
	gda_init();
	guint i;
	gint n_errors = 0;

	for (i = 0; i < sizeof (tests) / sizeof (ATest); i++) {
		ATest *test = &tests[i];

		if (! do_test (test))
			n_errors++;
	}

	if (n_errors == 0)
		g_print ("Ok: %d tests passed\n", i);
	else
		g_print ("Failed: %d tests total, %d failed\n", i, n_errors);
	return n_errors ? 1 : 0;
}
Пример #11
0
int main(int argc, const char *argv[])
{
	gda_init ();
	guint nfailed = 0;

	nfailed += test1 ();
	nfailed += test2 ();
	nfailed += test3 ();
	nfailed += test4 ();
	nfailed += test5 ();

	g_unlink ("test-cnc-opendb.db");

	if (nfailed > 0)
		g_print ("Test failed %u times\n", nfailed);
	else
		g_print ("Test Ok\n");
	return nfailed ? 1 : 0;
}
Пример #12
0
G_MODULE_EXPORT void
plugin_init (PlannerPlugin *plugin)
{
	PlannerPluginPriv *priv;
	GtkUIManager      *ui;
	gint               i = -1;
	gchar             *filename;

	priv = g_new0 (PlannerPluginPriv, 1);
	plugin->priv = priv;

	gda_init (PACKAGE, VERSION, 0, NULL);

	g_object_set_data (G_OBJECT (plugin->main_window),
			   PROJECT_ID,
			   GINT_TO_POINTER (i));
	g_object_set_data (G_OBJECT (plugin->main_window),
			   "sql-plugin-revision",
			   GINT_TO_POINTER (i));

	g_object_set_data (G_OBJECT (plugin->main_window),
			   "sql-plugin",
			   plugin);

	/* Create the actions, get the ui manager and merge the whole */
	priv->actions = gtk_action_group_new ("SQL plugin actions");
	gtk_action_group_set_translation_domain (priv->actions, GETTEXT_PACKAGE);

	gtk_action_group_add_actions (priv->actions,
				      entries,
				      G_N_ELEMENTS (entries),
				      plugin);

	ui = planner_window_get_ui_manager (plugin->main_window);
	gtk_ui_manager_insert_action_group (ui, priv->actions, 0);

	filename = mrp_paths_get_ui_dir ("sql-plugin.ui");
	gtk_ui_manager_add_ui_from_file (ui, filename, NULL);
	g_free (filename);

	gtk_ui_manager_ensure_update (ui);
}
Пример #13
0
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;
}
Пример #14
0
int
main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
{
	int number_failed = 0;
	fork_tests = FALSE;
	gchar **env;
	const gchar *cnc_string;

	env = g_get_environ ();
	cnc_string = g_environ_getenv (env, "POSTGRESQL_DBCREATE_PARAMS");
	if (cnc_string == NULL) {
		g_message ("No enviroment variable POSTGRESQL_DBCREATE_PARAMS was set. No PostgreSQL provider tests will be performed."
		          "Set this environment variable in order to get access to your server. Example: export POSTGRESQL_DBCREATE_PARAMS=\"HOST=postgres;ADM_LOGIN=$POSTGRES_USER;ADM_PASSWORD=$POSTGRES_PASSWORD\"");
		g_strfreev (env);
		return EXIT_SUCCESS;
	}

	gda_init ();

	pinfo = gda_config_get_provider_info (PROVIDER);
	if (!pinfo) {
		g_warning ("Could not find provider information for %s", PROVIDER);
		return EXIT_SUCCESS;
	}
	g_print ("============= Provider Meta Partial 1 Update now testing: %s =============\n", pinfo->id);

	number_failed = prov_test_common_setup ();

	if (cnc) {
		number_failed += prov_test_common_check_meta_partial ();
		number_failed += prov_test_common_clean ();
	}

	g_print ("Test %s\n", (number_failed == 0) ? "Ok" : "failed");
	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Пример #15
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;
}
Пример #16
0
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;
}
Пример #17
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;
}
Пример #18
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;
}
Пример #19
0
int
main (int argc, char *argv[])
{
	gda_init ();

	GdaSqlBuilder *b;

	/* INSERT INTO customers (e, f, g) VALUES (##p1::string, 15, 'joe') */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);

	gda_sql_builder_set_table (b, "customers");

	gda_sql_builder_add_field_value_id (b,
				   gda_sql_builder_add_id (b, "e"),
				   gda_sql_builder_add_param (b, "p1", G_TYPE_STRING, FALSE));
	gda_sql_builder_add_field_value_id (b,
				   gda_sql_builder_add_id (b, "f"),
				   gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 15));
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "g"),
				   gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "joe"));

	render_as_sql (b);
	g_object_unref (b);


	/* UPDATE products set ref='A0E''FESP' WHERE id = 14 */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_UPDATE);

	gda_sql_builder_set_table (b, "products");
	gda_sql_builder_add_field_value (b, "ref", G_TYPE_STRING, "A0E'FESP");
	GdaSqlBuilderId id_field = gda_sql_builder_add_id (b, "id");
	GdaSqlBuilderId id_value = gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 14);
	GdaSqlBuilderId id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ, id_field, id_value, 0);
	gda_sql_builder_set_where (b, id_cond);

	render_as_sql (b);

	/* reuse the same GdaSqlBuilder object to change the WHERE condition to: WHERE id = ##theid::int */
	gda_sql_builder_set_where (b,
				   gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ,
							     id_field,
							     gda_sql_builder_add_param (b, "theid", G_TYPE_INT, FALSE),
							     0));
	render_as_sql (b);
	g_object_unref (b);

	/* DELETE FROM items WHERE id = ##theid::int */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_DELETE);

	gda_sql_builder_set_table (b, "items");
	id_field = gda_sql_builder_add_id (b, "id");
	GdaSqlBuilderId id_param = gda_sql_builder_add_param (b, "theid", G_TYPE_INT, FALSE);
	id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ, id_field, id_param, 0);
	gda_sql_builder_set_where (b, id_cond);

	render_as_sql (b);
	g_object_unref (b);

	/*
	 * The next statement shows automatic quoting of reserved SQL keywords (DATE and SELECT here)
	 *
	 * SELECT c."date", name, date AS person FROM "select" as c, orders
	 */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);

	GdaSqlBuilderId id_table = gda_sql_builder_add_id (b, "select"); /* SELECT is an SQL reserved keyword */
	GdaSqlBuilderId id_target1 = gda_sql_builder_select_add_target_id (b, id_table, "c");
	GdaSqlBuilderId id_target2 = gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "orders"),
					   NULL);
	GdaSqlBuilderId id_join = gda_sql_builder_select_join_targets (b, id_target1, id_target2, GDA_SQL_SELECT_JOIN_INNER, 0);

	gda_sql_builder_add_field_value_id (b,
				   gda_sql_builder_add_id (b, "c.date"), 0); /* DATE is an SQL reserved keyword */
	gda_sql_builder_add_field_value_id (b,
				   gda_sql_builder_add_id (b, "name"),
				   gda_sql_builder_add_id (b, "person"));

	render_as_sql (b);

	/* reuse the same GdaSqlBuilder object to change the INNER join's condition */
	gda_sql_builder_join_add_field (b, id_join, "id");

	render_as_sql (b);
	g_object_unref (b);

	/* SELECT myfunc (a, 5, 'Joe') FROM mytable */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "mytable"),
					   NULL);
	GdaSqlBuilderId id_function_myfunc = gda_sql_builder_add_function (b, "myfunc",
				      gda_sql_builder_add_id (b, "a"),
				      gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 5),
				      gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "Joe"),
				      0);
	gda_sql_builder_add_field_value_id (b, id_function_myfunc, 0);
	render_as_sql (b);

	/* reuse the same GdaSqlBuilder object to have:
	 * SELECT myfunc (a, 5, 'Joe'), MAX (myfunc (a, 5, 'Joe'), b, 10) FROM mytable */
	GdaSqlBuilderId id_b = gda_sql_builder_add_id (b, "b");
	GdaSqlBuilderId id_b_value = gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 10);

	GdaSqlBuilderId args[] = {id_function_myfunc, id_b, id_b_value};
	GdaSqlBuilderId id_function_max = gda_sql_builder_add_function_v (b, "MAX", args, 3);
	gda_sql_builder_add_field_value_id (b, id_function_max, 0);

	render_as_sql (b);
	g_object_unref (b);

	/* testing identifiers which are SQL reserved keywords */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_UPDATE);

	gda_sql_builder_set_table (b, "select");
	gda_sql_builder_add_field_value_id (b,
				   gda_sql_builder_add_id (b, "date"),
				   gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "2009-05-27"));
	id_field = gda_sql_builder_add_id (b, "id");
	id_value = gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 14);
	id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ, id_field, id_value, 0);
	gda_sql_builder_set_where (b, id_cond);

	render_as_sql (b);
	g_object_unref (b);

	/* testing identifiers which are SQL reserved keywords */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "date"),
					   NULL);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "MyTable"),
					   NULL);
	GdaSqlBuilderId id_function = gda_sql_builder_add_function (b, "date",
				      gda_sql_builder_add_id (b, "a"),
				      gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 5),
				      gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "Joe"),
				      0);
	gda_sql_builder_add_field_value_id (b, id_function, 0);
	render_as_sql (b);
	g_object_unref (b);

	/* Subselect: SELECT name FROM master WHERE id IN (SELECT id FROM subdata) */
	GdaSqlStatement *sub;
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "id"), 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "subdata"),
					   NULL);
	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "name"), 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "master"),
					   NULL);
	id_field = gda_sql_builder_add_id (b, "id");
	GdaSqlBuilderId id_subselect = gda_sql_builder_add_sub_select (b, sub);
	gda_sql_statement_free (sub);
	id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_IN, id_field, id_subselect, 0);
	gda_sql_builder_set_where (b, id_cond);
	render_as_sql (b);
	g_object_unref (b);

	/* SELECT id, name, (SELECT MAX (ts) FROM documents AS d WHERE t.id = d.topic) FROM topics AS t */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "documents"), "d");
	gda_sql_builder_add_field_value_id (b,
				      gda_sql_builder_add_function (b, "MAX",
								    gda_sql_builder_add_id (b, "ts"),
								    0), 0);
	gda_sql_builder_set_where (b,
				   gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ,
							     gda_sql_builder_add_id (b, "t.id"),
							     gda_sql_builder_add_id (b, "d.topic"), 0));
	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "topics"), "t");
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "id"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "name"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_sub_select (b, sub), 0);
	gda_sql_statement_free (sub);

	render_as_sql (b);
	g_object_unref (b);

	/* Subselect in INSERT: INSERT INTO customers (e, f, g) SELECT id, name, location FROM subdate */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "id"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "name"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "location"), 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "subdate"),
					   NULL);
	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);

	gda_sql_builder_set_table (b, "customers");
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "e"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "f"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "g"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_sub_select (b, sub), 0);
	gda_sql_statement_free (sub);

	render_as_sql (b);
	g_object_unref (b);


	/* compound: SELECT id, name FROM subdata1 UNION SELECT ident, lastname FROM subdata2 */
	GdaSqlStatement *sub1, *sub2;
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "id"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "name"), 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "subdata1"),
					   NULL);
	sub1 = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "ident"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "lastname"), 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "subdata2"),
					   NULL);
	sub2 = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_COMPOUND);
	gda_sql_builder_compound_add_sub_select (b, sub1);
	gda_sql_builder_compound_add_sub_select (b, sub2);
	gda_sql_statement_free (sub1);
	gda_sql_statement_free (sub2);
	render_as_sql (b);
	g_object_unref (b);

	/* SELECT CASE WHEN price < 1.200000 THEN 2 ELSE 1 END FROM data */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_LT,
					    gda_sql_builder_add_id (b, "price"),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_FLOAT, 1.2), 0);

	GdaSqlBuilderId id_case = gda_sql_builder_add_case (b,
						  0,
						  gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 1),
						  id_cond, gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 2),
						  0);
	gda_sql_builder_add_field_value_id (b, id_case, 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "data"),
					   NULL);
	render_as_sql (b);
	g_object_unref (b);

	/* SELECT CASE tag WHEN 'Alpha' THEN 1 WHEN 'Bravo' THEN 2 WHEN 'Charlie' THEN 3 ELSE 0 END FROM data */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	id_case = gda_sql_builder_add_case (b,
					    gda_sql_builder_add_id (b, "tag"),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 0),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "Alpha"),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 1),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "Bravo"),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 2),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "Charlie"),
					    gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 3),
					    0);
	gda_sql_builder_add_field_value_id (b, id_case, 0);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "data"),
					   NULL);
	render_as_sql (b);
	g_object_unref (b);

	/*
	 * SELECT people.firstname AS person, people.lastname, "date" AS birthdate, age FROM people
	 */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);

	gda_sql_builder_select_add_field (b, "firstname", "people", "person");
	gda_sql_builder_select_add_field (b, "lastname", "people", NULL);
	gda_sql_builder_select_add_field (b, "date", NULL, "birthdate");
	gda_sql_builder_select_add_field (b, "age", NULL, NULL);
	gda_sql_builder_select_add_target_id (b,
					   gda_sql_builder_add_id (b, "people"),
					   NULL);

	render_as_sql (b);
	g_object_unref (b);

	/* INSERT INTO customers (f, g) VALUES (15, 'joe') */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);

	gda_sql_builder_set_table (b, "customers");
	gda_sql_builder_add_field_value (b, "f", G_TYPE_INT, 15);
	gda_sql_builder_add_field_value (b, "g", G_TYPE_STRING, "joe");

	render_as_sql (b);
	g_object_unref (b);

	/* Create a WHERE clause in a statement and reuse it in another one:
	 *
	 * - the first SQL built is DELETE FROM items WHERE id = ##theid::int
	 * - the "id = ##theid::int" is exported from the first build and imported into the final build
	 * - the final SQL is: SELECT id FROM mytable WHERE (name = ##thename::string) AND (id = ##theid::int)
	 */
	GdaSqlExpr *expr;
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_DELETE);

	gda_sql_builder_set_table (b, "items");
	id_field = gda_sql_builder_add_id (b, "id");
	id_param = gda_sql_builder_add_param (b, "theid", G_TYPE_INT, FALSE);
	id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ, id_field , id_param, 0);
	gda_sql_builder_set_where (b, id_cond);

	render_as_sql (b);
	expr = gda_sql_builder_export_expression (b, id_cond);
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "id"), 0);
	gda_sql_builder_select_add_target_id (b,
					      gda_sql_builder_add_id (b, "mytable"),
					      NULL);
	id_field = gda_sql_builder_add_id (b, "name");
	id_param = gda_sql_builder_add_param (b, "thename", G_TYPE_STRING, FALSE);
	id_cond = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ, id_field, id_param, 0);

	gda_sql_builder_set_where (b,
				   gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_AND, id_cond,
							     gda_sql_builder_import_expression (b, expr),
							     0));
	gda_sql_expr_free (expr);
	render_as_sql (b);
	g_object_unref (b);

	/* Subselect: SELECT name FROM (SELECT id FROM subdata) as sub */
	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "id"), 0);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "name"), 0);
	gda_sql_builder_select_add_target_id (b,
					      gda_sql_builder_add_id (b, "subdata"),
					      NULL);
	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
	g_object_unref (b);

	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
	gda_sql_builder_add_field_value_id (b, gda_sql_builder_add_id (b, "name"), 0);
	gda_sql_builder_select_add_target_id (b,
					      gda_sql_builder_add_sub_select (b, sub), "sub");
	gda_sql_statement_free (sub);

	render_as_sql (b);
	g_object_unref (b);


	return 0;
}
Пример #20
0
int
main (int argc, char *argv[])
{
	GOptionContext *context;
	GError *error = NULL;
	int exit_status = EXIT_SUCCESS;
	GSList *list, *cnc_list = NULL;

	context = g_option_context_new ("[DSN|connection string]...");
	g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
        if (!g_option_context_parse (context, &argc, &argv, &error)) {
                g_print ("Can't parse arguments: %s\n", error->message);
		exit_status = EXIT_FAILURE;
		goto cleanup;
        }
        g_option_context_free (context);
        gda_init ();
	ask_pass = !ask_pass;

	config = g_new0 (HtmlConfig, 1);
	html_init_config (HTML_CONFIG (config));
	config->index = html_file_new (HTML_CONFIG (config), 
				       "index.html", "Providers status");
	config->dir = g_strdup (".");

	/* parse command line arguments for connections */
	if (argc > 1) {
		gint i;
		for (i = 1; i < argc; i++) {
			/* open connection */
			GdaConnection *cnc;
			cnc = open_connection (argv[i], &error);
			if (!cnc) {
				g_print ("Can't open connection to '%s': %s\n", argv[i],
					 error && error->message ? error->message : "No detail");
				exit_status = EXIT_FAILURE;
				goto cleanup;
			}
			cnc_list = g_slist_append (cnc_list, cnc);
		}
	}
	else {
		if (getenv ("GDA_SQL_CNC")) {
			GdaConnection *cnc;
			cnc = open_connection (getenv ("GDA_SQL_CNC"), &error);
			if (!cnc) {
				g_print ("Can't open connection defined by GDA_SQL_CNC: %s\n",
					 error && error->message ? error->message : "No detail");
				exit_status = EXIT_FAILURE;
				goto cleanup;
			}
			cnc_list = g_slist_append (cnc_list, cnc);
		}
		else {
			/* report status for all providers */
			GdaDataModel *providers;
			gint i, nb;
			
			providers = gda_config_list_providers ();
			nb = gda_data_model_get_n_rows (providers);
			for (i = 0; i < nb; i++) {
				GdaServerProvider *prov = NULL;
				const gchar *pname;
				const GValue *cvalue;

				cvalue = gda_data_model_get_value_at (providers, 0, i, &error);
				if (!cvalue) 
					g_error ("Can't load next provider: %s\n",
						 error && error->message ? error->message : "No detail");
				pname = g_value_get_string (cvalue);
				prov = gda_config_get_provider (pname, &error);
				if (!prov) 
					g_error ("Can't load the '%s' provider: %s\n", pname,
						 error && error->message ? error->message : "No detail");
				if (!report_provider_status (prov, NULL)) {
					exit_status = EXIT_FAILURE;
					goto cleanup;
				}
			}
			g_object_unref (providers);
		}
	}
	
	/* report provider's status for all the connections */
	for (list = cnc_list; list; list = list->next) {
		if (!report_provider_status (NULL, GDA_CONNECTION (list->data))) {
			exit_status = EXIT_FAILURE;
			goto cleanup;

		}
	}

	g_slist_foreach (HTML_CONFIG (config)->all_files, (GFunc) html_file_write, config);
	
	/* cleanups */
 cleanup:
	g_slist_foreach (cnc_list, (GFunc) g_object_unref, NULL);
	g_slist_free (cnc_list);

	return exit_status;
}
Пример #21
0
int 
main (int argc, char **argv)
{
	GdaReportEngine *eng;
	GdaConnection *cnc;
	GdaHolder *param;
	GdaReportDocument *doc;

	gda_init ();

	/* Doc object */
	doc = gda_report_docbook_document_new (NULL);
	g_object_get (G_OBJECT (doc), "engine", &eng, NULL);
	gda_report_document_set_template (doc, "customers-report-spec.xml");
	g_object_set (G_OBJECT (doc), "fo-stylesheet", "/usr/share/xml/docbook/stylesheet/nwalsh/fo/docbook.xsl", NULL);
	
	/* GdaConnection */
	cnc = open_connection ();
	gda_report_engine_declare_object (eng, G_OBJECT (cnc), "main_cnc");

	/* define parameters */
	param = gda_holder_new_string ("abstract", "-- This text is from a parameter set in the code, not in the spec. file --");
	gda_report_engine_declare_object (eng, G_OBJECT (param), "abstract");
	g_object_unref (param);

	/* create queries */
	GSList *queries, *list;
	queries = create_queries (cnc);
	for (list = queries; list; list = list->next) {
		gda_report_engine_declare_object (eng, G_OBJECT (list->data), g_object_get_data (G_OBJECT (list->data), "name"));
		g_object_unref (G_OBJECT (list->data));
	}
	g_slist_free (queries);
	g_object_unref (eng);

	/* use the doc object */
	GError *error = NULL;
	gchar *outfile = "customers-report-docbook.pdf";
	if (! (gda_report_document_run_as_pdf (doc, outfile, &error))) {
		g_print ("gda_report_document_run_as_pdf error: %s\n",
			 error && error->message ? error->message : "No detail");
		exit (1);
	}
	else
		g_print ("%s file generated\n", outfile);

#ifdef HTML
	outfile = "customers-report-docbook.html";
	if (! (gda_report_document_run_as_html (doc, outfile, &error))) {
		g_print ("gda_report_document_run_as_html error: %s\n",
			 error && error->message ? error->message : "No detail");
		exit (1);
	}
	else
		g_print ("%s file generated\n", outfile);
#endif

	g_object_unref (cnc);
	g_object_unref (doc);

	return 0;
}
Пример #22
0
int 
main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char** argv)
{
	xmlDocPtr doc;
        xmlNodePtr root, node;
	gint failures = 0;
	gint ntests = 0;
	gchar *fname;

	gda_init ();

	/* open connection */
	gchar *cnc_string;
	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_open_from_string ("SQLite", cnc_string, NULL,
					       GDA_CONNECTION_OPTIONS_READ_ONLY, NULL);
	if (!cnc) {
		g_print ("Failed to open connection, cnc_string = %s\n", cnc_string);
		exit (1);
	}
	if (!gda_connection_update_meta_store (cnc, NULL, NULL)) {
		g_print ("Failed to update meta store, cnc_string = %s\n", cnc_string);
		exit (1);
	}
	g_free (cnc_string);

	/* load file */
	fname = g_build_filename (ROOT_DIR, "tests", "parser", "testvalid.xml", NULL);
	if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
                g_print ("File '%s' does not exist\n", fname);
                exit (1);
        }

	/* use test data */
	doc = xmlParseFile (fname);
	g_free (fname);
	g_assert (doc);
	root = xmlDocGetRootElement (doc);
	g_assert (!strcmp ((gchar*) root->name, "testdata"));
	for (node = root->children; node; node = node->next) {
		if (strcmp ((gchar*) node->name, "test"))
			continue;
		xmlNodePtr snode;
		xmlChar *sql = NULL;
		xmlChar *id;
		gboolean valid = FALSE;

		id = xmlGetProp (node, BAD_CAST "id");
		for (snode = node->children; snode; snode = snode->next) {
			if (!strcmp ((gchar*) snode->name, "sql")) {
				sql = xmlNodeGetContent (snode);
				xmlChar *prop;
				prop = xmlGetProp (snode, (const xmlChar*) "valid");
				if (prop) {
					if ((*prop == 't') || (*prop == 'T') || (*prop == '1'))
						valid = TRUE;
					xmlFree (prop);
				}
			}
		}
		if (sql) {
			if (!do_test (id, sql, valid))
				failures++;
			ntests++;
		}

		/* mem free */
		if (sql) xmlFree (sql);
		if (id)	xmlFree (id);
	}
	xmlFreeDoc (doc);

	g_print ("TESTS COUNT: %d\n", ntests);
	g_print ("FAILURES: %d\n", failures);
  
	return failures != 0 ? 1 : 0;
}
Пример #23
0
gint 
main (int argc, char **argv) {
	gchar *xml_dir;
	GOptionContext *context;
	GError *error = NULL;

	context = g_option_context_new (_("Gda server operations list"));
        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);
		return 1;
        }
        g_option_context_free (context);

	gda_init ();
	xml_dir = gda_gbr_get_file_path (GDA_DATA_DIR, "libgda-6.0", NULL);
	g_print (_("Using XML descriptions in %s\n"), xml_dir);
	if (prov)
		g_print ("For provider %s\n", prov);

	if (prov) {
		prov_obj = gda_config_get_provider (prov, &error);
		if (!prov_obj) {
			g_print (_("Could not create provider object: %s\n"), 
				 error && error->message ? error->message : _("No detail"));
			return 1;
		}
	}

	if (list_ops) {
		GdaServerOperationType type;
		if (prov)
			g_print (_("Existing operation types for provider '%s':\n"), prov);
		else
			g_print (_("Existing operation types:\n"));
		for (type = GDA_SERVER_OPERATION_CREATE_DB; type < GDA_SERVER_OPERATION_LAST; type++) {
			if (! prov_obj ||
			    (prov_obj && gda_server_provider_supports_operation (prov_obj, NULL, type, NULL)))
				g_print ("%s\n", gda_server_operation_op_type_to_string (type));
		}
		return 0;
	}

	GdaServerOperationType type;
	for (type = GDA_SERVER_OPERATION_CREATE_DB; type != GDA_SERVER_OPERATION_LAST; type++) {
		xmlDocPtr doc;
		GError *error = NULL;
		gboolean op_supported;

		if (op && strcmp (op, gda_server_operation_op_type_to_string (type)))
			continue;

		g_print (_("Description for type: %s\n"), gda_server_operation_op_type_to_string (type));
		doc = merge_specs (xml_dir, type, &op_supported, &error);
		if (doc) {
			if (out_tree) {
				GdaTree *tree;
				GdaTreeManager *mgr;

				tree = gda_tree_new ();
				mgr = gda_tree_mgr_xml_new (xmlDocGetRootElement (doc), "prov_name|id|name|gdatype|node_type|descr");
				gda_tree_add_manager (tree,  mgr);
				gda_tree_manager_add_manager (mgr, mgr);
				g_object_unref (mgr);
				gda_tree_update_all (tree, NULL);
				gda_tree_dump (tree, NULL, NULL);
				g_object_unref (tree);
			}
			else {
				xmlChar *buf;
				gint len;
				xmlKeepBlanksDefault (0);
				xmlDocDumpFormatMemory (doc, &buf, &len, 1);
				g_print ("%s\n", buf);
				xmlFree (buf);
			}
			xmlFreeDoc (doc);
		}
		else {
			if (!op_supported)
				g_print (_("Operation not supported\n"));
			else
				g_print (_("Error: %s\n"), error && error->message ? error->message : _("No detail"));
			if (error)
				g_error_free (error);
		}
	}
	g_free (xml_dir);

	return 0;
}
Пример #24
0
int
main (int argc, char **argv)
{
	DBusGConnection *session_bus;
	GError *error = NULL;
	RBShell *rb_shell;
	gboolean activated;
	gboolean autostarted;
	char *accel_map_file = NULL;
	char *desktop_file_path;

	GOptionContext *context;
	static const GOptionEntry options []  = {
		{ "debug",           'd', 0, G_OPTION_ARG_NONE,         &debug,           N_("Enable debug output"), NULL },
		{ "debug-match",     'D', 0, G_OPTION_ARG_STRING,       &debug_match,     N_("Enable debug output matching a specified string"), NULL },
		{ "no-update",	       0, 0, G_OPTION_ARG_NONE,         &no_update,       N_("Do not update the library with file changes"), NULL },
		{ "no-registration", 'n', 0, G_OPTION_ARG_NONE,         &no_registration, N_("Do not register the shell"), NULL },
		{ "dry-run",	       0, 0, G_OPTION_ARG_NONE,         &dry_run,         N_("Don't save any data permanently (implies --no-registration)"), NULL },
		{ "disable-plugins",   0, 0, G_OPTION_ARG_NONE,         &disable_plugins, N_("Disable loading of plugins"), NULL },
		{ "rhythmdb-file",     0, 0, G_OPTION_ARG_STRING,       &rhythmdb_file,   N_("Path for database file to use"), NULL },
		{ "playlists-file",    0, 0, G_OPTION_ARG_STRING,       &playlists_file,   N_("Path for playlists file to use"), NULL },
		{ "quit",	     'q', 0, G_OPTION_ARG_NONE,         &quit,            N_("Quit Rhythmbox"), NULL },
		{ G_OPTION_REMAINING,  0, 0, G_OPTION_ARG_STRING_ARRAY, &remaining_args,  NULL, N_("[URI...]") },
		{ NULL }
	};

	g_thread_init (NULL);

	rb_profile_start ("starting rhythmbox");

	autostarted = (g_getenv ("DESKTOP_AUTOSTART_ID") != NULL);

#ifdef USE_UNINSTALLED_DIRS
	desktop_file_path = g_build_filename (SHARE_UNINSTALLED_BUILDDIR, "rhythmbox.desktop", NULL);

	g_setenv ("GSETTINGS_SCHEMA_DIR", SHARE_UNINSTALLED_BUILDDIR, TRUE);
#else
	desktop_file_path = g_build_filename (DATADIR, "applications", "rhythmbox.desktop", NULL);
#endif
	egg_set_desktop_file (desktop_file_path);
	g_free (desktop_file_path);

	context = g_option_context_new (NULL);
	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);

	rb_profile_start ("initializing gstreamer");
	g_option_context_add_group (context, gst_init_get_option_group ());
	rb_profile_end ("initializing gstreamer");

	g_option_context_add_group (context, egg_sm_client_get_option_group ());
	g_option_context_add_group (context, gtk_get_option_group (TRUE));

	setlocale (LC_ALL, NULL);

	rb_profile_start ("parsing command line options");
	if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) {
		g_print (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
			 error->message, argv[0]);
		g_error_free (error);
		g_option_context_free (context);
		exit (1);
	}
	g_option_context_free (context);
	rb_profile_end ("parsing command line options");

	g_random_set_seed (time (0));

#ifdef ENABLE_NLS
	/* initialize i18n */
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

	/* ask for utf-8 message text from GStreamer too,
	 * since it doesn't do that itself.
	 */
	bind_textdomain_codeset ("gstreamer-0.10", "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#endif

	if (!debug && debug_match)
		rb_debug_init_match (debug_match);
	else
		rb_debug_init (debug);
	rb_debug ("initializing Rhythmbox %s", VERSION);

#if defined(USE_UNINSTALLED_DIRS)
	g_irepository_prepend_search_path (SHARE_UNINSTALLED_BUILDDIR "/../bindings/gi");
#endif

	/* TODO: kill this function */
	rb_threads_init ();
	gdk_threads_enter ();

	activated = FALSE;

	rb_debug ("going to create DBus object");

	dbus_g_thread_init ();

	session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
	if (session_bus == NULL) {
		g_warning ("couldn't connect to session bus: %s", (error) ? error->message : "(null)");
		g_clear_error (&error);
	} else if (!no_registration) {
		guint request_name_reply;
		int flags;
#ifndef DBUS_NAME_FLAG_DO_NOT_QUEUE
		flags = DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT;
#else
		flags = DBUS_NAME_FLAG_DO_NOT_QUEUE;
#endif

		DBusGProxy *bus_proxy;

		bus_proxy = dbus_g_proxy_new_for_name (session_bus,
						       "org.freedesktop.DBus",
						       "/org/freedesktop/DBus",
						       "org.freedesktop.DBus");

		if (!dbus_g_proxy_call (bus_proxy,
					"RequestName",
					&error,
					G_TYPE_STRING,
					"org.gnome.Rhythmbox",
					G_TYPE_UINT,
					flags,
					G_TYPE_INVALID,
					G_TYPE_UINT,
					&request_name_reply,
					G_TYPE_INVALID)) {
			g_warning ("Failed to invoke RequestName: %s",
				   error->message);
		}
		g_object_unref (bus_proxy);

		if (request_name_reply == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
		    || request_name_reply == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER)
			activated = FALSE;
		else if (request_name_reply == DBUS_REQUEST_NAME_REPLY_EXISTS
			 || request_name_reply == DBUS_REQUEST_NAME_REPLY_IN_QUEUE)
			activated = TRUE;
		else {
			g_warning ("Got unhandled reply %u from RequestName",
				   request_name_reply);
			activated = FALSE;
		}
	}

	if (!activated) {
		if (quit) {
			rb_debug ("was asked to quit, but no instance was running");
			gdk_notify_startup_complete ();
			exit (0);
		}
#ifdef WITH_RHYTHMDB_GDA
		gda_init (PACKAGE, VERSION, argc, argv);
#endif

		rb_refstring_system_init ();

#ifdef USE_UNINSTALLED_DIRS
		rb_file_helpers_init (TRUE);
#else
		rb_file_helpers_init (FALSE);
#endif

		/* XXX not sure what to do with this.  should we move it to
		 * the config dir, or leave it where it is?
		 */
		accel_map_file = g_build_filename (g_get_home_dir (),
						   ".gnome2",
						   "accels",
						   "rhythmbox",
						   NULL);
		gtk_accel_map_load (accel_map_file);


		rb_debug ("Going to create a new shell");

		rb_stock_icons_init ();

		g_setenv ("PULSE_PROP_media.role", "music", TRUE);

		rb_shell = rb_shell_new (no_registration, no_update, dry_run, autostarted, disable_plugins, rhythmdb_file, playlists_file);
		g_object_weak_ref (G_OBJECT (rb_shell), main_shell_weak_ref_cb, NULL);
		if (!no_registration && session_bus != NULL) {
			dbus_g_object_type_install_info (RB_TYPE_SHELL, &dbus_glib_rb_shell_object_info);
			dbus_g_connection_register_g_object (session_bus, "/org/gnome/Rhythmbox/Shell", G_OBJECT (rb_shell));

			g_signal_connect (G_OBJECT (rb_shell),
					  "database-load-complete",
					  G_CALLBACK (database_load_complete),
					  NULL);
		}
	} else if (!no_registration && session_bus != NULL) {
		DBusGProxy *shell_proxy;
		guint32 current_time;
		current_time = gdk_x11_display_get_user_time (gdk_display_get_default ());
		shell_proxy = dbus_g_proxy_new_for_name_owner (session_bus,
							       "org.gnome.Rhythmbox",
							       "/org/gnome/Rhythmbox/Shell",
							       "org.gnome.Rhythmbox.Shell",
							       &error);
		if (!shell_proxy) {
			g_warning ("Couldn't create proxy for Rhythmbox shell: %s",
				   error->message);
		} else {
			if (quit) {
				dbus_g_proxy_call_no_reply (shell_proxy, "quit",
							    G_TYPE_INVALID);
			} else {
				load_uri_args ((const char **) remaining_args, (GFunc) dbus_load_uri, shell_proxy);
				dbus_g_proxy_call_no_reply (shell_proxy, "present",
							    G_TYPE_UINT, current_time,
							    G_TYPE_INVALID);
			}
			g_object_unref (G_OBJECT (shell_proxy));
		}
	}

	if (activated) {
		gdk_notify_startup_complete ();
	} else {

		rb_profile_start ("mainloop");
#ifdef ENABLE_PYTHON
		if (rb_python_init_successful ()) {
			pyg_begin_allow_threads;
			gtk_main ();
			pyg_end_allow_threads;
		} else {
			gtk_main ();
		}
#else
		gtk_main ();
#endif
		rb_profile_end ("mainloop");

		rb_debug ("out of toplevel loop");

		rb_file_helpers_shutdown ();
		rb_stock_icons_shutdown ();
		rb_refstring_system_shutdown ();
	}

	gst_deinit ();

	rb_debug ("THE END");
	rb_profile_end ("starting rhythmbox");

	if (accel_map_file != NULL) {
		gtk_accel_map_save (accel_map_file);
	}

	gdk_threads_leave ();

	exit (0);
}
Пример #25
0
int
main (int argc, char** argv)
{
    xmlDocPtr doc;
    xmlNodePtr root, node;
    GdaSqlParser *parser;
    gint failures = 0;
    gint ntests = 0;
    gchar *fname;
    GHashTable *parsers_hash;
    GdaDataModel *providers_model;
    gint i;

    gda_init ();

    /* load file */
    fname = g_build_filename (ROOT_DIR, "tests", "parser", "testdata.xml", NULL);
    if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
        g_print ("File '%s' does not exist\n", fname);
        exit (1);
    }

    /* create parsers */
    parsers_hash = g_hash_table_new (g_str_hash, g_str_equal);
    providers_model = gda_config_list_providers ();
    for (i = 0; i < gda_data_model_get_n_rows (providers_model); i++) {
        const GValue *pname;
        GError *lerror = NULL;
        pname = gda_data_model_get_value_at (providers_model, 0, i, &lerror);
        if (!pname) {
            g_print ("Can't get data model's value: %s",
                     lerror && lerror->message ? lerror->message : "No detail");
            exit (1);
        }
        parser = create_parser_for_provider (g_value_get_string (pname));
        g_hash_table_insert (parsers_hash, g_strdup (g_value_get_string (pname)), parser);
        g_print ("Created parser for provider %s\n", g_value_get_string (pname));
    }
    g_object_unref (providers_model);
    g_hash_table_insert (parsers_hash, "", gda_sql_parser_new ());

    /* use test data */
    doc = xmlParseFile (fname);
    g_free (fname);
    g_assert (doc);
    root = xmlDocGetRootElement (doc);
    g_assert (!strcmp ((gchar*) root->name, "testdata"));
    for (node = root->children; node; node = node->next) {
        if (strcmp ((gchar*) node->name, "test"))
            continue;
        xmlNodePtr snode;
        xmlChar *sql = NULL;
        xmlChar *id;
        xmlChar *prov_name;

        prov_name = xmlGetProp (node, BAD_CAST "provider");
        if (prov_name) {
            parser = g_hash_table_lookup (parsers_hash, (gchar *) prov_name);
            xmlFree (prov_name);
        }
        else
            parser = g_hash_table_lookup (parsers_hash, "");
        if (!parser)
            continue;

        id = xmlGetProp (node, BAD_CAST "id");
        for (snode = node->children; snode; snode = snode->next) {
            if (!strcmp ((gchar*) snode->name, "sql"))
                sql = xmlNodeGetContent (snode);
            else if (!strcmp ((gchar*) snode->name, "expected")) {
                xmlChar *expected;
                xmlChar *mode;

                expected = xmlNodeGetContent (snode);
                mode = xmlGetProp (snode, BAD_CAST "mode");
                if (sql) {
                    g_object_set (G_OBJECT (parser), "mode",
                                  mode && !strcmp ((gchar *) mode, "delim") ?
                                  GDA_SQL_PARSER_MODE_DELIMIT : GDA_SQL_PARSER_MODE_PARSE,
                                  NULL);
                    failures += do_test (parser, id,  sql, expected, NULL, NULL);
                    ntests++;
                }
                else
                    g_print ("===== Test '%s' doe not have any <sql> tag!\n", id);
                if (expected) xmlFree (expected);
                if (mode) xmlFree (mode);
            }
            else if (!strcmp ((gchar*) snode->name, "error")) {
                xmlChar *error_line, *error_col;
                xmlChar *mode;
                mode = xmlGetProp (snode, BAD_CAST "mode");
                error_line = xmlGetProp (snode, BAD_CAST "line");
                error_col = xmlGetProp (snode, BAD_CAST "col");
                if (sql) {
                    g_object_set (G_OBJECT (parser), "mode",
                                  mode && !strcmp ((gchar *) mode, "delim") ?
                                  GDA_SQL_PARSER_MODE_DELIMIT : GDA_SQL_PARSER_MODE_PARSE,
                                  NULL);
                    failures += do_test (parser, id, sql, NULL, error_line, error_col);
                    ntests++;
                }
                else
                    g_print ("===== Test '%s' doe not have any <sql> tag!\n", id);

                if (mode) xmlFree (mode);
                if (error_line)	xmlFree (error_line);
                if (error_col) xmlFree (error_col);
            }
        }

        /* mem free */
        if (sql) xmlFree (sql);
        if (id)	xmlFree (id);
    }
    xmlFreeDoc (doc);

    g_print ("TESTS COUNT: %d\n", ntests);
    g_print ("FAILURES: %d\n", failures);

    return failures != 0 ? 1 : 0;
}
Пример #26
0
gboolean __midgard_connection_open(
		MidgardConnection *mgd, 
		GHashTable **hashtable, gboolean init_schema)
{
	g_return_val_if_fail(mgd != NULL, FALSE);

	MIDGARD_ERRNO_SET (mgd, MGD_ERR_OK);

	gchar *host, *dbname, *dbuser, *dbpass, *loglevel, *tmpstr;
	guint port = 0;
	gchar *auth = NULL;
	MidgardConfig *config = mgd->priv->config;
	host = config->host;
	dbname = config->database;
	dbuser = config->dbuser;
	dbpass = config->dbpass;
	loglevel = config->loglevel;
	port = config->dbport;
	gboolean enable_threads = config->gdathreads;

	/* Get 30% performance boost for non threaded applications */
	if(!enable_threads) 
		g_setenv("LIBGDA_NO_THREADS", "yes", TRUE);

	/* Initialize libgda */
	gda_init ();

	midgard_connection_set_loglevel(mgd, loglevel, NULL);

	if(config->priv->dbtype == MIDGARD_DB_TYPE_SQLITE) {

		gchar *path = NULL;
		gchar *dbdir = config->dbdir;
		if (!dbdir || *dbdir == '\0') {
			const gchar *sqlite_dir[] = {"data", NULL};
			path = midgard_core_config_build_path(sqlite_dir, NULL, TRUE);
		} else {
			path = g_strdup(dbdir);
		}

		tmpstr = g_strconcat("DB_DIR=", path, ";", "DB_NAME=", dbname, NULL);
		g_free(path);

	} else if (config->priv->dbtype == MIDGARD_DB_TYPE_ORACLE) {

		GString *cnc = g_string_sized_new(100);
		cnc_add_part(cnc, "TNSNAME", dbname, MGD_MYSQL_HOST);
		cnc_add_part(cnc, "HOST", host, MGD_MYSQL_HOST);
		cnc_add_part(cnc, "DB_NAME", dbname, MGD_MYSQL_DATABASE);
		tmpstr = g_string_free(cnc, FALSE);
		cnc = g_string_sized_new(100);
		cnc_add_part(cnc, "USERNAME", dbuser, MGD_MYSQL_USERNAME);
		cnc_add_part(cnc, "PASSWORD", dbpass, MGD_MYSQL_PASSWORD);
		auth = g_string_free(cnc, FALSE);

	} else { 
		
		GString *cnc = g_string_sized_new(100);
		cnc_add_part(cnc, "HOST", host, MGD_MYSQL_HOST);

		if (port > 0) {

			GString *_strp = g_string_new("");
			g_string_append_printf (_strp, "%d", port);
			cnc_add_part (cnc, "PORT", _strp->str, "");
			g_string_free (_strp, TRUE);
		}

		cnc_add_part(cnc, "DB_NAME", dbname, MGD_MYSQL_DATABASE);
		tmpstr = g_string_free(cnc, FALSE);
		GString *auth_str = g_string_sized_new(100);
		cnc_add_part(auth_str, "USERNAME", dbuser, MGD_MYSQL_USERNAME);
		cnc_add_part(auth_str, "PASSWORD", dbpass, MGD_MYSQL_PASSWORD);
		auth = g_string_free(auth_str, FALSE);
	}

	GError *error = NULL;
	GdaConnection *connection = gda_connection_open_from_string(
			config->dbtype, tmpstr, auth, GDA_CONNECTION_OPTIONS_NONE, &error);
	g_free(auth);	

	if(connection == NULL) {

		MIDGARD_ERRNO_SET_STRING (mgd, MGD_ERR_NOT_CONNECTED, 
				" Database [%s]. %s", tmpstr, error->message);

		g_free(tmpstr);

		return FALSE;
	
	} 

	g_free(tmpstr);

	mgd->priv->parser = gda_connection_create_parser (connection);
	if (!mgd->priv->parser)
		mgd->priv->parser = gda_sql_parser_new();
	g_assert (mgd->priv->parser != NULL);

	mgd->priv->connection = connection;
	midgard_core_connection_connect_error_callback (mgd);	

	if(init_schema) {
		
		if(!g_type_from_name("midgard_quota")) {
			
			MidgardSchema *schema = g_object_new(MIDGARD_TYPE_SCHEMA, NULL);
			gchar *path = g_build_path(G_DIR_SEPARATOR_S, config->sharedir, "MidgardObjects.xml", NULL);
			midgard_schema_init(schema, (const gchar *)path);
			g_free(path);
			midgard_schema_read_dir(schema, config->sharedir);
			
			mgd->priv->schema = schema;
		}
	}

	//midgard_connection_set_loglevel(mgd, loglevel, NULL);

	/* Loads available authentication types */
	midgard_core_connection_initialize_auth_types(mgd);

	g_signal_emit (mgd, MIDGARD_CONNECTION_GET_CLASS (mgd)->signal_id_connected, 0);

	return TRUE;
}
Пример #27
0
void 
midgard_init() 
{	
	GType type;
	/* g_type_init_with_debug_flags(G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_NONE); */


	gda_init ();

	type = MIDGARD_TYPE_BLOB;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_USER;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_CONNECTION;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_CONFIG;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_COLLECTOR;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_QUERY_BUILDER;
	g_assert (type != 0);
	g_type_class_ref (type);
	
	type = MIDGARD_TYPE_DBOBJECT;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_OBJECT;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_VIEW;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_METADATA;
	g_assert (type != 0);
	g_type_class_ref (type);
	/* Initialize MidgardMetadataClass */
	MidgardMetadata *m = g_object_new (MIDGARD_TYPE_METADATA, NULL);
	g_object_unref (m);

	type = MIDGARD_TYPE_WORKSPACE;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_WORKSPACE_STORAGE;
	g_assert (type != 0);

	type = MIDGARD_TYPE_REPLIGARD;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_BASE_ABSTRACT;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_QUERY_EXECUTOR;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_QUERY_SELECT;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_BASE_INTERFACE;
	g_assert (type != 0);

	type = MIDGARD_TYPE_BASE_MIXIN;
	g_assert (type != 0);

	type = MIDGARD_TYPE_JOB;
	g_assert (type != 0);

	type = MIDGARD_TYPE_OBJECT_REFERENCE;
	g_assert (type != 0);
	g_type_class_ref (type);

	type = MIDGARD_TYPE_SQL_CONTENT_MANAGER_JOB;
	g_assert (type != 0);
	g_type_class_ref (type);

	/* Register transform function explicitly, we need own routine */
	g_value_register_transform_func (G_TYPE_STRING, G_TYPE_FLOAT, __transform_string_to_float);
  	g_value_register_transform_func (G_TYPE_STRING, G_TYPE_BOOLEAN, __transform_string_to_boolean);
}