Skip to content

Commit

Permalink
Fix for ticket #400 (postgresql passwords with spaces didn't work). Now
Browse files Browse the repository at this point in the history
also works with other punctuation characters too.
Also a tidy up of some odd \ characters


git-svn-id: http://svn.osgeo.org/qgis/trunk@6113 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
g_j_m committed Nov 18, 2006
1 parent 2c74287 commit dfe8492
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/gui/qgsdbsourceselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,13 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
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
// allow null password entry in case its valid for the database
}
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
Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgsnewconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ void QgsNewConnection::testConnection()
// following line uses Qt SQL plugin - currently not used
// QSqlDatabase *testCon = QSqlDatabase::addDatabase("QPSQL7","testconnection");

// Need to escape the password to allow for single quotes and backslashes
QString password = txtPassword->text();
password.replace('\\', "\\\\");
password.replace('\'', "\\'");

QString connInfo =
"host=" + txtHost->text() +
" dbname=" + txtDatabase->text() +
" port=" + txtPort->text() +
" user=" + txtUsername->text() +
" password=" + txtPassword->text();
" password='" + password + "'";
PGconn *pd = PQconnectdb(connInfo.toLocal8Bit().data());
// std::cout << pd->ErrorMessage();
if (PQstatus(pd) == CONNECTION_OK)
Expand Down
51 changes: 36 additions & 15 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,32 @@ QgsPostgresProvider::QgsPostgresProvider(QString const & uri)
{
mUri.username = parm[1];
}
parm = QStringList::split("=", conParts[4]);
if(parm.size() == 2)
{
mUri.password = parm[1];
}

// 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 password = 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 < password.length() && (password[n] != '\'' || escaped))
{
if (password[n] == '\\')
escaped = true;
else
escaped = false;
n++;
}
// The -1 is to remove the trailing ' character
mUri.password = password.left(n-1);
}
else
/* end uri structure */

QgsDebugMsg("Geometry column is: " + geometryColumn);
Expand Down Expand Up @@ -266,7 +287,7 @@ QgsPostgresProvider::QgsPostgresProvider(QString const & uri)
QString attnum = PQgetvalue(tresult, 0, 0);
PQclear(tresult);

QgsDebugMsg("Field: " + attnum + " maps to " + QString::number(i) + " " + fieldName + ", " + \
QgsDebugMsg("Field: " + attnum + " maps to " + QString::number(i) + " " + fieldName + ", " +
fieldType + " (" + QString::number(fldtyp) + "), " + fieldSize + ", " + QString::number(fieldModifier));

attributeFieldsIdMap[attnum.toInt()] = i;
Expand Down Expand Up @@ -1344,13 +1365,13 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
temp.table_type = PQgetvalue(result, i, 6);
temp.column_type = PQgetvalue(result, i, 7);

QgsDebugMsg(temp.view_schema + "." \
+ temp.view_name + "." \
+ temp.view_column_name + " <- " \
+ temp.table_schema + "." \
+ temp.table_name + "." \
+ temp.column_name + " is a '" \
+ temp.table_type + "' of type " \
QgsDebugMsg(temp.view_schema + "."
+ temp.view_name + "."
+ temp.view_column_name + " <- "
+ temp.table_schema + "."
+ temp.table_name + "."
+ temp.column_name + " is a '"
+ temp.table_type + "' of type "
+ temp.column_type);

columnRelations[temp.view_schema + '.' +
Expand Down Expand Up @@ -2471,8 +2492,8 @@ void QgsPostgresProvider::customEvent( QCustomEvent * e )

QgsDebugMsg("QgsPostgresProvider: new extent has been saved");

QgsDebugMsg("QgsPostgresProvider: Set extent to: " + QString::number(layerExtent.xMin()) + ", " + \
QString::number(layerExtent.yMin()) + " " + QString::number(layerExtent.xMax()) + ", " + \
QgsDebugMsg("QgsPostgresProvider: Set extent to: " + QString::number(layerExtent.xMin()) + ", " +
QString::number(layerExtent.yMin()) + " " + QString::number(layerExtent.xMax()) + ", " +
QString::number(layerExtent.yMax()));

QgsDebugMsg("QgsPostgresProvider: emitting fullExtentCalculated()");
Expand Down

0 comments on commit dfe8492

Please sign in to comment.