[libgda] Handle cases in gda_connection_string_split(), thanks to Andrea Zagli



commit bf330bedad4d2886f4d5b224075d72870cbeaff4
Author: Vivien Malerba <malerba gnome-db org>
Date:   Thu Aug 19 15:36:33 2010 +0200

    Handle cases in gda_connection_string_split(), thanks to Andrea Zagli
    
    where username and/or password could be specified in the
    connection string using USERNAME=... and PASSWORD=...

 libgda/gda-util.c                    |   60 ++++++++++++++++++++++++++++++++++
 tests/.gitignore                     |    1 +
 tests/Makefile.am                    |   11 +++++-
 tests/test-connection-string-split.c |   54 ++++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 2 deletions(-)
---
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index 2ecb247..1920fc1 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -2392,6 +2392,26 @@ gda_dsn_split (const gchar *string, gchar **out_dsn, gchar **out_username, gchar
  * in @string, the various parts are strings
  * which are expected to be encoded using an RFC 1738 compliant encoding. If they are specified, 
  * the returned provider, username and password strings are correctly decoded.
+ *
+ * For example all the following connection strings:
+ * <programlisting><![CDATA[
+PostgreSQL://meme:pass DB_NAME=mydb;HOST=server
+PostgreSQL://meme DB_NAME=mydb;HOST=server;PASSWORD=pass
+PostgreSQL://meme DB_NAME=mydb;PASSWORD=pass;HOST=server
+PostgreSQL://meme PASSWORD=pass;DB_NAME=mydb;HOST=server
+PostgreSQL://DB_NAME=mydb;HOST=server;USERNAME=meme;PASSWORD=pass
+PostgreSQL://DB_NAME=mydb;HOST=server;PASSWORD=pass;USERNAME=meme
+PostgreSQL://DB_NAME=mydb;USERNAME=meme;PASSWORD=pass;HOST=server
+PostgreSQL://PASSWORD=pass;USERNAME=meme;DB_NAME=mydb;HOST=server
+PostgreSQL://:pass USERNAME=meme;DB_NAME=mydb;HOST=server
+PostgreSQL://:pass DB_NAME=mydb;HOST=server;USERNAME=meme]]></programlisting>
+ *
+ * will return the following new strings (double quotes added here to delimit strings):
+ * <programlisting><![CDATA[
+out_cnc_params: "DB_NAME=mydb;HOST=server"
+out_provider: "PostgreSQL"
+out_username: "meme"
+out_password: "pass"]]></programlisting>
  */
 void
 gda_connection_string_split (const gchar *string, gchar **out_cnc_params, gchar **out_provider, 
@@ -2440,6 +2460,46 @@ gda_connection_string_split (const gchar *string, gchar **out_cnc_params, gchar
 	if (!*out_cnc_params)
 		*out_cnc_params = g_strdup (ap);
 
+	if (*out_cnc_params) {
+		gchar *pos;
+
+		pos = g_strrstr (*out_cnc_params, "USERNAME=");
+		if (pos) {
+			for (ptr = pos + 9; ptr && *ptr != '\0' && *ptr != ';'; ptr++);
+			if (ptr != pos + 9)
+				*out_username = g_strndup (pos + 9, ptr - (pos + 9));
+			gchar *tmp;
+			gint lastpos;
+			tmp = g_strndup (*out_cnc_params, pos - *out_cnc_params);
+			*out_cnc_params = g_strconcat (tmp,
+						       (ptr ? (ptr + 1 ? ptr + 1 : ptr) : ""),
+						       NULL);
+			g_free (tmp);
+			tmp = *out_cnc_params;
+			lastpos = strlen(tmp) - 1;
+			if (tmp [lastpos] == ';')
+				tmp [lastpos] = 0;
+		}
+		
+		pos = g_strrstr (*out_cnc_params, "PASSWORD=");
+		if (pos) {
+			for (ptr = pos + 9; ptr && *ptr != '\0' && *ptr != ';'; ptr++);
+			if (ptr != pos + 9)
+				*out_password = g_strndup (pos + 9, ptr - (pos + 9));
+			gchar *tmp;
+			gint lastpos;
+			tmp = g_strndup (*out_cnc_params, pos - *out_cnc_params);
+			*out_cnc_params = g_strconcat (tmp,
+						       (ptr ? (ptr + 1 ? ptr + 1 : ptr) : ""),
+						       NULL);
+			g_free (tmp);
+			tmp = *out_cnc_params;
+			lastpos = strlen(tmp) - 1;
+			if (tmp [lastpos] == ';')
+				tmp [lastpos] = 0;
+		}
+	}
+
 	/* RFC 1738 decode provider, username and password strings */
 	gda_rfc1738_decode (*out_provider);
 	gda_rfc1738_decode (*out_username);
diff --git a/tests/.gitignore b/tests/.gitignore
index bbc7462..746b555 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -3,3 +3,4 @@ test-ddl-creator
 test-sql-identifier
 test-identifiers-quotes
 test-sql-builder
+test-connection-string-split
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ed7576f..d0aeb48 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,6 @@
 noinst_LTLIBRARIES = libgda-test-4.0.la
-TESTS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-sql-builder
-check_PROGRAMS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-sql-builder
+TESTS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-sql-builder test-connection-string-split
+check_PROGRAMS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-sql-builder test-connection-string-split
 
 
 SUBDIRS = providers parser value-holders meta-store data-models multi-threading
@@ -68,4 +68,11 @@ test_sql_builder_LDADD = \
         $(top_builddir)/libgda/libgda-4.0.la \
         $(LIBGDA_LIBS)
 
+test_connection_string_split_SOURCES = \
+        test-connection-string-split.c
+
+test_connection_string_split_LDADD = \
+        $(top_builddir)/libgda/libgda-4.0.la \
+        $(LIBGDA_LIBS)
+
 EXTRA_DIST = dbstruct.xml
\ No newline at end of file
diff --git a/tests/test-connection-string-split.c b/tests/test-connection-string-split.c
new file mode 100644
index 0000000..0a3ff69
--- /dev/null
+++ b/tests/test-connection-string-split.c
@@ -0,0 +1,54 @@
+#include <libgda/libgda.h>
+#include <string.h>
+int
+main (int argc, char *argv[])
+{
+	g_type_init();
+	gda_init ();
+
+	gchar *str[] = {
+	"PostgreSQL://meme:pass DB_NAME=mydb;HOST=server",
+	"PostgreSQL://meme DB_NAME=mydb;HOST=server;PASSWORD=pass",
+	"PostgreSQL://meme DB_NAME=mydb;PASSWORD=pass;HOST=server",
+	"PostgreSQL://meme PASSWORD=pass;DB_NAME=mydb;HOST=server",
+	"PostgreSQL://DB_NAME=mydb;HOST=server;USERNAME=meme;PASSWORD=pass",
+	"PostgreSQL://DB_NAME=mydb;HOST=server;PASSWORD=pass;USERNAME=meme",
+        "PostgreSQL://DB_NAME=mydb;USERNAME=meme;PASSWORD=pass;HOST=server",
+        "PostgreSQL://PASSWORD=pass;USERNAME=meme;DB_NAME=mydb;HOST=server",
+        "PostgreSQL://:pass USERNAME=meme;DB_NAME=mydb;HOST=server",
+        "PostgreSQL://:pass DB_NAME=mydb;HOST=server;USERNAME=meme",
+	NULL};
+
+	gint i;
+	for (i = 0; ;i++) {
+		if (!str[i])
+			break;
+		gchar *cnc_params, *prov, *user, *pass;
+		gda_connection_string_split (str[i], &cnc_params, &prov, &user, &pass);
+		g_print ("[%s]\n  cnc_params=[%s]\n  prov      =[%s]\n  user      =[%s]\n  pass      =[%s]\n",
+			str[i], cnc_params, prov, user, pass);
+		if (strcmp (cnc_params, "DB_NAME=mydb;HOST=server")) {
+			g_print ("Wrong cnc_params result\n");
+			return 1;
+		}
+		if (strcmp (prov, "PostgreSQL")) {
+			g_print ("Wrong provider result\n");
+			return 1;
+		}
+		if (strcmp (user, "meme")) {
+			g_print ("Wrong username result\n");
+			return 1;
+		}
+		if (strcmp (pass, "pass")) {
+			g_print ("Wrong password result\n");
+			return 1;
+		}
+		g_free (cnc_params);
+		g_free (prov);
+		g_free (user);
+		g_free (pass);
+	}
+
+	return 0;
+}
+



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