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
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct PgFieldDescriptor {
17 name: String,
18 table_oid: i32,
19 col_attr_num: i16,
20
21 // NOTE: Static code for data type. To see the oid of a specific type in Postgres,
22 // use the following command:
23 // SELECT oid FROM pg_type WHERE typname = 'int4';
24 type_oid: i32,
25
26 type_len: i16,
27 type_modifier: i32,
28 format_code: i16,
29}
30
31impl PgFieldDescriptor {
32 pub fn new(name: String, type_oid: i32, type_len: i16) -> Self {
33 let type_modifier = -1;
34 let format_code = 0;
35 let table_oid = 0;
36 let col_attr_num = 0;
37
38 Self {
39 name,
40 table_oid,
41 col_attr_num,
42 type_oid,
43 type_len,
44 type_modifier,
45 format_code,
46 }
47 }
48
49 /// Set the format code as binary format.
50 /// NOTE: Format code is text format by default.
51 pub fn set_to_binary(&mut self) {
52 self.format_code = 1;
53 }
54
55 pub fn get_name(&self) -> &str {
56 &self.name
57 }
58
59 pub fn get_table_oid(&self) -> i32 {
60 self.table_oid
61 }
62
63 pub fn get_col_attr_num(&self) -> i16 {
64 self.col_attr_num
65 }
66
67 pub fn get_type_oid(&self) -> i32 {
68 self.type_oid
69 }
70
71 pub fn get_type_len(&self) -> i16 {
72 self.type_len
73 }
74
75 pub fn get_type_modifier(&self) -> i32 {
76 self.type_modifier
77 }
78
79 pub fn get_format_code(&self) -> i16 {
80 self.format_code
81 }
82}