r19822 - gnucash/trunk/src/backend/dbi - Bug 628409: SQL coerces identifiers to lowercase, but postgres's C interface is case sensitive.

John Ralls jralls at code.gnucash.org
Tue Nov 16 21:14:13 EST 2010


Author: jralls
Date: 2010-11-16 21:14:13 -0500 (Tue, 16 Nov 2010)
New Revision: 19822
Trac: http://svn.gnucash.org/trac/changeset/19822

Modified:
   gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c
Log:
Bug 628409: SQL coerces identifiers to lowercase, but postgres's C interface is case sensitive.

Modified: gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c
===================================================================
--- gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c	2010-11-17 01:31:37 UTC (rev 19821)
+++ gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c	2010-11-17 02:14:13 UTC (rev 19822)
@@ -891,7 +891,7 @@
     gint result = 0;
     gchar* protocol = NULL;
     gchar* host = NULL;
-    gchar* dbname = NULL;
+    gchar *dbname = NULL, *dbnamelc = NULL;
     gchar* username = NULL;
     gchar* password = NULL;
     gboolean success = FALSE;
@@ -910,6 +910,11 @@
                              &username, &password, &dbname );
     if ( portnum == 0 )
         portnum = PGSQL_DEFAULT_PORT;
+/* Postgres's SQL interface coerces identifiers to lower case, but the
+ * C interface is case-sensitive. This results in a mixed-case dbname
+ * being created (with a lower case name) but then dbi can't conect to
+ * it. To work around this, coerce the name to lowercase first. */
+    dbnamelc = g_utf8_strdown( dbname, -1 );
 
     // Try to connect to the db.  If it doesn't exist and the create
     // flag is TRUE, we'll need to connect to the 'postgres' db and execute the
@@ -926,7 +931,7 @@
         goto exit;
     }
     dbi_conn_error_handler( be->conn, pgsql_error_fn, be );
-    if ( !set_standard_connection_options( qbe, be->conn, host, portnum, dbname, username, password ) )
+    if ( !set_standard_connection_options( qbe, be->conn, host, portnum, dbnamelc, username, password ) )
     {
         goto exit;
     }
@@ -957,7 +962,7 @@
         if ( create )
         {
             dbi_result dresult;
-            result = dbi_conn_set_option( be->conn, "dbname", "postgres" );
+            result = dbi_conn_set_option( be->conn, "dbnamelc", "postgres" );
             if ( result < 0 )
             {
                 PERR( "Error setting 'dbname' option\n" );
@@ -971,14 +976,14 @@
                 qof_backend_set_error( qbe, ERR_BACKEND_SERVER_ERR );
                 goto exit;
             }
-            dresult = dbi_conn_queryf( be->conn, "CREATE DATABASE %s WITH ENCODING 'UTF8'", dbname );
+            dresult = dbi_conn_queryf( be->conn, "CREATE DATABASE %s WITH ENCODING 'UTF8'", dbnamelc );
             if ( dresult == NULL )
             {
                 PERR( "Unable to create database '%s'\n", dbname );
                 qof_backend_set_error( qbe, ERR_BACKEND_SERVER_ERR );
                 goto exit;
             }
-	    dbi_conn_queryf( be->conn, "ALTER DATABASE %s SET standard_conforming_strings TO on", dbname );
+	    dbi_conn_queryf( be->conn, "ALTER DATABASE %s SET standard_conforming_strings TO on", dbnamelc );
             dbi_conn_close( be->conn );
 
             // Try again to connect to the db
@@ -990,7 +995,7 @@
                 goto exit;
             }
             dbi_conn_error_handler( be->conn, pgsql_error_fn, be );
-            if ( !set_standard_connection_options( qbe, be->conn, host, PGSQL_DEFAULT_PORT, dbname, username, password ) )
+            if ( !set_standard_connection_options( qbe, be->conn, host, PGSQL_DEFAULT_PORT, dbnamelc, username, password ) )
             {
                 goto exit;
             }
@@ -1024,6 +1029,7 @@
     g_free( username );
     g_free( password );
     g_free( dbname );
+    g_free( dbnamelc );
 
     LEAVE (" ");
 }



More information about the gnucash-changes mailing list