Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10034 from rldhont/server-feature-id-primary-keys…
…-revival [Server] Use primary keys to build feature id
- Loading branch information
Showing
14 changed files
with
529 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/*************************************************************************** | ||
qgsserverfeatureid.cpp | ||
----------------------- | ||
begin : May 17, 2019 | ||
copyright : (C) 2019 by René-Luc DHONT | ||
email : rldhont at 3liz 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 "qgsserverfeatureid.h" | ||
#include "qgsfeature.h" | ||
#include "qgsfeaturerequest.h" | ||
#include "qgsvectordataprovider.h" | ||
#include "qgsexpression.h" | ||
|
||
QString QgsServerFeatureId::getServerFid( const QgsFeature &feature, const QgsAttributeList &pkAttributes ) | ||
{ | ||
if ( pkAttributes.isEmpty() ) | ||
{ | ||
return QString::number( feature.id() ); | ||
} | ||
|
||
QStringList pkValues; | ||
QgsAttributeList::const_iterator it = pkAttributes.constBegin(); | ||
if ( it != pkAttributes.constEnd() ) | ||
{ | ||
pkValues.append( feature.attribute( *it ).toString() ); | ||
} | ||
return pkValues.join( pkSeparator() ); | ||
} | ||
|
||
QgsFeatureRequest QgsServerFeatureId::updateFeatureRequestFromServerFids( QgsFeatureRequest &featureRequest, const QStringList &serverFids, const QgsVectorDataProvider *provider ) | ||
{ | ||
const QgsAttributeList &pkAttributes = provider->pkAttributeIndexes(); | ||
|
||
if ( pkAttributes.isEmpty() ) | ||
{ | ||
QgsFeatureIds fids; | ||
for ( const QString &serverFid : serverFids ) | ||
{ | ||
fids.insert( serverFid.toLongLong() ); | ||
} | ||
featureRequest.setFilterFids( fids ); | ||
return featureRequest; | ||
} | ||
|
||
QStringList expList; | ||
for ( const QString &serverFid : serverFids ) | ||
{ | ||
expList.append( QgsServerFeatureId::getExpressionFromServerFid( serverFid, provider ) ); | ||
} | ||
|
||
if ( expList.count() == 1 ) | ||
{ | ||
featureRequest.setFilterExpression( expList.at( 0 ) ); | ||
} | ||
else | ||
{ | ||
QString fullExpression; | ||
for ( const QString &exp : qgis::as_const( expList ) ) | ||
{ | ||
if ( !fullExpression.isEmpty() ) | ||
{ | ||
fullExpression.append( QStringLiteral( " OR " ) ); | ||
} | ||
fullExpression.append( QStringLiteral( "( " ) ); | ||
fullExpression.append( exp ); | ||
fullExpression.append( QStringLiteral( " )" ) ); | ||
} | ||
featureRequest.setFilterExpression( fullExpression ); | ||
} | ||
|
||
return featureRequest; | ||
} | ||
|
||
QString QgsServerFeatureId::getExpressionFromServerFid( const QString &serverFid, const QgsVectorDataProvider *provider ) | ||
{ | ||
const QgsAttributeList &pkAttributes = provider->pkAttributeIndexes(); | ||
|
||
if ( pkAttributes.isEmpty() ) | ||
{ | ||
return QString(); | ||
} | ||
|
||
const QgsFields &fields = provider->fields(); | ||
|
||
QString expressionString; | ||
QStringList pkValues = serverFid.split( pkSeparator() ); | ||
int pkExprSize = std::min( pkAttributes.size(), pkValues.size() ); | ||
for ( int i = 0; i < pkExprSize; ++i ) | ||
{ | ||
if ( i > 0 ) | ||
{ | ||
expressionString.append( QStringLiteral( " AND " ) ); | ||
} | ||
|
||
QString fieldName = fields[ pkAttributes.at( i ) ].name(); | ||
expressionString.append( QgsExpression::createFieldEqualityExpression( fieldName, QVariant( pkValues.at( i ) ) ) ); | ||
} | ||
|
||
return expressionString; | ||
} | ||
|
||
QString QgsServerFeatureId::pkSeparator() | ||
{ | ||
return QStringLiteral( "@@" ); | ||
} |
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,86 @@ | ||
/*************************************************************************** | ||
qgsserverfeatureid.h | ||
----------------------- | ||
begin : May 17, 2019 | ||
copyright : (C) 2019 by René-Luc DHONT | ||
email : rldhont at 3liz 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 QGSSERVERFEATUREID_H | ||
#define QGSSERVERFEATUREID_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include <QString> | ||
#include <QHash> | ||
|
||
#include "qgis_server.h" | ||
#include "qgsfield.h" | ||
|
||
class QgsVectorDataProvider; | ||
class QgsFeature; | ||
class QgsFeatureRequest; | ||
|
||
#ifdef SIP_RUN | ||
% ModuleHeaderCode | ||
#include "qgsserverfeatureid.h" | ||
% End | ||
#endif | ||
|
||
|
||
/** | ||
* \ingroup server | ||
* The QgsServerFeatureId namespace provides a way to use primary keys for | ||
* feature id. | ||
* \since QGIS 3.4.9 | ||
*/ | ||
namespace QgsServerFeatureId | ||
{ | ||
|
||
/** | ||
* Returns the feature id based on primary keys. | ||
* \param feature the feature | ||
* \param pkAttributes the primary keys list | ||
* \returns the feature id based on primary keys | ||
* \since QGIS 3.4.9 | ||
*/ | ||
SERVER_EXPORT QString getServerFid( const QgsFeature &feature, const QgsAttributeList &pkAttributes ); | ||
|
||
/** | ||
* Returns the feature request based on feature ids build with primary keys. | ||
* \param featureRequest the feature request to update | ||
* \param serverFids the feature ids build with QgsServerFeatureId::getServerFid | ||
* \param provider the vector layer provider to provide fields and primary keys list | ||
* \returns the feature request updated | ||
* \since QGIS 3.4.9 | ||
*/ | ||
SERVER_EXPORT QgsFeatureRequest updateFeatureRequestFromServerFids( QgsFeatureRequest &featureRequest, const QStringList &serverFids, const QgsVectorDataProvider *provider ); | ||
|
||
/** | ||
* Returns the expression feature id based on primary keys. | ||
* \param serverFid the feature id build with primary keys | ||
* \param provider the vector layer provider to provide fields and primary keys list | ||
* \returns the feature id based on primary keys | ||
* \since QGIS 3.4.9 | ||
*/ | ||
SERVER_EXPORT QString getExpressionFromServerFid( const QString &serverFid, const QgsVectorDataProvider *provider ); | ||
|
||
/** | ||
* Returns the primary keys separator | ||
* \returns @@ the primary keys separator | ||
* \since QGIS 3.4.9 | ||
*/ | ||
SERVER_EXPORT QString pkSeparator(); | ||
|
||
}; | ||
|
||
#endif |
Oops, something went wrong.