QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsserverfeatureid.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsserverfeatureid.cpp
3  -----------------------
4  begin : May 17, 2019
5  copyright : (C) 2019 by René-Luc DHONT
6  email : rldhont at 3liz dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsserverfeatureid.h"
19 #include "qgsfeature.h"
20 #include "qgsfeaturerequest.h"
21 #include "qgsvectordataprovider.h"
22 #include "qgsexpression.h"
23 
24 QString QgsServerFeatureId::getServerFid( const QgsFeature &feature, const QgsAttributeList &pkAttributes )
25 {
26  if ( pkAttributes.isEmpty() )
27  {
28  return QString::number( feature.id() );
29  }
30 
31  QStringList pkValues;
32  QgsAttributeList::const_iterator it = pkAttributes.constBegin();
33  if ( it != pkAttributes.constEnd() )
34  {
35  pkValues.append( feature.attribute( *it ).toString() );
36  }
37  return pkValues.join( pkSeparator() );
38 }
39 
41 {
42  const QgsAttributeList &pkAttributes = provider->pkAttributeIndexes();
43 
44  if ( pkAttributes.isEmpty() )
45  {
46  QgsFeatureIds fids;
47  for ( const QString &serverFid : serverFids )
48  {
49  fids.insert( serverFid.toLongLong() );
50  }
51  featureRequest.setFilterFids( fids );
52  return featureRequest;
53  }
54 
55  QStringList expList;
56  for ( const QString &serverFid : serverFids )
57  {
58  expList.append( QgsServerFeatureId::getExpressionFromServerFid( serverFid, provider ) );
59  }
60 
61  if ( expList.count() == 1 )
62  {
63  featureRequest.setFilterExpression( expList.at( 0 ) );
64  }
65  else
66  {
67  QString fullExpression;
68  for ( const QString &exp : qgis::as_const( expList ) )
69  {
70  if ( !fullExpression.isEmpty() )
71  {
72  fullExpression.append( QStringLiteral( " OR " ) );
73  }
74  fullExpression.append( QStringLiteral( "( " ) );
75  fullExpression.append( exp );
76  fullExpression.append( QStringLiteral( " )" ) );
77  }
78  featureRequest.setFilterExpression( fullExpression );
79  }
80 
81  return featureRequest;
82 }
83 
84 QString QgsServerFeatureId::getExpressionFromServerFid( const QString &serverFid, const QgsVectorDataProvider *provider )
85 {
86  const QgsAttributeList &pkAttributes = provider->pkAttributeIndexes();
87 
88  if ( pkAttributes.isEmpty() )
89  {
90  return QString();
91  }
92 
93  const QgsFields &fields = provider->fields();
94 
95  QString expressionString;
96  QStringList pkValues = serverFid.split( pkSeparator() );
97  int pkExprSize = std::min( pkAttributes.size(), pkValues.size() );
98  for ( int i = 0; i < pkExprSize; ++i )
99  {
100  if ( i > 0 )
101  {
102  expressionString.append( QStringLiteral( " AND " ) );
103  }
104 
105  QString fieldName = fields[ pkAttributes.at( i ) ].name();
106  expressionString.append( QgsExpression::createFieldEqualityExpression( fieldName, QVariant( pkValues.at( i ) ) ) );
107  }
108 
109  return expressionString;
110 }
111 
113 {
114  return QStringLiteral( "@@" );
115 }
QgsFeatureId id
Definition: qgsfeature.h:64
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
SERVER_EXPORT QString getServerFid(const QgsFeature &feature, const QgsAttributeList &pkAttributes)
Returns the feature id based on primary keys.
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
Container of fields for a vector layer.
Definition: qgsfields.h:42
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value)
Create an expression allowing to evaluate if a field is equal to a value.
SERVER_EXPORT QgsFeatureRequest updateFeatureRequestFromServerFids(QgsFeatureRequest &featureRequest, const QStringList &serverFids, const QgsVectorDataProvider *provider)
Returns the feature request based on feature ids build with primary keys.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
SERVER_EXPORT QString getExpressionFromServerFid(const QString &serverFid, const QgsVectorDataProvider *provider)
Returns the expression feature id based on primary keys.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
This is the base class for vector data providers.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
SERVER_EXPORT QString pkSeparator()
Returns the primary keys separator.