MySQL  8.0.20
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, 2019, Oracle and/or its affiliates. All rights reserved.
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 <string>
27 #include "my_config.h"
29 #include "nullable.h"
30 
31 namespace 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.
45 
46  /// The pattern's current database.
48 
49  /// The rewrite rule's replacement string.
51 
52  /// True if the rule is enabled.
53  bool is_enabled;
54 
55  /// The plugin's message, write-only.
57 
58  /// The pattern's digest, write-only.
60 
61  /// The normalized pattern, write-only.
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  */
69  explicit Persisted_rule(rts::Cursor *c) {
70  copy_and_set(&pattern, c, c->pattern_column());
71  copy_and_set(&pattern_db, c, c->pattern_database_column());
72  copy_and_set(&replacement, c, c->replacement_column());
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 = Mysql::Nullable<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 = Mysql::Nullable<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 = Mysql::Nullable<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 Nullable<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  */
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.
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
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:160
column_id pattern_digest_column() const
Definition: service_rules_table.h:87
column_id pattern_column() const
Definition: service_rules_table.h:80
Mysql::Nullable< std::string > normalized_pattern
The normalized pattern, write-only.
Definition: persisted_rule.h:62
void set_message(const std::string &message_arg)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:83
Persisted_rule(rts::Cursor *c)
Constructs a Persisted_rule object that copies all data into the current heap.
Definition: persisted_rule.h:69
bool is_enabled
True if the rule is enabled.
Definition: persisted_rule.h:53
void set_if_present(rts::Cursor *cursor, rts::Cursor::column_id column, Mysql::Nullable< std::string > value)
Writes a string value to the cursor&#39;s column if it exists.
Definition: persisted_rule.h:130
int column_id
Definition: service_rules_table.h:65
Mysql::Nullable< std::string > message
The plugin&#39;s message, write-only.
Definition: persisted_rule.h:56
A rule as persisted on disk.
Definition: persisted_rule.h:41
Definition: service_rules_table.h:45
int write()
Writes the row in the write buffer to the table at the current row.
Definition: rules_table_service.cc:173
column_id message_column() const
Definition: service_rules_table.h:86
void free_string(const char *str)
Frees a const char pointer allocated in the server&#39;s dynamic library using new[]. ...
Definition: rules_table_service.cc:64
Mysql::Nullable< std::string > pattern_db
The pattern&#39;s current database.
Definition: persisted_rule.h:47
Mysql::Nullable< std::string > replacement
The rewrite rule&#39;s replacement string.
Definition: persisted_rule.h:50
const T_value & value() const
Returns actual value of object.
Definition: nullable.h:88
void set_pattern_digest(const std::string &s)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:88
Mysql::Nullable< std::string > pattern
The rewrite rule&#39;s pattern string.
Definition: persisted_rule.h:44
Mysql::Nullable< std::string > pattern_digest
The pattern&#39;s digest, write-only.
Definition: persisted_rule.h:59
Writable cursor that allows reading and updating of rows in a persistent table.
Definition: service_rules_table.h:63
Plugin service that provides access to the rewrite rules table that is used by the Rewriter plugin...
void copy_and_set(Mysql::Nullable< std::string > *property, rts::Cursor *c, int colno)
Reads from a Cursor and writes to a property of type Nullable<string> after forcing a copy of the str...
Definition: persisted_rule.h:118
bool has_value() const
Returns true if object has not-NULL value assigned.
Definition: nullable.h:82
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:135
const string value("\alue\)
column_id pattern_database_column() const
Definition: service_rules_table.h:81
void set_normalized_pattern(const std::string &s)
Convenience function, may be called with a const char*.
Definition: persisted_rule.h:93
void make_writeable()
Prepares the write buffer for updating the current row.
Definition: rules_table_service.cc:155
column_id replacement_column() const
Definition: service_rules_table.h:84
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
column_id enabled_column() const
Definition: service_rules_table.h:85
column_id normalized_pattern_column() const
Definition: service_rules_table.h:88