GCC Code Coverage Report


Directory: libimgdoc2/
File: libimgdoc2/src/doc/documentMetadataBase.h
Date: 2025-02-03 12:41:04
Exec Total Coverage
Lines: 3 3 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH
2 //
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <limits>
8 #include <vector>
9 #include <optional>
10 #include <memory>
11 #include <type_traits>
12 #include <utility>
13 #include <string>
14 #include "IDocumentMetadata.h"
15 #include "../db/IDbStatement.h"
16 #include "document.h"
17
18 class DocumentMetadataBase
19 {
20 private:
21 std::shared_ptr<Document> document_;
22 protected:
23 constexpr static char kPathDelimiter_ = '/';
24 public:
25 DocumentMetadataBase() = delete;
26 protected:
27 172 explicit DocumentMetadataBase(std::shared_ptr<Document> document) : document_(std::move(document))
28 172 {}
29
30 /// Values that are used for the field "TypeDiscriminator" in the database table.
31 enum class DatabaseDataTypeValue : int
32 {
33 invalid = std::numeric_limits< std::underlying_type_t<DatabaseDataTypeValue>>::max(),
34 null = 0,
35 int32 = 1,
36 uint32 = 2,
37 doublefloat = 3,
38 singlefloat = 4,
39 utf8string = 5,
40 json = 6,
41 };
42
43 //void BindAncestorId(const std::shared_ptr<IDbStatement>& database_statement, int binding_index, const std::optional<imgdoc2::dbIndex>& parent);
44 int BindTypeDiscriminatorAndData(const std::shared_ptr<IDbStatement>& database_statement, int binding_index, DatabaseDataTypeValue type, const imgdoc2::IDocumentMetadata::metadata_item_variant& value);
45
46 static DatabaseDataTypeValue DetermineDatabaseDataTypeValueOrThrow(imgdoc2::DocumentMetadataType type, const imgdoc2::IDocumentMetadata::metadata_item_variant& value);
47 static DatabaseDataTypeValue DetermineDatabaseDataTypeValue(imgdoc2::DocumentMetadataType type, const imgdoc2::IDocumentMetadata::metadata_item_variant& value);
48
49 /// Parse the path string, i.e. split it at the delimiter '/'. The number of parts is returned in
50 /// 'count_of_parts_in_path' if it is not null. Then the database is queried, and we return a vector
51 /// corresponding to the parts of the path. The vector contains the primary key for those nodes. If
52 /// a part cannot be found, then the query stops at this point (and the vector returned contains less
53 /// elements than the number of parts in the path). So, only if the complete path can be resolved, the
54 /// size of the returned vector is equal to the number of parts in the path.
55 ///
56 /// \param path The path to be mapped.
57 /// \param [in,out] count_of_parts_in_path If non-null, the parts as determined by the path string.
58 ///
59 /// \returns The primary keys of the nodes which could be mapped.
60 std::vector<imgdoc2::dbIndex> GetNodeIdsForPath(const std::string& path, size_t* count_of_parts_in_path);
61
62 std::vector<imgdoc2::dbIndex> GetNodeIdsForPathParts(const std::vector<std::string_view>& parts);
63
64 bool TryMapPathAndGetTerminalNode(const std::string& path, std::optional<imgdoc2::dbIndex>* terminal_node_id);
65
66 15230 [[nodiscard]] const std::shared_ptr<Document>& GetDocument() const { return this->document_; }
67
68 /// Splits a the specified path (at the delimiter kPathDelimiter_) into its parts.
69 /// If there is zero-length part, then an imgdoc2::invalid_path_exception is thrown.
70 ///
71 /// \param path The path to be split.
72 ///
73 /// \returns A vector containing the parts;
74 static std::vector<std::string_view> SplitPath(const std::string_view& path);
75
76 bool CheckIfItemExists(imgdoc2::dbIndex primary_key);
77 private:
78 std::shared_ptr<IDbStatement> CreateQueryForNodeIdsForPath(const std::vector<std::string_view>& path_parts);
79 };
80