git-svn-id: http://svn.osgeo.org/qgis/trunk@15258 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
jef 2011-02-24 17:53:50 +00:00
parent ff57fcc648
commit cdc9ae7c5d
6 changed files with 137 additions and 47 deletions

View File

@ -39,6 +39,14 @@ public:
const QString& aPassword, const QString& aPassword,
SSLmode sslmode = SSLprefer ); 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 //! Set database
//! \note added in 1.4 //! \note added in 1.4
void setDatabase( const QString &database ); void setDatabase( const QString &database );
@ -73,6 +81,9 @@ public:
QString port() const; QString port() const;
SSLmode sslMode() const; SSLmode sslMode() const;
// added in 1.7
QString service() const;
void setSql(QString sql); void setSql(QString sql);
// added in 1.2 // added in 1.2

View File

@ -48,14 +48,15 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
QSettings settings; QSettings settings;
QString key = "/PostgreSQL/connections/" + connName; QString key = "/PostgreSQL/connections/" + connName;
txtService->setText( settings.value( key + "/service" ).toString() );
txtHost->setText( settings.value( key + "/host" ).toString() ); txtHost->setText( settings.value( key + "/host" ).toString() );
txtDatabase->setText( settings.value( key + "/database" ).toString() );
QString port = settings.value( key + "/port" ).toString(); QString port = settings.value( key + "/port" ).toString();
if ( port.length() == 0 ) if ( port.length() == 0 )
{ {
port = "5432"; port = "5432";
} }
txtPort->setText( port ); txtPort->setText( port );
txtDatabase->setText( settings.value( key + "/database" ).toString() );
cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() ); cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() );
cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometrycolumnsOnly", false ).toBool() ); cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometrycolumnsOnly", false ).toBool() );
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() ); cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
@ -102,7 +103,8 @@ void QgsPgNewConnection::accept()
// warn if entry was renamed to an existing connection // warn if entry was renamed to an existing connection
if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) && 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, QMessageBox::question( this,
tr( "Save connection" ), tr( "Save connection" ),
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ), tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
@ -119,9 +121,10 @@ void QgsPgNewConnection::accept()
} }
baseKey += txtName->text(); baseKey += txtName->text();
settings.setValue( baseKey + "/service", txtService->text() );
settings.setValue( baseKey + "/host", txtHost->text() ); settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/port", txtPort->text() ); settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" ); settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" ); settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() ); settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
@ -160,9 +163,18 @@ QgsPgNewConnection::~QgsPgNewConnection()
void QgsPgNewConnection::testConnection() void QgsPgNewConnection::testConnection()
{ {
QgsDataSourceURI uri; QgsDataSourceURI uri;
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), if ( !txtService->text().isEmpty() )
txtUsername->text(), txtPassword->text(), {
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() ); 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(); QString conninfo = uri.connectionInfo();
QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" ); QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );

View File

@ -137,11 +137,12 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
if ( QMessageBox::Ok != QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel ) ) if ( QMessageBox::Ok != QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel ) )
return; return;
settings.remove( key + "/service" );
settings.remove( key + "/host" ); settings.remove( key + "/host" );
settings.remove( key + "/port" );
settings.remove( key + "/database" ); settings.remove( key + "/database" );
settings.remove( key + "/username" ); settings.remove( key + "/username" );
settings.remove( key + "/password" ); settings.remove( key + "/password" );
settings.remove( key + "/port" );
settings.remove( key + "/sslmode" ); settings.remove( key + "/sslmode" );
settings.remove( key + "/publicOnly" ); settings.remove( key + "/publicOnly" );
settings.remove( key + "/geometryColumnsOnly" ); settings.remove( key + "/geometryColumnsOnly" );
@ -415,17 +416,23 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
QString key = "/PostgreSQL/connections/" + cmbConnections->currentText(); QString key = "/PostgreSQL/connections/" + cmbConnections->currentText();
QString database = settings.value( key + "/database" ).toString(); QString service = settings.value( key + "/service" ).toString();
QString username = settings.value( key + "/username" ).toString(); QString host = settings.value( key + "/host" ).toString();
QString password = settings.value( key + "/password" ).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; QgsDataSourceURI uri;
uri.setConnection( settings.value( key + "/host" ).toString(), if ( !service.isEmpty() )
settings.value( key + "/port" ).toString(), {
database, uri.setConnection( service, database, username, password, sslmode );
username, }
password, else
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() ); {
uri.setConnection( host, port, database, username, password, sslmode );
}
bool searchPublicOnly = settings.value( key + "/publicOnly" ).toBool(); bool searchPublicOnly = settings.value( key + "/publicOnly" ).toBool();
bool searchGeometryColumnsOnly = settings.value( key + "/geometryColumnsOnly" ).toBool(); bool searchGeometryColumnsOnly = settings.value( key + "/geometryColumnsOnly" ).toBool();
@ -511,7 +518,9 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
"Check your username and password and try again.\n\n" "Check your username and password and try again.\n\n"
"The database said:\n%3" ) "The database said:\n%3" )
.arg( settings.value( key + "/database" ).toString() ) .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 ) ) ) ); .arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
} }

View File

@ -118,7 +118,7 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer ), mKeyC
} }
else if ( pname == "service" ) else if ( pname == "service" )
{ {
QgsDebugMsg( "service keyword ignored" ); mService = pval;
} }
else if ( pname == "user" ) else if ( pname == "user" )
{ {
@ -234,6 +234,11 @@ void QgsDataSourceURI::setUsername( QString username )
mUsername = username; mUsername = username;
} }
QString QgsDataSourceURI::service() const
{
return mService;
}
QString QgsDataSourceURI::host() const QString QgsDataSourceURI::host() const
{ {
return mHost; return mHost;
@ -401,37 +406,46 @@ QString QgsDataSourceURI::getValue( const QString &uri, int &i )
QString QgsDataSourceURI::connectionInfo() const QString QgsDataSourceURI::connectionInfo() const
{ {
QString connectionInfo = "dbname='" + escape( mDatabase ) + "'"; QStringList connectionItems;
if ( mHost != "" ) if ( mDatabase != "" )
{ {
connectionInfo += " host=" + mHost; connectionItems << "dbname='" + escape( mDatabase ) + "'";
}
if ( mService != "" )
{
connectionItems << "service='" + escape( mService ) + "'";
}
else if ( mHost != "" )
{
connectionItems << "host=" + mHost;
if ( mPort != "" ) if ( mPort != "" )
connectionInfo += " port=" + mPort; connectionItems << "port=" + mPort;
} }
if ( mUsername != "" ) if ( mUsername != "" )
{ {
connectionInfo += " user='" + escape( mUsername ) + "'"; connectionItems << "user='" + escape( mUsername ) + "'";
if ( mPassword != "" ) if ( mPassword != "" )
{ {
connectionInfo += " password='" + escape( mPassword ) + "'"; connectionItems << "password='" + escape( mPassword ) + "'";
} }
} }
if ( mSSLmode == SSLdisable ) if ( mSSLmode == SSLdisable )
connectionInfo += " sslmode=disable"; connectionItems << "sslmode=disable";
else if ( mSSLmode == SSLallow ) else if ( mSSLmode == SSLallow )
connectionInfo += " sslmode=allow"; connectionItems << "sslmode=allow";
else if ( mSSLmode == SSLrequire ) else if ( mSSLmode == SSLrequire )
connectionInfo += " sslmode=require"; connectionItems << "sslmode=require";
#if 0 #if 0
else if ( mSSLmode == SSLprefer ) else if ( mSSLmode == SSLprefer )
connectionInfo += " sslmode=prefer"; connectionItems << "sslmode=prefer";
#endif #endif
return connectionInfo; return connectionItems.join( " " );
} }
QString QgsDataSourceURI::uri() const QString QgsDataSourceURI::uri() const
@ -482,6 +496,19 @@ void QgsDataSourceURI::setConnection( const QString &host,
mSSLmode = sslmode; 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, void QgsDataSourceURI::setDataSource( const QString &schema,
const QString &table, const QString &table,
const QString &geometryColumn, const QString &geometryColumn,

View File

@ -57,6 +57,14 @@ class CORE_EXPORT QgsDataSourceURI
const QString& aPassword, const QString& aPassword,
SSLmode sslmode = SSLprefer ); 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 //! Set database
// \note added in 1.4 // \note added in 1.4
void setDatabase( const QString &database ); void setDatabase( const QString &database );
@ -100,6 +108,9 @@ class CORE_EXPORT QgsDataSourceURI
QString password() const; QString password() const;
enum SSLmode sslMode() const; enum SSLmode sslMode() const;
// added in 1.7
QString service() const;
// added in version 1.2 // added in version 1.2
QString keyColumn() const; QString keyColumn() const;
void setKeyColumn( QString column ); void setKeyColumn( QString column );
@ -113,10 +124,12 @@ class CORE_EXPORT QgsDataSourceURI
//! host name //! host name
QString mHost; QString mHost;
//! database name
QString mDatabase;
//! port the database server listens on //! port the database server listens on
QString mPort; QString mPort;
//! service name
QString mService;
//! database name
QString mDatabase;
//! schema //! schema
QString mSchema; QString mSchema;
//! spatial table //! spatial table

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>352</width> <width>352</width>
<height>413</height> <height>428</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -70,6 +70,16 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Service</string>
</property>
<property name="buddy">
<cstring>txtService</cstring>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="TextLabel1"> <widget class="QLabel" name="TextLabel1">
<property name="text"> <property name="text">
@ -80,16 +90,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="TextLabel2">
<property name="text">
<string>Database</string>
</property>
<property name="buddy">
<cstring>txtDatabase</cstring>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="TextLabel2_2"> <widget class="QLabel" name="TextLabel2_2">
<property name="text"> <property name="text">
@ -100,11 +100,24 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="TextLabel2">
<property name="text">
<string>Database</string>
</property>
<property name="buddy">
<cstring>txtDatabase</cstring>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="TextLabel3_3"> <widget class="QLabel" name="TextLabel3_3">
<property name="text"> <property name="text">
<string>SSL mode</string> <string>SSL mode</string>
</property> </property>
<property name="buddy">
<cstring>cbxSSLmode</cstring>
</property>
</widget> </widget>
</item> </item>
<item> <item>
@ -145,10 +158,10 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="txtHost"/> <widget class="QLineEdit" name="txtService"/>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="txtDatabase"/> <widget class="QLineEdit" name="txtHost"/>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="txtPort"> <widget class="QLineEdit" name="txtPort">
@ -157,6 +170,9 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLineEdit" name="txtDatabase"/>
</item>
<item> <item>
<widget class="QComboBox" name="cbxSSLmode"/> <widget class="QComboBox" name="cbxSSLmode"/>
</item> </item>
@ -255,7 +271,7 @@
<string>Allow geometryless tables</string> <string>Allow geometryless tables</string>
</property> </property>
<property name="checked"> <property name="checked">
<bool>true</bool> <bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -274,18 +290,20 @@
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<tabstops> <tabstops>
<tabstop>txtName</tabstop> <tabstop>txtName</tabstop>
<tabstop>txtService</tabstop>
<tabstop>txtHost</tabstop> <tabstop>txtHost</tabstop>
<tabstop>txtDatabase</tabstop>
<tabstop>txtPort</tabstop> <tabstop>txtPort</tabstop>
<tabstop>txtDatabase</tabstop>
<tabstop>cbxSSLmode</tabstop> <tabstop>cbxSSLmode</tabstop>
<tabstop>txtUsername</tabstop> <tabstop>txtUsername</tabstop>
<tabstop>txtPassword</tabstop> <tabstop>txtPassword</tabstop>
<tabstop>chkStoreUsername</tabstop> <tabstop>chkStoreUsername</tabstop>
<tabstop>chkStorePassword</tabstop> <tabstop>chkStorePassword</tabstop>
<tabstop>btnConnect</tabstop>
<tabstop>cb_geometryColumnsOnly</tabstop> <tabstop>cb_geometryColumnsOnly</tabstop>
<tabstop>cb_publicSchemaOnly</tabstop> <tabstop>cb_publicSchemaOnly</tabstop>
<tabstop>cb_allowGeometrylessTables</tabstop>
<tabstop>cb_useEstimatedMetadata</tabstop> <tabstop>cb_useEstimatedMetadata</tabstop>
<tabstop>btnConnect</tabstop>
<tabstop>buttonBox</tabstop> <tabstop>buttonBox</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>