forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_parser_utils.h
More file actions
163 lines (152 loc) · 4.99 KB
/
Copy pathob_parser_utils.h
File metadata and controls
163 lines (152 loc) · 4.99 KB
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
/*
* Copyright (c) 2025 OceanBase.
*
* 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
*
* http://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.
*/
#ifndef _OB_PARSER_UTILS_H
#define _OB_PARSER_UTILS_H 1
#include "lib/utility/ob_print_utils.h"
#include "sql/parser/parse_node.h"
#include "lib/ob_name_def.h"
#include "common/ob_smart_call.h"
#include "lib/utility/ob_hang_fatal_error.h"
#include "share/ob_errno.h"
const char* get_type_name(int type);
namespace oceanbase
{
namespace sql
{
inline void databuff_print_stmt_location(char *buf, int64_t buf_len, int64_t &pos, const ObStmtLoc &obj)
{
if (obj.first_column_ > 0 || obj.last_column_ > 0 || obj.first_line_ > 0 || obj.last_line_ > 0) {
J_COMMA();
int first_line = obj.first_line_;
int last_line = obj.last_line_;
int first_column = obj.first_column_;
int last_column = obj.last_column_;
J_KV(K(first_line),
K(last_line),
K(first_column),
K(last_column));
}
}
inline int databuff_print_obj(char *buf, const int64_t buf_len, int64_t &pos, const ParseNode &obj)
{
int ret = OB_SUCCESS;
J_OBJ_START();
J_KV(N_TYPE, get_type_name(obj.type_),
N_INT_VALUE, obj.value_,
N_STR_VALUE_LEN, obj.str_len_,
N_STR_VALUE, common::ObString(obj.str_len_, obj.str_value_));
databuff_print_stmt_location(buf, buf_len, pos, obj.stmt_loc_);
if (0 < obj.num_child_) {
J_COMMA();
J_NAME(N_CHILDREN);
J_COLON();
J_ARRAY_START();
for (int64_t i = 0; i < obj.num_child_; ++i) {
if (NULL == obj.children_[i]) {
J_EMPTY_OBJ();
} else if (OB_FAIL(SMART_CALL(databuff_print_obj(buf, buf_len, pos, *obj.children_[i])))) {
}
if (i != obj.num_child_ - 1) { common::databuff_printf(buf, buf_len, pos, ", "); }
}
J_ARRAY_END();
}
J_OBJ_END();
return ret;
}
inline void print_indent(char *buf, const int64_t buf_len, int64_t &pos, const int level)
{
for (int i=0; i<level; ++i) {
common::databuff_printf(buf, buf_len, pos, "\t");
}
}
inline void databuff_simple_print_obj(char *buf, const int64_t buf_len, const int64_t index, int64_t &pos, const ParseNode *obj, int level)
{
print_indent(buf, buf_len, pos, level);
if (NULL != obj) {
common::databuff_printf(buf,
buf_len, pos,
"|--[%ld],[%s], str_value_=[%.*s], value=[%ld]\n",
index,
get_type_name(obj->type_),
int32_t(obj->str_len_),
obj->str_value_,
obj->value_);
} else {
common::databuff_printf(buf, buf_len, pos, "|--[%ld]\n", index);
}
for (int64_t i = 0; i < obj->num_child_; ++i) {
if (NULL != obj->children_[i]) {
databuff_simple_print_obj(buf, buf_len, i, pos, obj->children_[i], level + 1);
}
}
}
class ObParserResultTreePrintWrapper
{
public:
explicit ObParserResultTreePrintWrapper(const ParseNode &parse_tree)
:parse_tree_(parse_tree)
{}
int64_t to_string(char* buf, const int64_t buf_len) const
{
int64_t pos = 0;
common::databuff_printf(buf, buf_len, pos, "\n");
databuff_simple_print_obj(buf, buf_len, 0, pos, &parse_tree_, 0);
return pos;
}
private:
DISALLOW_COPY_AND_ASSIGN(ObParserResultTreePrintWrapper);
private:
const ParseNode &parse_tree_;
};
class ObParserResultPrintWrapper
{
public:
explicit ObParserResultPrintWrapper(const ParseNode &parse_tree)
:parse_tree_(parse_tree)
{}
int64_t to_string(char* buf, const int64_t buf_len) const
{
int64_t pos = 0;
int ret = OB_SUCCESS;
if (OB_FAIL(SMART_CALL(databuff_print_obj(buf, buf_len, pos, parse_tree_)))) {
pos = 0;
J_OBJ_START();
J_KV(N_TYPE, get_type_name(parse_tree_.type_),
N_INT_VALUE, parse_tree_.value_,
N_STR_VALUE_LEN, parse_tree_.str_len_,
N_STR_VALUE, common::ObString(parse_tree_.str_len_, parse_tree_.str_value_));
databuff_print_stmt_location(buf, buf_len, pos, parse_tree_.stmt_loc_);
J_OBJ_END();
}
return pos;
}
private:
DISALLOW_COPY_AND_ASSIGN(ObParserResultPrintWrapper);
private:
const ParseNode &parse_tree_;
};
struct ObCharsets4Parser
{
explicit ObCharsets4Parser() :
string_collation_(common::CS_TYPE_UTF8MB4_GENERAL_CI),
nls_collation_(common::CS_TYPE_INVALID)
{}
common::ObCollationType string_collation_; //collation type for the string to parse
common::ObCollationType nls_collation_; //oracle database collation for validating identifiers
};
} // end namespace sql
} // end namespace oceanbase
#endif /* _OB_PARSER_UTILS_H */