1 #ifndef SQL_SERVERS_INCLUDED
2 #define SQL_SERVERS_INCLUDED
3 
4 /* Copyright (c) 2006, 2021, Oracle and/or its affiliates.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License, version 2.0,
8    as published by the Free Software Foundation.
9 
10    This program is also distributed with certain software (including
11    but not limited to OpenSSL) that is licensed under separate terms,
12    as designated in a particular file or component or in included license
13    documentation.  The authors of MySQL hereby grant you an additional
14    permission to link the program and your derivative works with the
15    separately licensed software that they have included with MySQL.
16 
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License, version 2.0, for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
25 
26 #include "my_global.h"                  /* uint */
27 #include "sql_cmd.h"
28 #include "sql_string.h"
29 #include "sql_alloc.h"
30 
31 class THD;
32 struct TABLE;
33 typedef struct st_mem_root MEM_ROOT;
34 
35 class FOREIGN_SERVER : public Sql_alloc
36 {
37 public:
38   char *server_name;
39   long port;
40   size_t server_name_length;
41   char *db, *scheme, *username, *password, *socket, *owner, *host, *sport;
42 
FOREIGN_SERVER()43   FOREIGN_SERVER()
44     : server_name(NULL), port(-1), server_name_length(0), db(NULL),
45     scheme(NULL), username(NULL), password(NULL), socket(NULL),
46     owner(NULL), host(NULL), sport(NULL)
47   { }
48 };
49 
50 
51 /* cache handlers */
52 bool servers_init(bool dont_read_server_table);
53 bool servers_reload(THD *thd);
54 void servers_free(bool end=0);
55 
56 /* lookup functions */
57 FOREIGN_SERVER *get_server_by_name(MEM_ROOT *mem, const char *server_name,
58                                    FOREIGN_SERVER *server_buffer);
59 
60 
61 /**
62    This class represent server options as set by the parser.
63  */
64 
65 class Server_options
66 {
67 public:
68   static const long PORT_NOT_SET= -1;
69   LEX_STRING m_server_name;
70 private:
71   long m_port;
72   LEX_STRING m_host;
73   LEX_STRING m_db;
74   LEX_STRING m_username;
75   LEX_STRING m_password;
76   LEX_STRING m_scheme;
77   LEX_STRING m_socket;
78   LEX_STRING m_owner;
79 
80 public:
set_port(long port)81   void set_port(long port)               { m_port= port; }
set_host(LEX_STRING host)82   void set_host(LEX_STRING host)         { m_host= host; }
set_db(LEX_STRING db)83   void set_db(LEX_STRING db)             { m_db= db; }
set_username(LEX_STRING username)84   void set_username(LEX_STRING username) { m_username= username; }
set_password(LEX_STRING password)85   void set_password(LEX_STRING password) { m_password= password; }
set_scheme(LEX_STRING scheme)86   void set_scheme(LEX_STRING scheme)     { m_scheme= scheme; }
set_socket(LEX_STRING socket)87   void set_socket(LEX_STRING socket)     { m_socket= socket; }
set_owner(LEX_STRING owner)88   void set_owner(LEX_STRING owner)       { m_owner= owner; }
89 
get_port()90   long get_port() const            { return m_port; }
get_host()91   const char *get_host() const     { return m_host.str; }
get_db()92   const char *get_db() const       { return m_db.str; }
get_username()93   const char *get_username() const { return m_username.str; }
get_password()94   const char *get_password() const { return m_password.str; }
get_scheme()95   const char *get_scheme() const   { return m_scheme.str; }
get_socket()96   const char *get_socket() const   { return m_socket.str; }
get_owner()97   const char *get_owner() const    { return m_owner.str; }
98 
99   /**
100      Reset all strings to NULL and port to PORT_NOT_SET.
101      This prepares the structure for being used by a new statement.
102   */
103   void reset();
104 
105   /**
106      Create a cache entry and insert it into the cache.
107 
108      @returns false if entry was created and inserted, true otherwise.
109   */
110   bool insert_into_cache() const;
111 
112   /**
113      Update a cache entry.
114 
115      @param existing  Cache entry to update
116 
117      @returns false if the entry was updated, true otherwise.
118   */
119   bool update_cache(FOREIGN_SERVER *existing) const;
120 
121   /**
122      Create a record representing these server options,
123      ready to be inserted into the mysql.servers table.
124 
125      @param table  Table to be inserted into.
126   */
127   void store_new_server(TABLE *table) const;
128 
129   /**
130      Create a record for updating a row in the mysql.servers table.
131 
132      @param table     Table to be updated.
133      @param existing  Cache entry represeting the existing values.
134   */
135   void store_altered_server(TABLE *table, FOREIGN_SERVER *existing) const;
136 };
137 
138 
139 /**
140    This class has common code for CREATE/ALTER/DROP SERVER statements.
141 */
142 
143 class Sql_cmd_common_server : public Sql_cmd
144 {
145 protected:
146   TABLE *table;
147 
Sql_cmd_common_server()148   Sql_cmd_common_server()
149     : table(NULL)
150   { }
151 
~Sql_cmd_common_server()152   virtual ~Sql_cmd_common_server()
153   { }
154 
155   /**
156      Check permissions and open the mysql.servers table.
157 
158      @param thd  Thread context
159 
160      @returns false if success, true otherwise
161   */
162   bool check_and_open_table(THD *thd);
163 };
164 
165 
166 /**
167    This class implements the CREATE SERVER statement.
168 */
169 
170 class Sql_cmd_create_server : public Sql_cmd_common_server
171 {
172   /**
173      Server_options::m_server_name contains the name of the
174      server to create. The remaining Server_options fields
175      contain options as set by the parser.
176      Unset options are NULL (or PORT_NOT_SET for port).
177   */
178   const Server_options *m_server_options;
179 
180 public:
Sql_cmd_create_server(Server_options * server_options)181   Sql_cmd_create_server(Server_options *server_options)
182     : Sql_cmd_common_server(), m_server_options(server_options)
183   { }
184 
sql_command_code()185   enum_sql_command sql_command_code() const
186   { return SQLCOM_CREATE_SERVER; }
187 
188   /**
189      Create a new server by inserting a row into the
190      mysql.server table and creating a cache entry.
191 
192      @param thd  Thread context
193 
194      @returns false if success, true otherwise
195   */
196   bool execute(THD *thd);
197 };
198 
199 
200 /**
201    This class implements the ALTER SERVER statement.
202 */
203 
204 class Sql_cmd_alter_server : public Sql_cmd_common_server
205 {
206   /**
207      Server_options::m_server_name contains the name of the
208      server to change. The remaining Server_options fields
209      contain changed options as set by the parser.
210      Unchanged options are NULL (or PORT_NOT_SET for port).
211   */
212   const Server_options *m_server_options;
213 
214 public:
Sql_cmd_alter_server(Server_options * server_options)215   Sql_cmd_alter_server(Server_options *server_options)
216     : Sql_cmd_common_server(), m_server_options(server_options)
217   { }
218 
sql_command_code()219   enum_sql_command sql_command_code() const
220   { return SQLCOM_ALTER_SERVER; }
221 
222   /**
223      Alter an existing server by updating the matching row in the
224      mysql.servers table and updating the cache entry.
225 
226      @param thd  Thread context
227 
228      @returns false if success, true otherwise
229   */
230   bool execute(THD *thd);
231 };
232 
233 
234 /**
235    This class implements the DROP SERVER statement.
236 */
237 
238 class Sql_cmd_drop_server : public Sql_cmd_common_server
239 {
240   /// Name of server to drop
241   LEX_STRING m_server_name;
242 
243   /// Is this DROP IF EXISTS?
244   bool m_if_exists;
245 
246 public:
Sql_cmd_drop_server(LEX_STRING server_name,bool if_exists)247   Sql_cmd_drop_server(LEX_STRING server_name,
248                       bool if_exists)
249     : Sql_cmd_common_server(),
250     m_server_name(server_name), m_if_exists(if_exists)
251   { }
252 
sql_command_code()253   enum_sql_command sql_command_code() const
254   { return SQLCOM_DROP_SERVER; }
255 
256   /**
257      Drop an existing server by deleting the matching row from the
258      mysql.servers table and removing the cache entry.
259 
260      @param thd  Thread context
261 
262      @returns false if success, true otherwise
263   */
264   bool execute(THD *thd);
265 };
266 
267 
268 #endif /* SQL_SERVERS_INCLUDED */
269