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 "statement_builder.h"
26 #include "ngs_common/protocol_protobuf.h"
27 #include "xpl_error.h"
28 #include "ngs_common/bind.h"
29
30
add_collection(const Collection & collection) const31 void xpl::Statement_builder::add_collection(const Collection &collection) const
32 {
33 if (!collection.has_name() || collection.name().empty())
34 throw ngs::Error_code(ER_X_BAD_TABLE, "Invalid name of table/collection");
35
36 if (collection.has_schema() && !collection.schema().empty())
37 m_builder.put_identifier(collection.schema()).dot();
38
39 m_builder.put_identifier(collection.name());
40 }
41
42
add_filter(const Filter & filter) const43 void xpl::Crud_statement_builder::add_filter(const Filter &filter) const
44 {
45 if (filter.IsInitialized())
46 m_builder.put(" WHERE ").put_expr(filter);
47 }
48
49
add_order_item(const Order_item & item) const50 void xpl::Crud_statement_builder::add_order_item(const Order_item &item) const
51 {
52 m_builder.put_expr(item.expr());
53 if (item.direction() == ::Mysqlx::Crud::Order::DESC)
54 m_builder.put(" DESC");
55 }
56
57
add_order(const Order_list & order) const58 void xpl::Crud_statement_builder::add_order(const Order_list &order) const
59 {
60 if (order.size() == 0)
61 return;
62
63 m_builder.put(" ORDER BY ")
64 .put_list(order, ngs::bind(&Crud_statement_builder::add_order_item,
65 this, ngs::placeholders::_1));
66 }
67
68
add_limit(const Limit & limit,const bool no_offset) const69 void xpl::Crud_statement_builder::add_limit(const Limit &limit,
70 const bool no_offset) const
71 {
72 if (!limit.IsInitialized())
73 return;
74
75 m_builder.put(" LIMIT ");
76 if (limit.has_offset())
77 {
78 if (no_offset && limit.offset() != 0)
79 throw ngs::Error_code(ER_X_INVALID_ARGUMENT,
80 "Invalid parameter: non-zero offset "
81 "value not allowed for this operation");
82 if (!no_offset)
83 m_builder.put(limit.offset()).put(", ");
84 }
85 m_builder.put(limit.row_count());
86 }
87