MySQL 9.3.0
Source Code Documentation
change.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the 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 Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_JSON_MAPPER_VIEW_CHANGE_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_JSON_MAPPER_VIEW_CHANGE_H_
28
30
31#include <memory>
32#include <string>
33#include <utility>
34#include <vector>
42#include "mysqld_error.h"
43
44namespace mrs {
45namespace database {
46namespace dv {
47
48class RowInsert;
49class RowDeleteReferencing;
50
51// merge with RowChange and rename to RowChange
53 : public Query,
54 public std::enable_shared_from_this<JsonMappingUpdater::Operation> {
55 protected:
56 size_t affected_ = 0;
57
58 public:
59 std::weak_ptr<Operation> parent_;
60 std::shared_ptr<Table> table_;
62
63 public:
64 Operation(std::shared_ptr<Operation> parent, std::shared_ptr<Table> table,
65 const ObjectRowOwnership &row_ownership)
66 : parent_(parent), table_(table), row_ownership_(row_ownership) {}
67
68 Operation(std::shared_ptr<Operation> parent, std::shared_ptr<Table> table,
69 const PrimaryKeyColumnValues &pk,
70 const ObjectRowOwnership &row_ownership)
71 : parent_(parent),
73 row_ownership_(row_ownership),
74 pk_(pk) {}
75
76 virtual const PrimaryKeyColumnValues &primary_key() const { return pk_; }
77
78 virtual void run(MySQLSession *session) = 0;
79
80 const std::shared_ptr<Table> &table() const { return table_; }
81
82 std::shared_ptr<Operation> parent() const {
83 if (!parent_.expired()) return parent_.lock();
84 return {};
85 }
86
87 bool is_root() const { return parent_.expired() == true; }
88
89 size_t affected() const { return affected_; }
90
91 virtual void process(JSONInputObject input);
92
94 const PrimaryKeyColumnValues & /* parent_pk */) {}
95
96 virtual void append_match_condition(mysqlrouter::sqlstring &sql) const;
97
98 protected:
99 virtual void on_value(const Column & /* column */,
100 const JSONInputObject::MemberReference & /* value */) {}
101
102 virtual void on_no_value(const Column & /* column */,
104
106 JSONInputArray input) = 0;
107
109 JSONInputObject input) = 0;
110
111 protected:
113 bool qualify_table) const {
114 if (row_ownership_.enabled()) {
116 " ", qualify_table
119 return true;
120 }
121 return false;
122 }
123
125 std::vector<std::shared_ptr<JsonMappingUpdater::Operation>> *parents)
126 const;
127
128 void execute(MySQLSession *session) override {
129 Query::execute(session);
130 affected_ = session->affected_rows();
131 }
132
133 protected:
135};
136
137//
138
140 protected:
141 RowChangeOperation(std::shared_ptr<Operation> parent,
142 std::shared_ptr<Table> table,
143 const ObjectRowOwnership &row_ownership)
144 : Operation(parent, table, row_ownership) {}
145
146 RowChangeOperation(std::shared_ptr<Operation> parent,
147 std::shared_ptr<Table> table,
148 const PrimaryKeyColumnValues &pk,
149 const ObjectRowOwnership &row_ownership)
150 : Operation(parent, table, pk, row_ownership) {}
151
152 public:
153 bool empty() const { return columns_.empty(); }
154
156 JSONInputObject input) override;
157
158 public:
159 std::shared_ptr<RowChangeOperation> add_update_referenced_from_this(
160 const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk);
161
162 virtual std::shared_ptr<RowChangeOperation>
164 const PrimaryKeyColumnValues &pk);
165
166 std::shared_ptr<RowChangeOperation> add_update_referencing_this(
167 const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk,
168 bool error_if_not_found = true);
169
170 std::shared_ptr<RowChangeOperation> add_clear_all_referencing_this(
171 const ForeignKeyReference &fk);
172
173 std::shared_ptr<RowChangeOperation> add_insert_referenced_from_this(
174 const ForeignKeyReference &fk);
175
176 std::shared_ptr<RowChangeOperation> add_insert_referencing_this(
177 const ForeignKeyReference &fk);
178
179 std::shared_ptr<RowChangeOperation> add_upsert_referencing_this(
180 const ForeignKeyReference &fk);
181
182 std::shared_ptr<RowDeleteReferencing> add_delete_referencing_this(
183 const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk);
184
185 std::shared_ptr<RowDeleteReferencing> add_delete_all_referencing_this(
186 const ForeignKeyReference &fk);
187
188 void on_value(const Column &column,
190
191 virtual void on_value(const Column &column,
193
194 void on_no_value(const Column &column,
195 const JSONInputObject::MemberReference &) override;
196
197 // FK reference from this table to the PK of another table
198 virtual void on_referenced_row(
199 const ForeignKeyReference &fk, const JSONInputObject &input,
200 std::optional<PrimaryKeyColumnValues> child_pk);
201
202 // FK reference from another table to the PK of this
204 std::shared_ptr<RowChangeOperation> ref_op);
205
206 virtual void set_column_sql_value(const Column &column,
208
209 void set_column_value(const Column &column,
211
212 void on_parent_pk_resolved(const PrimaryKeyColumnValues &parent_pk) override;
213
215 const PrimaryKeyColumnValues &parent_pk);
216
217 protected:
218 virtual void run_before(std::shared_ptr<Operation> op) {
219 before_.push_back(op);
220 }
221
222 void cancel_before(std::shared_ptr<Operation> op) {
223 auto it = std::find(before_.begin(), before_.end(), op);
224 assert(it != before_.end());
225 before_.erase(it);
226 }
227
228 virtual void run_after(std::shared_ptr<Operation> op) {
229 after_.push_back(op);
230 }
231
232 protected:
233 std::vector<mysqlrouter::sqlstring> columns_;
234 std::vector<mysqlrouter::sqlstring> values_;
235 std::vector<bool> not_updatable_;
236 std::optional<ForeignKeyReference> pending_fk_to_parent_;
237
238 std::list<std::shared_ptr<Operation>> before_;
239 std::list<std::shared_ptr<Operation>> after_;
240};
241
242} // namespace dv
243} // namespace database
244} // namespace mrs
245
246#endif // ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_JSON_MAPPER_VIEW_CHANGE_H_
Definition: object_row_ownership.h:45
bool enabled() const
Definition: object_row_ownership.h:85
mysqlrouter::sqlstring owner_check_expr() const
Definition: object_row_ownership.h:98
Definition: query.h:37
virtual void execute(MySQLSession *session)
Definition: query.cc:91
Definition: json_input.h:157
Definition: json_input.h:61
std::shared_ptr< Operation > parent() const
Definition: change.h:82
virtual void process_to_one(const ForeignKeyReference &ref, JSONInputObject input)=0
virtual const PrimaryKeyColumnValues & primary_key() const
Definition: change.h:76
const std::shared_ptr< Table > & table() const
Definition: change.h:80
bool is_root() const
Definition: change.h:87
PrimaryKeyColumnValues pk_
Definition: change.h:134
mysqlrouter::sqlstring join_to_parent(std::vector< std::shared_ptr< JsonMappingUpdater::Operation > > *parents) const
Definition: change.cc:95
std::shared_ptr< Table > table_
Definition: change.h:60
size_t affected() const
Definition: change.h:89
const ObjectRowOwnership & row_ownership_
Definition: change.h:61
virtual void on_parent_pk_resolved(const PrimaryKeyColumnValues &)
Definition: change.h:93
virtual void on_value(const Column &, const JSONInputObject::MemberReference &)
Definition: change.h:99
virtual void append_match_condition(mysqlrouter::sqlstring &sql) const
Definition: change.cc:116
Operation(std::shared_ptr< Operation > parent, std::shared_ptr< Table > table, const PrimaryKeyColumnValues &pk, const ObjectRowOwnership &row_ownership)
Definition: change.h:68
void execute(MySQLSession *session) override
Definition: change.h:128
virtual void process_to_many(const ForeignKeyReference &ref, JSONInputArray input)=0
bool add_row_owner_check(mysqlrouter::sqlstring *sql, bool qualify_table) const
Definition: change.h:112
std::weak_ptr< Operation > parent_
Definition: change.h:59
Operation(std::shared_ptr< Operation > parent, std::shared_ptr< Table > table, const ObjectRowOwnership &row_ownership)
Definition: change.h:64
virtual void on_no_value(const Column &, const JSONInputObject::MemberReference &)
Definition: change.h:102
virtual void process(JSONInputObject input)
Definition: change.cc:47
virtual void run(MySQLSession *session)=0
void on_referencing_row(const ForeignKeyReference &fk, std::shared_ptr< RowChangeOperation > ref_op)
Definition: change.cc:357
void on_parent_pk_resolved(const PrimaryKeyColumnValues &parent_pk) override
Definition: change.cc:368
void on_value(const Column &column, const JSONInputObject::MemberReference &value) override
Definition: change.cc:279
std::shared_ptr< RowDeleteReferencing > add_delete_referencing_this(const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk)
Definition: change.cc:256
std::shared_ptr< RowChangeOperation > add_clear_all_referencing_this(const ForeignKeyReference &fk)
Definition: change.cc:206
virtual void run_before(std::shared_ptr< Operation > op)
Definition: change.h:218
RowChangeOperation(std::shared_ptr< Operation > parent, std::shared_ptr< Table > table, const ObjectRowOwnership &row_ownership)
Definition: change.h:141
virtual std::shared_ptr< RowChangeOperation > add_dummy_update_referenced_from_this(const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk)
Definition: change.cc:181
std::list< std::shared_ptr< Operation > > before_
Definition: change.h:238
std::shared_ptr< RowChangeOperation > add_insert_referencing_this(const ForeignKeyReference &fk)
Definition: change.cc:237
std::list< std::shared_ptr< Operation > > after_
Definition: change.h:239
std::vector< mysqlrouter::sqlstring > columns_
Definition: change.h:233
std::vector< mysqlrouter::sqlstring > values_
Definition: change.h:234
void set_column_value(const Column &column, const mysqlrouter::sqlstring &value)
Definition: change.cc:418
std::shared_ptr< RowChangeOperation > add_update_referencing_this(const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk, bool error_if_not_found=true)
Definition: change.cc:192
std::shared_ptr< RowChangeOperation > add_upsert_referencing_this(const ForeignKeyReference &fk)
Definition: change.cc:246
virtual void on_referenced_row(const ForeignKeyReference &fk, const JSONInputObject &input, std::optional< PrimaryKeyColumnValues > child_pk)
Definition: change.cc:323
RowChangeOperation(std::shared_ptr< Operation > parent, std::shared_ptr< Table > table, const PrimaryKeyColumnValues &pk, const ObjectRowOwnership &row_ownership)
Definition: change.h:146
void resolve_fk_to_parent(const ForeignKeyReference &fk, const PrimaryKeyColumnValues &parent_pk)
Definition: change.cc:376
virtual void run_after(std::shared_ptr< Operation > op)
Definition: change.h:228
std::shared_ptr< RowChangeOperation > add_update_referenced_from_this(const ForeignKeyReference &fk, const PrimaryKeyColumnValues &pk)
Definition: change.cc:170
void cancel_before(std::shared_ptr< Operation > op)
Definition: change.h:222
std::shared_ptr< RowDeleteReferencing > add_delete_all_referencing_this(const ForeignKeyReference &fk)
Definition: change.cc:268
void on_no_value(const Column &column, const JSONInputObject::MemberReference &) override
Definition: change.cc:313
std::optional< ForeignKeyReference > pending_fk_to_parent_
Definition: change.h:236
std::vector< bool > not_updatable_
Definition: change.h:235
void process_to_one(const ForeignKeyReference &ref, JSONInputObject input) override
Definition: change.cc:127
virtual void set_column_sql_value(const Column &column, const mysqlrouter::sqlstring &value)
Definition: change.cc:391
std::shared_ptr< RowChangeOperation > add_insert_referenced_from_this(const ForeignKeyReference &fk)
Definition: change.cc:226
bool empty() const
Definition: change.h:153
Definition: mysql_session.h:157
virtual uint64_t affected_rows() noexcept
Definition: mysql_session.cc:936
Definition: utils_sqlstring.h:67
sqlstring & append_preformatted_sep(const std::string &separator, const sqlstring &s)
Definition: utils_sqlstring.cc:489
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:39
entry::ForeignKeyReference ForeignKeyReference
Definition: select.h:54
entry::Column Column
Definition: select.h:53
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
std::map< std::string, mysqlrouter::sqlstring > PrimaryKeyColumnValues
Definition: select.h:43
Definition: authorize_manager.h:48
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
Definition: set_operation.h:33