MySQL 8.4.0
Source Code Documentation
persisted_rule.h
Go to the documentation of this file.
1#ifndef PERSISTED_RULE_INCLUDED
2#define PERSISTED_RULE_INCLUDED
3/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <memory>
27#include <optional>
28#include <string>
29#include "my_config.h"
31
32namespace rts = rules_table_service;
33
34/**
35 @file persisted_rule.h
36
37 The facilities for easily manipulating nullable values from a
38 rules_table_service::Cursor.
39*/
40
41/// A rule as persisted on disk.
43 public:
44 /// The rewrite rule's pattern string.
45 std::optional<std::string> pattern;
46
47 /// The pattern's current database.
48 std::optional<std::string> pattern_db;
49
50 /// The rewrite rule's replacement string.
51 std::optional<std::string> replacement;
52
53 /// True if the rule is enabled.
55
56 /// The plugin's message, write-only.
57 std::optional<std::string> message;
58
59 /// The pattern's digest, write-only.
60 std::optional<std::string> pattern_digest;
61
62 /// The normalized pattern, write-only.
63 std::optional<std::string> normalized_pattern;
64
65 /**
66 Constructs a Persisted_rule object that copies all data into the current
67 heap. The interface is constructed this way due to on some OS'es
68 (e.g. Windows), every shared library has its own heap.
69 */
74
75 const char *is_enabled_c = (c->fetch_string(c->enabled_column()));
76 if (is_enabled_c != nullptr && is_enabled_c[0] == 'Y')
77 is_enabled = true;
78 else
79 is_enabled = false;
80 rts::free_string(is_enabled_c);
81 }
82
83 /// Convenience function, may be called with a const char*.
84 void set_message(const std::string &message_arg) {
85 message = std::optional<std::string>(message_arg);
86 }
87
88 /// Convenience function, may be called with a const char*.
89 void set_pattern_digest(const std::string &s) {
90 pattern_digest = std::optional<std::string>(s);
91 }
92
93 /// Convenience function, may be called with a const char*.
94 void set_normalized_pattern(const std::string &s) {
95 normalized_pattern = std::optional<std::string>(s);
96 }
97
98 /**
99 Writes the values in this Persisted_rule to the table at the row pointed
100 to by the cursor. Values that don't have a corresponding column in the
101 table will be ignored.
102 */
104 c->make_writeable();
105
109
110 return c->write();
111 }
112
113 private:
114 /**
115 Reads from a Cursor and writes to a property of type std::optional<string>
116 after forcing a copy of the string buffer. The function calls a member
117 function in Cursor that is located in the server's dynamic library.
118 */
119 void copy_and_set(std::optional<std::string> *property, rts::Cursor *c,
120 int colno) {
121 const char *value = c->fetch_string(colno);
122 if (value != nullptr) {
123 std::string tmp;
124 tmp.assign(value);
125 *property = tmp;
126 }
127 rts::free_string(value);
128 }
129
130 /// Writes a string value to the cursor's column if it exists.
132 std::optional<std::string> value) {
133 if (column == rts::Cursor::ILLEGAL_COLUMN_ID) return;
134 if (!value.has_value()) {
135 cursor->set(column, nullptr, 0);
136 return;
137 }
138 const std::string &s = value.value();
139 cursor->set(column, s.c_str(), s.length());
140 }
141};
142
143#endif // PERSISTED_RULE_INCLUDED
A rule as persisted on disk.
Definition: persisted_rule.h:42
void set_if_present(rts::Cursor *cursor, rts::Cursor::column_id column, std::optional< std::string > value)
Writes a string value to the cursor's column if it exists.
Definition: persisted_rule.h:131
void set_message(const std::string &message_arg)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:84
void copy_and_set(std::optional< std::string > *property, rts::Cursor *c, int colno)
Reads from a Cursor and writes to a property of type std::optional<string> after forcing a copy of th...
Definition: persisted_rule.h:119
void set_pattern_digest(const std::string &s)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:89
bool write_to(rts::Cursor *c)
Writes the values in this Persisted_rule to the table at the row pointed to by the cursor.
Definition: persisted_rule.h:103
std::optional< std::string > message
The plugin's message, write-only.
Definition: persisted_rule.h:57
std::optional< std::string > normalized_pattern
The normalized pattern, write-only.
Definition: persisted_rule.h:63
bool is_enabled
True if the rule is enabled.
Definition: persisted_rule.h:54
std::optional< std::string > pattern_digest
The pattern's digest, write-only.
Definition: persisted_rule.h:60
std::optional< std::string > replacement
The rewrite rule's replacement string.
Definition: persisted_rule.h:51
std::optional< std::string > pattern_db
The pattern's current database.
Definition: persisted_rule.h:48
void set_normalized_pattern(const std::string &s)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:94
std::optional< std::string > pattern
The rewrite rule's pattern string.
Definition: persisted_rule.h:45
Persisted_rule(rts::Cursor *c)
Constructs a Persisted_rule object that copies all data into the current heap.
Definition: persisted_rule.h:70
Writable cursor that allows reading and updating of rows in a persistent table.
Definition: service_rules_table.h:64
int write()
Writes the row in the write buffer to the table at the current row.
Definition: rules_table_service.cc:177
column_id pattern_digest_column() const
Definition: service_rules_table.h:88
column_id normalized_pattern_column() const
Definition: service_rules_table.h:89
column_id message_column() const
Definition: service_rules_table.h:87
column_id enabled_column() const
Definition: service_rules_table.h:86
void set(int colno, const char *str, size_t length)
Sets the value of column colno to a string value.
Definition: rules_table_service.cc:164
static const column_id ILLEGAL_COLUMN_ID
Definition: service_rules_table.h:68
const char * fetch_string(int fieldno)
Fetches the value of the column with the given number as a C string.
Definition: rules_table_service.cc:139
column_id pattern_column() const
Definition: service_rules_table.h:81
void make_writeable()
Prepares the write buffer for updating the current row.
Definition: rules_table_service.cc:159
column_id pattern_database_column() const
Definition: service_rules_table.h:82
column_id replacement_column() const
Definition: service_rules_table.h:85
int column_id
Definition: service_rules_table.h:66
Definition: service_rules_table.h:46
void free_string(const char *str)
Frees a const char pointer allocated in the server's dynamic library using new[].
Definition: rules_table_service.cc:68
Plugin service that provides access to the rewrite rules table that is used by the Rewriter plugin.