pgwire/pg_field_descriptor.rs
1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::types::Format;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct PgFieldDescriptor {
19 name: String,
20 table_oid: i32,
21 col_attr_num: i16,
22
23 // NOTE: Static code for data type. To see the oid of a specific type in Postgres,
24 // use the following command:
25 // SELECT oid FROM pg_type WHERE typname = 'int4';
26 type_oid: i32,
27
28 type_len: i16,
29 type_modifier: i32,
30 format: Format,
31}
32
33impl PgFieldDescriptor {
34 pub fn new(name: String, type_oid: i32, type_len: i16) -> Self {
35 let type_modifier = -1;
36 let table_oid = 0;
37 let col_attr_num = 0;
38
39 Self {
40 name,
41 table_oid,
42 col_attr_num,
43 type_oid,
44 type_len,
45 type_modifier,
46 format: Format::Text,
47 }
48 }
49
50 /// Set the format code as binary format.
51 /// NOTE: Format code is text format by default.
52 pub fn set_to_binary(&mut self) {
53 self.format = Format::Binary;
54 }
55
56 pub fn get_name(&self) -> &str {
57 &self.name
58 }
59
60 pub fn get_table_oid(&self) -> i32 {
61 self.table_oid
62 }
63
64 pub fn get_col_attr_num(&self) -> i16 {
65 self.col_attr_num
66 }
67
68 pub fn get_type_oid(&self) -> i32 {
69 self.type_oid
70 }
71
72 pub fn get_type_len(&self) -> i16 {
73 self.type_len
74 }
75
76 pub fn get_type_modifier(&self) -> i32 {
77 self.type_modifier
78 }
79
80 pub fn get_format_code(&self) -> i16 {
81 self.format.to_i8() as _
82 }
83}