[libgda/LIBGDA_4.0] Handle cases in gda_connection_string_split(), thanks to Andrea Zagli
- From: Vivien Malerba <vivien src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda/LIBGDA_4.0] Handle cases in gda_connection_string_split(), thanks to Andrea Zagli
- Date: Thu, 26 Aug 2010 18:51:57 +0000 (UTC)
commit 9af4721bdf17bfa3187922f943df44a996290f71
Author: Vivien Malerba <malerba gnome-db org>
Date: Thu Aug 26 19:41:10 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 | 62 +++++++++++++++++++++++++++++++++-
tests/.gitignore | 1 +
tests/Makefile.am | 11 +++++-
tests/test-connection-string-split.c | 54 +++++++++++++++++++++++++++++
4 files changed, 125 insertions(+), 3 deletions(-)
---
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index e498cc0..b2996b1 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -2146,7 +2146,27 @@ gda_dsn_split (const gchar *string, gchar **out_dsn, gchar **out_username, gchar
* Extract the provider, connection parameters, username and password from @string.
* 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 correclty decoded.
+ * 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,
@@ -2195,6 +2215,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 bdadec1..d5638e1 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -2,3 +2,4 @@ test-bin-converter
test-ddl-creator
test-sql-identifier
test-identifiers-quotes
+test-connection-string-split
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ddcb879..edbd78c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,7 @@
noinst_LTLIBRARIES = libgda-test-4.0.la
-TESTS = test-ddl-creator test-bin-converter test-sql-identifier
-check_PROGRAMS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes
+TESTS = test-ddl-creator test-bin-converter test-sql-identifier
+check_PROGRAMS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-connection-string-split
SUBDIRS = providers parser value-holders meta-store data-models multi-threading
@@ -61,4 +61,11 @@ test_identifiers_quotes_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]