[libgda/LIBGDA_5.0] GdaSql: added ldap_attributes and ldap_search commands



commit aa4ca4f91d7d5ee1ccd0dfb7bebc6a8570415930
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sun Feb 5 15:53:09 2012 +0100

    GdaSql: added ldap_attributes and ldap_search commands

 tools/command-exec.c          |    6 ++-
 tools/gda-sql.c               |  125 +++++++++++++++++++++++++++++++++++++++++
 tools/help/C/gda-sql-help.xml |   60 ++++++++++++++++++++
 3 files changed, 190 insertions(+), 1 deletions(-)
---
diff --git a/tools/command-exec.c b/tools/command-exec.c
index a98e754..e4d4b41 100644
--- a/tools/command-exec.c
+++ b/tools/command-exec.c
@@ -30,6 +30,7 @@
 #endif
 #include <sql-parser/gda-statement-struct-util.h>
 #include "tools-utils.h"
+#include <virtual/gda-ldap-connection.h>
 
 /*
  *  gda_internal_command_arg_remove_quotes
@@ -485,7 +486,7 @@ help_xml_doc_to_string (xmlDocPtr helpdoc, const gchar *command_name, gint width
 }
 
 GdaInternalCommandResult *
-gda_internal_command_help (SqlConsole *console, G_GNUC_UNUSED GdaConnection *cnc,
+gda_internal_command_help (SqlConsole *console, GdaConnection *cnc,
 			   const gchar **args, G_GNUC_UNUSED GError **error,
 			   GdaInternalCommandsList *clist)
 {
@@ -567,6 +568,9 @@ gda_internal_command_help (SqlConsole *console, G_GNUC_UNUSED GdaConnection *cnc
 			if (console && command->limit_to_main)
 				continue;
 
+			if (g_str_has_prefix (command->name, "ldap") && cnc && !GDA_IS_LDAP_CONNECTION (cnc))
+				continue;
+
 			if (!current_group || strcmp (current_group, command->group)) {
 				current_group = command->group;
 				if (list != clist->group_ordered)
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index 858912d..05a55f1 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -125,6 +125,7 @@ typedef struct {
 	GString *partial_command;
 
 	GHashTable *parameters; /* key = name, value = G_TYPE_STRING GdaParameter */
+	gchar *ldap_attributes; /* default LDAP attributes */
 
 #ifdef HAVE_LIBSOUP
 	WebServer *server;
@@ -192,6 +193,7 @@ main (int argc, char *argv[])
 	has_threads = g_thread_supported ();
 	data = g_new0 (MainData, 1);
 	data->parameters = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+	data->ldap_attributes = g_strdup ("cn");
 	main_data = data;
 
 	/* output file */
@@ -1866,6 +1868,9 @@ data_model_to_string (SqlConsole *console, GdaDataModel *model)
 	static gboolean env_set = FALSE;
 	OutputFormat of;
 
+	if (!GDA_IS_DATA_MODEL (model))
+		return NULL;
+
 	if (!env_set) {
 		if (! getenv ("GDA_DATA_MODEL_DUMP_TITLE"))
 			g_setenv ("GDA_DATA_MODEL_DUMP_TITLE", "Yes", TRUE);
@@ -2155,6 +2160,13 @@ static GdaInternalCommandResult *extra_command_undeclare_fk (SqlConsole *console
 							     const gchar **args,
 							     GError **error, gpointer data);
 
+static GdaInternalCommandResult *extra_command_ldap_search (SqlConsole *console, GdaConnection *cnc,
+							    const gchar **args,
+							    GError **error, gpointer data);
+static GdaInternalCommandResult *extra_command_ldap_attributes (SqlConsole *console, GdaConnection *cnc,
+								const gchar **args,
+								GError **error, gpointer data);
+
 static GdaInternalCommandsList *
 build_internal_commands_list (void)
 {
@@ -2646,6 +2658,30 @@ build_internal_commands_list (void)
 	c->limit_to_main = TRUE;
 	commands->commands = g_slist_prepend (commands->commands, c);
 
+	c = g_new0 (GdaInternalCommand, 1);
+	c->group = _("LDAP");
+	c->name = g_strdup_printf (_("%s <filter> [<base|onelevel|subtree> [<base DN>]]"), "ldap_search");
+	c->description = _("Search LDAP entries");
+	c->args = NULL;
+	c->command_func = (GdaInternalCommandFunc) extra_command_ldap_search;
+	c->user_data = NULL;
+	c->arguments_delimiter_func = NULL;
+	c->unquote_args = TRUE;
+	c->limit_to_main = TRUE;
+	commands->commands = g_slist_prepend (commands->commands, c);
+
+	c = g_new0 (GdaInternalCommand, 1);
+	c->group = _("LDAP");
+	c->name = g_strdup_printf (_("%s [attribute,[attribute],...]"), "ldap_attributes");
+	c->description = _("Set or get the default attrbutes manipulated by LDAP commands");
+	c->args = NULL;
+	c->command_func = (GdaInternalCommandFunc) extra_command_ldap_attributes;
+	c->user_data = NULL;
+	c->arguments_delimiter_func = NULL;
+	c->unquote_args = TRUE;
+	c->limit_to_main = TRUE;
+	commands->commands = g_slist_prepend (commands->commands, c);
+
 	/* comes last */
 	c = g_new0 (GdaInternalCommand, 1);
 	c->group = _("General");
@@ -4847,6 +4883,93 @@ extra_command_pivot (SqlConsole *console, GdaConnection *cnc, const gchar **args
 }
 
 static GdaInternalCommandResult *
+extra_command_ldap_search (SqlConsole *console, GdaConnection *cnc, const gchar **args,
+			   GError **error, gpointer data)
+{
+	GdaInternalCommandResult *res = NULL;
+	ConnectionSetting *cs;
+	GdaDataModel *model, *copy;
+	cs = get_current_connection_settings (console);
+	if (!cs) {
+		g_set_error (error, 0, 0, "%s", 
+			     _("No connection specified"));
+		return NULL;
+	}
+
+	if (! GDA_IS_LDAP_CONNECTION (cs->cnc)) {
+		g_set_error (error, 0, 0, "%s", 
+			     _("Connection is not an LDAP connection"));
+		return NULL;
+	}
+
+	const gchar *filter = NULL;
+	gchar *lfilter = NULL;
+	const gchar *scope = NULL;
+	const gchar *base_dn = NULL;
+	GdaLdapSearchScope lscope = GDA_LDAP_SEARCH_SUBTREE;
+
+        if (args[0] && *args[0]) {
+                filter = args[0];
+                if (args[1] && *args[1]) {
+			scope = args [1];
+			if (!g_ascii_strcasecmp (scope, "base"))
+				lscope = GDA_LDAP_SEARCH_BASE;
+			else if (!g_ascii_strcasecmp (scope, "onelevel"))
+				lscope = GDA_LDAP_SEARCH_ONELEVEL;
+			else if (!g_ascii_strcasecmp (scope, "subtree"))
+				lscope = GDA_LDAP_SEARCH_SUBTREE;
+			else {
+				g_set_error (error, 0, 0, _("Unknown search scope '%s'"), scope);
+				return NULL;
+			}
+			if (args[2] && *args[2])
+				base_dn = args[2];
+		}
+        }
+
+	if (!filter) {
+		g_set_error (error, 0, 0, "%s", 
+			     _("Missing filter which to operate"));
+		return NULL;
+	}
+
+	if (*filter != '(')
+		lfilter = g_strdup_printf ("(%s)", filter);
+	
+	model = gda_data_model_ldap_new (cs->cnc, base_dn, lfilter ? lfilter : filter, main_data->ldap_attributes, lscope);
+	g_free (lfilter);
+	copy = (GdaDataModel*) gda_data_model_array_copy_model (model, error);
+	g_object_unref (model);
+	if (!copy)
+		return NULL;
+
+	res = g_new0 (GdaInternalCommandResult, 1);
+	res->type = GDA_INTERNAL_COMMAND_RESULT_DATA_MODEL;
+	res->u.model = copy;
+	return res;
+}
+
+static GdaInternalCommandResult *
+extra_command_ldap_attributes (G_GNUC_UNUSED SqlConsole *console, G_GNUC_UNUSED GdaConnection *cnc, const gchar **args,
+			       GError **error, gpointer data)
+{
+	GdaInternalCommandResult *res = NULL;
+
+        if (args[0] && *args[0]) {
+		g_free (main_data->ldap_attributes);
+		main_data->ldap_attributes = g_strdup (args[0]);
+		res = g_new0 (GdaInternalCommandResult, 1);
+		res->type = GDA_INTERNAL_COMMAND_RESULT_EMPTY;
+        }
+	else {
+		res = g_new0 (GdaInternalCommandResult, 1);
+		res->type = GDA_INTERNAL_COMMAND_RESULT_TXT;
+		res->u.txt = g_string_new (main_data->ldap_attributes);
+	}
+	return res;
+}
+
+static GdaInternalCommandResult *
 extra_command_export (SqlConsole *console, GdaConnection *cnc, const gchar **args,
 		      GError **error, gpointer data)
 {
@@ -5116,6 +5239,8 @@ extra_command_graph (SqlConsole *console, GdaConnection *cnc, const gchar **args
 		return NULL;
 }
 
+
+
 #ifdef HAVE_LIBSOUP
 static GdaInternalCommandResult *
 extra_command_httpd (SqlConsole *console, G_GNUC_UNUSED GdaConnection *cnc, const gchar **args,
diff --git a/tools/help/C/gda-sql-help.xml b/tools/help/C/gda-sql-help.xml
index 71ab98d..dff3200 100644
--- a/tools/help/C/gda-sql-help.xml
+++ b/tools/help/C/gda-sql-help.xml
@@ -312,4 +312,64 @@ uspan        |              |               |               |               |
       <comment>This example defines the ID variabe (beware, case sensitive) ID to 23. Later you can then run a query like "SELECT * FROM customers WHERE id=##ID::int"</comment>
     </example>
   </command>
+
+  <!-- ".ldap_attributes" command -->
+  <command name="ldap_attributes">
+    <shortdescription>Set or show the list of attributes take into account by LDAP commands
+    </shortdescription>
+    <usage>
+      <synopsis>.ldap_attributes [attribute,[attribute],...]</synopsis>
+      <comment>Use this command to specify or display which attributes the LDAP commands operate on; if no
+      argument is passed, then the current list of attributes is shown</comment>
+      <comment>The attributes list is a CVS separated list of individual LDAP attributes, for example "cn,uid,email"</comment>
+      <comment>LDAP allows some attributes to be multi valued (e.g. the "objectClass" attribute). When dealing with multi
+      valued attributes, the default behaviour is to return an error for the corresponding value. This default behaviour can
+      be changed by specifying a modifier among "CVS" (values are listed in a CVS syntax), "*" (each row is duplicated with
+      each value of the attribute), "1" (only the 1st value of the attribute is shown), "CONCAT" (all the values are
+      made into a string, separated by newlines) or "NULL" (a NULL value is used).
+      </comment>
+    </usage>
+    <example>
+      <synopsis>.ldap_attributes</synopsis>
+      <comment>Shows the current attributes (by default the "cn" attribute)</comment>
+      <raw>cn</raw>
+    </example>
+    <example>
+      <synopsis>.ldap_attributes cn,objectClass::*</synopsis>
+      <comment>Define the attibutes as "cn" and "objectClass", and because "objectClass" is generally multi valued,
+      specify that a new row be created in the output for each value of "objectClass" for each entry</comment>
+    </example>
+  </command>
+
+  <!-- ".ldap_search" command -->
+  <command name="ldap_search">
+    <shortdescription>Search LDAP entries using a filter and various criteria
+    </shortdescription>
+    <usage>
+      <synopsis>.ldap_search @filter@ [ base|onelevel|subtree@ [ base DN ]]</synopsis>
+      <comment>The @filter@ argument is the only mandatory and has to be a valid LDAP filter expression. The
+      other arguments, if present are in order the search scope (among "base", "onelevel" and "subtree"), and the
+      base DN (which is the starting point of the search)</comment>
+      <comment>The results of the search will be a table where the first column is the DN of the entry, and
+      the other columns are mapped to the LDAP attributes (see the "ldap_attributes" command)</comment>
+      <comment>LDAP filters must respect the RFC 2254 (see http://www.faqs.org/rfcs/rfc2254.html) and what the
+      actual LDAP server understands. Note that if the outer most opening and cloding parenthesis are omitted, they
+      will be automatically added. Examples of LDAP search filter are: "(objectClass=*)" to fetch all objects,
+      "(&amp;(objectClass=inetOrgPerson)(!cn=tom))" to fetch all persons except "tom". Operators are: "=" for equal,
+      "~=" for approximately equal "&lt;=" "&gt;=" for lexicographically less than or equal and greater than or equal
+      respectively, "&amp;", "|" and "!" for the AND, OR and NOT logical operators respectively.</comment>
+    </usage>
+    <example>
+      <synopsis>.ldap_attributes cn,uid,mail</synopsis>
+      <synopsis>.ldap_search cn=* onelevel "ou=People,dc=example,dc=gnome-db"</synopsis>
+      <comment></comment>
+      <raw>Distinguished name                              | cn                 | uid        | mail                  
+------------------------------------------------+--------------------+------------+-----------------------
+uid=abarnes,ou=People,dc=example,dc=gnome-db    | Anne-Louise Barnes | abarnes    | abarnes example com   
+uid=abergin,ou=People,dc=example,dc=gnome-db    | Andy Bergin        | abergin    | abergin example com   
+uid=achassin,ou=People,dc=example,dc=gnome-db   | Ashley Chassin     | achassin   | achassin example com
+[...]
+(150 rows)</raw>
+    </example>
+  </command>
 </gda-sql-help>
\ No newline at end of file



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