QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsServerFeatureId::getExpressionFromServerFid
SERVER_EXPORT QString getExpressionFromServerFid(const QString &serverFid, const QgsVectorDataProvider *provider)
Returns the expression feature id based on primary keys.
Definition: qgsserverfeatureid.cpp:84
qgsserverfeatureid.h
qgsfeaturerequest.h
QgsVectorDataProvider::pkAttributeIndexes
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
Definition: qgsvectordataprovider.cpp:333
QgsServerFeatureId::pkSeparator
SERVER_EXPORT QString pkSeparator()
Returns the primary keys separator.
Definition: qgsserverfeatureid.cpp:112
QgsServerFeatureId::getServerFid
SERVER_EXPORT QString getServerFid(const QgsFeature &feature, const QgsAttributeList &pkAttributes)
Returns the feature id based on primary keys.
Definition: qgsserverfeatureid.cpp:24
qgsexpression.h
QgsVectorDataProvider::fields
QgsFields fields() const override=0
Returns the fields associated with this data provider.
QgsFields
Definition: qgsfields.h:44
qgsfeature.h
QgsFields::append
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
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:129
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
qgsvectordataprovider.h
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
QgsFeatureRequest::setFilterFids
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
Definition: qgsfeaturerequest.cpp:110
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsExpression::createFieldEqualityExpression
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value)
Create an expression allowing to evaluate if a field is equal to a value.
Definition: qgsexpression.cpp:1062
QgsServerFeatureId::updateFeatureRequestFromServerFids
SERVER_EXPORT QgsFeatureRequest updateFeatureRequestFromServerFids(QgsFeatureRequest &featureRequest, const QStringList &serverFids, const QgsVectorDataProvider *provider)
Returns the feature request based on feature ids build with primary keys.
Definition: qgsserverfeatureid.cpp:40
QgsVectorDataProvider
Definition: qgsvectordataprovider.h:58
QgsFeature
Definition: qgsfeature.h:55