forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_sql_parser.cpp
More file actions
122 lines (112 loc) · 3.85 KB
/
Copy pathob_sql_parser.cpp
File metadata and controls
122 lines (112 loc) · 3.85 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
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL
#include "ob_sql_parser.h"
#include "sql/parser/parse_node.h"
#include "sql/parser/parse_malloc.h"
#include "share/ob_errno.h"
#include "lib/utility/ob_macro_utils.h"
#ifndef SQL_PARSER_COMPILATION
#include "lib/ash/ob_active_session_guard.h"
#endif
#include "sql/parser/parser_utility.h"
#include <openssl/md5.h>
namespace oceanbase
{
using namespace common;
namespace sql
{
int ObSQLParser::parse(const char * str_ptr, const int64_t str_len, ParseResult &result)
{
int ret = OB_SUCCESS;
#ifndef SQL_PARSER_COMPILATION
// proxy don't need this, only for observer
ObActiveSessionGuard::get_stat().in_parse_ = true;
#endif
if (OB_FAIL(parse_init(&result))) {
// do nothing
} else if (OB_FAIL(parse_sql(&result, str_ptr, static_cast<size_t>(str_len)))) {
// do nothing
}
#ifndef SQL_PARSER_COMPILATION
ObActiveSessionGuard::get_stat().in_parse_ = false;
#endif
return ret;
}
int ObSQLParser::parse_and_gen_sqlid(void *malloc_pool,
const char *str_ptr,
const int64_t str_len,
const int64_t len,
char *sql_id)
{
int ret = OB_SUCCESS;
ParseResult *parse_result = (ParseResult *)parse_malloc(sizeof(ParseResult), malloc_pool);
if (OB_ISNULL(parse_result)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
memset((void *)parse_result, 0, sizeof(ParseResult));
parse_result->is_fp_ = true;
parse_result->is_multi_query_ = false;
parse_result->malloc_pool_ = malloc_pool;
parse_result->is_ignore_hint_ = false;
parse_result->need_parameterize_ = true;
parse_result->pl_parse_info_.is_pl_parse_ = false;
parse_result->minus_ctx_.has_minus_ = false;
parse_result->minus_ctx_.pos_ = -1;
parse_result->minus_ctx_.raw_sql_offset_ = -1;
parse_result->is_for_trigger_ = false;
parse_result->is_for_remap_ = false;
parse_result->is_dynamic_sql_ = false;
parse_result->is_batched_multi_enabled_split_ = false;
parse_result->may_bool_value_ = false;
int64_t new_length = str_len + 1;
char *buf = (char *)parse_malloc(new_length, parse_result->malloc_pool_);
if (OB_UNLIKELY(NULL == buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
parse_result->param_nodes_ = NULL;
parse_result->tail_param_node_ = NULL;
parse_result->no_param_sql_ = buf;
parse_result->no_param_sql_buf_len_ = new_length;
ret = parse(str_ptr, new_length, *parse_result);
}
if (OB_SUCC(ret)) {
ret = gen_sqlid(parse_result->no_param_sql_,
parse_result->no_param_sql_len_,
len,
sql_id);
}
}
return ret;
}
int ObSQLParser::gen_sqlid(const char* paramed_sql, const int64_t sql_len,
const int64_t len,
char *sql_id)
{
int ret = OB_SUCCESS;
const int32_t MD5_LENGTH = 16;
if (OB_ISNULL(sql_id) || len < 32) {
ret = OB_INVALID_ARGUMENT;
} else {
unsigned char md5_buf[MD5_LENGTH];
unsigned char *res = MD5(reinterpret_cast<const unsigned char *>(paramed_sql),
sql_len, md5_buf);
if (OB_ISNULL(res)) {
ret = OB_ERR_UNEXPECTED;
} else {
ret = parser_to_hex_cstr(md5_buf, MD5_LENGTH, sql_id, len);
}
}
return ret;
}
}
}