MySQL 9.7.0
Source Code Documentation
dml.cc File Reference
#include "sql/json_duality_view/dml.h"
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <functional>
#include <memory>
#include <optional>
#include <ostream>
#include "scope_guard.h"
#include "sql/sql_error.h"
#include <concepts>
#include <ranges>
#include <type_traits>
#include "base64.h"
#include "field_types.h"
#include "mysql/strings/m_ctype.h"
#include "mysqld_error.h"
#include "sql-common/json_dom.h"
#include "sql/binlog.h"
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/item_json_func.h"
#include "sql/json_duality_view/content_tree.h"
#include "sql/json_duality_view/ostream_utils.h"
#include "sql/json_duality_view/utils.h"
#include "sql/parse_tree_nodes.h"
#include "sql/sql_class.h"
#include "sql/sql_data_change.h"
#include "sql/sql_insert.h"
#include "sql/sql_lex.h"
#include "sql/statement/statement.h"
#include "sql/table.h"
#include "sql_string.h"

Classes

struct  jdv::Size_first_comparator
 Comparator which orders in the same way as Json_object orders its keys. More...
 
struct  jdv::Json_type_traits< Json_object >
 
struct  jdv::Json_type_traits< Json_array >
 
struct  jdv::Resolve_column
 Represents columns in the base table which will be populated based on the Json document passed in. More...
 
struct  jdv::Resolve_row
 Holds the resolved values for a row of a base table. More...
 
struct  jdv::Single_object_binding
 Binds a single Json_object and a jdv::Content_tree_node. More...
 
struct  jdv::Two_object_binding
 Binds two Json_objects (existing_object and new_object) and a jdv::Content_tree_node. More...
 

Namespaces

namespace  jdv
 
namespace  jdv_unit
 

Typedefs

using jdv::Resolve_column_vec = std::vector< Resolve_column >
 
template<typename T >
using jdv::Index_entry = std::reference_wrapper< T >
 Convenience alias. More...
 
template<typename T >
using jdv::Index = std::span< Index_entry< T > >
 Convenience alias. More...
 

Enumerations

enum class  jdv::Stmt_state { jdv::EXECUTE = 0 , jdv::SKIP = 1 , jdv::ERROR = 2 }
 

Functions

static const char * jdv::em_wrap (const char *arg)
 
static int jdv::em_wrap (int arg)
 
static std::string jdv::em_wrap (std::string_view sv)
 Overload which wraps string_views in strings which are guaranteed to be null-terminated. More...
 
static std::string jdv::em_wrap (const Json_path &jp)
 Overload which wraps Json_paths in strings, after first creating string representation in String buffer. More...
 
static decltype(auto) jdv::em_unwrap (auto arg)
 Identity unwrapper. More...
 
static const char * jdv::em_unwrap (const std::string &str)
 Overload which unwraps strings by calling c_str()-. More...
 
template<int CODE, typename... Args>
static void jdv::my_jdv_error (Args... args_to_wrap)
 Wraps calls to my_error() unwrapping wrapped arguments. More...
 
template<typename JT >
static JT * jdv::inspect_valid_dom (Json_dom *valid_dom)
 Inspects an assumed valid Json_dom (typically an exising value in UPDATE). More...
 
template<typename JT >
static std::pair< enum_json_type, JT * > jdv::inspect_dom (Json_dom *dom)
 Inspects a Json_dom which may or may not have the expected type and may also be nullptr, (typically a value provided as user input). More...
 
static std::string jdv::json_wrapper_to_string (const Json_wrapper &jw)
 Formats a Json_wrapper as string for debugging. More...
 
static std::string jdv::json_dom_to_string (Json_dom *jdom)
 Formats a Json_dom as string for debugging. More...
 
static bool jdv::is_nil_dom (Json_dom *jdom)
 Predicate to determine if a dom does not represent a valid value i.e. More...
 
static bool jdv::is_auto_increment (const Field &fld)
 Convenience wrapper predicate which returns true for AUTO_INCREMENT columns. More...
 
static int jdv::compare_doms (Json_dom *ajd, Json_dom *bjd)
 Compares Json_dom pointers which may be nullptr by considering nullptrs to be greater than all non-nullptr values. More...
 
static bool jdv::expect_b64_dom (enum_field_types ft, const CHARSET_INFO &csi)
 Predicate to determine if json dom is expected to be base64 encoded based on SQL type and charset-. More...
 
static bool jdv::col_expects_b64 (const Key_column_info &kci)
 Predicate to determine if base column expects json values to be base64 encoded. More...
 
static bool jdv::append_b64_dom (std::string *sbufp, const String &buf, Json_dom *jd)
 Converts base64 string to its binary representation, escapes this to be a string literal, wraps in single quotes, prepends character set introducer _binary before appending to statement. More...
 
static bool jdv::append_json_dom (std::string *sbufp, Json_dom *jd, bool b64=false)
 Append a Json_dom to a string. More...
 
static bool jdv::do_in_substatement_context (THD *thd, auto &&f)
 Sets up the environment for using Regular_statement_handle for JDVs. More...
 
static Json_domjdv::get_val (const Json_object *jo, std::string_view k)
 Convenience function for obtaining the member of a Json_object. More...
 
static bool jdv::is_zero (Json_dom *doc)
 Convenience function for comparing a Json document to unsigned 0. More...
 
static bool jdv::auto_generate_on_zero (THD *thd)
 Convenience function for checking if the connection supports using 0 to request explicit generation of AUTO_INCREMENT values. More...
 
static bool jdv::field_will_be_auto_generated (THD *thd, const Field &fld, Json_dom *doc)
 Convenience function which checks if a Json document value inserted in a field will result in a generated AUTO_INCREMENT value. More...
 
static std::ostream & jdv::operator<< (std::ostream &os, const Resolve_column &rc)
 
static std::ostream & jdv::operator<< (std::ostream &os, const Resolve_row &rr)
 
template<typename BIN >
static int jdv::compare_bindings (const BIN &abin, const BIN &bbin)
 Compares bindings based on: More...
 
static std::ostream & jdv::operator<< (std::ostream &os, const Single_object_binding &bin)
 
static Json_domjdv::get_pk_dom (const Two_object_binding &bin)
 Returns either the existing or the resolved pk value for a Two_object_binding. More...
 
static std::ostream & jdv::operator<< (std::ostream &os, const Two_object_binding &bin)
 
template<typename T >
static int jdv::operator<=> (const Index_entry< T > &a, const Index_entry< T > &b)
 Spaceship operator which forwards to the indexed type. More...
 
template<typename T >
static bool jdv::operator== (const Index_entry< T > &a, const Index_entry< T > &b)
 Equality operator which forwards to the indexed type. More...
 
static const TABLE_SHAREjdv::get_share (const auto &bin)
 Convenience function which returns the TABLE_SHARE* for the binding. More...
 
static std::string_view jdv::get_table_cache_key (const auto &bin)
 Convenience function which returns the table_cache_key of the binding as a std::string_view. More...
 
template<typename RT , typename BIN >
static Index< RT > jdv::make_mr_index (THD *thd, BIN &bindings)
 Create an index over the bindings vector which is allocated on THD::mem_root. More...
 
static bool jdv::resolve_column_range (const std::ranges::random_access_range auto &bindings)
 Goes through the input json (bound_object) and uses the join condition to infer column values which are not provided explicitly. More...
 
static bool jdv::resolve_columns (const std::ranges::random_access_range auto &bindings)
 Iterates over a range of bindings and initializes the resolve_columns vector. More...
 
static Json_domjdv::get_pk_dom (const Single_object_binding &bin)
 Returns the resolved pk value for a Single_object binding. More...
 
static const Resolve_column & jdv::get_pk_rc (const auto &bin)
 Returns a const ref to the Resolve_row of the primary key column. More...
 
static int jdv::get_deferred_resolve_weight (const auto &bin)
 Returns an integer weight which allows sorting bindings with deferred_resolve for the primary key after non-generated values. More...
 
template<typename BV >
static bool jdv::check_for_share_pk_duplicates (THD *thd, BV &bindings)
 Creates an index over the bindings sorted on TABLE_SHARE* and pk-dom value. More...
 
static void jdv::merge_bindings_for_update (Index< Two_object_binding > &bindings)
 Merges bindings for existing and input rows when doing UPDATE. More...
 
static bool jdv::check_for_unmatched_input_keys (THD *thd, Json_object *input_obj, const Content_tree_node &ct_node)
 Check Json_object being passed in for keys which are not present in the JDV definition (including _metadata for the root object), and report error if that is the case. More...
 
static bool jdv::is_equal (Json_dom *ajd, Json_dom *bjd)
 Compare Json_doms for equality using a Json_wrapper. More...
 
static std::unique_ptr< Resolve_row > jdv::make_rr_up (Resolve_row *parent)
 Helper function to create unique_ptr to Resolve_row. More...
 
static bool jdv::push_object_child_bindings (std::size_t pbx, const Content_tree_node *child_ct_node, std::vector< Single_object_binding > *stack)
 Overload for Single_object_binding (INSERT/DELETE) with Json_object (JSON_OBJECT). More...
 
static bool jdv::push_array_child_bindings (std::size_t pbx, const Content_tree_node *child_ct_node, std::vector< Single_object_binding > *stack)
 Overload for Single_object_binding (INSERT/DELETE) with Json_array (JSON_ARRAYAGG). More...
 
static bool jdv::push_object_child_bindings (std::size_t pbx, const Content_tree_node *child_ct_node, std::vector< Two_object_binding > *stack)
 Overload for Two_object_binding (UPDATE) with Json_object (JSON_OBJECT). More...
 
static bool jdv::push_array_child_bindings (std::size_t pbx, const Content_tree_node *child_ct_node, std::vector< Two_object_binding > *stack)
 Overload for Two_object_binding (UPDATE) with Json_array (JSON_ARRAY_AGG). More...
 
template<typename BIN >
static bool jdv::flatten (std::vector< BIN > *stack)
 Takes a stack (std::vector) with a single binding at the top. More...
 
static std::optional< std::vector< bool > > jdv::select_diff_vector (THD *thd, const auto &binding)
 Check if the row represented by the binding already exists, and if so return a diff vector (true for columns which currently does not have the value of the binding). More...
 
static bool jdv::run_substmt (THD *thd, Diagnostics_area *caller_da, std::string_view stmt, ulonglong *affected_rows)
 Uses a Regular_statement_handle to execute a single statement passed as argument. More...
 
static Stmt_state jdv::make_insert (THD *thd, const auto &binding, auto *sbufp)
 Produces an INSERT statement from a binding. More...
 
static Stmt_state jdv::make_delete (THD *, const Single_object_binding &binding, auto *sbufp)
 Produces a delete statement from a Single_object_binding. More...
 
static Json_domjdv::etag_dom (Json_object *root_object)
 Obtains the dom of the etag sub-object. More...
 
static bool jdv::check_etag (const Two_object_binding &binding)
 Checks if etags in existing and bound objects match. More...
 
static Stmt_state jdv::make_update (THD *thd, const Two_object_binding &binding, auto *sbufp)
 Produces an UPDATE statement from a Two_object_binding. More...
 
static bool jdv::check_input_json (Json_dom *input_dom)
 
static bool jdv::jdv_handle_insert (THD *thd, Json_wrapper *jw, Content_tree_node *ct_node, ulonglong *affected_rows)
 Creates bindings from inserted Json_object, orders them, creates and executes INSERT statements against the base tables. More...
 
static bool jdv::jdv_handle_update (THD *thd, Json_wrapper *jw, Json_wrapper *existing, Content_tree_node *ct_node, ulonglong *affected_rows)
 Creates bindings from existing and updated Json_object, orders them, creates and executes UPDATE/INSERT/DELETE statements against the base tables. More...
 
static bool jdv::jdv_handle_delete (THD *thd, Json_wrapper *jw, Content_tree_node *ct_node, ulonglong *affected_rows)
 Creates bindings from deleted Json_object, orders them, creates and executes DELETE statements against the base tables. More...
 
static bool jdv::jdv_prepare_base (THD *thd, const Table_ref *view, bool is_single_table_plan)
 Performs common sanity checks. More...
 
bool jdv::jdv_prepare_insert (THD *thd, const Table_ref *view, Sql_cmd_insert_base *sql_insert_cmd)
 Performs sanity checks specific to insert. More...
 
bool jdv::jdv_prepare_update (THD *thd, const Table_ref *view, bool is_single_table_plan)
 Performs sanity checks specific to update. More...
 
bool jdv::jdv_prepare_delete (THD *thd, const Table_ref *view, bool is_single_table_plan)
 Performs sanity checks specific to delete. More...
 
static decltype(auto) jdv::create_sctx_guard (THD *thd, const Table_ref *dvtr)
 Installs the view's security context if it exists and creates a scope guard to restore the original context. More...
 
static bool jdv::write_binlog (THD *thd)
 Performs statement-based binlogging for DML operations on a JSON Duality view. More...
 
bool jdv::jdv_insert (THD *thd, const Table_ref *dvtr, const mem_root_deque< List_item * > &values)
 Entry point called from sql_insert.cc, bool Sql_cmd_insert_values::execute_inner(THD *thd);. More...
 
bool jdv::jdv_update (THD *thd, const Table_ref *dvtr, const mem_root_deque< Item * > *seldq, const mem_root_deque< Item * > *upddq, ulonglong *affected_rows)
 Entry point called from sql_update.cc bool Sql_cmd_update::update_single_table(THD *thd);. More...
 
bool jdv::jdv_delete (THD *thd, const Table_ref *dvtr, ulonglong *affected_rows)
 Entry point called from sql_delete.cc, bool Sql_cmd_delete::delete_from_single_table(THD *thd);. More...
 
std::string jdv_unit::test_ostream_operators ()
 Uses debug ostream operators to generate a string which can be checked in unit tests. More...
 

Variables

static constexpr std::string_view jdv::metadatakey = "_metadata"
 
static constexpr std::string_view jdv::etagkey = "etag"
 
constexpr Size_first_comparator jdv::size_less
 
static constexpr std::string_view jdv::TYPE_HEADER_PREFIX = "base64:type"
 
static constexpr std::size_t jdv::TYPE_HEADER_MAXSZ
 
static constexpr auto jdv::ERROR_INDICATOR = static_cast<std::size_t>(-1)
 
static Json_uint jdv::ZERO {0}
 Reusable object to simplify comparisons against uint 0 (explicit generation request) More...
 
static const Json_wrapper jdv::ZEROW {&ZERO, true}
 Reusable Json_wrapper around ZERO object to simplify comparisons against uint 0 (explicit generation request) More...
 
template<typename T >
static auto jdv::unwrap_index_entry
 Lambda object to unwrap an index entry. More...
 
static auto jdv::share_pk_less
 Less comparator which compares Index_entry objects based on their TABLE_SHARE and primary key column value. More...
 
constexpr std::size_t jdv::STMT_RESERVE_SIZE = 512
 
constexpr std::size_t jdv::BINDINGS_RESERVE_SIZE = 48