-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to filter joined fields with a virtual layer
- Loading branch information
Hugo Mercier
committed
Jan 7, 2016
1 parent
4bb09d0
commit bbf2137
Showing
12 changed files
with
221 additions
and
30 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
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,12 @@ | ||
/** | ||
* Utils class for QgsVirtualLayerDefinition | ||
*/ | ||
class QgsVirtualLayerDefinitionUtils | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsvirtuallayerdefinitionutils.h> | ||
%End | ||
public: | ||
//! Get a virtual layer definition from a vector layer where vector joins are replaced by SQL LEFT JOINs | ||
static QgsVirtualLayerDefinition fromJoinedLayer( QgsVectorLayer* joinedLayer ); | ||
}; |
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
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
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
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,80 @@ | ||
/*************************************************************************** | ||
qgsvirtuallayerdefinitionutils.cpp | ||
begin : Jan 2016 | ||
copyright : (C) 2016 Hugo Mercier, Oslandia | ||
email : hugo dot mercier at oslandia 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 "qgsvirtuallayerdefinitionutils.h" | ||
#include "qgsvectorlayer.h" | ||
#include "qgsvectordataprovider.h" | ||
|
||
QgsVirtualLayerDefinition QgsVirtualLayerDefinitionUtils::fromJoinedLayer( QgsVectorLayer* layer ) | ||
{ | ||
QgsVirtualLayerDefinition def; | ||
|
||
QStringList leftJoins; | ||
QStringList columns; | ||
|
||
columns << "t.*"; // columns from the main layer | ||
|
||
// look for the uid | ||
const QgsFields& fields = layer->dataProvider()->fields(); | ||
{ | ||
QgsAttributeList pk = layer->dataProvider()->pkAttributeIndexes(); | ||
if ( pk.size() == 1 ) | ||
{ | ||
def.setUid( fields.field( pk[0] ).name() ); | ||
} | ||
else | ||
{ | ||
// find an uid name | ||
QString uid = "uid"; | ||
while ( fields.fieldNameIndex( uid ) != -1 ) | ||
uid += "_"; // add "_" each time this name already exists | ||
|
||
// add a column | ||
columns << "t.rowid AS " + uid; | ||
def.setUid( uid ); | ||
} | ||
} | ||
|
||
int joinIdx = 0; | ||
foreach ( const QgsVectorJoinInfo& join, layer->vectorJoins() ) | ||
{ | ||
QString joinName = QString( "j%1" ).arg( ++joinIdx ); | ||
QString prefix = join.prefix.isEmpty() ? layer->name() + "_" : join.prefix; | ||
|
||
leftJoins << QString( "LEFT JOIN %1 AS %2 ON t.\"%3\"=%2.\"%5\"" ).arg( join.joinLayerId ).arg( joinName ).arg( join.joinFieldName ).arg( join.targetFieldName ); | ||
if ( join.joinFieldNamesSubset() ) | ||
{ | ||
foreach ( const QString& f, *join.joinFieldNamesSubset() ) | ||
{ | ||
columns << joinName + "." + f + " AS " + prefix + f; | ||
} | ||
} | ||
else | ||
{ | ||
const QgsFields& joinedFields = layer->dataProvider()->fields(); | ||
for ( int i = 0; i < joinedFields.count(); i++ ) | ||
{ | ||
const QgsField& f = joinedFields.field( i ); | ||
columns << joinName + "." + f.name() + " AS " + prefix + f.name(); | ||
} | ||
} | ||
} | ||
|
||
QString query = "SELECT " + columns.join( ", " ) + " FROM " + layer->id() + " AS t " + leftJoins.join( " " ); | ||
def.setQuery( query ); | ||
|
||
return def; | ||
} |
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,34 @@ | ||
/*************************************************************************** | ||
qgsvirtuallayerdefinitionutils.h | ||
begin : Jan, 2016 | ||
copyright : (C) 2016 Hugo Mercier, Oslandia | ||
email : hugo dot mercier at oslandia 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 QGSVIRTUALLAYERDEFINITION_UTILS_H | ||
#define QGSVIRTUALLAYERDEFINITION_UTILS_H | ||
|
||
#include "qgsvirtuallayerdefinition.h" | ||
|
||
class QgsVectorLayer; | ||
|
||
/** | ||
* Utils class for QgsVirtualLayerDefinition | ||
*/ | ||
class CORE_EXPORT QgsVirtualLayerDefinitionUtils | ||
{ | ||
public: | ||
//! Get a virtual layer definition from a vector layer where vector joins are replaced by SQL LEFT JOINs | ||
static QgsVirtualLayerDefinition fromJoinedLayer( QgsVectorLayer* joinedLayer ); | ||
}; | ||
|
||
#endif |
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