Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
easy way to access to postgres connections
  • Loading branch information
brushtyler committed Jul 12, 2011
1 parent 62e62f6 commit d329121
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/providers/postgres/CMakeLists.txt
Expand Up @@ -2,16 +2,22 @@
########################################################
# Files

SET(PG_SRCS qgspostgresprovider.cpp)
SET(PG_MOC_HDRS qgspostgresprovider.h)
SET(PG_SRCS
qgspostgresprovider.cpp
qgspostgresconnection.cpp
)
SET(PG_MOC_HDRS
qgspostgresprovider.h
qgspostgresconnection.h
)


########################################################
# Build

QT4_WRAP_CPP(PG_MOC_SRCS ${PG_MOC_HDRS})

INCLUDE_DIRECTORIES (
INCLUDE_DIRECTORIES(
../../core
${POSTGRES_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
Expand Down
132 changes: 132 additions & 0 deletions src/providers/postgres/qgspostgresconnection.cpp
@@ -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;
}

51 changes: 51 additions & 0 deletions src/providers/postgres/qgspostgresconnection.h
@@ -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

0 comments on commit d329121

Please sign in to comment.