oracledb 26.0.0-beta.1

Lightweight driver for Oracle Database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//-----------------------------------------------------------------------------
// Copyright (c) 2026, Oracle and/or its affiliates.
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// write_buffer.rs
//
// Defines the structure used for serializing data into a buffer.
//-----------------------------------------------------------------------------

use crate::client::Client;
use crate::constants;
use crate::db_type::DbType;

pub struct WriteBuffer {
    buf: Vec<u8>,
}

impl WriteBuffer {
    /// Clears the buffer of all content.
    pub fn clear(&mut self) {
        self.buf.clear();
    }

    /// Returns a reference to the buffer contents.
    pub fn get_buf(&self) -> &[u8] {
        &self.buf[..]
    }

    /// Returns the length of the buffer.
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// Creates a new instance.
    pub fn new() -> Self {
        Self {
            buf: Vec::<u8>::new(),
        }
    }

    /// Reserves the specified number of bytes in the buffer for later use. The
    /// current length of the buffer (before reservation takes place) is
    /// returned as a convenience.
    pub fn reserve_bytes(&mut self, num_bytes: usize) -> usize {
        let orig_len = self.buf.len();
        self.buf.resize(orig_len + num_bytes, 0);
        orig_len
    }

    /// Writes the contents of the slice to the buffer.
    pub fn write_bytes(&mut self, value: &[u8]) {
        self.buf.extend(value);
    }

    /// Writes an optional byte slice using the TTC double-length format.
    pub fn write_bytes_with_double_length(&mut self, value: Option<&[u8]>) {
        if let Some(value) = value {
            self.write_ub4(value.len().try_into().unwrap());
            if !value.is_empty() {
                self.write_bytes_with_length(value);
            }
        } else {
            self.write_ub4(0);
        }
    }

    /// Writes the contents of the slice to the buffer but first encodes a
    /// length.
    pub fn write_bytes_with_length(&mut self, value: &[u8]) {
        let mut num_bytes = value.len();
        if num_bytes <= constants::TTC_MAX_SHORT_LENGTH.into() {
            self.write_u8(num_bytes.try_into().unwrap());
            self.write_bytes(value);
        } else {
            self.write_u8(constants::TTC_LONG_LENGTH_INDICATOR);
            let mut remaining_data = value;
            while num_bytes > 0 {
                let chunk_len =
                    std::cmp::min(num_bytes, constants::TTC_CHUNK_SIZE);
                self.write_ub4(chunk_len.try_into().unwrap());
                self.buf.extend(&remaining_data[..chunk_len]);
                remaining_data = &remaining_data[chunk_len..];
                num_bytes -= chunk_len;
            }
            self.write_ub4(0);
        }
    }

    /// Writes the function header to the buffer.
    pub fn write_function_header(&mut self, client: &Client, fn_type: u8) {
        self.write_rpc_header(
            client,
            constants::TTC_MSG_TYPE_FUNCTION,
            fn_type,
        );
    }

    /// Writes the piggyback header to the buffer.
    pub fn write_piggyback_header(
        &mut self,
        client: &Client,
        piggyback_type: u8,
    ) {
        self.write_rpc_header(
            client,
            constants::TTC_MSG_TYPE_PIGGYBACK,
            piggyback_type,
        );
    }

    /// Writes a QLocator (quasi-locator) which is used for writing values
    /// like JSON and vectors to the buffer in the format required by the
    /// database.
    pub fn write_qlocator(&mut self, encoded_data: &WriteBuffer) {
        self.write_ub4(40); // fixed length of QLocator
        self.write_u8(40); // repeated length
        self.write_u16be(38); // length less 2 bytes
        self.write_u16be(4); // QLocator version
        self.write_u8(
            constants::TTC_LOB_LOC_FLAGS_VALUE_BASED
                | constants::TTC_LOB_LOC_FLAGS_BLOB
                | constants::TTC_LOB_LOC_FLAGS_ABSTRACT,
        );
        self.write_u8(constants::TTC_LOB_LOC_FLAGS_INIT);
        self.write_u16be(0); // additional flags (unused)
        self.write_u16be(1); // byt1
        self.write_u64be(encoded_data.buf.len().try_into().unwrap());
        self.write_u16be(0); // unused
        self.write_u16be(0); // character set id (unused)
        self.write_u16be(0); // unused
        self.write_u64be(0); // unused
        self.write_u64be(0); // unused
        self.write_bytes_with_length(&encoded_data.buf);
    }

    /// Writes the RPC header to the buffer. The RPC could be a function or a
    /// piggyback. Only one function is sent in a request but many piggybacks
    /// could be sent at the same time.
    pub fn write_rpc_header(
        &mut self,
        client: &Client,
        message_type: u8,
        rpc_type: u8,
    ) {
        self.write_u8(message_type);
        self.write_u8(rpc_type);
        self.write_u8(0); // RPC sequence number (unused)
        if client.supports_ttc_field_version(
            constants::TTC_FIELD_VERSION_23_1_EXT_1,
        ) {
            self.write_ub8(0); // token number
        }
    }

    /// Writes a string to the buffer.
    pub fn write_str(&mut self, value: &str) {
        self.write_bytes(value.as_bytes());
    }

    /// Writes a u16 to the buffer as a length-encoded integer.
    pub fn write_ub2(&mut self, value: u16) {
        if value == 0 {
            self.write_u8(0);
        } else if value < u8::MAX.into() {
            self.write_u8(1);
            self.write_u8(value.try_into().unwrap());
        } else {
            self.write_u8(2);
            self.write_u16be(value);
        }
    }

    /// Writes a u32 to the buffer as a length-encoded integer.
    pub fn write_ub4(&mut self, value: u32) {
        if value == 0 {
            self.write_u8(0);
        } else if value < u8::MAX.into() {
            self.write_u8(1);
            self.write_u8(value.try_into().unwrap());
        } else if value < u16::MAX.into() {
            self.write_u8(2);
            self.write_u16be(value.try_into().unwrap());
        } else {
            self.write_u8(4);
            self.write_u32be(value);
        }
    }

    /// Writes a u64 to the buffer as a length-encoded integer.
    pub fn write_ub8(&mut self, value: u64) {
        if value == 0 {
            self.write_u8(0);
        } else if value < u8::MAX.into() {
            self.write_u8(1);
            self.write_u8(value.try_into().unwrap());
        } else if value < u16::MAX.into() {
            self.write_u8(2);
            self.write_u16be(value.try_into().unwrap());
        } else if value < u32::MAX.into() {
            self.write_u8(4);
            self.write_u32be(value.try_into().unwrap());
        } else {
            self.write_u8(8);
            self.write_u64be(value);
        }
    }

    /// Writes an unsigned 8-bit integer to the buffer.
    pub fn write_u8(&mut self, value: u8) {
        self.buf.push(value);
    }

    /// Writes a unsigned 16-bit integer to the buffer in big endian format.
    pub fn write_u16be(&mut self, value: u16) {
        self.buf.extend(value.to_be_bytes());
    }

    /// Writes an unsigned 16-bit integer to the buffer in little endian
    /// format.
    pub fn write_u16le(&mut self, value: u16) {
        self.buf.extend(value.to_le_bytes());
    }

    /// Writes an unsigned 32-bit integer to the buffer in big endian format.
    pub fn write_u32be(&mut self, value: u32) {
        self.buf.extend(value.to_be_bytes());
    }

    /// Writes an unsigned 32-bit integer to the buffer in big endian format at
    /// the specified offset.
    pub fn write_u32be_at(&mut self, value: u32, offset: usize) {
        let bytes = value.to_be_bytes();
        self.buf[offset..offset + 4].copy_from_slice(&bytes);
    }

    /// Writes an unsigned 64-bit integer to the buffer in big endian format.
    pub fn write_u64be(&mut self, value: u64) {
        self.buf.extend(value.to_be_bytes());
    }
}

pub trait ToBuf {
    /// Writes the value to the buffer.
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        db_type: &'static DbType,
        write_length: bool,
    );

    /// Writes a null value to the buffer.
    fn to_buf_null(&self, buf: &mut WriteBuffer) {
        buf.write_u8(0);
    }
}

impl ToBuf for bool {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        if *self {
            if write_length {
                buf.write_u8(2);
            }
            buf.write_u8(1);
            buf.write_u8(1);
        } else {
            if write_length {
                buf.write_u8(1);
            }
            buf.write_u8(0);
        }
    }
}

impl ToBuf for f32 {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        let mut conv_buf = self.to_be_bytes();
        if conv_buf[0] & 0x80 == 0 {
            conv_buf[0] |= 0x80;
        } else {
            for value in &mut conv_buf {
                *value = !*value;
            }
        }
        if write_length {
            buf.write_bytes_with_length(&conv_buf);
        } else {
            buf.write_bytes(&conv_buf);
        }
    }
}

impl ToBuf for f64 {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        let mut conv_buf = self.to_be_bytes();
        if conv_buf[0] & 0x80 == 0 {
            conv_buf[0] |= 0x80;
        } else {
            for value in &mut conv_buf {
                *value = !*value;
            }
        }
        if write_length {
            buf.write_bytes_with_length(&conv_buf);
        } else {
            buf.write_bytes(&conv_buf);
        }
    }
}

impl ToBuf for String {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        if write_length {
            buf.write_bytes_with_length(self.as_bytes());
        } else {
            buf.write_bytes(self.as_bytes());
        }
    }
}

impl ToBuf for &str {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        if write_length {
            buf.write_bytes_with_length(self.as_bytes());
        } else {
            buf.write_bytes(self.as_bytes());
        }
    }
}

impl ToBuf for Vec<u8> {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        if write_length {
            buf.write_bytes_with_length(self);
        } else {
            buf.write_bytes(self);
        }
    }
}

impl ToBuf for &[u8] {
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        _db_type: &'static DbType,
        write_length: bool,
    ) {
        if write_length {
            buf.write_bytes_with_length(self);
        } else {
            buf.write_bytes(self);
        }
    }
}

impl<T> ToBuf for Option<T>
where
    T: ToBuf + Default,
{
    fn to_buf(
        &self,
        buf: &mut WriteBuffer,
        db_type: &'static DbType,
        write_length: bool,
    ) {
        if let Some(value) = self {
            <T>::to_buf(value, buf, db_type, write_length);
        } else {
            <T>::default().to_buf_null(buf);
        }
    }
}