[libgda] GdaSql & GdaBrowser: added --data-files-list option



commit 68108abccfbf8625a7020bb0dc5579869593c051
Author: Vivien Malerba <malerba gnome-db org>
Date:   Fri Jan 7 14:04:42 2011 +0100

    GdaSql & GdaBrowser: added --data-files-list option
    
    to list all the files created by these tool which contain
    information reated to each connection, and to list their status.

 tools/Makefile.am                  |    1 -
 tools/browser/browser-connection.c |   10 +-
 tools/browser/main.c               |   55 +++++++----
 tools/config-info.c                |  185 ++++++++++++++++++++++++++++++++++++
 tools/config-info.h                |    7 +-
 tools/dict-file-name.c             |   88 -----------------
 tools/gda-sql.c                    |   33 +++++--
 7 files changed, 259 insertions(+), 120 deletions(-)
---
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 4f4ea9c..ee24934 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -106,7 +106,6 @@ webdata_DATA = \
 	irb.css
 
 EXTRA_DIST = \
-	dict-file-name.c \
 	gda-sql-res.rc \
 	gda-sql.ico \
 	$(webdata_DATA)
diff --git a/tools/browser/browser-connection.c b/tools/browser/browser-connection.c
index c695c50..267a238 100644
--- a/tools/browser/browser-connection.c
+++ b/tools/browser/browser-connection.c
@@ -26,12 +26,10 @@
 #include <sql-parser/gda-sql-parser.h>
 #include <libgda/gda-sql-builder.h>
 #include <libgda-ui/gdaui-enums.h>
+#include "../config-info.h"
 
 #include "browser-connection-priv.h"
 
-/* code inclusion */
-#include "../dict-file-name.c"
-
 #define CHECK_RESULTS_SHORT_TIMER 200
 #define CHECK_RESULTS_LONG_TIMER 2
 
@@ -396,8 +394,8 @@ browser_connection_set_property (GObject *object,
 			g_object_get (G_OBJECT (bcnc->priv->cnc),
 				      "dsn", &cnc_info,
 				      "cnc-string", &cnc_string, NULL);
-			dict_file_name = compute_dict_file_name (cnc_info ? gda_config_get_dsn_info (cnc_info) : NULL,
-								 cnc_string);
+			dict_file_name = config_info_compute_dict_file_name (cnc_info ? gda_config_get_dsn_info (cnc_info) : NULL,
+									     cnc_string);
 			g_free (cnc_string);
 			if (dict_file_name) {
 				if (! g_file_test (dict_file_name, G_FILE_TEST_EXISTS))
@@ -409,6 +407,8 @@ browser_connection_set_property (GObject *object,
 				if (store)
 					update_store = TRUE;
 			}
+			config_info_update_meta_store_properties (store, bcnc->priv->cnc);
+
 			bcnc->priv->dict_file_name = dict_file_name;
 			g_object_set (G_OBJECT (bcnc->priv->cnc), "meta-store", store, NULL);
 			if (update_store) {
diff --git a/tools/browser/main.c b/tools/browser/main.c
index bf74c5f..53ea110 100644
--- a/tools/browser/main.c
+++ b/tools/browser/main.c
@@ -52,16 +52,33 @@ main_browser_core_init_factories (void)
 	return factories;
 }
 
+static void
+output_data_model (GdaDataModel *model)
+{
+	if (! getenv ("GDA_DATA_MODEL_DUMP_TITLE"))
+		g_setenv ("GDA_DATA_MODEL_DUMP_TITLE", "Yes", TRUE);
+	if (! getenv ("GDA_DATA_MODEL_NULL_AS_EMPTY"))
+		g_setenv ("GDA_DATA_MODEL_NULL_AS_EMPTY", "Yes", TRUE);
+	if (isatty (fileno (stdout))) {
+		if (! getenv ("GDA_DATA_MODEL_DUMP_TRUNCATE"))
+			g_setenv ("GDA_DATA_MODEL_DUMP_TRUNCATE", "-1", TRUE);
+	}
+	gda_data_model_dump (model, NULL);	
+}
+
+
 /* options */
 gchar *perspective = NULL;
 gboolean list_configs = FALSE;
 gboolean list_providers = FALSE;
+gboolean list_data_files = FALSE;
 
 static GOptionEntry entries[] = {
         { "perspective", 'p', 0, G_OPTION_ARG_STRING, &perspective, "Perspective", "default perspective "
 	  "to use when opening windows"},
         { "list-dsn", 'l', 0, G_OPTION_ARG_NONE, &list_configs, "List configured data sources and exit", NULL },
         { "list-providers", 'L', 0, G_OPTION_ARG_NONE, &list_providers, "List installed database providers and exit", NULL },
+        { "data-files-list", 0, 0, G_OPTION_ARG_NONE, &list_data_files, "List files used to hold information related to each connection", NULL },
 	{ NULL, 0, 0, 0, NULL, NULL, NULL }
 };
 
@@ -94,15 +111,7 @@ main (int argc, char *argv[])
 			model = config_info_list_all_providers ();
 
 		if (model) {
-			if (! getenv ("GDA_DATA_MODEL_DUMP_TITLE"))
-				g_setenv ("GDA_DATA_MODEL_DUMP_TITLE", "Yes", TRUE);
-			if (! getenv ("GDA_DATA_MODEL_NULL_AS_EMPTY"))
-				g_setenv ("GDA_DATA_MODEL_NULL_AS_EMPTY", "Yes", TRUE);
-			if (isatty (fileno (stdout))) {
-				if (! getenv ("GDA_DATA_MODEL_DUMP_TRUNCATE"))
-					g_setenv ("GDA_DATA_MODEL_DUMP_TRUNCATE", "-1", TRUE);
-			}
-			gda_data_model_dump (model, NULL);
+			output_data_model (model);
 			g_object_unref (model);
 		}
 		else {
@@ -115,18 +124,28 @@ main (int argc, char *argv[])
 	if (list_configs) {
 		gda_init ();
 		GdaDataModel *model = config_info_list_all_dsn ();
-		if (! getenv ("GDA_DATA_MODEL_DUMP_TITLE"))
-			g_setenv ("GDA_DATA_MODEL_DUMP_TITLE", "Yes", TRUE);
-		if (! getenv ("GDA_DATA_MODEL_NULL_AS_EMPTY"))
-			g_setenv ("GDA_DATA_MODEL_NULL_AS_EMPTY", "Yes", TRUE);
-		if (isatty (fileno (stdout))) {
-			if (! getenv ("GDA_DATA_MODEL_DUMP_TRUNCATE"))
-				g_setenv ("GDA_DATA_MODEL_DUMP_TRUNCATE", "-1", TRUE);
-		}
-		gda_data_model_dump (model, NULL);
+		output_data_model (model);
 		g_object_unref (model);
 		return 0;
 	}
+	if (list_data_files) {
+		gchar *confdir;
+		GdaDataModel *model;
+
+		gda_init ();
+		confdir = config_info_compute_dict_directory ();
+		g_print (_("All files are in the directory: %s\n"), confdir);
+		g_free (confdir);
+		model = config_info_list_data_files (&error);
+		if (model) {
+			output_data_model (model);
+			g_object_unref (model);
+		}
+		else
+			g_print (_("Can't get the list of files used to store information about each connection: %s\n"),
+				 error->message);	
+		return 0;
+	}
 
 	gdaui_init ();
 	gtk_init (&argc, &argv);
diff --git a/tools/config-info.c b/tools/config-info.c
index 40ff86d..a2768da 100644
--- a/tools/config-info.c
+++ b/tools/config-info.c
@@ -127,3 +127,188 @@ config_info_detail_provider (const gchar *provider, GError **error)
 	g_object_unref (prov_list);
 	return NULL;
 }
+
+GdaDataModel *
+config_info_list_data_files (GError **error)
+{
+	const gchar *name;
+	gchar *confdir;
+	GdaDataModel *model;
+	GDir *dir;
+
+	/* open directory */
+	confdir = config_info_compute_dict_directory ();
+	dir = g_dir_open (confdir, 0, error);
+	if (!dir) {
+		g_free (confdir);
+		return NULL;
+	}
+
+	/* create model */
+	model = gda_data_model_array_new (5);
+	gda_data_model_set_column_name (model, 0, _("File name"));
+	gda_data_model_set_column_name (model, 1, _("DSN"));
+	gda_data_model_set_column_name (model, 2, _("Last used"));
+	gda_data_model_set_column_name (model, 3, _("Provider"));
+	gda_data_model_set_column_name (model, 4, _("Connection string"));
+
+	while ((name = g_dir_read_name (dir))) {
+		GValue *value;
+		gint row;
+		gchar *copy, *dsn, *fname;
+		GdaDsnInfo *dsninfo;
+
+		if (! g_str_has_suffix (name, ".db"))
+			continue;
+		if (! g_str_has_prefix (name, "gda-sql-"))
+			continue;
+
+		fname = g_build_filename (confdir, name, NULL);
+		copy = g_strdup (name);
+
+		row = gda_data_model_append_row (model, NULL);
+
+		g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), name);
+		gda_data_model_set_value_at (model, 0, row, value, NULL);
+		gda_value_free (value);
+
+		dsn = copy + 8;
+		dsn [strlen (dsn) - 3] = 0;
+		dsninfo = gda_config_get_dsn_info (dsn);
+		if (dsninfo) {
+			g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), dsn);
+			gda_data_model_set_value_at (model, 1, row, value, NULL);
+			gda_value_free (value);
+		}
+
+		/* Open the file */
+		GdaMetaStore *store;
+		gchar *attvalue;
+		store = gda_meta_store_new_with_file (fname);
+		if (gda_meta_store_get_attribute_value (store, "last-used", &attvalue, NULL)) {
+			value = gda_value_new_from_string (attvalue, G_TYPE_DATE);
+			g_free (attvalue);
+			gda_data_model_set_value_at (model, 2, row, value, NULL);
+			gda_value_free (value);
+		}
+		if (gda_meta_store_get_attribute_value (store, "cnc-provider", &attvalue, NULL)) {
+			g_value_take_string ((value = gda_value_new (G_TYPE_STRING)), attvalue);
+			gda_data_model_set_value_at (model, 3, row, value, NULL);
+			gda_value_free (value);
+		}
+		if (gda_meta_store_get_attribute_value (store, "cnc-string", &attvalue, NULL)) {
+			g_value_take_string ((value = gda_value_new (G_TYPE_STRING)), attvalue);
+			gda_data_model_set_value_at (model, 4, row, value, NULL);
+			gda_value_free (value);
+		}
+		g_object_unref (store);
+
+		g_free (copy);
+		g_free (fname);
+	}
+	
+	g_free (confdir);
+	g_dir_close (dir);
+
+	return model;
+}
+
+
+
+
+
+
+gchar *
+config_info_compute_dict_directory (void)
+{
+	gchar *confdir;
+
+	confdir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "libgda", NULL);
+	if (!g_file_test (confdir, G_FILE_TEST_EXISTS)) {
+		g_free (confdir);
+		confdir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), ".libgda", NULL);
+	}
+
+	return confdir;
+}
+
+static void
+compute_dict_file_name_foreach_cb (const gchar *key, G_GNUC_UNUSED const gchar *value, GSList **list)
+{
+	if (!*list) 
+		*list = g_slist_prepend (NULL, (gpointer) key);
+	else
+		*list = g_slist_insert_sorted (*list, (gpointer) key, (GCompareFunc) strcmp);
+}
+
+gchar *
+config_info_compute_dict_file_name (GdaDsnInfo *info, const gchar *cnc_string)
+{
+	gchar *filename = NULL;
+	gchar *confdir;
+
+	confdir = config_info_compute_dict_directory ();
+	if (info) {		
+		filename = g_strdup_printf ("%s%sgda-sql-%s.db", 
+					    confdir, G_DIR_SEPARATOR_S,
+					    info->name);
+	}
+	else {
+#if GLIB_CHECK_VERSION(2,16,0)
+		GdaQuarkList *ql;
+		GSList *list, *sorted_list = NULL;
+		GString *string = NULL;
+		ql = gda_quark_list_new_from_string (cnc_string);
+
+		gda_quark_list_foreach (ql, (GHFunc) compute_dict_file_name_foreach_cb, &sorted_list);
+		for (list = sorted_list; list; list = list->next) {
+			const gchar *value;
+			gchar *evalue;
+
+			if (!string)
+				string = g_string_new ("");
+			else
+				g_string_append_c (string, ',');
+
+			value = gda_quark_list_find (ql, (gchar *) list->data);
+			evalue = gda_rfc1738_encode (value);
+			g_string_append_printf (string, ",%s=%s", (gchar *) list->data, evalue);
+			g_free (evalue);
+		}
+		gda_quark_list_free (ql);
+
+		if (string) {
+			gchar *chname;
+			chname = g_compute_checksum_for_string (G_CHECKSUM_SHA1, string->str, -1);
+			g_string_free (string, TRUE);
+			filename = g_strdup_printf ("%s%sgda-sql-%s.db", 
+					    confdir, G_DIR_SEPARATOR_S,
+					    chname);
+			g_free (chname);
+		}
+#endif
+	}
+
+	g_free (confdir);
+	return filename;
+}
+
+void
+config_info_update_meta_store_properties (GdaMetaStore *mstore, GdaConnection *rel_cnc)
+{
+	GDate *date;
+	GValue *dvalue;
+	gchar *tmp;
+	date = g_date_new ();
+	g_date_set_time_t (date, time (NULL));
+	g_value_take_boxed ((dvalue = gda_value_new (G_TYPE_DATE)), date);
+	tmp = gda_value_stringify (dvalue);
+	gda_value_free (dvalue);
+	gda_meta_store_set_attribute_value (mstore, "last-used", tmp, NULL);
+	g_free (tmp);
+	
+	gda_meta_store_set_attribute_value (mstore, "cnc-string",
+					    gda_connection_get_cnc_string (rel_cnc), NULL);
+	gda_meta_store_set_attribute_value (mstore, "cnc-provider",
+					    gda_connection_get_provider_name (rel_cnc), NULL);
+}
diff --git a/tools/config-info.h b/tools/config-info.h
index 752847d..e2c0789 100644
--- a/tools/config-info.h
+++ b/tools/config-info.h
@@ -1,5 +1,5 @@
 /* 
- * Copyright (C) 2010 The GNOME Foundation.
+ * Copyright (C) 2010 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  *      Vivien Malerba <malerba gnome-db org>
@@ -24,8 +24,13 @@
 
 #include <libgda/libgda.h>
 
+gchar        *config_info_compute_dict_directory (void);
+gchar        *config_info_compute_dict_file_name (GdaDsnInfo *info, const gchar *cnc_string);
+void          config_info_update_meta_store_properties (GdaMetaStore *mstore, GdaConnection *rel_cnc);
+
 GdaDataModel *config_info_list_all_dsn (void);
 GdaDataModel *config_info_list_all_providers (void);
 GdaDataModel *config_info_detail_provider (const gchar *provider, GError **error);
+GdaDataModel *config_info_list_data_files (GError **error);
 
 #endif
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index 7b382f1..6c4252c 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -1,5 +1,5 @@
 /* GDA - SQL console
- * Copyright (C) 2007 - 2010 The GNOME Foundation.
+ * Copyright (C) 2007 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  * 	Vivien Malerba <malerba gnome-db org>
@@ -57,9 +57,6 @@ typedef void (*sighandler_t)(int);
 #include "web-server.h"
 #endif
 
-/* code inclusion */
-#include "dict-file-name.c"
-
 /* options */
 gboolean show_version = FALSE;
 
@@ -70,6 +67,8 @@ gboolean interactive = FALSE;
 gboolean list_configs = FALSE;
 gboolean list_providers = FALSE;
 
+gboolean list_data_files = FALSE;
+
 gchar *outfile = NULL;
 gboolean has_threads;
 
@@ -90,6 +89,7 @@ static GOptionEntry entries[] = {
 	{ "http-port", 's', 0, G_OPTION_ARG_INT, &http_port, "Run embedded HTTP server on specified port", "port" },
 	{ "http-token", 't', 0, G_OPTION_ARG_STRING, &auth_token, "Authentication token (required to authenticate clients)", "token phrase" },
 #endif
+        { "data-files-list", 0, 0, G_OPTION_ARG_NONE, &list_data_files, "List files used to hold information related to each connection", NULL },
         { NULL, 0, 0, 0, NULL, NULL, NULL }
 };
 
@@ -215,6 +215,23 @@ main (int argc, char *argv[])
 		g_object_unref (model);
 		goto cleanup;
 	}
+	if (list_data_files) {
+		gchar *confdir;
+		GdaDataModel *model;
+
+		confdir = config_info_compute_dict_directory ();
+		g_print (_("All files are in the directory: %s\n"), confdir);
+		g_free (confdir);
+		model = config_info_list_data_files (&error);
+		if (model) {
+			output_data_model (model);
+			g_object_unref (model);
+		}
+		else
+			g_print (_("Can't get the list of files used to store information about each connection: %s\n"),
+				 error->message);	
+		goto cleanup;
+	}
 
 	/* commands file */
 	if (commandsfile) {
@@ -1412,7 +1429,7 @@ open_connection (SqlConsole *console, const gchar *cnc_name, const gchar *cnc_st
 		gchar *cnc_string;
 		g_object_get (G_OBJECT (newcnc),
 			      "cnc-string", &cnc_string, NULL);
-		dict_file_name = compute_dict_file_name (info, cnc_string);
+		dict_file_name = config_info_compute_dict_file_name (info, cnc_string);
 		g_free (cnc_string);
 
 		cs = g_new0 (ConnectionSetting, 1);
@@ -1442,7 +1459,7 @@ open_connection (SqlConsole *console, const gchar *cnc_name, const gchar *cnc_st
 			if (! g_file_test (dict_file_name, G_FILE_TEST_EXISTS))
 				update_store = TRUE;
 			store = gda_meta_store_new_with_file (dict_file_name);
-			g_print (_("All the meta data associated to the '%s' connection will be stored "
+			g_print (_("All the information related to the '%s' connection will be stored "
 				   "in the '%s' file\n"),
 				 cs->name, dict_file_name);
 		}
@@ -1452,6 +1469,8 @@ open_connection (SqlConsole *console, const gchar *cnc_name, const gchar *cnc_st
 				update_store = TRUE;
 		}
 
+		config_info_update_meta_store_properties (store, newcnc);
+
 		g_object_set (G_OBJECT (cs->cnc), "meta-store", store, NULL);
 		if (update_store) {
 			GError *lerror = NULL;
@@ -1692,7 +1711,7 @@ data_model_to_string (SqlConsole *console, GdaDataModel *model)
 			g_setenv ("GDA_DATA_MODEL_DUMP_TITLE", "Yes", TRUE);
 		if (! getenv ("GDA_DATA_MODEL_NULL_AS_EMPTY"))
 			g_setenv ("GDA_DATA_MODEL_NULL_AS_EMPTY", "Yes", TRUE);
-		if (! main_data->output_stream || isatty (main_data->output_stream)) {
+		if (! main_data->output_stream || isatty (fileno (main_data->output_stream))) {
 			if (! getenv ("GDA_DATA_MODEL_DUMP_TRUNCATE"))
 				g_setenv ("GDA_DATA_MODEL_DUMP_TRUNCATE", "-1", TRUE);
 		}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]