MySQL 8.3.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, 2023, 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 also distributed 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 included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#include <memory>
26#include <optional>
27#include <string>
28#include "my_config.h"
30
31namespace rts = rules_table_service;
32
33/**
34 @file persisted_rule.h
35
36 The facilities for easily manipulating nullable values from a
37 rules_table_service::Cursor.
38*/
39
40/// A rule as persisted on disk.
42 public:
43 /// The rewrite rule's pattern string.
44 std::optional<std::string> pattern;
45
46 /// The pattern's current database.
47 std::optional<std::string> pattern_db;
48
49 /// The rewrite rule's replacement string.
50 std::optional<std::string> replacement;
51
52 /// True if the rule is enabled.
54
55 /// The plugin's message, write-only.
56 std::optional<std::string> message;
57
58 /// The pattern's digest, write-only.
59 std::optional<std::string> pattern_digest;
60
61 /// The normalized pattern, write-only.
62 std::optional<std::string> normalized_pattern;
63
64 /**
65 Constructs a Persisted_rule object that copies all data into the current
66 heap. The interface is constructed this way due to on some OS'es
67 (e.g. Windows), every shared library has its own heap.
68 */
73
74 const char *is_enabled_c = (c->fetch_string(c->enabled_column()));
75 if (is_enabled_c != nullptr && is_enabled_c[0] == 'Y')
76 is_enabled = true;
77 else
78 is_enabled = false;
79 rts::free_string(is_enabled_c);
80 }
81
82 /// Convenience function, may be called with a const char*.
83 void set_message(const std::string &message_arg) {
84 message = std::optional<std::string>(message_arg);
85 }
86
87 /// Convenience function, may be called with a const char*.
88 void set_pattern_digest(const std::string &s) {
89 pattern_digest = std::optional<std::string>(s);
90 }
91
92 /// Convenience function, may be called with a const char*.
93 void set_normalized_pattern(const std::string &s) {
94 normalized_pattern = std::optional<std::string>(s);
95 }
96
97 /**
98 Writes the values in this Persisted_rule to the table at the row pointed
99 to by the cursor. Values that don't have a corresponding column in the
100 table will be ignored.
101 */
103 c->make_writeable();
104
108
109 return c->write();
110 }
111
112 private:
113 /**
114 Reads from a Cursor and writes to a property of type std::optional<string>
115 after forcing a copy of the string buffer. The function calls a member
116 function in Cursor that is located in the server's dynamic library.
117 */
118 void copy_and_set(std::optional<std::string> *property, rts::Cursor *c,
119 int colno) {
120 const char *value = c->fetch_string(colno);
121 if (value != nullptr) {
122 std::string tmp;
123 tmp.assign(value);
124 *property = tmp;
125 }
126 rts::free_string(value);
127 }
128
129 /// Writes a string value to the cursor's column if it exists.
131 std::optional<std::string> value) {
132 if (column == rts::Cursor::ILLEGAL_COLUMN_ID) return;
133 if (!value.has_value()) {
134 cursor->set(column, nullptr, 0);
135 return;
136 }
137 const std::string &s = value.value();
138 cursor->set(column, s.c_str(), s.length());
139 }
140};
141
142#endif // PERSISTED_RULE_INCLUDED
A rule as persisted on disk.
Definition: persisted_rule.h:41
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:130
void set_message(const std::string &message_arg)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:83
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:118
void set_pattern_digest(const std::string &s)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:88
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:102
std::optional< std::string > message
The plugin's message, write-only.
Definition: persisted_rule.h:56
std::optional< std::string > normalized_pattern
The normalized pattern, write-only.
Definition: persisted_rule.h:62
bool is_enabled
True if the rule is enabled.
Definition: persisted_rule.h:53
std::optional< std::string > pattern_digest
The pattern's digest, write-only.
Definition: persisted_rule.h:59
std::optional< std::string > replacement
The rewrite rule's replacement string.
Definition: persisted_rule.h:50
std::optional< std::string > pattern_db
The pattern's current database.
Definition: persisted_rule.h:47
void set_normalized_pattern(const std::string &s)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:93
std::optional< std::string > pattern
The rewrite rule's pattern string.
Definition: persisted_rule.h:44
Persisted_rule(rts::Cursor *c)
Constructs a Persisted_rule object that copies all data into the current heap.
Definition: persisted_rule.h:69
Writable cursor that allows reading and updating of rows in a persistent table.
Definition: service_rules_table.h:63
int write()
Writes the row in the write buffer to the table at the current row.
Definition: rules_table_service.cc:176
column_id pattern_digest_column() const
Definition: service_rules_table.h:87
column_id normalized_pattern_column() const
Definition: service_rules_table.h:88
column_id message_column() const
Definition: service_rules_table.h:86
column_id enabled_column() const
Definition: service_rules_table.h:85
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:163
static const column_id ILLEGAL_COLUMN_ID
Definition: service_rules_table.h:67
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:138
column_id pattern_column() const
Definition: service_rules_table.h:80
void make_writeable()
Prepares the write buffer for updating the current row.
Definition: rules_table_service.cc:158
column_id pattern_database_column() const
Definition: service_rules_table.h:81
column_id replacement_column() const
Definition: service_rules_table.h:84
int column_id
Definition: service_rules_table.h:65
Definition: service_rules_table.h:45
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:67
Plugin service that provides access to the rewrite rules table that is used by the Rewriter plugin.