fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Array.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef FBCPP_ARRAY_H
26#define FBCPP_ARRAY_H
27
28#include "fb-cpp-export.h"
29#include "fb-api.h"
30#include "Descriptor.h"
31#include <cstddef>
32#include <optional>
33#include <span>
34#include <string>
35#include <type_traits>
36#include <vector>
37
38
42namespace fbcpp
43{
44 class Attachment;
45 class Transaction;
46
50 class ArrayId final
51 {
52 public:
54 bool isEmpty() const noexcept
55 {
56 return id.gds_quad_high == 0 && id.gds_quad_low == 0;
57 }
58
59 public:
61 ISC_QUAD id{0, 0};
62 };
63
67 struct ArrayBound final
68 {
70 short lower;
71
73 short upper;
74 };
75
79 struct ArrayDescriptor final
80 {
82 std::string relation;
83
85 std::string field;
86
89
91 int scale;
92
94 unsigned elementLength;
95
97 std::vector<ArrayBound> bounds;
98 };
99
103 class FB_CPP_EXPORT Array final
104 {
105 public:
107 Array(Attachment& attachment, Transaction& transaction, ArrayDescriptor descriptor, ArrayId id = {});
108
109 Array(const Array&) = delete;
110 Array& operator=(const Array&) = delete;
111 Array(Array&&) = delete;
112 Array& operator=(Array&&) = delete;
113
114 public:
116 const ArrayId& getId() const noexcept
117 {
118 return id;
119 }
120
122 const ArrayDescriptor& getDescriptor() const noexcept
123 {
124 return descriptor;
125 }
126
128 std::size_t getSliceLength() const;
129
131 unsigned read(std::span<std::byte> buffer);
132
134 void write(std::span<const std::byte> buffer);
135
137 template <typename T, std::size_t Extent>
138 requires(!std::is_same_v<std::remove_cv_t<T>, std::byte>)
139 unsigned read(std::span<T, Extent> buffer)
140 {
141 static_assert(std::is_trivially_copyable_v<T>);
142 return read(std::as_writable_bytes(buffer));
143 }
144
146 template <typename T, std::size_t Extent>
147 requires(!std::is_same_v<std::remove_cv_t<T>, std::byte>)
148 void write(std::span<const T, Extent> buffer)
149 {
150 static_assert(std::is_trivially_copyable_v<T>);
151 write(std::as_bytes(buffer));
152 }
153
154 private:
155 void validateBuffer(std::size_t size) const;
156 ISC_ARRAY_DESC buildIscDescriptor() const;
157
158 private:
159 Attachment& attachment;
160 Transaction& transaction;
161 ArrayDescriptor descriptor;
162 ArrayId id;
163 };
164} // namespace fbcpp
165
166
167#endif // FBCPP_ARRAY_H
Represents a Firebird array identifier.
Definition Array.h:51
bool isEmpty() const noexcept
Returns whether this array identifier is empty.
Definition Array.h:54
Provides slice access to a Firebird array.
Definition Array.h:104
const ArrayDescriptor & getDescriptor() const noexcept
Returns the descriptor used for slice operations.
Definition Array.h:122
unsigned read(std::span< T, Extent > buffer)
Reads the complete array slice into elements matching the Firebird element representation.
Definition Array.h:139
const ArrayId & getId() const noexcept
Returns the current array identifier.
Definition Array.h:116
void write(std::span< const T, Extent > buffer)
Writes the complete array slice from elements matching the Firebird element representation.
Definition Array.h:148
Represents a connection to a Firebird database.
Definition Attachment.h:220
Represents a transaction in one or more Firebird databases.
fb-cpp namespace.
Definition Array.h:43
DescriptorOriginalType
Descriptor original type.
Definition Descriptor.h:50
Defines one dimension of a Firebird array.
Definition Array.h:68
short upper
Inclusive upper bound.
Definition Array.h:73
short lower
Inclusive lower bound.
Definition Array.h:70
Defines the element representation and bounds of a Firebird array.
Definition Array.h:80
std::string field
Array field name; required when creating a new array.
Definition Array.h:85
int scale
Decimal scale of each array element.
Definition Array.h:91
DescriptorOriginalType elementType
SQL type of each array element.
Definition Array.h:88
unsigned elementLength
Size in bytes of each array element in Firebird's wire representation.
Definition Array.h:94
std::vector< ArrayBound > bounds
Inclusive bounds for each dimension, from outermost to innermost.
Definition Array.h:97
std::string relation
Relation containing this array field; required when creating a new array.
Definition Array.h:82