Skip to content

Commit 76f82b9

Browse files
author
g_j_m
committed
Fix for ticket #400 (postgresql passwords with spaces didn't work). Now
also works with other punctuation characters too. Also a tidy up of some odd \ characters git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6113 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e321270 commit 76f82b9

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

src/gui/qgsdbsourceselect.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,13 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
323323
QString password = QInputDialog::getText(tr("Password for ") + database + "@" + host,
324324
tr("Please enter your password:"),
325325
QLineEdit::Password, QString::null, &makeConnection, this);
326-
327-
// allow null password entry in case its valid for the database
326+
// allow null password entry in case its valid for the database
328327
}
329-
connString += " password=" + password;
328+
329+
// Need to escape the password to allow for single quotes and backslashes
330+
password.replace('\\', "\\\\");
331+
password.replace('\'', "\\'");
332+
connString += " password='" + password + "'";
330333
#ifdef QGISDEBUG
331334
std::cout << "Connection info: " << connString.toLocal8Bit().data() << std::endl;
332335
#endif

src/gui/qgsnewconnection.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ void QgsNewConnection::testConnection()
9999
// following line uses Qt SQL plugin - currently not used
100100
// QSqlDatabase *testCon = QSqlDatabase::addDatabase("QPSQL7","testconnection");
101101

102+
// Need to escape the password to allow for single quotes and backslashes
103+
QString password = txtPassword->text();
104+
password.replace('\\', "\\\\");
105+
password.replace('\'', "\\'");
106+
102107
QString connInfo =
103108
"host=" + txtHost->text() +
104109
" dbname=" + txtDatabase->text() +
105110
" port=" + txtPort->text() +
106111
" user=" + txtUsername->text() +
107-
" password=" + txtPassword->text();
112+
" password='" + password + "'";
108113
PGconn *pd = PQconnectdb(connInfo.toLocal8Bit().data());
109114
// std::cout << pd->ErrorMessage();
110115
if (PQstatus(pd) == CONNECTION_OK)

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,32 @@ QgsPostgresProvider::QgsPostgresProvider(QString const & uri)
147147
{
148148
mUri.username = parm[1];
149149
}
150-
parm = QStringList::split("=", conParts[4]);
151-
if(parm.size() == 2)
152-
{
153-
mUri.password = parm[1];
154-
}
150+
151+
// The password can have '=' and ' ' characters in it, so we can't
152+
// use the split on '=' and ' ' technique - use indexOf()
153+
// instead.
154+
QString key="password='";
155+
int i = connInfo.indexOf(key);
156+
if (i != -1)
157+
{
158+
QString password = connInfo.mid(i+key.length());
159+
// Now walk through the string till we find a ' character, but
160+
// need to allow for an escaped ' character (which will be the
161+
// \' character pair).
162+
int n = 0;
163+
bool escaped = false;
164+
while (n < password.length() && (password[n] != '\'' || escaped))
165+
{
166+
if (password[n] == '\\')
167+
escaped = true;
168+
else
169+
escaped = false;
170+
n++;
171+
}
172+
// The -1 is to remove the trailing ' character
173+
mUri.password = password.left(n-1);
174+
}
175+
else
155176
/* end uri structure */
156177

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

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

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

1347-
QgsDebugMsg(temp.view_schema + "." \
1348-
+ temp.view_name + "." \
1349-
+ temp.view_column_name + " <- " \
1350-
+ temp.table_schema + "." \
1351-
+ temp.table_name + "." \
1352-
+ temp.column_name + " is a '" \
1353-
+ temp.table_type + "' of type " \
1368+
QgsDebugMsg(temp.view_schema + "."
1369+
+ temp.view_name + "."
1370+
+ temp.view_column_name + " <- "
1371+
+ temp.table_schema + "."
1372+
+ temp.table_name + "."
1373+
+ temp.column_name + " is a '"
1374+
+ temp.table_type + "' of type "
13541375
+ temp.column_type);
13551376

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

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

2474-
QgsDebugMsg("QgsPostgresProvider: Set extent to: " + QString::number(layerExtent.xMin()) + ", " + \
2475-
QString::number(layerExtent.yMin()) + " " + QString::number(layerExtent.xMax()) + ", " + \
2495+
QgsDebugMsg("QgsPostgresProvider: Set extent to: " + QString::number(layerExtent.xMin()) + ", " +
2496+
QString::number(layerExtent.yMin()) + " " + QString::number(layerExtent.xMax()) + ", " +
24762497
QString::number(layerExtent.yMax()));
24772498

24782499
QgsDebugMsg("QgsPostgresProvider: emitting fullExtentCalculated()");

0 commit comments

Comments
 (0)