Coverage Report

Created: 2025-12-31 08:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrsf_frmts/ngw/ogrngwfielddomain.cpp
Line
Count
Source
1
/*******************************************************************************
2
 *  Project: NextGIS Web Driver
3
 *  Purpose: Implements NextGIS Web Driver
4
 *  Author: Dmitry Baryshnikov, dmitry.baryshnikov@nextgis.com
5
 *  Language: C++
6
 *******************************************************************************
7
 *  The MIT License (MIT)
8
 *
9
 *  Copyright (c) 2025, NextGIS <info@nextgis.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 *******************************************************************************/
13
14
#include "ogr_ngw.h"
15
16
/*
17
 * OGRNGWCodedFieldDomain()
18
 */
19
OGRNGWCodedFieldDomain::OGRNGWCodedFieldDomain(
20
    const CPLJSONObject &oResourceJsonObject)
21
0
{
22
0
    nResourceID = oResourceJsonObject.GetLong("resource/id", 0);
23
0
    nResourceParentID = oResourceJsonObject.GetLong("resource/parent/id", 0);
24
0
    osCreationDate = oResourceJsonObject.GetString("resource/creation_date");
25
0
    osDisplayName = oResourceJsonObject.GetString("resource/display_name");
26
0
    osKeyName = oResourceJsonObject.GetString("resource/keyname");
27
0
    osDescription = oResourceJsonObject.GetString("resource/description");
28
29
0
    std::set<GIntBig> keys;
30
31
0
    bool bOnlyDigitsKeys = true;
32
0
    std::vector<OGRCodedValue> aoDom1, aoDom2, aoDom3;
33
0
    auto oItems = oResourceJsonObject.GetObj("lookup_table/items");
34
0
    for (const auto &oItem : oItems.GetChildren())
35
0
    {
36
0
        if (bOnlyDigitsKeys)
37
0
        {
38
0
            auto nNum = CPLAtoGIntBig(oItem.GetName().c_str());
39
0
            if (keys.find(nNum) != keys.end())
40
0
            {
41
0
                bOnlyDigitsKeys = false;
42
0
            }
43
0
            else
44
0
            {
45
0
                keys.insert(nNum);
46
0
            }
47
0
        }
48
49
0
        OGRCodedValue cv1;
50
0
        cv1.pszCode = CPLStrdup(oItem.GetName().c_str());
51
0
        cv1.pszValue = CPLStrdup(oItem.ToString().c_str());
52
0
        aoDom1.emplace_back(cv1);
53
54
0
        OGRCodedValue cv2;
55
0
        cv2.pszCode = CPLStrdup(oItem.GetName().c_str());
56
0
        cv2.pszValue = CPLStrdup(oItem.ToString().c_str());
57
0
        aoDom2.emplace_back(cv2);
58
59
0
        OGRCodedValue cv3;
60
0
        cv3.pszCode = CPLStrdup(oItem.GetName().c_str());
61
0
        cv3.pszValue = CPLStrdup(oItem.ToString().c_str());
62
0
        aoDom3.emplace_back(cv3);
63
0
    }
64
65
0
    auto osName = osDisplayName;
66
0
    auto oDom = std::make_shared<OGRCodedFieldDomain>(
67
0
        osName, osDescription, OFTString, OFSTNone, std::move(aoDom1));
68
0
    apDomains[0] = std::move(oDom);
69
70
0
    if (bOnlyDigitsKeys)
71
0
    {
72
0
        osName = osDisplayName + " (number)";
73
0
        oDom = std::make_shared<OGRCodedFieldDomain>(
74
0
            osName, osDescription, OFTInteger, OFSTNone, std::move(aoDom2));
75
0
        apDomains[1] = std::move(oDom);
76
77
0
        osName = osDisplayName + " (bigint)";
78
0
        oDom = std::make_shared<OGRCodedFieldDomain>(
79
0
            osName, osDescription, OFTInteger64, OFSTNone, std::move(aoDom3));
80
0
        apDomains[2] = std::move(oDom);
81
0
    }
82
0
}
83
84
/*
85
 * ToFieldDomain()
86
 */
87
const OGRFieldDomain *
88
OGRNGWCodedFieldDomain::ToFieldDomain(OGRFieldType eFieldType) const
89
0
{
90
0
    for (size_t i = 0; i < apDomains.size(); ++i)
91
0
    {
92
0
        if (apDomains[i] && apDomains[i]->GetFieldType() == eFieldType)
93
0
        {
94
0
            return apDomains[i].get();
95
0
        }
96
0
    }
97
0
    return nullptr;
98
0
}
99
100
/*
101
 * GetID()
102
 */
103
GIntBig OGRNGWCodedFieldDomain::GetID() const
104
0
{
105
0
    return nResourceID;
106
0
}
107
108
/*
109
 * GetDomainsNames()
110
 */
111
std::string OGRNGWCodedFieldDomain::GetDomainsNames() const
112
0
{
113
0
    std::string osOut;
114
0
    for (size_t i = 0; i < apDomains.size(); ++i)
115
0
    {
116
0
        if (apDomains[i])
117
0
        {
118
0
            if (osOut.empty())
119
0
            {
120
0
                osOut = apDomains[i]->GetName();
121
0
            }
122
0
            else
123
0
            {
124
0
                osOut += ", " + apDomains[i]->GetName();
125
0
            }
126
0
        }
127
0
    }
128
0
    return osOut;
129
0
}
130
131
/**
132
 * HasDomainName()
133
 */
134
bool OGRNGWCodedFieldDomain::HasDomainName(const std::string &osName) const
135
0
{
136
0
    if (osName.empty())
137
0
    {
138
0
        return false;
139
0
    }
140
0
    for (size_t i = 0; i < apDomains.size(); ++i)
141
0
    {
142
0
        if (apDomains[i] && apDomains[i]->GetName() == osName)
143
0
        {
144
0
            return true;
145
0
        }
146
0
    }
147
0
    return false;
148
0
}