summaryrefslogtreecommitdiffstats
path: root/storage/federatedx/federatedx_io_mysql.cc
blob: d420de738b92aeada95bc596bcb4efb7f3e20cbd (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
Copyright (c) 2007, Antony T Curtis
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

    * Neither the name of FederatedX nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#define MYSQL_SERVER 1
#include <my_global.h>
#include "sql_priv.h"
#include <mysqld_error.h>

#include "ha_federatedx.h"

#include "m_string.h"
#include "mysqld_error.h"
#include "sql_servers.h"

#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation                          // gcc: Class implementation
#endif


#define SAVEPOINT_REALIZED  1
#define SAVEPOINT_RESTRICT  2
#define SAVEPOINT_EMITTED 4


typedef struct federatedx_savepoint
{
  ulong level;
  uint  flags;
} SAVEPT;

struct mysql_position
{
  MYSQL_RES* result;
  MYSQL_ROW_OFFSET offset;
};


class federatedx_io_mysql :public federatedx_io
{
  MYSQL mysql; /* MySQL connection */
  DYNAMIC_ARRAY savepoints;
  bool requested_autocommit;
  bool actual_autocommit;

  int actual_query(const char *buffer, size_t length);
  bool test_all_restrict() const;
public:
  federatedx_io_mysql(FEDERATEDX_SERVER *);
  ~federatedx_io_mysql();

  int simple_query(const char *fmt, ...);
  int query(const char *buffer, size_t length);
  virtual FEDERATEDX_IO_RESULT *store_result();

  virtual size_t max_query_size() const;

  virtual my_ulonglong affected_rows() const;
  virtual my_ulonglong last_insert_id() const;

  virtual int error_code();
  virtual const char *error_str();

  void reset();
  int commit();
  int rollback();

  int savepoint_set(ulong sp);
  ulong savepoint_release(ulong sp);
  ulong savepoint_rollback(ulong sp);
  void savepoint_restrict(ulong sp);

  ulong last_savepoint() const;
  ulong actual_savepoint() const;
  bool is_autocommit() const;

  bool table_metadata(ha_statistics *stats, const char *table_name,
                      uint table_name_length, uint flag);

  /* resultset operations */

  virtual void free_result(FEDERATEDX_IO_RESULT *io_result);
  virtual unsigned int get_num_fields(FEDERATEDX_IO_RESULT *io_result);
  virtual my_ulonglong get_num_rows(FEDERATEDX_IO_RESULT *io_result);
  virtual FEDERATEDX_IO_ROW *fetch_row(FEDERATEDX_IO_RESULT *io_result,
                                       FEDERATEDX_IO_ROWS **current= NULL);
  virtual ulong *fetch_lengths(FEDERATEDX_IO_RESULT *io_result);
  virtual const char *get_column_data(FEDERATEDX_IO_ROW *row,
                                      unsigned int column);
  virtual bool is_column_null(const FEDERATEDX_IO_ROW *row,
                              unsigned int column) const;

  virtual size_t get_ref_length() const;
  virtual void mark_position(FEDERATEDX_IO_RESULT *io_result,
                             void *ref, FEDERATEDX_IO_ROWS *current);
  virtual int seek_position(FEDERATEDX_IO_RESULT **io_result,
                            const void *ref);
  virtual void set_thd(void *thd);
};


federatedx_io *instantiate_io_mysql(MEM_ROOT *server_root,
                                    FEDERATEDX_SERVER *server)
{
  return new (server_root) federatedx_io_mysql(server);
}


federatedx_io_mysql::federatedx_io_mysql(FEDERATEDX_SERVER *aserver)
  : federatedx_io(aserver),
    requested_autocommit(TRUE), actual_autocommit(TRUE)
{
  DBUG_ENTER("federatedx_io_mysql::federatedx_io_mysql");

  bzero(&mysql, sizeof(MYSQL));
  bzero(&savepoints, sizeof(DYNAMIC_ARRAY));

  my_init_dynamic_array(PSI_INSTRUMENT_ME, &savepoints, sizeof(SAVEPT), 16, 16, MYF(0));
  
  DBUG_VOID_RETURN;
}


federatedx_io_mysql::~federatedx_io_mysql()
{
  DBUG_ENTER("federatedx_io_mysql::~federatedx_io_mysql");

  mysql_close(&mysql);
  delete_dynamic(&savepoints);

  DBUG_VOID_RETURN;
}


void federatedx_io_mysql::reset()
{
  reset_dynamic(&savepoints);
  set_active(FALSE);
  
  requested_autocommit= TRUE;
  mysql.reconnect= 1;
}


int federatedx_io_mysql::commit()
{
  int error= 0;
  DBUG_ENTER("federatedx_io_mysql::commit");
  
  if (!actual_autocommit && (error= actual_query("COMMIT", 6)))
    rollback();
  
  reset();
  
  DBUG_RETURN(error);
}

int federatedx_io_mysql::rollback()
{
  int error= 0;
  DBUG_ENTER("federatedx_io_mysql::rollback");
  
  if (!actual_autocommit)
    error= actual_query("ROLLBACK", 8);
  else
    error= ER_WARNING_NOT_COMPLETE_ROLLBACK;

  reset();
  
  DBUG_RETURN(error);
}


ulong federatedx_io_mysql::last_savepoint() const
{
  SAVEPT *savept= NULL;
  DBUG_ENTER("federatedx_io_mysql::last_savepoint");

  if (savepoints.elements)
    savept= dynamic_element(&savepoints, savepoints.elements - 1, SAVEPT *);

  DBUG_RETURN(savept ? savept->level : 0);
}


ulong federatedx_io_mysql::actual_savepoint() const
{
  SAVEPT *savept= NULL;
  uint index= savepoints.elements;
  DBUG_ENTER("federatedx_io_mysql::last_savepoint");

  while (index)
  {
    savept= dynamic_element(&savepoints, --index, SAVEPT *);
    if (savept->flags & SAVEPOINT_REALIZED)
    break;
  savept= NULL;
  }

  DBUG_RETURN(savept ? savept->level : 0);
}

bool federatedx_io_mysql::is_autocommit() const
{
  return actual_autocommit;
}


int federatedx_io_mysql::savepoint_set(ulong sp)
{
  int error;
  SAVEPT savept;
  DBUG_ENTER("federatedx_io_mysql::savepoint_set");
  DBUG_PRINT("info",("savepoint=%lu", sp));
  DBUG_ASSERT(sp > last_savepoint());

  savept.level= sp;
  savept.flags= 0;

  if ((error= insert_dynamic(&savepoints, (uchar*) &savept) ? -1 : 0))
    goto err;

  set_active(TRUE);
  mysql.reconnect= 0;
  requested_autocommit= FALSE;

err:
  DBUG_RETURN(error);
}


ulong federatedx_io_mysql::savepoint_release(ulong sp)
{
  SAVEPT *savept, *last= NULL;
  DBUG_ENTER("federatedx_io_mysql::savepoint_release");
  DBUG_PRINT("info",("savepoint=%lu", sp));
  
  while (savepoints.elements)
  {
    savept= dynamic_element(&savepoints, savepoints.elements - 1, SAVEPT *);
    if (savept->level < sp)
      break;
    if ((savept->flags & (SAVEPOINT_REALIZED | SAVEPOINT_RESTRICT)) == SAVEPOINT_REALIZED)
      last= savept;
    savepoints.elements--;
  }

  if (last)
  {
    char buffer[STRING_BUFFER_USUAL_SIZE];
    size_t length= my_snprintf(buffer, sizeof(buffer),
              "RELEASE SAVEPOINT save%lu", last->level);
    actual_query(buffer, length);
  }

  DBUG_RETURN(last_savepoint()); 
}


ulong federatedx_io_mysql::savepoint_rollback(ulong sp)
{
  SAVEPT *savept;
  uint index;
  DBUG_ENTER("federatedx_io_mysql::savepoint_release");
  DBUG_PRINT("info",("savepoint=%lu", sp));
  
  while (savepoints.elements)
  {
    savept= dynamic_element(&savepoints, savepoints.elements - 1, SAVEPT *);
    if (savept->level <= sp)
      break;
    savepoints.elements--;
  }

  for (index= savepoints.elements, savept= NULL; index;)
  {
    savept= dynamic_element(&savepoints, --index, SAVEPT *);
    if (savept->flags & SAVEPOINT_REALIZED)
    break;
  savept= NULL;
  }
  
  if (savept && !(savept->flags & SAVEPOINT_RESTRICT))
  {
    char buffer[STRING_BUFFER_USUAL_SIZE];
    size_t length= my_snprintf(buffer, sizeof(buffer),
              "ROLLBACK TO SAVEPOINT save%lu", savept->level);
    actual_query(buffer, length);
  }

  DBUG_RETURN(last_savepoint());
}


void federatedx_io_mysql::savepoint_restrict(ulong sp)
{
  SAVEPT *savept;
  uint index= savepoints.elements;
  DBUG_ENTER("federatedx_io_mysql::savepoint_restrict");
  
  while (index)
  {
    savept= dynamic_element(&savepoints, --index, SAVEPT *);
  if (savept->level > sp)
    continue;
  if (savept->level < sp)
    break;
  savept->flags|= SAVEPOINT_RESTRICT;
  break;
  }
  
  DBUG_VOID_RETURN;
}


int federatedx_io_mysql::simple_query(const char *fmt, ...)
{
  char buffer[STRING_BUFFER_USUAL_SIZE];
  size_t length;
  int error;
  va_list arg;
  DBUG_ENTER("federatedx_io_mysql::simple_query");

  va_start(arg, fmt);  
  length= my_vsnprintf(buffer, sizeof(buffer), fmt, arg);
  va_end(arg);
  
  error= query(buffer, length);
  
  DBUG_RETURN(error);
}


bool federatedx_io_mysql::test_all_restrict() const
{
  bool result= FALSE;
  SAVEPT *savept;
  uint index= savepoints.elements;
  DBUG_ENTER("federatedx_io_mysql::test_all_restrict");
  
  while (index)
  {
    savept= dynamic_element(&savepoints, --index, SAVEPT *);
  if ((savept->flags & (SAVEPOINT_REALIZED | 
                        SAVEPOINT_RESTRICT)) == SAVEPOINT_REALIZED ||
    (savept->flags & SAVEPOINT_EMITTED))
      DBUG_RETURN(FALSE);
    if (savept->flags & SAVEPOINT_RESTRICT)
    result= TRUE;
  }
  
  DBUG_RETURN(result); 
}


int federatedx_io_mysql::query(const char *buffer, size_t length)
{
  int error;
  bool wants_autocommit= requested_autocommit | is_readonly();
  DBUG_ENTER("federatedx_io_mysql::query");

  if (!wants_autocommit && test_all_restrict())
    wants_autocommit= TRUE;

  if (wants_autocommit != actual_autocommit)
  {
    if ((error= actual_query(wants_autocommit ? "SET AUTOCOMMIT=1"
                                            : "SET AUTOCOMMIT=0", 16)))
    DBUG_RETURN(error);                         
    mysql.reconnect= wants_autocommit ? 1 : 0;
    actual_autocommit= wants_autocommit;
  }
  
  if (!actual_autocommit && last_savepoint() != actual_savepoint())
  {
    SAVEPT *savept= dynamic_element(&savepoints, savepoints.elements - 1, 
                                SAVEPT *);
    if (!(savept->flags & SAVEPOINT_RESTRICT))
  {
      char buf[STRING_BUFFER_USUAL_SIZE];
      size_t len= my_snprintf(buf, sizeof(buf),
                  "SAVEPOINT save%lu", savept->level);
      if ((error= actual_query(buf, len)))
    DBUG_RETURN(error);                         
    set_active(TRUE);
    savept->flags|= SAVEPOINT_EMITTED;
    }
    savept->flags|= SAVEPOINT_REALIZED;
  }

  if (!(error= actual_query(buffer, length)))
    set_active(is_active() || !actual_autocommit);

  DBUG_RETURN(error);
}


int federatedx_io_mysql::actual_query(const char *buffer, size_t length)
{
  int error;
  DBUG_ENTER("federatedx_io_mysql::actual_query");

  if (!mysql.net.vio)
  {
    my_bool my_true= 1;

    if (!(mysql_init(&mysql)))
      DBUG_RETURN(-1);
  
    /*
	BUG# 17044 Federated Storage Engine is not UTF8 clean
	Add set names to whatever charset the table is at open
	of table
    */
    /* this sets the csname like 'set names utf8' */
    mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, get_charsetname());
    mysql_options(&mysql, MYSQL_OPT_USE_THREAD_SPECIFIC_MEMORY,
                  (char*) &my_true);

    if (!mysql_real_connect(&mysql,
                            get_hostname(),
                            get_username(),
                            get_password(),
                            get_database(),
                            get_port(),
                            get_socket(), 0))
      DBUG_RETURN(ER_CONNECT_TO_FOREIGN_DATA_SOURCE);
    mysql.reconnect= 1;
  }

  if (!(error= mysql_real_query(&mysql, STRING_WITH_LEN("set time_zone='+00:00'"))))
    error= mysql_real_query(&mysql, buffer, (ulong)length);
  
  DBUG_RETURN(error);
}

size_t federatedx_io_mysql::max_query_size() const
{
  return mysql.net.max_packet_size;
}


my_ulonglong federatedx_io_mysql::affected_rows() const
{
  return mysql.affected_rows;
}


my_ulonglong federatedx_io_mysql::last_insert_id() const
{
  return mysql.insert_id;
}


int federatedx_io_mysql::error_code()
{
  return mysql_errno(&mysql);
}


const char *federatedx_io_mysql::error_str()
{
  return mysql_error(&mysql);
}

FEDERATEDX_IO_RESULT *federatedx_io_mysql::store_result()
{
  FEDERATEDX_IO_RESULT *result;
  DBUG_ENTER("federatedx_io_mysql::store_result");

  result= (FEDERATEDX_IO_RESULT *) mysql_store_result(&mysql);

  DBUG_RETURN(result);
}


void federatedx_io_mysql::free_result(FEDERATEDX_IO_RESULT *io_result)
{
  mysql_free_result((MYSQL_RES *) io_result);
}


unsigned int federatedx_io_mysql::get_num_fields(FEDERATEDX_IO_RESULT *io_result)
{
  return mysql_num_fields((MYSQL_RES *) io_result);
}


my_ulonglong federatedx_io_mysql::get_num_rows(FEDERATEDX_IO_RESULT *io_result)
{
  return mysql_num_rows((MYSQL_RES *) io_result);
}


FEDERATEDX_IO_ROW *federatedx_io_mysql::fetch_row(FEDERATEDX_IO_RESULT *io_result,
                                                  FEDERATEDX_IO_ROWS **current)
{
  MYSQL_RES *result= (MYSQL_RES*)io_result;
  if (current)
    *current= (FEDERATEDX_IO_ROWS *) result->data_cursor;
  return (FEDERATEDX_IO_ROW *) mysql_fetch_row(result);
}


ulong *federatedx_io_mysql::fetch_lengths(FEDERATEDX_IO_RESULT *io_result)
{
  return mysql_fetch_lengths((MYSQL_RES *) io_result);
}


const char *federatedx_io_mysql::get_column_data(FEDERATEDX_IO_ROW *row,
                                                 unsigned int column)
{
  return ((MYSQL_ROW)row)[column];
}


bool federatedx_io_mysql::is_column_null(const FEDERATEDX_IO_ROW *row,
                                         unsigned int column) const
{
  return !((MYSQL_ROW)row)[column];
}

bool federatedx_io_mysql::table_metadata(ha_statistics *stats,
                                         const char *table_name,
                                         uint table_name_length, uint flag)
{
  char status_buf[FEDERATEDX_QUERY_BUFFER_SIZE];
  FEDERATEDX_IO_RESULT *result= 0;
  FEDERATEDX_IO_ROW *row;
  String status_query_string(status_buf, sizeof(status_buf), &my_charset_bin);
  int error;

  status_query_string.length(0);
  status_query_string.append(STRING_WITH_LEN("SHOW TABLE STATUS LIKE "));
  append_ident(&status_query_string, table_name,
               table_name_length, value_quote_char);

  if (query(status_query_string.ptr(), status_query_string.length()))
    goto error;

  status_query_string.length(0);

  result= store_result();

  /*
    We're going to use fields num. 4, 12 and 13 of the resultset,
    so make sure we have these fields.
  */
  if (!result || (get_num_fields(result) < 14))
    goto error;

  if (!get_num_rows(result))
    goto error;

  if (!(row= fetch_row(result)))
    goto error;

  /*
    deleted is set in ha_federatedx::info
  */
  /*
    need to figure out what this means as far as federatedx is concerned,
    since we don't have a "file"

    data_file_length = ?
    index_file_length = ?
    delete_length = ?
  */
  if (!is_column_null(row, 4))
    stats->records= (ha_rows) my_strtoll10(get_column_data(row, 4),
	                                   (char**) 0, &error);
  if (!is_column_null(row, 5))
    stats->mean_rec_length= (ulong) my_strtoll10(get_column_data(row, 5),
	                                         (char**) 0, &error);

  stats->data_file_length= stats->records * stats->mean_rec_length;

  if (!is_column_null(row, 12))
    stats->update_time= (time_t) my_strtoll10(get_column_data(row, 12),
	                                      (char**) 0, &error);
  if (!is_column_null(row, 13))
    stats->check_time= (time_t) my_strtoll10(get_column_data(row, 13),
	                                     (char**) 0, &error);

  free_result(result);
  return 0;

error:
  if (!mysql_errno(&mysql))
  {
    mysql.net.last_errno= ER_NO_SUCH_TABLE;
    strmake_buf(mysql.net.last_error, "Remote table does not exist");
  }
  free_result(result);
  return 1;
}



size_t federatedx_io_mysql::get_ref_length() const
{
  return sizeof(mysql_position);
}


void federatedx_io_mysql::mark_position(FEDERATEDX_IO_RESULT *io_result,
                                        void *ref, FEDERATEDX_IO_ROWS *current)
{
  mysql_position& pos= *reinterpret_cast<mysql_position*>(ref);
  pos.result= (MYSQL_RES *) io_result;
  pos.offset= (MYSQL_ROW_OFFSET) current;
}

int federatedx_io_mysql::seek_position(FEDERATEDX_IO_RESULT **io_result,
                                       const void *ref)
{
  const mysql_position& pos= *reinterpret_cast<const mysql_position*>(ref);

  if (!pos.result || !pos.offset)
    return HA_ERR_END_OF_FILE;

  pos.result->current_row= 0;
  pos.result->data_cursor= pos.offset;
  *io_result= (FEDERATEDX_IO_RESULT*) pos.result;

  return 0;
}

void federatedx_io_mysql::set_thd(void *thd)
{
  mysql.net.thd= thd;
}