Skip to content

Commit

Permalink
implement #3522
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@15258 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Feb 24, 2011
1 parent ff57fcc commit cdc9ae7
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 43 deletions.
11 changes: 11 additions & 0 deletions python/core/qgsdatasourceuri.sip
Expand Up @@ -39,6 +39,14 @@ public:
const QString& aPassword,
SSLmode sslmode = SSLprefer );

//! Set all connection related members at once
//! \note added in 1.7
void setConnection(const QString& service,
const QString& aDatabase,
const QString& aUsername,
const QString& aPassword,
SSLmode sslmode = SSLprefer );

//! Set database
//! \note added in 1.4
void setDatabase( const QString &database );
Expand Down Expand Up @@ -73,6 +81,9 @@ public:
QString port() const;
SSLmode sslMode() const;

// added in 1.7
QString service() const;

void setSql(QString sql);

// added in 1.2
Expand Down
24 changes: 18 additions & 6 deletions src/app/postgres/qgspgnewconnection.cpp
Expand Up @@ -48,14 +48,15 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
QSettings settings;

QString key = "/PostgreSQL/connections/" + connName;
txtService->setText( settings.value( key + "/service" ).toString() );
txtHost->setText( settings.value( key + "/host" ).toString() );
txtDatabase->setText( settings.value( key + "/database" ).toString() );
QString port = settings.value( key + "/port" ).toString();
if ( port.length() == 0 )
{
port = "5432";
}
txtPort->setText( port );
txtDatabase->setText( settings.value( key + "/database" ).toString() );
cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() );
cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometrycolumnsOnly", false ).toBool() );
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
Expand Down Expand Up @@ -102,7 +103,8 @@ void QgsPgNewConnection::accept()

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

baseKey += txtName->text();
settings.setValue( baseKey + "/service", txtService->text() );
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
Expand Down Expand Up @@ -160,9 +163,18 @@ QgsPgNewConnection::~QgsPgNewConnection()
void QgsPgNewConnection::testConnection()
{
QgsDataSourceURI uri;
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
txtUsername->text(), txtPassword->text(),
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
if ( !txtService->text().isEmpty() )
{
uri.setConnection( txtService->text(), txtDatabase->text(),
txtUsername->text(), txtPassword->text(),
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
}
else
{
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
txtUsername->text(), txtPassword->text(),
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
}
QString conninfo = uri.connectionInfo();
QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );

Expand Down
31 changes: 20 additions & 11 deletions src/app/postgres/qgspgsourceselect.cpp
Expand Up @@ -137,11 +137,12 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
if ( QMessageBox::Ok != QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel ) )
return;

settings.remove( key + "/service" );
settings.remove( key + "/host" );
settings.remove( key + "/port" );
settings.remove( key + "/database" );
settings.remove( key + "/username" );
settings.remove( key + "/password" );
settings.remove( key + "/port" );
settings.remove( key + "/sslmode" );
settings.remove( key + "/publicOnly" );
settings.remove( key + "/geometryColumnsOnly" );
Expand Down Expand Up @@ -415,17 +416,23 @@ void QgsPgSourceSelect::on_btnConnect_clicked()

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

QString database = settings.value( key + "/database" ).toString();
QString username = settings.value( key + "/username" ).toString();
QString password = settings.value( key + "/password" ).toString();
QString service = settings.value( key + "/service" ).toString();
QString host = settings.value( key + "/host" ).toString();
QString port = settings.value( key + "/port" ).toString();
QString database = settings.value( key + "/database" ).toString();
QString username = settings.value( key + "/username" ).toString();
QString password = settings.value( key + "/password" ).toString();
QgsDataSourceURI::SSLmode sslmode = ( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt();

QgsDataSourceURI uri;
uri.setConnection( settings.value( key + "/host" ).toString(),
settings.value( key + "/port" ).toString(),
database,
username,
password,
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );
if ( !service.isEmpty() )
{
uri.setConnection( service, database, username, password, sslmode );
}
else
{
uri.setConnection( host, port, database, username, password, sslmode );
}

bool searchPublicOnly = settings.value( key + "/publicOnly" ).toBool();
bool searchGeometryColumnsOnly = settings.value( key + "/geometryColumnsOnly" ).toBool();
Expand Down Expand Up @@ -511,7 +518,9 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
"Check your username and password and try again.\n\n"
"The database said:\n%3" )
.arg( settings.value( key + "/database" ).toString() )
.arg( settings.value( key + "/host" ).toString() )
.arg( !settings.value( key + "/service" ).toString().isEmpty()
? settings.value( key + "/service" ).toString()
: settings.value( key + "/host" ).toString() )
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
}

Expand Down
51 changes: 39 additions & 12 deletions src/core/qgsdatasourceuri.cpp
Expand Up @@ -118,7 +118,7 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer ), mKeyC
}
else if ( pname == "service" )
{
QgsDebugMsg( "service keyword ignored" );
mService = pval;
}
else if ( pname == "user" )
{
Expand Down Expand Up @@ -234,6 +234,11 @@ void QgsDataSourceURI::setUsername( QString username )
mUsername = username;
}

QString QgsDataSourceURI::service() const
{
return mService;
}

QString QgsDataSourceURI::host() const
{
return mHost;
Expand Down Expand Up @@ -401,37 +406,46 @@ QString QgsDataSourceURI::getValue( const QString &uri, int &i )

QString QgsDataSourceURI::connectionInfo() const
{
QString connectionInfo = "dbname='" + escape( mDatabase ) + "'";
QStringList connectionItems;

if ( mDatabase != "" )
{
connectionItems << "dbname='" + escape( mDatabase ) + "'";
}

if ( mHost != "" )
if ( mService != "" )
{
connectionItems << "service='" + escape( mService ) + "'";
}
else if ( mHost != "" )
{
connectionInfo += " host=" + mHost;
connectionItems << "host=" + mHost;
if ( mPort != "" )
connectionInfo += " port=" + mPort;
connectionItems << "port=" + mPort;
}

if ( mUsername != "" )
{
connectionInfo += " user='" + escape( mUsername ) + "'";
connectionItems << "user='" + escape( mUsername ) + "'";

if ( mPassword != "" )
{
connectionInfo += " password='" + escape( mPassword ) + "'";
connectionItems << "password='" + escape( mPassword ) + "'";
}
}

if ( mSSLmode == SSLdisable )
connectionInfo += " sslmode=disable";
connectionItems << "sslmode=disable";
else if ( mSSLmode == SSLallow )
connectionInfo += " sslmode=allow";
connectionItems << "sslmode=allow";
else if ( mSSLmode == SSLrequire )
connectionInfo += " sslmode=require";
connectionItems << "sslmode=require";
#if 0
else if ( mSSLmode == SSLprefer )
connectionInfo += " sslmode=prefer";
connectionItems << "sslmode=prefer";
#endif

return connectionInfo;
return connectionItems.join( " " );
}

QString QgsDataSourceURI::uri() const
Expand Down Expand Up @@ -482,6 +496,19 @@ void QgsDataSourceURI::setConnection( const QString &host,
mSSLmode = sslmode;
}

void QgsDataSourceURI::setConnection( const QString &service,
const QString &database,
const QString &username,
const QString &password,
SSLmode sslmode )
{
mService = service;
mDatabase = database;
mUsername = username;
mPassword = password;
mSSLmode = sslmode;
}

void QgsDataSourceURI::setDataSource( const QString &schema,
const QString &table,
const QString &geometryColumn,
Expand Down
17 changes: 15 additions & 2 deletions src/core/qgsdatasourceuri.h
Expand Up @@ -57,6 +57,14 @@ class CORE_EXPORT QgsDataSourceURI
const QString& aPassword,
SSLmode sslmode = SSLprefer );

//! Set all connection related members at once (for the service case)
//! \note This optional sslmode parameter has been added in version 1.7
void setConnection( const QString& aService,
const QString& aDatabase,
const QString& aUsername,
const QString& aPassword,
SSLmode sslmode = SSLprefer );

//! Set database
// \note added in 1.4
void setDatabase( const QString &database );
Expand Down Expand Up @@ -100,6 +108,9 @@ class CORE_EXPORT QgsDataSourceURI
QString password() const;
enum SSLmode sslMode() const;

// added in 1.7
QString service() const;

// added in version 1.2
QString keyColumn() const;
void setKeyColumn( QString column );
Expand All @@ -113,10 +124,12 @@ class CORE_EXPORT QgsDataSourceURI

//! host name
QString mHost;
//! database name
QString mDatabase;
//! port the database server listens on
QString mPort;
//! service name
QString mService;
//! database name
QString mDatabase;
//! schema
QString mSchema;
//! spatial table
Expand Down

0 comments on commit cdc9ae7

Please sign in to comment.