[libgda] Tools: added the ".info" command



commit c1b44b2001dc3e83e0c3317fa6aed2eb2d6e94b0
Author: Vivien Malerba <malerba gnome-db org>
Date:   Tue Dec 2 22:20:03 2014 +0100

    Tools: added the ".info" command

 tools/common/t-app.c        |   86 ++++++++++++++++++++++++++++++++++++
 tools/common/t-connection.c |  103 +++++++++++++++++++++++++++++++++++++++---
 tools/common/t-connection.h |    5 ++-
 3 files changed, 185 insertions(+), 9 deletions(-)
---
diff --git a/tools/common/t-app.c b/tools/common/t-app.c
index 466c503..e4e275c 100644
--- a/tools/common/t-app.c
+++ b/tools/common/t-app.c
@@ -822,6 +822,8 @@ static ToolCommandResult *extra_command_copyright (ToolCommand *command, guint a
                                                   TContext *console, GError **error);
 static ToolCommandResult *extra_command_option (ToolCommand *command, guint argc, const gchar **argv,
                                                TContext *console, GError **error);
+static ToolCommandResult *extra_command_info (ToolCommand *command, guint argc, const gchar **argv,
+                                             TContext *console, GError **error);
 static ToolCommandResult *extra_command_quit (ToolCommand *command, guint argc, const gchar **argv,
                                              TContext *console, GError **error);
 static ToolCommandResult *extra_command_cd (ToolCommand *command, guint argc, const gchar **argv,
@@ -1188,6 +1190,17 @@ build_commands (TApp *self, TAppFeatures features)
        base_tool_command_group_add (self->term_commands, c);
 
        c = g_new0 (ToolCommand, 1);
+       c->group = _("General");
+       c->group_id = NULL;
+       c->name = g_strdup_printf (_("%s [<NAME>]"), "info");
+       c->description = _("Show a piece of information, or all information about the connection");
+       c->command_func = (ToolCommandFunc) extra_command_info;
+#ifdef HAVE_LIBSOUP
+       base_tool_command_group_add (self->web_commands, c);
+#endif
+       base_tool_command_group_add (self->term_commands, c);
+
+       c = g_new0 (ToolCommand, 1);
        c->group = _("Query buffer & query favorites");
        c->group_id = NULL;
        c->name = g_strdup_printf (_("%s [<FILE>]"), "e");
@@ -2309,6 +2322,8 @@ extra_command_option (ToolCommand *command, guint argc, const gchar **argv,
                        GValue *value;
                        GdaHolder *opt;
                        opt = GDA_HOLDER (list->data);
+                       if (!gda_holder_is_valid (opt))
+                               continue;
                        row = gda_data_model_append_row (model, NULL);
 
                        value = gda_value_new_from_string (gda_holder_get_id (opt), G_TYPE_STRING);
@@ -2332,6 +2347,77 @@ extra_command_option (ToolCommand *command, guint argc, const gchar **argv,
        return res;
 }
 
+static ToolCommandResult *
+extra_command_info (ToolCommand *command, guint argc, const gchar **argv,
+                   TContext *console, GError **error)
+{
+       ToolCommandResult *res = NULL;
+       const gchar *oname = NULL;
+
+       g_assert (console);
+       g_assert (global_t_app);
+
+       if (argv[0] && *argv[0])
+               oname = argv[0];
+
+       if (oname) {
+               GdaSet *infos;
+               infos = t_connection_get_all_infos (t_context_get_connection (console));
+               GdaHolder *opt = gda_set_get_holder (infos, oname);
+               if (opt) {
+                       res = g_new0 (ToolCommandResult, 1);
+                       res->type = BASE_TOOL_COMMAND_RESULT_SET;
+                       res->u.set = gda_set_new (NULL);
+                       gda_set_add_holder (res->u.set, gda_holder_copy (opt));
+               }
+               else {
+                       g_set_error (error, T_ERROR, T_COMMAND_ARGUMENTS_ERROR,
+                                    _("No option named '%s'"), oname);
+                       return NULL;
+               }
+       }
+       else {
+               /* list parameter's values */
+               GdaDataModel *model;
+               GSList *list;
+               model = gda_data_model_array_new_with_g_types (3, G_TYPE_STRING, G_TYPE_STRING, 
G_TYPE_STRING);
+               gda_data_model_set_column_title (model, 0, _("Name"));
+               gda_data_model_set_column_title (model, 1, _("Value"));
+               gda_data_model_set_column_title (model, 2, _("Description"));
+               g_object_set_data (G_OBJECT (model), "name", _("Current connection's information"));
+               for (list = t_connection_get_all_infos (t_context_get_connection (console))->holders;
+                    list;
+                    list = list->next) {
+                       gint row;
+                       gchar *str;
+                       GValue *value;
+                       GdaHolder *info;
+                       info = GDA_HOLDER (list->data);
+                       if (!gda_holder_is_valid (info))
+                               continue;
+                       row = gda_data_model_append_row (model, NULL);
+
+                       value = gda_value_new_from_string (gda_holder_get_id (info), G_TYPE_STRING);
+                       gda_data_model_set_value_at (model, 0, row, value, NULL);
+                       gda_value_free (value);
+
+                       str = gda_holder_get_value_str (info, NULL);
+                       value = gda_value_new_from_string (str ? str : "(NULL)", G_TYPE_STRING);
+                       gda_data_model_set_value_at (model, 1, row, value, NULL);
+                       gda_value_free (value);
+
+                       value = (GValue*) gda_holder_get_attribute (info, GDA_ATTRIBUTE_DESCRIPTION);
+                       gda_data_model_set_value_at (model, 2, row, value, NULL);
+               }
+
+               res = g_new0 (ToolCommandResult, 1);
+               res->type = BASE_TOOL_COMMAND_RESULT_DATA_MODEL;
+               res->u.model = model;
+       }
+
+       return res;
+}
+
 
 static ToolCommandResult *
 extra_command_quit (ToolCommand *command, guint argc, const gchar **argv,
diff --git a/tools/common/t-connection.c b/tools/common/t-connection.c
index d5a0d0d..f74d7c9 100644
--- a/tools/common/t-connection.c
+++ b/tools/common/t-connection.c
@@ -66,6 +66,7 @@ struct _TConnectionPrivate {
         GdaConnection *store_cnc;
 
         GdaSet        *variables;
+       GdaSet        *infos;
 };
 
 
@@ -239,6 +240,7 @@ t_connection_init (TConnection *tcnc)
 
        tcnc->priv->variables = NULL;
        tcnc->priv->query_buffer = NULL;
+       tcnc->priv->infos = gda_set_new (NULL);
 }
 
 static void
@@ -334,9 +336,16 @@ have_meta_store_ready (TConnection *tcnc, GError **error)
                else 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 information related to the '%s' connection will be stored "
-                          "in the '%s' file\n"),
-                        t_connection_get_name (tcnc), dict_file_name);
+
+               GdaHolder *h;
+               h = gda_set_get_holder (tcnc->priv->infos, "meta_filename");
+               if (!h) {
+                       h = gda_holder_new (G_TYPE_STRING);
+                       g_object_set (h, "id", "meta_filename",
+                                     "description", _("File containing the meta data associated to the 
connection"), NULL);
+                       gda_set_add_holder (tcnc->priv->infos, h);
+               }
+               g_assert (gda_holder_set_value_str (h, NULL, dict_file_name, NULL));
        }
        else {
                store = gda_meta_store_new (NULL);
@@ -550,6 +559,9 @@ t_connection_dispose (GObject *object)
 
                t_connection_set_busy_state (tcnc, FALSE, NULL);
 
+               if (tcnc->priv->infos)
+                       g_object_unref (tcnc->priv->infos);
+
                g_free (tcnc->priv);
                tcnc->priv = NULL;
                /*g_print ("=== Disposed TConnection %p\n", tcnc);*/
@@ -893,11 +905,35 @@ t_connection_open (const gchar *cnc_name, const gchar *cnc_string, const gchar *
                /* show date format */
                GDateDMY order[3];
                gchar sep;
-               if (gda_connection_get_date_format (t_connection_get_cnc (tcnc), &order[0], &order[1], 
&order[2], &sep, NULL)) {
-                       g_print (_("Date format for this connection will be: %s%c%s%c%s, where YYYY is the 
year, MM the month and DD the day\n"),
-                                (order [0] == G_DATE_DAY) ? "DD" : ((order [0] == G_DATE_MONTH) ? "MM" : 
"YYYY"), sep,
-                                (order [1] == G_DATE_DAY) ? "DD" : ((order [1] == G_DATE_MONTH) ? "MM" : 
"YYYY"), sep,
-                                (order [2] == G_DATE_DAY) ? "DD" : ((order [2] == G_DATE_MONTH) ? "MM" : 
"YYYY"));
+               if (gda_connection_get_date_format (t_connection_get_cnc (tcnc),
+                                                   &order[0], &order[1], &order[2], &sep, NULL)) {
+                       GString *string;
+                       guint i;
+                       string = g_string_new ("");
+                       for (i = 0; i < 3; i++) {
+                               if (i > 0)
+                                       g_string_append_c (string, sep);
+                               if (order [i] == G_DATE_DAY)
+                                       g_string_append (string, "DD");
+                               else if (order [i] == G_DATE_MONTH)
+                                       g_string_append (string, "MM");
+                               else
+                                       g_string_append (string, "YYYY");
+                       }
+                       g_print (_("Date format for this connection will be: %s, where YYYY is the year, MM 
the month and DD the day\n"),
+                                string->str);
+
+                       GdaHolder *h;
+                       h = gda_set_get_holder (tcnc->priv->infos, "date_format");
+                       if (!h) {
+                               h = gda_holder_new (G_TYPE_STRING);
+                               g_object_set (h, "id", "date_format",
+                                             "description", _("Format of the date used for by connection, 
where "
+                                                              "YYYY is the year, MM the month and DD the 
day"), NULL);
+                               gda_set_add_holder (tcnc->priv->infos, h);
+                       }
+                       g_assert (gda_holder_set_value_str (h, NULL, string->str, NULL));
+                       g_string_free (string, TRUE);
                }
        }
 
@@ -2382,3 +2418,54 @@ t_connection_get_query_buffer (TConnection *tcnc)
        g_return_val_if_fail (T_IS_CONNECTION (tcnc), NULL);
        return tcnc->priv->query_buffer;
 }
+
+
+/**
+ * t_connection_get_all_infos:
+ * @tcnc: a #TConnection
+ *
+ * Returns: (transfer none): a #GdaSet containing all the names informations anout @tcnc
+ */
+GdaSet *
+t_connection_get_all_infos (TConnection *tcnc)
+{
+       g_return_val_if_fail (T_IS_CONNECTION (tcnc), NULL);
+
+       /* refresh some information here */
+       /* provider name */
+       GdaHolder *h;
+       h = gda_set_get_holder (tcnc->priv->infos, "db_provider");
+       if (!h) {
+               h = gda_holder_new (G_TYPE_STRING);
+               g_object_set (h, "id", "db_provider",
+                             "description", _("Database provider"), NULL);
+               gda_set_add_holder (tcnc->priv->infos, h);
+       }
+       g_assert (gda_holder_set_value_str (h, NULL, gda_connection_get_provider_name (tcnc->priv->cnc), 
NULL));
+
+       /* database name */
+       h = gda_set_get_holder (tcnc->priv->infos, "db_name");
+       if (!h) {
+               h = gda_holder_new (G_TYPE_STRING);
+               g_object_set (h, "id", "db_name",
+                             "description", _("Database name"), NULL);
+               gda_set_add_holder (tcnc->priv->infos, h);
+       }
+       const gchar *str;
+       str = gda_connection_get_cnc_string (tcnc->priv->cnc);
+       GdaQuarkList *ql;
+       ql = gda_quark_list_new_from_string (str);
+       if (ql) {
+               const gchar *name;
+               name = gda_quark_list_find (ql, "DB_NAME");
+               if (name)
+                       g_assert (gda_holder_set_value_str (h, NULL, name, NULL));
+               else
+                       gda_holder_force_invalid (h);
+               gda_quark_list_free (ql);
+       }
+       else
+               gda_holder_force_invalid (h);
+
+       return tcnc->priv->infos;
+}
diff --git a/tools/common/t-connection.h b/tools/common/t-connection.h
index e3586c9..703e9cf 100644
--- a/tools/common/t-connection.h
+++ b/tools/common/t-connection.h
@@ -211,7 +211,10 @@ gboolean             t_connection_undeclare_table   (TConnection *tcnc,
                                                     const gchar *table_name,
                                                     GError **error);
 
-#endif
+#endif /* HAVE_LDAP */
+
+/* Information about the connection */
+GdaSet              *t_connection_get_all_infos (TConnection *tcnc);
 
 G_END_DECLS
 


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