-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrules_table_service.cc
194 lines (157 loc) · 6.09 KB
/
rules_table_service.cc
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
/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file rules_table_service.cc
Implementation of the service for accessing the rewrite rules table.
*/
#include <string.h>
#include <sys/types.h>
#include "my_base.h"
#include "my_bitmap.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "mysql/service_rules_table.h"
#include "mysql/strings/m_ctype.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/sql_base.h"
#include "sql/table.h"
#include "sql/transaction.h"
#include "sql_string.h"
#include "thr_lock.h"
class THD;
namespace rules_table_service {
int MY_ATTRIBUTE((visibility("default")))
dummy_function_to_ensure_we_are_linked_into_the_server() {
return 1;
}
const char *db_name = "query_rewrite";
const char *table_name = "rewrite_rules";
int Cursor::read() {
TABLE *table = m_table_list->table;
// Read the next non-deleted record.
do {
m_last_read_status = table->file->ha_rnd_next(table->record[0]);
} while (m_last_read_status == HA_ERR_RECORD_DELETED);
if (m_last_read_status != 0) m_is_finished = true;
return m_last_read_status;
}
void free_string(const char *str) { delete[] str; }
static void add_column(MY_BITMAP *map, Cursor::column_id column) {
if (column != Cursor::ILLEGAL_COLUMN_ID) bitmap_set_bit(map, column);
}
Cursor::Cursor(THD *mysql_thd)
: m_thd(mysql_thd),
m_table_list(nullptr),
m_is_finished(true),
m_table_is_malformed(true) {
m_table_list = new Table_ref(db_name, strlen(db_name), table_name,
strlen(table_name), "alias", TL_WRITE_DEFAULT);
if (m_table_list == nullptr) return; // Error
m_table_list->updating = true;
if (open_and_lock_tables(m_thd, m_table_list,
MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY))
return; // Error
TABLE *table = m_table_list->table;
if (table == nullptr) return; // Error
m_pattern_column = field_index("pattern");
m_pattern_database_column = field_index("pattern_database");
m_replacement_column = field_index("replacement");
m_enabled_column = field_index("enabled");
// The following columns are not required for the Cursor to work.
m_message_column = field_index("message");
m_pattern_digest_column = field_index("pattern_digest");
m_normalized_pattern_column = field_index("normalized_pattern");
if (m_pattern_column == ILLEGAL_COLUMN_ID ||
m_pattern_database_column == ILLEGAL_COLUMN_ID ||
m_replacement_column == ILLEGAL_COLUMN_ID ||
m_enabled_column == ILLEGAL_COLUMN_ID) {
trans_rollback_stmt(m_thd);
close_thread_tables(m_thd);
/*
We are never going to use this table, we might as well get rid of the
reference to it.
*/
delete m_table_list;
m_table_list = nullptr;
m_table_is_malformed = true;
return; // Error
} else
m_table_is_malformed = false;
add_column(table->read_set, pattern_column());
add_column(table->read_set, pattern_database_column());
add_column(table->read_set, replacement_column());
add_column(table->read_set, enabled_column());
add_column(table->read_set, message_column());
add_column(table->write_set, enabled_column());
add_column(table->write_set, message_column());
add_column(table->write_set, pattern_digest_column());
add_column(table->write_set, normalized_pattern_column());
if (m_table_list->table->file->ha_rnd_init(true) != 0) return; // Error
// No error occurred, set this to false.
m_is_finished = false;
read();
}
const char *Cursor::fetch_string(int fieldno) {
Field **fields = m_table_list->table->field;
Field *field = fields[fieldno];
if (field->is_null()) return nullptr;
String value_buf;
String *value = field->val_str(&value_buf);
const size_t length = value->length();
char *res = new char[length + 1];
strncpy(res, value->ptr(), length);
res[length] = '\0';
return res;
}
int Cursor::field_index(const char *field_name) {
TABLE *table = m_table_list->table;
for (uint i = 0; i < table->s->fields; ++i)
if (strcmp(table->field[i]->field_name, field_name) == 0) return i;
return -1;
}
void Cursor::make_writeable() {
TABLE *table = m_table_list->table;
memcpy(table->record[1], table->record[0], table->s->rec_buff_length);
}
void Cursor::set(int colno, const char *str, size_t length) {
TABLE *table = m_table_list->table;
Field *field = table->field[colno];
const CHARSET_INFO *charset = &my_charset_utf8mb3_unicode_ci;
if (str == nullptr)
field->set_null(0);
else {
field->store(str, length, charset);
field->set_notnull(0);
}
}
int Cursor::write() {
TABLE *table = m_table_list->table;
return table->file->ha_update_row(table->record[1], table->record[0]);
}
bool Cursor::had_serious_read_error() const {
return m_last_read_status != 0 && m_last_read_status != HA_ERR_END_OF_FILE;
}
Cursor::~Cursor() {
if (m_table_list != nullptr && m_table_list->table != nullptr)
m_table_list->table->file->ha_rnd_end();
delete m_table_list;
}
Cursor end() { return Cursor(); }
} // namespace rules_table_service