Skip to content
Permalink
Browse files

[FEATURE][API] postgres provider refactoring:

- use m prefix for member variables and s prefix for class variables
- refactor QgsPostgresProvider::Conn and QgsPostgresConnection to QgsPostgresConn
- put QgsColumnTypeThread into separate file
- use QgsPostgresConn in QgsPgSourceSelect
- cleaner abort of column type thread (e.g. remove unfinished columns from
  selection list)
- [API] move QgsDbTableModel as QgsPgTableModel to provider
- [FEATURE] support for arbitrary key (including non-numeric and multi column)
- [FEATURE][API] support for requesting a certain geometry type and/or srid in QgsDataSourceURI
 * no more explicit filter strings
 * support for empty tables without gemetry_columns entry or GEOMETRY type.
  • Loading branch information
jef-n committed Jan 9, 2012
1 parent c099afc commit ce4fc1daa5b49d053790b258d1361a2203282453
@@ -378,13 +378,10 @@ QPixmap QgsLegendLayer::getOriginalPixmap()
{
case QGis::Point:
return QgisApp::getThemePixmap( "/mIconPointLayer.png" );
break;
case QGis::Line:
return QgisApp::getThemePixmap( "/mIconLineLayer.png" );
break;
case QGis::Polygon:
return QgisApp::getThemePixmap( "/mIconPolygonLayer.png" );
break;
case QGis::NoGeometry:
return QgisApp::getThemePixmap( "/mIconTableLayer.png" );
default:
@@ -54,7 +54,6 @@ SET(QGIS_CORE_SRCS
qgsdatasourceuri.cpp
qgsdataitem.cpp
qgsdbfilterproxymodel.cpp
qgsdbtablemodel.cpp
qgsdiagram.cpp
qgsdiagramrendererv2.cpp
qgsdistancearea.cpp
@@ -235,7 +234,6 @@ SET(QGIS_CORE_MOC_HDRS
qgscontexthelp.h
qgscoordinatetransform.h
qgsdataitem.h
qgsdbtablemodel.h
qgsdataprovider.h
qgshttptransaction.h
qgsmaplayer.h
@@ -90,7 +90,7 @@ class CORE_EXPORT QGis
DegreesMinutesSeconds = 4,
DegreesDecimalMinutes = 5,
UnknownUnit = 3
} ;
};

//! User defined event types
enum UserEvent
@@ -27,6 +27,7 @@ QgsDataSourceURI::QgsDataSourceURI()
, mKeyColumn( "" )
, mUseEstimatedMetadata( false )
, mSelectAtIdDisabled( false )
, mGeometryType( QGis::WKBUnknown )
{
// do nothing
}
@@ -36,6 +37,7 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri )
, mKeyColumn( "" )
, mUseEstimatedMetadata( false )
, mSelectAtIdDisabled( false )
, mGeometryType( QGis::WKBUnknown )
{
int i = 0;
while ( i < uri.length() )
@@ -123,6 +125,42 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri )
{
mUseEstimatedMetadata = pval == "true";
}
else if ( pname == "srid" )
{
mSrid = pval;
}
else if ( pname == "type" )
{
QString geomTypeUpper = pval.toUpper();
if ( geomTypeUpper == "POINT" )
{
mGeometryType = QGis::WKBPoint;
}
else if ( geomTypeUpper == "MULTIPOINT" )
{
mGeometryType = QGis::WKBMultiPoint;
}
else if ( geomTypeUpper == "LINESTRING" )
{
mGeometryType = QGis::WKBLineString;
}
else if ( geomTypeUpper == "MULTILINESTRING" )
{
mGeometryType = QGis::WKBMultiLineString;
}
else if ( geomTypeUpper == "POLYGON" )
{
mGeometryType = QGis::WKBPolygon;
}
else if ( geomTypeUpper == "MULTIPOLYGON" )
{
mGeometryType = QGis::WKBMultiPolygon;
}
else
{
mGeometryType = QGis::WKBUnknown;
}
}
else if ( pname == "selectatid" )
{
mSelectAtIdDisabled = pval == "false";
@@ -483,6 +521,16 @@ QString QgsDataSourceURI::uri() const
theUri += QString( " estimatedmetadata=true" );
}

if ( !mSrid.isEmpty() )
{
theUri += QString( " srid=%1" ).arg( mSrid );
}

if ( mGeometryType != QGis::WKBUnknown && mGeometryType != QGis::WKBNoGeometry )
{
theUri += QString( " type=%1" ).arg( QGis::qgisFeatureTypes[mGeometryType] + 3 );
}

if ( mSelectAtIdDisabled )
{
theUri += QString( " selectatid=false" );
@@ -552,3 +600,23 @@ void QgsDataSourceURI::setDatabase( const QString &database )
{
mDatabase = database;
}

QGis::WkbType QgsDataSourceURI::geometryType() const
{
return mGeometryType;
}

void QgsDataSourceURI::setGeometryType( QGis::WkbType geometryType )
{
mGeometryType = geometryType;
}

QString QgsDataSourceURI::srid() const
{
return mSrid;
}

void QgsDataSourceURI::setSrid( QString srid )
{
mSrid = srid;
}
@@ -19,7 +19,7 @@
#ifndef QGSDATASOURCEURI_H
#define QGSDATASOURCEURI_H

#include <QString>
#include "qgis.h"

/** \ingroup core
* Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
@@ -117,6 +117,12 @@ class CORE_EXPORT QgsDataSourceURI
QString keyColumn() const;
void setKeyColumn( QString column );

// added in 1.9
QGis::WkbType geometryType() const;
void setGeometryType( QGis::WkbType type );
QString srid() const;
void setSrid( QString srid );

private:
void skipBlanks( const QString &uri, int &i );
QString getValue( const QString &uri, int &i );
@@ -148,10 +154,14 @@ class CORE_EXPORT QgsDataSourceURI
enum SSLmode mSSLmode;
//! key column
QString mKeyColumn;
//Use estimated metadata flag
//! Use estimated metadata flag
bool mUseEstimatedMetadata;
//Disable SelectAtId capability (eg. to trigger the attribute table memory model for expensive views)
//! Disable SelectAtId capability (eg. to trigger the attribute table memory model for expensive views)
bool mSelectAtIdDisabled;
//! geometry type (or QGis::WKBUnknown if not specified)
QGis::WkbType mGeometryType;
//! SRID or a null string if not specified
QString mSrid;
};

#endif //QGSDATASOURCEURI_H
@@ -4,17 +4,21 @@

SET(PG_SRCS
qgspostgresprovider.cpp
qgspostgresconnection.cpp
qgspostgresconn.cpp
qgspostgresdataitems.cpp
qgspgsourceselect.cpp
qgspgnewconnection.cpp
qgspgtablemodel.cpp
qgscolumntypethread.cpp
)
SET(PG_MOC_HDRS
qgspostgresprovider.h
qgspostgresconnection.h
qgspostgresconn.h
qgspostgresdataitems.h
qgspgsourceselect.h
qgspgnewconnection.h
qgspgtablemodel.h
qgscolumntypethread.h
)


@@ -0,0 +1,64 @@
/***************************************************************************
qgscolumntypethread.cpp - lookup postgres geometry type and srid in a thread
-------------------
begin : 3.1.2012
copyright : (C) 2012 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/

/***************************************************************************
* *
* 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 "qgscolumntypethread.h"

#include <QMetaType>

QgsGeomColumnTypeThread::QgsGeomColumnTypeThread( QgsPostgresConn *conn, bool useEstimatedMetaData )
: QThread()
, mConn( conn )
, mUseEstimatedMetadata( useEstimatedMetaData )
{
qRegisterMetaType<QgsPostgresLayerProperty>( "QgsPostgresLayerProperty" );
}

void QgsGeomColumnTypeThread::addGeometryColumn( QgsPostgresLayerProperty layerProperty )
{
layerProperties << layerProperty;
}

void QgsGeomColumnTypeThread::stop()
{
mStopped = true;
}

void QgsGeomColumnTypeThread::run()
{
if ( !mConn )
return;

mStopped = false;

foreach( QgsPostgresLayerProperty layerProperty, layerProperties )
{
if ( !mStopped )
{
mConn->retrieveLayerTypes( layerProperty, mUseEstimatedMetadata );
}
else
{
layerProperty.type = "";
}

// Now tell the layer list dialog box...
emit setLayerType( layerProperty );
}

mConn->disconnect();
mConn = 0;
}
@@ -0,0 +1,53 @@
/***************************************************************************
qgscolumntypethread.cpp - lookup postgres geometry type and srid in a thread
-------------------
begin : 3.1.2012
copyright : (C) 2012 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/

/***************************************************************************
* *
* 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 QGSCOLUMNTYPETHREAD_H
#define QGSCOLUMNTYPETHREAD_H

#include <QThread>
#include "qgspostgresconn.h"

// A class that determines the geometry type of a given database
// schema.table.column, with the option of doing so in a separate
// thread.

class QgsGeomColumnTypeThread : public QThread
{
Q_OBJECT
public:
QgsGeomColumnTypeThread( QgsPostgresConn *conn, bool useEstimatedMetaData );
void addGeometryColumn( QgsPostgresLayerProperty layerProperty );

// These functions get the layer types and pass that information out
// by emitting the setLayerType() signal.
virtual void run();

signals:
void setLayerType( QgsPostgresLayerProperty layerProperty );

public slots:
void stop();

private:
QgsGeomColumnTypeThread() {}

QgsPostgresConn *mConn;
bool mUseEstimatedMetadata;
bool mStopped;
QList<QgsPostgresLayerProperty> layerProperties;
};

#endif // QGSCOLUMNTYPETHREAD_H
@@ -0,0 +1,53 @@
/***************************************************************************
qgsdbfilterproxymodel.cpp - description
-------------------------
begin : Dec 2007
copyright : (C) 2007 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/

/***************************************************************************
* *
* 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 "qgsdbfilterproxymodel.h"

QgsDbFilterProxyModel::QgsDbFilterProxyModel( QObject* parent ): QSortFilterProxyModel( parent )
{

}

QgsDbFilterProxyModel::~QgsDbFilterProxyModel()
{

}

bool QgsDbFilterProxyModel::filterAcceptsRow( int row, const QModelIndex & source_parent ) const
{
//if parent is valid, we have a toplevel item that should be always shown
if ( !source_parent.isValid() )
{
return true;
}

//else we have a row that describes a table and that
//should be tested using the given wildcard/regexp
return QSortFilterProxyModel::filterAcceptsRow( row, source_parent );
}

void QgsDbFilterProxyModel::_setFilterWildcard( const QString& pattern )
{
QSortFilterProxyModel::setFilterWildcard( pattern );
emit layoutChanged();
}

void QgsDbFilterProxyModel::_setFilterRegExp( const QString& pattern )
{
QSortFilterProxyModel::setFilterRegExp( pattern );
emit layoutChanged();
}

0 comments on commit ce4fc1d

Please sign in to comment.
You can’t perform that action at this time.