Skip to content

Commit cdc9ae7

Browse files
author
jef
committed
implement #3522
git-svn-id: http://svn.osgeo.org/qgis/trunk@15258 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ff57fcc commit cdc9ae7

File tree

6 files changed

+133
-43
lines changed

6 files changed

+133
-43
lines changed

python/core/qgsdatasourceuri.sip

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public:
3939
const QString& aPassword,
4040
SSLmode sslmode = SSLprefer );
4141

42+
//! Set all connection related members at once
43+
//! \note added in 1.7
44+
void setConnection(const QString& service,
45+
const QString& aDatabase,
46+
const QString& aUsername,
47+
const QString& aPassword,
48+
SSLmode sslmode = SSLprefer );
49+
4250
//! Set database
4351
//! \note added in 1.4
4452
void setDatabase( const QString &database );
@@ -73,6 +81,9 @@ public:
7381
QString port() const;
7482
SSLmode sslMode() const;
7583

84+
// added in 1.7
85+
QString service() const;
86+
7687
void setSql(QString sql);
7788

7889
// added in 1.2

src/app/postgres/qgspgnewconnection.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
4848
QSettings settings;
4949

5050
QString key = "/PostgreSQL/connections/" + connName;
51+
txtService->setText( settings.value( key + "/service" ).toString() );
5152
txtHost->setText( settings.value( key + "/host" ).toString() );
52-
txtDatabase->setText( settings.value( key + "/database" ).toString() );
5353
QString port = settings.value( key + "/port" ).toString();
5454
if ( port.length() == 0 )
5555
{
5656
port = "5432";
5757
}
5858
txtPort->setText( port );
59+
txtDatabase->setText( settings.value( key + "/database" ).toString() );
5960
cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() );
6061
cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometrycolumnsOnly", false ).toBool() );
6162
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
@@ -102,7 +103,8 @@ void QgsPgNewConnection::accept()
102103

103104
// warn if entry was renamed to an existing connection
104105
if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
105-
settings.contains( baseKey + txtName->text() + "/host" ) &&
106+
( settings.contains( baseKey + txtName->text() + "/service" ) ||
107+
settings.contains( baseKey + txtName->text() + "/host" ) ) &&
106108
QMessageBox::question( this,
107109
tr( "Save connection" ),
108110
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
@@ -119,9 +121,10 @@ void QgsPgNewConnection::accept()
119121
}
120122

121123
baseKey += txtName->text();
124+
settings.setValue( baseKey + "/service", txtService->text() );
122125
settings.setValue( baseKey + "/host", txtHost->text() );
123-
settings.setValue( baseKey + "/database", txtDatabase->text() );
124126
settings.setValue( baseKey + "/port", txtPort->text() );
127+
settings.setValue( baseKey + "/database", txtDatabase->text() );
125128
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
126129
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
127130
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
@@ -160,9 +163,18 @@ QgsPgNewConnection::~QgsPgNewConnection()
160163
void QgsPgNewConnection::testConnection()
161164
{
162165
QgsDataSourceURI uri;
163-
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
164-
txtUsername->text(), txtPassword->text(),
165-
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
166+
if ( !txtService->text().isEmpty() )
167+
{
168+
uri.setConnection( txtService->text(), txtDatabase->text(),
169+
txtUsername->text(), txtPassword->text(),
170+
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
171+
}
172+
else
173+
{
174+
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
175+
txtUsername->text(), txtPassword->text(),
176+
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
177+
}
166178
QString conninfo = uri.connectionInfo();
167179
QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );
168180

src/app/postgres/qgspgsourceselect.cpp

+20-11
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
137137
if ( QMessageBox::Ok != QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel ) )
138138
return;
139139

140+
settings.remove( key + "/service" );
140141
settings.remove( key + "/host" );
142+
settings.remove( key + "/port" );
141143
settings.remove( key + "/database" );
142144
settings.remove( key + "/username" );
143145
settings.remove( key + "/password" );
144-
settings.remove( key + "/port" );
145146
settings.remove( key + "/sslmode" );
146147
settings.remove( key + "/publicOnly" );
147148
settings.remove( key + "/geometryColumnsOnly" );
@@ -415,17 +416,23 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
415416

416417
QString key = "/PostgreSQL/connections/" + cmbConnections->currentText();
417418

418-
QString database = settings.value( key + "/database" ).toString();
419-
QString username = settings.value( key + "/username" ).toString();
420-
QString password = settings.value( key + "/password" ).toString();
419+
QString service = settings.value( key + "/service" ).toString();
420+
QString host = settings.value( key + "/host" ).toString();
421+
QString port = settings.value( key + "/port" ).toString();
422+
QString database = settings.value( key + "/database" ).toString();
423+
QString username = settings.value( key + "/username" ).toString();
424+
QString password = settings.value( key + "/password" ).toString();
425+
QgsDataSourceURI::SSLmode sslmode = ( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt();
421426

422427
QgsDataSourceURI uri;
423-
uri.setConnection( settings.value( key + "/host" ).toString(),
424-
settings.value( key + "/port" ).toString(),
425-
database,
426-
username,
427-
password,
428-
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );
428+
if ( !service.isEmpty() )
429+
{
430+
uri.setConnection( service, database, username, password, sslmode );
431+
}
432+
else
433+
{
434+
uri.setConnection( host, port, database, username, password, sslmode );
435+
}
429436

430437
bool searchPublicOnly = settings.value( key + "/publicOnly" ).toBool();
431438
bool searchGeometryColumnsOnly = settings.value( key + "/geometryColumnsOnly" ).toBool();
@@ -511,7 +518,9 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
511518
"Check your username and password and try again.\n\n"
512519
"The database said:\n%3" )
513520
.arg( settings.value( key + "/database" ).toString() )
514-
.arg( settings.value( key + "/host" ).toString() )
521+
.arg( !settings.value( key + "/service" ).toString().isEmpty()
522+
? settings.value( key + "/service" ).toString()
523+
: settings.value( key + "/host" ).toString() )
515524
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
516525
}
517526

src/core/qgsdatasourceuri.cpp

+39-12
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer ), mKeyC
118118
}
119119
else if ( pname == "service" )
120120
{
121-
QgsDebugMsg( "service keyword ignored" );
121+
mService = pval;
122122
}
123123
else if ( pname == "user" )
124124
{
@@ -234,6 +234,11 @@ void QgsDataSourceURI::setUsername( QString username )
234234
mUsername = username;
235235
}
236236

237+
QString QgsDataSourceURI::service() const
238+
{
239+
return mService;
240+
}
241+
237242
QString QgsDataSourceURI::host() const
238243
{
239244
return mHost;
@@ -401,37 +406,46 @@ QString QgsDataSourceURI::getValue( const QString &uri, int &i )
401406

402407
QString QgsDataSourceURI::connectionInfo() const
403408
{
404-
QString connectionInfo = "dbname='" + escape( mDatabase ) + "'";
409+
QStringList connectionItems;
410+
411+
if ( mDatabase != "" )
412+
{
413+
connectionItems << "dbname='" + escape( mDatabase ) + "'";
414+
}
405415

406-
if ( mHost != "" )
416+
if ( mService != "" )
417+
{
418+
connectionItems << "service='" + escape( mService ) + "'";
419+
}
420+
else if ( mHost != "" )
407421
{
408-
connectionInfo += " host=" + mHost;
422+
connectionItems << "host=" + mHost;
409423
if ( mPort != "" )
410-
connectionInfo += " port=" + mPort;
424+
connectionItems << "port=" + mPort;
411425
}
412426

413427
if ( mUsername != "" )
414428
{
415-
connectionInfo += " user='" + escape( mUsername ) + "'";
429+
connectionItems << "user='" + escape( mUsername ) + "'";
416430

417431
if ( mPassword != "" )
418432
{
419-
connectionInfo += " password='" + escape( mPassword ) + "'";
433+
connectionItems << "password='" + escape( mPassword ) + "'";
420434
}
421435
}
422436

423437
if ( mSSLmode == SSLdisable )
424-
connectionInfo += " sslmode=disable";
438+
connectionItems << "sslmode=disable";
425439
else if ( mSSLmode == SSLallow )
426-
connectionInfo += " sslmode=allow";
440+
connectionItems << "sslmode=allow";
427441
else if ( mSSLmode == SSLrequire )
428-
connectionInfo += " sslmode=require";
442+
connectionItems << "sslmode=require";
429443
#if 0
430444
else if ( mSSLmode == SSLprefer )
431-
connectionInfo += " sslmode=prefer";
445+
connectionItems << "sslmode=prefer";
432446
#endif
433447

434-
return connectionInfo;
448+
return connectionItems.join( " " );
435449
}
436450

437451
QString QgsDataSourceURI::uri() const
@@ -482,6 +496,19 @@ void QgsDataSourceURI::setConnection( const QString &host,
482496
mSSLmode = sslmode;
483497
}
484498

499+
void QgsDataSourceURI::setConnection( const QString &service,
500+
const QString &database,
501+
const QString &username,
502+
const QString &password,
503+
SSLmode sslmode )
504+
{
505+
mService = service;
506+
mDatabase = database;
507+
mUsername = username;
508+
mPassword = password;
509+
mSSLmode = sslmode;
510+
}
511+
485512
void QgsDataSourceURI::setDataSource( const QString &schema,
486513
const QString &table,
487514
const QString &geometryColumn,

src/core/qgsdatasourceuri.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ class CORE_EXPORT QgsDataSourceURI
5757
const QString& aPassword,
5858
SSLmode sslmode = SSLprefer );
5959

60+
//! Set all connection related members at once (for the service case)
61+
//! \note This optional sslmode parameter has been added in version 1.7
62+
void setConnection( const QString& aService,
63+
const QString& aDatabase,
64+
const QString& aUsername,
65+
const QString& aPassword,
66+
SSLmode sslmode = SSLprefer );
67+
6068
//! Set database
6169
// \note added in 1.4
6270
void setDatabase( const QString &database );
@@ -100,6 +108,9 @@ class CORE_EXPORT QgsDataSourceURI
100108
QString password() const;
101109
enum SSLmode sslMode() const;
102110

111+
// added in 1.7
112+
QString service() const;
113+
103114
// added in version 1.2
104115
QString keyColumn() const;
105116
void setKeyColumn( QString column );
@@ -113,10 +124,12 @@ class CORE_EXPORT QgsDataSourceURI
113124

114125
//! host name
115126
QString mHost;
116-
//! database name
117-
QString mDatabase;
118127
//! port the database server listens on
119128
QString mPort;
129+
//! service name
130+
QString mService;
131+
//! database name
132+
QString mDatabase;
120133
//! schema
121134
QString mSchema;
122135
//! spatial table

0 commit comments

Comments
 (0)