fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
RequestMessageFormat.cpp
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#include "RequestMessageFormat.h"
26#include "firebird/impl/blr.h"
27
28
29namespace fbcpp::request
30{
31 static std::uint8_t scaleToByte(int scale) noexcept
32 {
33 return static_cast<std::uint8_t>(static_cast<unsigned>(scale) & 0xFFu);
34 }
35
36
37 RequestMessageFormat& RequestMessageFormat::addField(DescriptorAdjustedType adjustedType, unsigned dataLength,
38 unsigned alignment, int scale, unsigned charSetId, int subType, bool isNullable)
39 {
40 const auto alignedOffset = static_cast<unsigned>((length + alignment - 1u) & ~(alignment - 1u));
41
42 Descriptor descriptor{
43 .originalType = static_cast<DescriptorOriginalType>(adjustedType),
44 .adjustedType = adjustedType,
45 .scale = scale,
46 .length = dataLength,
47 .offset = alignedOffset,
48 .nullOffset = 0,
49 .isNullable = isNullable,
50 .name = {},
51 .relation = {},
52 .alias = {},
53 .owner = {},
54 .charSetId = charSetId,
55 .subType = subType,
56 };
57
58 length = alignedOffset + dataLength;
59 blrFieldIndexes.push_back(static_cast<unsigned>(descriptors.size()) + nullableCount);
60
61 if (isNullable)
62 {
63 descriptor.nullOffset = (length + 1u) & ~1u;
64 length = descriptor.nullOffset + 2u;
65 ++nullableCount;
66 }
67
68 descriptors.push_back(std::move(descriptor));
69 return *this;
70 }
71
72 std::vector<std::byte> RequestMessageFormat::buildBlrMessageClause(unsigned messageNumber) const
73 {
74 if (messageNumber > 255u)
75 throw FbCppException("Message number must be between 0 and 255");
76
77 unsigned entryCount = static_cast<unsigned>(descriptors.size());
78 for (const auto& descriptor : descriptors)
79 {
80 if (descriptor.isNullable)
81 ++entryCount;
82 }
83
84 std::vector<std::byte> blr;
85 blr.reserve(3u + 64u * entryCount);
86
87 appendByte(blr, blr_message);
88 appendByte(blr, static_cast<std::uint8_t>(messageNumber));
89 appendWord(blr, static_cast<std::uint16_t>(entryCount));
90
91 for (const auto& descriptor : descriptors)
92 {
93 appendBlrDataType(blr, descriptor);
94
95 if (descriptor.isNullable)
96 {
97 appendByte(blr, blr_short);
98 appendByte(blr, 0u);
99 }
100 }
101
102 return blr;
103 }
104
105 void RequestMessageFormat::appendBlrDataType(std::vector<std::byte>& blr, const Descriptor& descriptor) const
106 {
107 switch (descriptor.adjustedType)
108 {
110 appendByte(blr, blr_bool);
111 break;
112
114 appendByte(blr, blr_short);
115 appendByte(blr, scaleToByte(descriptor.scale));
116 break;
117
119 appendByte(blr, blr_long);
120 appendByte(blr, scaleToByte(descriptor.scale));
121 break;
122
124 appendByte(blr, blr_int64);
125 appendByte(blr, scaleToByte(descriptor.scale));
126 break;
127
129 appendByte(blr, blr_int128);
130 appendByte(blr, scaleToByte(descriptor.scale));
131 break;
132
134 appendByte(blr, blr_float);
135 break;
136
138 appendByte(blr, blr_double);
139 break;
140
142 appendByte(blr, blr_dec64);
143 break;
144
146 appendByte(blr, blr_dec128);
147 break;
148
150 appendByte(blr, blr_sql_date);
151 break;
152
154 appendByte(blr, blr_sql_time);
155 break;
156
158 appendByte(blr, blr_timestamp);
159 break;
160
162 appendByte(blr, blr_sql_time_tz);
163 break;
164
166 appendByte(blr, blr_timestamp_tz);
167 break;
168
170 appendByte(blr, blr_quad);
171 appendByte(blr, 0u);
172 break;
173
175 if (descriptor.charSetId != 0)
176 {
177 appendByte(blr, blr_varying2);
178 appendWord(blr, static_cast<std::uint16_t>(descriptor.charSetId));
179 }
180 else
181 appendByte(blr, blr_varying);
182
183 appendWord(blr, static_cast<std::uint16_t>(descriptor.length - sizeof(std::uint16_t)));
184 break;
185
186 default:
187 throw FbCppException("Unsupported descriptor type");
188 }
189 }
190} // namespace fbcpp::request
Base exception class for all fb-cpp exceptions.
Definition Exception.h:231
std::vector< std::byte > buildBlrMessageClause(unsigned messageNumber) const
Generates the blr_message clause describing this message.
fb-cpp request namespace.
Definition Request.cpp:32
DescriptorAdjustedType
Descriptor adjusted type.
Definition Descriptor.h:161
@ BLOB
Binary large object.
@ TIME
Time of day without time zone.
@ DECFLOAT34
34-digit decimal floating point.
@ INT64
64-bit signed integer.
@ TIME_TZ
Time of day with time zone.
@ DECFLOAT16
16-digit decimal floating point.
@ INT16
16-bit signed integer.
@ STRING
String type (variable-length).
@ INT32
32-bit signed integer.
@ TIMESTAMP
Timestamp without time zone.
@ TIMESTAMP_TZ
Timestamp with time zone.
@ INT128
128-bit signed integer.
@ FLOAT
Single-precision floating point.
@ DOUBLE
Double-precision floating point.
DescriptorOriginalType
Descriptor original type.
Definition Descriptor.h:50
Describes a parameter or column.
Definition Descriptor.h:267
int scale
Decimal scale for numeric types; zero for non-numeric types.
Definition Descriptor.h:281
unsigned charSetId
Character set ID for string and BLOB columns.
Definition Descriptor.h:326
DescriptorAdjustedType adjustedType
Adjusted type after normalization for easier handling.
Definition Descriptor.h:276
unsigned length
Length in bytes of the column or parameter data.
Definition Descriptor.h:286