Index: src/app/qgsnewconnection.cpp =================================================================== --- src/app/qgsnewconnection.cpp (revision 7434) +++ src/app/qgsnewconnection.cpp (working copy) @@ -22,6 +22,8 @@ #include "qgsnewconnection.h" #include "qgscontexthelp.h" +#include "qgslogger.h" + extern "C" { #include @@ -104,12 +106,20 @@ password.replace('\\', "\\\\"); password.replace('\'', "\\'"); - QString connInfo = - "host=" + txtHost->text() + - " dbname=" + txtDatabase->text() + - " port=" + txtPort->text() + - " user=" + txtUsername->text() + - " password='" + password + "'"; + QString connInfo = "dbname=" + txtDatabase->text(); + + // Do we need TCP/IP connect info? + if( txtHost->text() != "" ) + connInfo += "host=" + txtHost->text() + + " port=" + txtPort->text(); + + // Do we need a username and password? + if( txtUsername->text() != "" ) + connInfo += " user=" + txtUsername->text() + + " password='" + password + "'"; + + QgsLogger::debug( "PQconnectdb("+connInfo+");" ); + PGconn *pd = PQconnectdb(connInfo.toLocal8Bit().data()); // std::cout << pd->ErrorMessage(); if (PQstatus(pd) == CONNECTION_OK) Index: src/app/qgsdbsourceselect.cpp =================================================================== --- src/app/qgsdbsourceselect.cpp (revision 7434) +++ src/app/qgsdbsourceselect.cpp (working copy) @@ -299,40 +299,50 @@ QSettings settings; QString key = "/PostgreSQL/connections/" + cmbConnections->currentText(); - QString connString = "host="; + QString database = settings.readEntry(key + "/database"); + QString connString = "dbname=" + database; + QString host = settings.readEntry(key + "/host"); - connString += host; - connString += " dbname="; - QString database = settings.readEntry(key + "/database"); - connString += database + " port="; - QString port = settings.readEntry(key + "/port"); - if(port.length() == 0){ - port = "5432"; + if( host != "" ) + { + connString += " host="+host; + + QString port = settings.readEntry(key + "/port"); + if(port.length() == 0){ + port = "5432"; + } + connString += " port=" + port; } - connString += port + " user="; + + bool makeConnection = true; + bool searchGeometryColumnsOnly = settings.readBoolEntry(key + "/geometryColumnsOnly"); + bool searchPublicOnly = settings.readBoolEntry(key + "/publicOnly"); + QString username = settings.readEntry(key + "/username"); - connString += username; QString password = settings.readEntry(key + "/password"); - bool searchPublicOnly = settings.readBoolEntry(key + "/publicOnly"); - bool searchGeometryColumnsOnly = settings.readBoolEntry(key + "/geometryColumnsOnly"); - bool makeConnection = true; - if (password == QString::null) + if( username != "" ) { - // get password from user - makeConnection = false; - QString password = QInputDialog::getText(tr("Password for ") + database + "@" + host, - tr("Please enter your password:"), - QLineEdit::Password, QString::null, &makeConnection, this); - // allow null password entry in case its valid for the database + connString += " user="+username; + if (password == QString::null) + { + // get password from user + makeConnection = false; + QString password = QInputDialog::getText(tr("Password for ") + database + "@" + host, + tr("Please enter your password:"), + QLineEdit::Password, QString::null, &makeConnection, this); + // allow null password entry in case its valid for the database + } + + // Need to escape the password to allow for single quotes and backslashes + password.replace('\\', "\\\\"); + password.replace('\'', "\\'"); + connString += " password='" + password + "'"; } - // Need to escape the password to allow for single quotes and backslashes - password.replace('\\', "\\\\"); - password.replace('\'', "\\'"); - connString += " password='" + password + "'"; #ifdef QGISDEBUG std::cout << "Connection info: " << connString.toLocal8Bit().data() << std::endl; #endif + if (makeConnection) { m_connInfo = connString; //host + " " + database + " " + username + " " + password; Index: src/core/qgsdatasourceuri.cpp =================================================================== --- src/core/qgsdatasourceuri.cpp (revision 7434) +++ src/core/qgsdatasourceuri.cpp (working copy) @@ -92,65 +92,71 @@ // parse the connection info QStringList conParts = QStringList::split(" ", connInfo); - QStringList parm = QStringList::split("=", conParts[0]); - if(parm.size() == 2) + int myPart; + + // Note, several components (everything but dbname) are optional, so + // leave the value empty if not found. + for( myPart = 0; myPart < conParts.size(); myPart++ ) { - host = parm[1]; - } - parm = QStringList::split("=", conParts[1]); - if(parm.size() == 2) - { - database = parm[1]; - } - parm = QStringList::split("=", conParts[2]); - if(parm.size() == 2) - { - port = parm[1]; - } + QStringList parm = QStringList::split("=", conParts[0]); - parm = QStringList::split("=", conParts[3]); - if(parm.size() == 2) - { - username = parm[1]; + if( parm.size() != 2 ) + continue; + + if( parm[0] == "host" ) + host = parm[1]; + + if( parm[0] == "dbname" ) + database = parm[1]; + + if( parm[0] == "port" ) + port = parm[1]; + + if( parm[0] == "user" ) + username = parm[1]; + + if( parm[0] == "password" ) + { + // The password can have '=' and ' ' characters in it, so we can't + // use the split on '=' and ' ' technique - use indexOf() + // instead. + QString key="password='"; + int i = connInfo.indexOf(key); + if (i != -1) + { + QString pass = connInfo.mid(i+key.length()); + // Now walk through the string till we find a ' character, but + // need to allow for an escaped ' character (which will be the + // \' character pair). + int n = 0; + bool escaped = false; + while (n < pass.length() && (pass[n] != '\'' || escaped)) + { + if (pass[n] == '\\') + escaped = true; + else + escaped = false; + n++; + } + // The -1 is to remove the trailing ' character + password = pass.left(n); + } + } } - - // The password can have '=' and ' ' characters in it, so we can't - // use the split on '=' and ' ' technique - use indexOf() - // instead. - QString key="password='"; - int i = connInfo.indexOf(key); - if (i != -1) - { - QString pass = connInfo.mid(i+key.length()); - // Now walk through the string till we find a ' character, but - // need to allow for an escaped ' character (which will be the - // \' character pair). - int n = 0; - bool escaped = false; - while (n < pass.length() && (pass[n] != '\'' || escaped)) - { - if (pass[n] == '\\') - escaped = true; - else - escaped = false; - n++; - } - // The -1 is to remove the trailing ' character - password = pass.left(n); - } } QString QgsDataSourceURI::text() const { - return QString("host=" + host + - " dbname=" + database + - " port=" + port + - " user=" + username + - " password='" + password + - "' table=" + schema + '.' + table + - " (" + geometryColumn + ")" + - " sql=" + sql); + QString connInfo = "dbname="+database; + + if( host != "" ) + connInfo += " host=" + host + " port=" + port; + + if( username != "" ) + connInfo += " user=" + username + " password='"+password+"'"; + + return connInfo; } void QgsDataSourceURI::setConnection(const QString& aHost,