| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h" |
| 25 | |
| 26 | #include <utility> |
| 27 | |
| 28 | #include "plugin/x/src/helper/to_string.h" |
| 29 | |
| 30 | namespace xpl { |
| 31 | namespace test { |
| 32 | |
| 33 | Identifier::Identifier(const std::string &name, |
| 34 | const std::string &schema_name) { |
| 35 | if (!name.empty()) m_base.set_name(name); |
| 36 | |
| 37 | if (!schema_name.empty()) m_base.set_schema_name(schema_name); |
| 38 | } |
| 39 | |
| 40 | Column_identifier::Column_identifier(const std::string &name, |
| 41 | const std::string &table_name, |
| 42 | const std::string &schema_name) { |
| 43 | if (!name.empty()) m_base.set_name(name); |
| 44 | |
| 45 | if (!table_name.empty()) m_base.set_table_name(table_name); |
| 46 | |
| 47 | if (!schema_name.empty()) m_base.set_schema_name(schema_name); |
| 48 | } |
| 49 | |
| 50 | Column_identifier::Column_identifier(const Document_path &path, |
| 51 | const std::string &name, |
| 52 | const std::string &table_name, |
| 53 | const std::string &schema_name) { |
| 54 | if (!name.empty()) m_base.set_name(name); |
| 55 | |
| 56 | if (!table_name.empty()) m_base.set_table_name(table_name); |
| 57 | |
| 58 | if (!schema_name.empty()) m_base.set_schema_name(schema_name); |
| 59 | |
| 60 | m_base.mutable_document_path()->CopyFrom(path); |
| 61 | } |
| 62 | |
| 63 | Scalar::Scalar(const int value) { |
| 64 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_SINT); |
| 65 | m_base.set_v_signed_int(value); |
| 66 | } |
| 67 | |
| 68 | Scalar::Scalar(const unsigned int value) { |
| 69 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_UINT); |
| 70 | m_base.set_v_unsigned_int(value); |
| 71 | } |
| 72 | |
| 73 | Scalar::Scalar(const bool value) { |
| 74 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_BOOL); |
| 75 | m_base.set_v_bool(value); |
| 76 | } |
| 77 | |
| 78 | Scalar::Scalar(const float value) { |
| 79 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_FLOAT); |
| 80 | m_base.set_v_float(value); |
| 81 | } |
| 82 | |
| 83 | Scalar::Scalar(const double value) { |
| 84 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_DOUBLE); |
| 85 | m_base.set_v_double(value); |
| 86 | } |
| 87 | |
| 88 | Scalar::Scalar(const Scalar::Octets &value) { |
| 89 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_OCTETS); |
| 90 | m_base.mutable_v_octets()->CopyFrom(value); |
| 91 | } |
| 92 | |
| 93 | Scalar::Scalar(const Scalar::String &value) { |
| 94 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_STRING); |
| 95 | m_base.mutable_v_string()->CopyFrom(value); |
| 96 | } |
| 97 | |
| 98 | Scalar::Scalar(const char *value) { |
| 99 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_STRING); |
| 100 | *m_base.mutable_v_string()->mutable_value() = value; |
| 101 | } |
| 102 | |
| 103 | Scalar::Scalar(Scalar::Null) { |
| 104 | m_base.set_type(Mysqlx::Datatypes::Scalar_Type_V_NULL); |
| 105 | } |
| 106 | |
| 107 | Scalar::String::String(const std::string &value) { m_base.set_value(value); } |
| 108 | |
| 109 | Scalar::Octets::Octets(const std::string &value, const Content_type type) { |
| 110 | m_base.set_value(value); |
| 111 | m_base.set_content_type(static_cast<uint32_t>(type)); |
| 112 | } |
| 113 | |
| 114 | Any::Any(const Scalar &scalar) { |
| 115 | m_base.set_type(Mysqlx::Datatypes::Any_Type_SCALAR); |
| 116 | m_base.mutable_scalar()->CopyFrom(scalar); |
| 117 | } |
| 118 | |
| 119 | Any::Any(const Object &obj) { |
| 120 | m_base.set_type(Mysqlx::Datatypes::Any_Type_OBJECT); |
| 121 | m_base.mutable_obj()->CopyFrom(obj); |
| 122 | } |
| 123 | |
| 124 | Any::Any(const Array &array) { |
| 125 | m_base.set_type(Mysqlx::Datatypes::Any_Type_ARRAY); |
| 126 | m_base.mutable_array()->CopyFrom(array); |
| 127 | } |
| 128 | |
| 129 | Expr::Expr(const Scalar &value) { |
| 130 | m_base.set_type(Mysqlx::Expr::Expr_Type_LITERAL); |
| 131 | m_base.mutable_literal()->CopyFrom(value); |
| 132 | } |
| 133 | |
| 134 | Expr::Expr(const Operator &oper) { |
| 135 | m_base.set_type(Mysqlx::Expr::Expr_Type_OPERATOR); |
| 136 | m_base.mutable_operator_()->CopyFrom(oper); |
| 137 | } |
| 138 | |
| 139 | Expr::Expr(const Function_call &func) { |
| 140 | m_base.set_type(Mysqlx::Expr::Expr_Type_FUNC_CALL); |
| 141 | m_base.mutable_function_call()->CopyFrom(func); |
| 142 | } |
| 143 | |
| 144 | Expr::Expr(const Column_identifier &id) { |
| 145 | m_base.set_type(Mysqlx::Expr::Expr_Type_IDENT); |
| 146 | m_base.mutable_identifier()->CopyFrom(id); |
| 147 | } |
| 148 | |
| 149 | Expr::Expr(const Object &obj) { |
| 150 | m_base.set_type(Mysqlx::Expr::Expr_Type_OBJECT); |
| 151 | m_base.mutable_object()->CopyFrom(obj); |
| 152 | } |
| 153 | |
| 154 | Expr::Expr(const Array &arr) { |
| 155 | m_base.set_type(Mysqlx::Expr::Expr_Type_ARRAY); |
| 156 | m_base.mutable_array()->CopyFrom(arr); |
| 157 | } |
| 158 | |
| 159 | Expr::Expr(const Placeholder &ph) { |
| 160 | m_base.set_type(Mysqlx::Expr::Expr_Type_PLACEHOLDER); |
| 161 | m_base.set_position(ph.value); |
| 162 | } |
| 163 | |
| 164 | Expr::Expr(const Variable &var) { |
| 165 | m_base.set_type(Mysqlx::Expr::Expr_Type_VARIABLE); |
| 166 | m_base.set_variable(var.value); |
| 167 | } |
| 168 | |
| 169 | Any::Object::Object(const std::initializer_list<Any::Object::Fld> &list) { |
| 170 | for (const Fld &f : list) { |
| 171 | Mysqlx::Datatypes::Object_ObjectField *item = m_base.add_fld(); |
| 172 | item->set_key(f.key); |
| 173 | item->mutable_value()->CopyFrom(f.value); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | Any::Object::Object(const std::string &key, Any *value) { |
| 178 | Mysqlx::Datatypes::Object_ObjectField *item = m_base.add_fld(); |
| 179 | item->set_key(key); |
| 180 | if (value) item->mutable_value()->CopyFrom(*value); |
| 181 | } |
| 182 | |
| 183 | Object::Object(const std::string &key, Expr *value) { |
| 184 | Mysqlx::Expr::Object_ObjectField *item = m_base.add_fld(); |
| 185 | item->set_key(key); |
| 186 | if (value) item->mutable_value()->CopyFrom(*value); |
| 187 | } |
| 188 | |
| 189 | Object::Object(const std::initializer_list<Fld> &list) { |
| 190 | for (const Fld &f : list) { |
| 191 | Mysqlx::Expr::Object_ObjectField *item = m_base.add_fld(); |
| 192 | item->set_key(f.key); |
| 193 | item->mutable_value()->CopyFrom(f.value); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Column::Column(const std::string &name, const std::string &alias) { |
| 198 | if (!name.empty()) m_base.set_name(name); |
| 199 | if (!alias.empty()) m_base.set_alias(alias); |
| 200 | } |
| 201 | |
| 202 | Column::Column(const Document_path &path, const std::string &name, |
| 203 | const std::string &alias) { |
| 204 | m_base.mutable_document_path()->CopyFrom(path); |
| 205 | if (!name.empty()) m_base.set_name(name); |
| 206 | if (!alias.empty()) m_base.set_alias(alias); |
| 207 | } |
| 208 | |
| 209 | Collection::Collection(const std::string &name, const std::string &schema) { |
| 210 | if (!name.empty()) m_base.set_name(name); |
| 211 | if (!schema.empty()) m_base.set_schema(schema); |
| 212 | } |
| 213 | |
| 214 | Projection::Projection(const Expr &source, const std::string &alias) { |
| 215 | m_base.mutable_source()->CopyFrom(source); |
| 216 | if (!alias.empty()) m_base.set_alias(alias); |
| 217 | } |
| 218 | |
| 219 | Order::Order(const Expr &expr, const Direction dir) { |
| 220 | m_base.mutable_expr()->CopyFrom(expr); |
| 221 | m_base.set_direction(dir); |
| 222 | } |
| 223 | |
| 224 | Limit::Limit(const uint64_t row_count, const uint64_t offset) { |
| 225 | if (row_count > 0) m_base.set_row_count(row_count); |
| 226 | if (offset > 0) m_base.set_offset(offset); |
| 227 | } |
| 228 | |
| 229 | Limit_expr::Limit_expr(const Expr &row_count) { |
| 230 | m_base.mutable_row_count()->CopyFrom(row_count); |
| 231 | } |
| 232 | |
| 233 | Limit_expr::Limit_expr(const Expr &row_count, const Expr &offset) { |
| 234 | m_base.mutable_row_count()->CopyFrom(row_count); |
| 235 | m_base.mutable_offset()->CopyFrom(offset); |
| 236 | } |
| 237 | |
| 238 | Update_operation::Update_operation(const Update_type &update_type, |
| 239 | const Column_identifier &source, |
| 240 | const Expr &value) { |
| 241 | m_base.set_operation(update_type); |
| 242 | m_base.mutable_source()->CopyFrom(source); |
| 243 | m_base.mutable_value()->CopyFrom(value); |
| 244 | } |
| 245 | |
| 246 | Update_operation::Update_operation(const Update_type &update_type, |
| 247 | const Document_path &source, |
| 248 | const Expr &value) { |
| 249 | m_base.set_operation(update_type); |
| 250 | m_base.mutable_source()->CopyFrom(Column_identifier(source)); |
| 251 | m_base.mutable_value()->CopyFrom(value); |
| 252 | } |
| 253 | |
| 254 | Update_operation::Update_operation(const Update_type &update_type, |
| 255 | const Document_path &source) { |
| 256 | m_base.set_operation(update_type); |
| 257 | m_base.mutable_source()->CopyFrom(Column_identifier(source)); |
| 258 | } |
| 259 | |
| 260 | Update_operation::Update_operation(const Update_type &update_type, |
| 261 | const Column_identifier &source) { |
| 262 | m_base.set_operation(update_type); |
| 263 | m_base.mutable_source()->CopyFrom(source); |
| 264 | } |
| 265 | |
| 266 | } // namespace test |
| 267 | } // namespace xpl |