1 /*
2 * Copyright (c) 2015, 2021, 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 also distributed 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 included with MySQL.
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 along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "insert_statement_builder.h"
26 #include "ngs_common/protocol_protobuf.h"
27 #include "xpl_error.h"
28
29
build(const Insert & msg) const30 void xpl::Insert_statement_builder::build(const Insert &msg) const
31 {
32 m_builder.put("INSERT INTO ");
33 add_collection(msg.collection());
34 add_projection(msg.projection(), is_table_data_model(msg));
35 add_values(msg.row(), is_table_data_model(msg) ? msg.projection().size() : 1);
36 }
37
38
add_projection(const Projection_list & projection,const bool is_relational) const39 void xpl::Insert_statement_builder::add_projection(const Projection_list &projection,
40 const bool is_relational) const
41 {
42 if (is_relational)
43 {
44 if (projection.size() != 0)
45 m_builder.put(" (")
46 .put_list(projection, ngs::bind(&Generator::put_identifier, m_builder,
47 ngs::bind(&Mysqlx::Crud::Column::name, ngs::placeholders::_1)))
48 .put(")");
49 }
50 else
51 {
52 if (projection.size() != 0)
53 throw ngs::Error_code(ER_X_BAD_PROJECTION, "Invalid projection for document operation");
54 m_builder.put(" (doc)");
55 }
56 }
57
58
add_values(const Row_list & values,const int projection_size) const59 void xpl::Insert_statement_builder::add_values(const Row_list &values, const int projection_size) const
60 {
61 if (values.size() == 0)
62 throw ngs::Error_code(ER_X_MISSING_ARGUMENT, "Missing row data for Insert");
63
64 m_builder.put(" VALUES ")
65 .put_list(values, ngs::bind(&Insert_statement_builder::add_row, this,
66 ngs::bind(&Insert_statement_builder::get_row_fields, this, ngs::placeholders::_1),
67 projection_size));
68 }
69
70
add_row(const Field_list & row,const int projection_size) const71 void xpl::Insert_statement_builder::add_row(const Field_list &row, const int projection_size) const
72 {
73 if ((row.size() == 0) || (projection_size && row.size() != projection_size))
74 throw ngs::Error_code(ER_X_BAD_INSERT_DATA, "Wrong number of fields in row being inserted");
75
76 m_builder.put("(").put_list(row, &Generator::put_expr).put(")");
77 }
78