Skip to content

Commit

Permalink
[FEATURE][API] postgres provider refactoring:
Browse files Browse the repository at this point in the history
- 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 ce4fc1d
Show file tree
Hide file tree
Showing 24 changed files with 3,651 additions and 3,796 deletions.
3 changes: 0 additions & 3 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions src/core/CMakeLists.txt
Expand Up @@ -54,7 +54,6 @@ SET(QGIS_CORE_SRCS
qgsdatasourceuri.cpp
qgsdataitem.cpp
qgsdbfilterproxymodel.cpp
qgsdbtablemodel.cpp
qgsdiagram.cpp
qgsdiagramrendererv2.cpp
qgsdistancearea.cpp
Expand Down Expand Up @@ -235,7 +234,6 @@ SET(QGIS_CORE_MOC_HDRS
qgscontexthelp.h
qgscoordinatetransform.h
qgsdataitem.h
qgsdbtablemodel.h
qgsdataprovider.h
qgshttptransaction.h
qgsmaplayer.h
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgis.h
Expand Up @@ -90,7 +90,7 @@ class CORE_EXPORT QGis
DegreesMinutesSeconds = 4,
DegreesDecimalMinutes = 5,
UnknownUnit = 3
} ;
};

//! User defined event types
enum UserEvent
Expand Down
68 changes: 68 additions & 0 deletions src/core/qgsdatasourceuri.cpp
Expand Up @@ -27,6 +27,7 @@ QgsDataSourceURI::QgsDataSourceURI()
, mKeyColumn( "" )
, mUseEstimatedMetadata( false )
, mSelectAtIdDisabled( false )
, mGeometryType( QGis::WKBUnknown )
{
// do nothing
}
Expand All @@ -36,6 +37,7 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri )
, mKeyColumn( "" )
, mUseEstimatedMetadata( false )
, mSelectAtIdDisabled( false )
, mGeometryType( QGis::WKBUnknown )
{
int i = 0;
while ( i < uri.length() )
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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;
}
16 changes: 13 additions & 3 deletions src/core/qgsdatasourceuri.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/providers/postgres/CMakeLists.txt
Expand Up @@ -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
)


Expand Down
64 changes: 64 additions & 0 deletions src/providers/postgres/qgscolumntypethread.cpp
@@ -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;
}
53 changes: 53 additions & 0 deletions src/providers/postgres/qgscolumntypethread.h
@@ -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
53 changes: 53 additions & 0 deletions src/providers/postgres/qgsdbfilterproxymodel.cpp
@@ -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.