aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libmysql_r/include/mysql/service_command.h
blob: ea1f7752259c0318b2d5110607c785e115de5152 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#ifndef MYSQL_SERVICE_COMMAND_INCLUDED
#define MYSQL_SERVICE_COMMAND_INCLUDED
/*  Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2.0,
    as published by the Free Software Foundation.

    This program is also distributed with certain software (including
    but not limited to OpenSSL) that is licensed under separate terms,
    as designated in a particular file or component or in included license
    documentation.  The authors of MySQL hereby grant you an additional
    permission to link the program and your derivative works with the
    separately licensed software that they have included with MySQL.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License, version 2.0, for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

/**
  @file include/mysql/service_command.h
  Header file for the Command service. This service is to provide means
  of executing different commands, like COM_QUERY, COM_STMT_PREPARE,
  in the server.
*/

#include "mysql/com_data.h"
#include "mysql/service_srv_session.h"

#include "decimal.h"
#include "mysql_time.h"
#ifndef MYSQL_ABI_CHECK
#include <stdint.h> /* uint32_t */
#include "field_types.h"
#include "m_ctype.h"
#endif

/* POD structure for the field metadata from the server */
struct st_send_field {
  const char *db_name;
  const char *table_name;
  const char *org_table_name;
  const char *col_name;
  const char *org_col_name;
  unsigned long length;
  unsigned int charsetnr;
  unsigned int flags;
  unsigned int decimals;
  enum_field_types type;
};

/**
  Indicates beginning of metadata for the result set

  @param ctx      Plugin's context
  @param num_cols Number of fields being sent
  @param flags    Flags to alter the metadata sending
  @param resultcs Charset of the result set

  @note resultcs is the charset in which the data should be encoded before
  sent to the client. This is the value of the session variable
  character_set_results. The implementor most probably will need to save
  this value in the context and use it as "to" charset in get_string().

  In case of CS_BINARY_REPRESENTATION, get_string() receives as a parameter
  the charset of the string, as it is stored on disk.

  In case of CS_TEXT_REPRESENTATION, the string value might be already a
  stringified value or non-string data, which is in character_set_results.

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*start_result_metadata_t)(void *ctx, uint num_cols, uint flags,
                                       const CHARSET_INFO *resultcs);

/**
  Field metadata is provided via this callback

  @param ctx     Plugin's context
  @param field   Field's metadata (see field.h)
  @param charset Field's charset

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*field_metadata_t)(void *ctx, struct st_send_field *field,
                                const CHARSET_INFO *charset);

/**
  Indicates end of metadata for the result set

  @param ctx            Plugin's context
  @param server_status  Status of server (see mysql_com.h, SERVER_STATUS_*)
  @param warn_count     Number of warnings generated during execution to the
                          moment when the metadata is sent.
  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*end_result_metadata_t)(void *ctx, uint server_status,
                                     uint warn_count);

/**
  Indicates the beginning of a new row in the result set/metadata

  @param ctx   Plugin's context

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*start_row_t)(void *ctx);

/**
  Indicates the end of the current row in the result set/metadata

  @param ctx   Plugin's context

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*end_row_t)(void *ctx);

/**
  An error occurred during execution

  This callback indicates that an error occurred during command
  execution and the partial row should be dropped. Server will raise error
  and return.

  @param ctx   Plugin's context
*/
typedef void (*abort_row_t)(void *ctx);

/**
  Return client's capabilities (see mysql_com.h, CLIENT_*)

  @param ctx     Plugin's context

  @return Bitmap of client's capabilities
*/
typedef ulong (*get_client_capabilities_t)(void *ctx);

/**
  Receive NULL value from server

  @param ctx  Plugin's context

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*get_null_t)(void *ctx);

/**
  Receive TINY/SHORT/LONG value from server

  @param ctx           Plugin's context
  @param value         Value received

  @note In order to know which type exactly was received, the plugin must
  track the metadata that was sent just prior to the result set.

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*get_integer_t)(void *ctx, longlong value);

/**
  Get LONGLONG value from server

  @param ctx           Plugin's context
  @param value         Value received
  @param is_unsigned   TRUE <=> value is unsigned

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*get_longlong_t)(void *ctx, longlong value, uint is_unsigned);

/**
  Receive DECIMAL value from server

  @param ctx   Plugin's context
  @param value Value received

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*get_decimal_t)(void *ctx, const decimal_t *value);

/**
  Receive FLOAT/DOUBLE from server

  @param ctx      Plugin's context
  @param value    Value received
  @param decimals Number of decimals

  @note In order to know which type exactly was received, the plugin must
  track the metadata that was sent just prior to the result set.

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*get_double_t)(void *ctx, double value, uint32_t decimals);

/**
  Get DATE value from server

  @param ctx      Plugin's context
  @param value    Value received

  @returns
    1  an error occurred during storing, server will abort the command
    0  ok
*/
typedef int (*get_date_t)(void *ctx, const MYSQL_TIME *value);

/**
  Receive TIME value from server

  @param ctx      Plugin's context
  @param value    Value received
  @param decimals Number of decimals

  @returns
    1  an error occurred during storing, server will abort the command
    0  ok
*/
typedef int (*get_time_t)(void *ctx, const MYSQL_TIME *value, uint decimals);

/**
  Receive DATETIME value from server

  @param ctx      Plugin's context
  @param value    Value received
  @param decimals Number of decimals

  @returns
    1  an error occurred during storing, server will abort the command
    0  ok
*/
typedef int (*get_datetime_t)(void *ctx, const MYSQL_TIME *value,
                              uint decimals);

/**
  Get STRING value from server

  @param ctx     Plugin's context
  @param value   Data
  @param length  Data length
  @param valuecs Data charset

  @note In case of CS_BINARY_REPRESENTATION, get_string() receives as
  a parameter the charset of the string, as it is stored on disk.

  In case of CS_TEXT_REPRESENTATION, the string value might be already a
  stringified value or non-string data, which is in character_set_results.

  @see start_result_metadata()

  @returns
    1  an error occurred, server will abort the command
    0  ok
*/
typedef int (*get_string_t)(void *ctx, const char *value, size_t length,
                            const CHARSET_INFO *valuecs);

/**
  Command ended with success

  @param ctx                  Plugin's context
  @param server_status        Status of server (see mysql_com.h,
                              SERVER_STATUS_*)
  @param statement_warn_count Number of warnings thrown during execution
  @param affected_rows        Number of rows affected by the command
  @param last_insert_id       Last insert id being assigned during execution
  @param message              A message from server
*/
typedef void (*handle_ok_t)(void *ctx, uint server_status,
                            uint statement_warn_count, ulonglong affected_rows,
                            ulonglong last_insert_id, const char *message);

/**
  Command ended with ERROR

  @param ctx       Plugin's context
  @param sql_errno Error code
  @param err_msg   Error message
  @param sqlstate  SQL state corresponding to the error code
*/
typedef void (*handle_error_t)(void *ctx, uint sql_errno, const char *err_msg,
                               const char *sqlstate);

/**
  Callback for shutdown notification from the server.

  @param ctx              Plugin's context
  @param server_shutdown  Whether this is a normal connection shutdown (0) or
                          server shutdown (1).
*/
typedef void (*shutdown_t)(void *ctx, int server_shutdown);

struct st_command_service_cbs {
  /*
    For a statement that returns a result, the flow of called callbacks will be:

    start_result_metadata()
      field_metadata()
      ....
    end_result_metadata() (in the classic protocol this generates an EOF packet)
    start_row()
      get_xxx()
      ...
    end_row()
    start_row()
      get_xxx()
      ...
    end_row()
    handle_ok()           (with data for an EOF packet)

    For a statement that does NOT return a result, but only status, like
    INSERT, UPDATE, DELETE, REPLACE, TRUNCATE, CREATE, DROP, ALTER, etc. only
    handle_ok() will be invoked, in case of success.

    All statements that result in an error will invoke handle_error().

    For statements that return a result set, handle_error() might be invoked
    even after metadata was sent. This will indicate an error during the
    execution of the statement.
  */

  /* Getting metadata */

  start_result_metadata_t start_result_metadata;
  field_metadata_t field_metadata;
  end_result_metadata_t end_result_metadata;
  start_row_t start_row;
  end_row_t end_row;
  abort_row_t abort_row;
  get_client_capabilities_t get_client_capabilities;

  /* Getting data */

  get_null_t get_null;
  get_integer_t get_integer;
  get_longlong_t get_longlong;
  get_decimal_t get_decimal;
  get_double_t get_double;
  get_date_t get_date;
  get_time_t get_time;
  get_datetime_t get_datetime;
  get_string_t get_string;

  /* Getting execution status */

  handle_ok_t handle_ok;
  handle_error_t handle_error;
  shutdown_t shutdown;
};

enum cs_text_or_binary {
  CS_TEXT_REPRESENTATION = 1, /* Let the server convert everything to string */
  CS_BINARY_REPRESENTATION = 2, /* Let the server use native types */
};

extern "C" struct command_service_st {
  int (*run_command)(MYSQL_SESSION session, enum enum_server_command command,
                     const union COM_DATA *data, const CHARSET_INFO *client_cs,
                     const struct st_command_service_cbs *callbacks,
                     enum cs_text_or_binary text_or_binary,
                     void *service_callbacks_ctx);
} * command_service;

#ifdef MYSQL_DYNAMIC_PLUGIN

#define command_service_run_command(t, command, data, cset, cbs, t_or_b, ctx) \
  command_service->run_command((t), (command), (data), (cset), (cbs),         \
                               (t_or_b), (ctx))
#else

/**
  Executes a server command in a session.


  There are two cases. Execution in a physical thread :
  1. initialized by the srv_session service
  2. NOT initialized by the srv_session service

  In case of 1, if there is currently attached session, and it is
  different from the passed one, the former will be automatically
  detached. The session to be used for the execution will then be
  attached. After the command is executed, the attached session will
  not be detached. It will be detached by a next call to run_command()
  with another session as parameter.  In other words, for all sessions
  used in a physical thread, there will be at most one in attached
  state.

  In case of 2, the current state (current_thd) will be
  preserved. Then the given session will move to attached state and
  the command will be executed. After the execution the state of the
  session will be changed to detached and the preserved state
  (current_thd) will be restored.

  The client charset is used for commands like COM_QUERY and
  COM_STMT_PREPARE to know how to threat the char* fields. This
  charset will be used until the next call of run_command when it may
  be changed again.

  @param session  The session
  @param command  The command to be executed.
  @param data     The data needed for the command to be executed
  @param client_cs The charset for the string data input(COM_QUERY for example)
  @param callbacks Callbacks to be used by the server to encode data and
                   to communicate with the client (plugin) side.
  @param text_or_binary Select which representation the server will use for the
                        data passed to the callbacks. For more information
                        @see cs_text_or_binary enum
  @param service_callbacks_ctx  Context passed to the command service callbacks

  @return
    0 success
    1 failure
*/
int command_service_run_command(MYSQL_SESSION session,
                                enum enum_server_command command,
                                const union COM_DATA *data,
                                const CHARSET_INFO *client_cs,
                                const struct st_command_service_cbs *callbacks,
                                enum cs_text_or_binary text_or_binary,
                                void *service_callbacks_ctx);

#endif /* MYSQL_DYNAMIC_PLUGIN */

#endif