Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
easy way to access to postgres connections
- Loading branch information
1 parent
62e62f6
commit d329121
Showing
3 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/*************************************************************************** | ||
qgspostgresconnection.cpp - PostgresSQL/PostGIS connection | ||
------------------- | ||
begin : 3 June 2011 | ||
copyright : (C) 2011 by Giuseppe Sucameli | ||
email : brush.tyler at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgspostgresconnection.h" | ||
|
||
#include <qgslogger.h> | ||
#include "qgspostgresprovider.h" | ||
#include "qgsproviderregistry.h" | ||
#include "qgsdatasourceuri.h" | ||
|
||
#include <QSettings> | ||
|
||
QStringList QgsPostgresConnection::connectionList() | ||
{ | ||
QSettings settings; | ||
settings.beginGroup( "/PostgreSQL/connections" ); | ||
return settings.childGroups(); | ||
} | ||
|
||
QString QgsPostgresConnection::selectedConnection() | ||
{ | ||
QSettings settings; | ||
return settings.value( "/PostgreSQL/connections/selected" ).toString(); | ||
} | ||
|
||
void QgsPostgresConnection::setSelectedConnection( QString name ) | ||
{ | ||
QSettings settings; | ||
return settings.setValue( "/PostgreSQL/connections/selected", name ); | ||
} | ||
|
||
|
||
QgsPostgresConnection::QgsPostgresConnection( QString theConnName ) : | ||
mConnName( theConnName ) | ||
{ | ||
QgsDebugMsg( "theConnName = " + theConnName ); | ||
|
||
QSettings settings; | ||
|
||
QString key = "/PostgreSQL/connections/" + mConnName; | ||
|
||
QString service = settings.value( key + "/service" ).toString(); | ||
QString host = settings.value( key + "/host" ).toString(); | ||
QString port = settings.value( key + "/port" ).toString(); | ||
if ( port.length() == 0 ) | ||
{ | ||
port = "5432"; | ||
} | ||
QString database = settings.value( key + "/database" ).toString(); | ||
|
||
//bool publicSchemaOnly = settings.value( key + "/publicOnly", false ).toBool(); | ||
//bool geometryColumnsOnly = settings.value( key + "/geometrycolumnsOnly", false ).toBool(); | ||
//bool allowGeometrylessTables = settings.value( key + "/allowGeometrylessTables", false ).toBool(); | ||
|
||
bool useEstimatedMetadata = settings.value( key + "/estimatedMetadata", false ).toBool(); | ||
int sslmode = settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt(); | ||
|
||
QString username; | ||
QString password; | ||
if ( settings.value( key + "/saveUsername" ).toString() == "true" ) | ||
{ | ||
username = settings.value( key + "/username" ).toString(); | ||
} | ||
|
||
if ( settings.value( key + "/savePassword" ).toString() == "true" ) | ||
{ | ||
password = settings.value( key + "/password" ).toString(); | ||
} | ||
|
||
// Old save setting | ||
if ( settings.contains( key + "/save" ) ) | ||
{ | ||
username = settings.value( key + "/username" ).toString(); | ||
|
||
if ( settings.value( key + "/save" ).toString() == "true" ) | ||
{ | ||
password = settings.value( key + "/password" ).toString(); | ||
} | ||
} | ||
|
||
QgsDataSourceURI uri; | ||
if ( !service.isEmpty() ) | ||
{ | ||
uri.setConnection( service, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode ); | ||
} | ||
else | ||
{ | ||
uri.setConnection( host, port, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode ); | ||
} | ||
uri.setUseEstimatedMetadata( useEstimatedMetadata ); | ||
mConnectionInfo = uri.connectionInfo(); | ||
|
||
QgsDebugMsg( QString( "Connection info: '%1'." ).arg( mConnectionInfo ) ); | ||
} | ||
|
||
QgsPostgresConnection::~QgsPostgresConnection() | ||
{ | ||
|
||
} | ||
|
||
QString QgsPostgresConnection::connectionInfo( ) | ||
{ | ||
return mConnectionInfo; | ||
} | ||
|
||
QgsPostgresProvider * QgsPostgresConnection::provider( ) | ||
{ | ||
// TODO: Create and bind to data provider | ||
|
||
// load the server data provider plugin | ||
QgsProviderRegistry * pReg = QgsProviderRegistry::instance(); | ||
|
||
QgsPostgresProvider *postgresProvider = | ||
( QgsPostgresProvider* ) pReg->provider( "postgres", mConnectionInfo ); | ||
|
||
return postgresProvider; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*************************************************************************** | ||
qgspostgresconnection.h - PostgresSQL/PostGIS connection | ||
------------------- | ||
begin : 3 June 2011 | ||
copyright : (C) 2011 by Giuseppe Sucameli | ||
email : brush.tyler at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSPOSTGRESCONNECTION_H | ||
#define QGSPOSTGRESCONNECTION_H | ||
|
||
#include <QStringList> | ||
|
||
class QgsPostgresProvider; | ||
|
||
/*! | ||
* \brief Connections management | ||
*/ | ||
class QgsPostgresConnection : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
//! Constructor | ||
QgsPostgresConnection( QString theConnName ); | ||
//! Destructor | ||
~QgsPostgresConnection(); | ||
|
||
static QStringList connectionList(); | ||
|
||
static QString selectedConnection(); | ||
static void setSelectedConnection( QString name ); | ||
|
||
public: | ||
QgsPostgresProvider *provider(); | ||
QString connectionInfo(); | ||
QString mConnName; | ||
QString mConnectionInfo; | ||
}; | ||
|
||
|
||
#endif // QGSPOSTGRESCONNECTION_H |