Coverage Report

Created: 2025-11-16 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/gcore/gdal_relationship.h
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  Declaration of GDALRelationship class
5
 * Author:   Nyall Dawson, <nyall dot dawson at gmail dot com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2022, Nyall Dawson <nyall dot dawson at gmail dot comg>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#ifndef GDALRELATIONSHIP_H_INCLUDED
14
#define GDALRELATIONSHIP_H_INCLUDED
15
16
#include "cpl_port.h"
17
#include "gdal.h"
18
19
#include <string>
20
#include <vector>
21
22
/************************************************************************/
23
/*                           Relationships                              */
24
/************************************************************************/
25
26
/**
27
 * Definition of a table relationship.
28
 *
29
 * GDALRelationship describes the relationship between two tables, including
30
 * properties such as the cardinality of the relationship and the participating
31
 * tables.
32
 *
33
 * Not all relationship properties are supported by all data formats.
34
 *
35
 * @since GDAL 3.6
36
 */
37
class CPL_DLL GDALRelationship
38
{
39
  protected:
40
    /*! @cond Doxygen_Suppress */
41
    std::string m_osName{};
42
    std::string m_osLeftTableName{};
43
    std::string m_osRightTableName{};
44
    GDALRelationshipCardinality m_eCardinality =
45
        GDALRelationshipCardinality::GRC_ONE_TO_MANY;
46
    std::string m_osMappingTableName{};
47
    std::vector<std::string> m_osListLeftTableFields{};
48
    std::vector<std::string> m_osListRightTableFields{};
49
    std::vector<std::string> m_osListLeftMappingTableFields{};
50
    std::vector<std::string> m_osListRightMappingTableFields{};
51
    GDALRelationshipType m_eType = GDALRelationshipType::GRT_ASSOCIATION;
52
    std::string m_osForwardPathLabel{};
53
    std::string m_osBackwardPathLabel{};
54
    std::string m_osRelatedTableType{};
55
56
    /*! @endcond */
57
58
  public:
59
    /**
60
     * Constructor for a relationship between two tables.
61
     * @param osName relationship name
62
     * @param osLeftTableName left table name
63
     * @param osRightTableName right table name
64
     * @param eCardinality cardinality of relationship
65
     */
66
    GDALRelationship(const std::string &osName,
67
                     const std::string &osLeftTableName,
68
                     const std::string &osRightTableName,
69
                     GDALRelationshipCardinality eCardinality =
70
                         GDALRelationshipCardinality::GRC_ONE_TO_MANY)
71
        : m_osName(osName), m_osLeftTableName(osLeftTableName),
72
          m_osRightTableName(osRightTableName), m_eCardinality(eCardinality)
73
0
    {
74
0
    }
75
76
    /** Get the name of the relationship */
77
    const std::string &GetName() const
78
0
    {
79
0
        return m_osName;
80
0
    }
81
82
    /** Get the cardinality of the relationship */
83
    GDALRelationshipCardinality GetCardinality() const
84
0
    {
85
0
        return m_eCardinality;
86
0
    }
87
88
    /** Get the name of the left (or base/origin) table in the relationship.
89
     *
90
     * @see GetRightTableName()
91
     */
92
    const std::string &GetLeftTableName() const
93
0
    {
94
0
        return m_osLeftTableName;
95
0
    }
96
97
    /** Get the name of the right (or related/destination) table in the
98
     * relationship */
99
    const std::string &GetRightTableName() const
100
0
    {
101
0
        return m_osRightTableName;
102
0
    }
103
104
    /** Get the name of the mapping table for many-to-many relationships.
105
     *
106
     * @see SetMappingTableName()
107
     */
108
    const std::string &GetMappingTableName() const
109
0
    {
110
0
        return m_osMappingTableName;
111
0
    }
112
113
    /** Sets the name of the mapping table for many-to-many relationships.
114
     *
115
     * @see GetMappingTableName()
116
     */
117
    void SetMappingTableName(const std::string &osName)
118
0
    {
119
0
        m_osMappingTableName = osName;
120
0
    }
121
122
    /** Get the names of the participating fields from the left table in the
123
     * relationship.
124
     *
125
     * @see GetRightTableFields()
126
     * @see SetLeftTableFields()
127
     */
128
    const std::vector<std::string> &GetLeftTableFields() const
129
0
    {
130
0
        return m_osListLeftTableFields;
131
0
    }
132
133
    /** Get the names of the participating fields from the right table in the
134
     * relationship.
135
     *
136
     * @see GetLeftTableFields()
137
     * @see SetRightTableFields()
138
     */
139
    const std::vector<std::string> &GetRightTableFields() const
140
0
    {
141
0
        return m_osListRightTableFields;
142
0
    }
143
144
    /** Sets the names of the participating fields from the left table in the
145
     * relationship.
146
     *
147
     * @see GetLeftTableFields()
148
     * @see SetRightTableFields()
149
     */
150
    void SetLeftTableFields(const std::vector<std::string> &osListFields)
151
0
    {
152
0
        m_osListLeftTableFields = osListFields;
153
0
    }
154
155
    /** Sets the names of the participating fields from the right table in the
156
     * relationship.
157
     *
158
     * @see GetRightTableFields()
159
     * @see SetLeftTableFields()
160
     */
161
    void SetRightTableFields(const std::vector<std::string> &osListFields)
162
0
    {
163
0
        m_osListRightTableFields = osListFields;
164
0
    }
165
166
    /** Get the names of the mapping table fields which correspond to the
167
     * participating fields from the left table in the relationship.
168
     *
169
     * @see GetRightMappingTableFields()
170
     * @see SetLeftMappingTableFields()
171
     */
172
    const std::vector<std::string> &GetLeftMappingTableFields() const
173
0
    {
174
0
        return m_osListLeftMappingTableFields;
175
0
    }
176
177
    /** Get the names of the mapping table fields which correspond to the
178
     * participating fields from the right table in the relationship.
179
     *
180
     * @see GetLeftMappingTableFields()
181
     * @see SetRightMappingTableFields()
182
     */
183
    const std::vector<std::string> &GetRightMappingTableFields() const
184
0
    {
185
0
        return m_osListRightMappingTableFields;
186
0
    }
187
188
    /** Sets the names of the mapping table fields which correspond to the
189
     * participating fields from the left table in the relationship.
190
     *
191
     * @see GetLeftMappingTableFields()
192
     * @see SetRightMappingTableFields()
193
     */
194
    void SetLeftMappingTableFields(const std::vector<std::string> &osListFields)
195
0
    {
196
0
        m_osListLeftMappingTableFields = osListFields;
197
0
    }
198
199
    /** Sets the names of the mapping table fields which correspond to the
200
     * participating fields from the right table in the relationship.
201
     *
202
     * @see GetRightMappingTableFields()
203
     * @see SetLeftMappingTableFields()
204
     */
205
    void
206
    SetRightMappingTableFields(const std::vector<std::string> &osListFields)
207
0
    {
208
0
        m_osListRightMappingTableFields = osListFields;
209
0
    }
210
211
    /** Get the type of the relationship.
212
     *
213
     * @see SetType()
214
     */
215
    GDALRelationshipType GetType() const
216
0
    {
217
0
        return m_eType;
218
0
    }
219
220
    /** Sets the type of the relationship.
221
     *
222
     * @see GetType()
223
     */
224
    void SetType(GDALRelationshipType eType)
225
0
    {
226
0
        m_eType = eType;
227
0
    }
228
229
    /** Get the label of the forward path for the relationship.
230
     *
231
     * The forward and backward path labels are free-form, user-friendly strings
232
     * which can be used to generate descriptions of the relationship between
233
     * features from the right and left tables.
234
     *
235
     * E.g. when the left table contains buildings and the right table contains
236
     * furniture, the forward path label could be "contains" and the backward
237
     * path label could be "is located within". A client could then generate a
238
     * user friendly description string such as "fire hose 1234 is located
239
     * within building 15a".
240
     *
241
     * @see SetForwardPathLabel()
242
     * @see GetBackwardPathLabel()
243
     */
244
    const std::string &GetForwardPathLabel() const
245
0
    {
246
0
        return m_osForwardPathLabel;
247
0
    }
248
249
    /** Sets the label of the forward path for the relationship.
250
     *
251
     * The forward and backward path labels are free-form, user-friendly strings
252
     * which can be used to generate descriptions of the relationship between
253
     * features from the right and left tables.
254
     *
255
     * E.g. when the left table contains buildings and the right table contains
256
     * furniture, the forward path label could be "contains" and the backward
257
     * path label could be "is located within". A client could then generate a
258
     * user friendly description string such as "fire hose 1234 is located
259
     * within building 15a".
260
     *
261
     * @see GetForwardPathLabel()
262
     * @see SetBackwardPathLabel()
263
     */
264
    void SetForwardPathLabel(const std::string &osLabel)
265
0
    {
266
0
        m_osForwardPathLabel = osLabel;
267
0
    }
268
269
    /** Get the label of the backward path for the relationship.
270
     *
271
     * The forward and backward path labels are free-form, user-friendly strings
272
     * which can be used to generate descriptions of the relationship between
273
     * features from the right and left tables.
274
     *
275
     * E.g. when the left table contains buildings and the right table contains
276
     * furniture, the forward path label could be "contains" and the backward
277
     * path label could be "is located within". A client could then generate a
278
     * user friendly description string such as "fire hose 1234 is located
279
     * within building 15a".
280
     *
281
     * @see SetBackwardPathLabel()
282
     * @see GetForwardPathLabel()
283
     */
284
    const std::string &GetBackwardPathLabel() const
285
0
    {
286
0
        return m_osBackwardPathLabel;
287
0
    }
288
289
    /** Sets the label of the backward path for the relationship.
290
     *
291
     * The forward and backward path labels are free-form, user-friendly strings
292
     * which can be used to generate descriptions of the relationship between
293
     * features from the right and left tables.
294
     *
295
     * E.g. when the left table contains buildings and the right table contains
296
     * furniture, the forward path label could be "contains" and the backward
297
     * path label could be "is located within". A client could then generate a
298
     * user friendly description string such as "fire hose 1234 is located
299
     * within building 15a".
300
     *
301
     * @see GetBackwardPathLabel()
302
     * @see SetForwardPathLabel()
303
     */
304
    void SetBackwardPathLabel(const std::string &osLabel)
305
0
    {
306
0
        m_osBackwardPathLabel = osLabel;
307
0
    }
308
309
    /** Get the type string of the related table.
310
     *
311
     * This a free-form string representing the type of related features, where
312
     * the exact interpretation is format dependent. For instance, table types
313
     * from GeoPackage relationships will directly reflect the categories from
314
     * the GeoPackage related tables extension (i.e. "media", "simple
315
     * attributes", "features", "attributes" and "tiles").
316
     *
317
     * @see SetRelatedTableType()
318
     */
319
    const std::string &GetRelatedTableType() const
320
0
    {
321
0
        return m_osRelatedTableType;
322
0
    }
323
324
    /** Sets the type string of the related table.
325
     *
326
     * This a free-form string representing the type of related features, where
327
     * the exact interpretation is format dependent. For instance, table types
328
     * from GeoPackage relationships will directly reflect the categories from
329
     * the GeoPackage related tables extension (i.e. "media", "simple
330
     * attributes", "features", "attributes" and "tiles").
331
     *
332
     * @see GetRelatedTableType()
333
     */
334
    void SetRelatedTableType(const std::string &osType)
335
0
    {
336
0
        m_osRelatedTableType = osType;
337
0
    }
338
339
    /** Convert a GDALRelationship* to a GDALRelationshipH.
340
     */
341
    static inline GDALRelationshipH ToHandle(GDALRelationship *poRelationship)
342
0
    {
343
0
        return static_cast<GDALRelationshipH>(poRelationship);
344
0
    }
345
346
    /** Convert a GDALRelationshipH to a GDALRelationship*.
347
     */
348
    static inline GDALRelationship *FromHandle(GDALRelationshipH hRelationship)
349
0
    {
350
0
        return static_cast<GDALRelationship *>(hRelationship);
351
0
    }
352
};
353
354
#endif