Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
public:

//! Constructor
QgsPgSourceSelect( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
QgsPgSourceSelect( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags, bool managerMode = false, bool embeddedMode = false );
//! Destructor
~QgsPgSourceSelect();
//! Populate the connection list combo box
Expand All @@ -116,6 +116,11 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
//! Connection info (database, host, user, password)
QString connectionInfo();

signals:
void addDatabaseLayers( QStringList const & layerPathList,
QString const & providerKey );
void connectionsChanged();

public slots:
//! Determines the tables the user selected and closes the dialog
void addTables();
Expand Down Expand Up @@ -155,11 +160,11 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
typedef QPair<QString, QString> geomPair;
typedef QList<geomPair> geomCol;

/**Inserts information about the spatial tables into mTableModel*/
bool getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables );
//! Connections manager mode
bool mManagerMode;

/** get primary key candidates (all int4 columns) */
QStringList pkCandidates( PGconn *pg, QString schemaName, QString viewName );
//! Embedded mode, without 'Close'
bool mEmbeddedMode;

// queue another query for the thread
void addSearchGeometryColumn( const QString &schema, const QString &table, const QString &column );
Expand All @@ -175,12 +180,10 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
// Our thread for doing long running queries
QgsGeomColumnTypeThread* mColumnTypeThread;
QString m_connInfo;
QString m_privConnInfo;
QStringList m_selectedTables;
bool mUseEstimatedMetadata;
// Storage for the range of layer type icons
QMap<QString, QPair<QString, QIcon> > mLayerIcons;
PGconn *pd;

//! Model that acts as datasource for mTableTreeWidget
QgsDbTableModel mTableModel;
Expand Down
132 changes: 132 additions & 0 deletions src/providers/postgres/qgspostgresconnection.cpp
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;
}

51 changes: 51 additions & 0 deletions src/providers/postgres/qgspostgresconnection.h
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
507 changes: 505 additions & 2 deletions src/providers/postgres/qgspostgresprovider.cpp

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,36 @@ extern "C"
#include <libpq-fe.h>
}
#include "qgsvectordataprovider.h"
#include "qgsdataitem.h"
#include "qgsrectangle.h"

#include <list>
#include <queue>
#include <fstream>
#include <set>

#include <QVector>

class QgsFeature;
class QgsField;
class QgsGeometry;


#include "qgsdatasourceuri.h"

/** Layer Property structure */
// TODO: Fill to Postgres/PostGIS specifications
struct QgsPostgresLayerProperty
{
// Postgres/PostGIS layer properties
QString type;
QString schemaName;
QString tableName;
QString geometryColName;
QStringList pkCols;
QString sql;
};

/**
\class QgsPostgresProvider
\brief Data provider for PostgreSQL/PostGIS layers.
Expand Down Expand Up @@ -239,6 +257,12 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
bool changeGeometryValues( QgsGeometryMap & geometry_map );

//! Get the list of supported layers
bool supportedLayers( QVector<QgsPostgresLayerProperty> &layers,
bool searchGeometryColumnsOnly = true,
bool searchPublicOnly = true,
bool allowGeometrylessTables = false );

//! Get the postgres connection
PGconn * pgConnection();

Expand Down Expand Up @@ -329,6 +353,12 @@ class QgsPostgresProvider : public QgsVectorDataProvider

QString whereClause( QgsFeatureId featureId ) const;

/** Gets information about the spatial tables */
bool getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables );

/** get primary key candidates (all int4 columns) */
QStringList pkCandidates( QString schemaName, QString viewName );

bool hasSufficientPermsAndCapabilities();

qint64 getBinaryInt( PGresult *queryResult, int row, int col );
Expand Down Expand Up @@ -373,6 +403,9 @@ class QgsPostgresProvider : public QgsVectorDataProvider
//! Data source URI struct for this layer
QgsDataSourceURI mUri;

//! List of the supported layers
QVector<QgsPostgresLayerProperty> layersSupported;

/**
* Flag indicating if the layer data source is a valid PostgreSQL layer
*/
Expand Down Expand Up @@ -714,4 +747,57 @@ class QgsPostgresProvider : public QgsVectorDataProvider
QString mPrimaryKeyDefault;
};

class QgsPGConnectionItem : public QgsDataCollectionItem
{
public:
QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path );
~QgsPGConnectionItem();

QVector<QgsDataItem*> createChildren();
virtual bool equal( const QgsDataItem *other );

QString mConnInfo;
QVector<QgsPostgresLayerProperty> mLayerProperties;
};

// WMS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem
// We have to use QgsDataCollectionItem and support layer methods if necessary
class QgsPGLayerItem : public QgsLayerItem
{
Q_OBJECT
public:
QgsPGLayerItem( QgsDataItem* parent, QString name, QString path,
QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperties );
~QgsPGLayerItem();

QString createUri();

QString mConnInfo;
QgsPostgresLayerProperty mLayerProperty;
};

class QgsPGSchemaItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path,
QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties );
~QgsPGSchemaItem();
};

class QgsPGRootItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsPGRootItem( QgsDataItem* parent, QString name, QString path );
~QgsPGRootItem();

QVector<QgsDataItem*> createChildren();

virtual QWidget * paramWidget();

public slots:
void connectionsChanged();
};

#endif