1 /*
2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
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
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 
25 #ifndef PLUGIN_X_SRC_ADMIN_CMD_INDEX_FIELD_H_
26 #define PLUGIN_X_SRC_ADMIN_CMD_INDEX_FIELD_H_
27 
28 #include <string>
29 
30 #include "plugin/x/src/admin_cmd_index.h"
31 
32 namespace xpl {
33 
34 class Query_string_builder;
35 
36 class Index_field : public Admin_command_index::Index_field_interface {
37  public:
38   static const Index_field *create(
39       const bool is_virtual_allowed,
40       const Admin_command_index::Index_field_info &info,
41       ngs::Error_code *error);
42 
43   ngs::Error_code add_column_if_necessary(
44       iface::Sql_session *sql_session, const std::string &schema,
45       const std::string &collection, Query_string_builder *qb) const override;
46   void add_field(Query_string_builder *qb) const override;
is_required()47   bool is_required() const override { return m_is_required; }
48 
49   bool is_column_exists(iface::Sql_session *sql_session,
50                         const std::string &schema,
51                         const std::string &collection,
52                         ngs::Error_code *error) const;
53   void add_column(Query_string_builder *qb) const;
54 
55  protected:
56   enum class Type_id {
57     k_tinyint,
58     k_smallint,
59     k_mediumint,
60     k_int,
61     k_integer,
62     k_bigint,
63     k_real,
64     k_float,
65     k_double,
66     k_decimal,
67     k_numeric,
68     k_date,
69     k_time,
70     k_timestamp,
71     k_datetime,
72     k_year,
73     k_bit,
74     k_blob,
75     k_text,
76     k_geojson,
77     k_fulltext,
78     k_char,
79     k_unsupported = 99
80   };
81 
Index_field(const std::string & path,const bool is_required,const std::string & name,const bool is_virtual_allowed)82   Index_field(const std::string &path, const bool is_required,
83               const std::string &name, const bool is_virtual_allowed)
84       : m_path(path),
85         m_is_required(is_required),
86         m_name(name),
87         m_is_virtual_allowed(is_virtual_allowed) {}
88 
89   virtual void add_type(Query_string_builder *qb) const = 0;
90   virtual void add_path(Query_string_builder *qb) const = 0;
add_length(Query_string_builder *)91   virtual void add_length(Query_string_builder * /*qb*/) const {}
92   virtual void add_options(Query_string_builder *qb) const;
93 
94   static Type_id get_type_id(const std::string &type_name);
95 
96   const std::string m_path;
97   const bool m_is_required{false};
98   const std::string m_name;
99   const bool m_is_virtual_allowed{false};
100 };
101 }  // namespace xpl
102 
103 #endif  // PLUGIN_X_SRC_ADMIN_CMD_INDEX_FIELD_H_
104