-
Notifications
You must be signed in to change notification settings - Fork 4
/
ha_ibmdb2i.cc
3895 lines (3232 loc) · 110 KB
/
ha_ibmdb2i.cc
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Licensed Materials - Property of IBM
DB2 Storage Engine Enablement
Copyright IBM Corporation 2007,2008
All rights reserved
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
(a) Redistributions of source code must retain this list of conditions, the
copyright notice in section {d} below, and the disclaimer following this
list of conditions.
(b) Redistributions in binary form must reproduce this list of conditions, the
copyright notice in section (d) below, and the disclaimer following this
list of conditions, in the documentation and/or other materials provided
with the distribution.
(c) The name of IBM may not be used to endorse or promote products derived from
this software without specific prior written permission.
(d) The text of the required copyright notice is:
Licensed Materials - Property of IBM
DB2 Storage Engine Enablement
Copyright IBM Corporation 2007,2008
All rights reserved
THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "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 IBM CORPORATION 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.
*/
/**
@file ha_ibmdb2i.cc
@brief
The ha_ibmdb2i storage engine provides an interface from MySQL to IBM DB2 for i.
*/
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
#define MYSQL_SERVER 1
#include "sql_plugin.h"
#include "sql_priv.h"
#include "key.h" // key_copy
#include "ha_ibmdb2i.h"
#include <mysql/plugin.h>
#include "db2i_ileBridge.h"
#include "db2i_charsetSupport.h"
#include <sys/utsname.h>
#include "db2i_safeString.h"
#include <my_pthread.h>
#include <m_ctype.h>
static const char __NOT_NULL_VALUE_EBCDIC = 0xF0; // '0'
static const char __NULL_VALUE_EBCDIC = 0xF1; // '1'
static const char __DEFAULT_VALUE_EBCDIC = 0xC4; // 'D'
static const char BlankASPName[19] = " ";
static const ha_rows DEFAULT_MAX_ROWS_TO_BUFFER = 4096;
static const char SAVEPOINT_PREFIX[] = {0xD4, 0xE8, 0xE2, 0xD7}; // MYSP (in EBCDIC)
OSVersion osVersion;
static handler *ibmdb2i_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root);
static void ibmdb2i_drop_database(handlerton *hton, char* path);
static int ibmdb2i_savepoint_set(handlerton *hton, THD* thd, void *sv);
static int ibmdb2i_savepoint_rollback(handlerton *hton, THD* thd, void *sv);
static int ibmdb2i_savepoint_release(handlerton *hton, THD* thd, void *sv);
static alter_table_operations ibmdb2i_alter_table_flags(alter_table_operations flags);
handlerton *ibmdb2i_hton;
static bool was_ILE_inited;
static const char ibmdb2i_hton_name[]= "IBMDB2I";
static const int ibmdb2i_hton_name_length=sizeof(ibmdb2i_hton_name)-1;
/* Tracks the number of open tables */
static HASH ibmdb2i_open_tables;
/* Mutex used to synchronize initialization of the hash */
static pthread_mutex_t ibmdb2i_mutex;
static const char *ha_ibmdb2i_exts[] = {
FID_EXT,
NullS
};
// ================================================================
// ================================================================
/*
* Appears gcc compiler 'packs' start of structs non-align boundary (align 1).
* Bug is 'char' size error char *(*resolve)(p1,p2),
* results in data corruption of following struct.
* Switch MYSQL_SYSVAR_BOOL to MYSQL_SYSVAR_UINT.
*/
// System variables
static char* ibmdb2i_rdb_name;
static MYSQL_SYSVAR_STR(rdb_name, ibmdb2i_rdb_name,
PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_READONLY,
"The name of the RDB to use",
NULL,
NULL,
BlankASPName);
// MYSQL_SYSVAR_BOOL(name, varname, opt, comment, check, update, def)
// MYSQL_SYSVAR_UINT(name, varname, opt, comment, check, update, def, min, max, blk)
// static my_bool ibmdb2i_assume_exclusive_use;
static uint32 ibmdb2i_assume_exclusive_use __attribute__((aligned(16)));
static MYSQL_SYSVAR_UINT(
assume_exclusive_use,
ibmdb2i_assume_exclusive_use,
0,
"Can MySQL assume that this process is the only one modifying the DB2 tables. ",
NULL,
NULL,
0, /* FALSE */
0,
1,
0
);
static uint32 ibmdb2i_system_trace __attribute__((aligned(16)));
static MYSQL_SYSVAR_UINT(system_trace_level, ibmdb2i_system_trace,
0,
"Set system tracing level",
NULL,
NULL,
0,
0,
63,
1);
// System variables (connect)
static MYSQL_THDVAR_UINT(lob_alloc_size,
0,
"Baseline allocation for lob read buffer",
NULL,
NULL,
2097152,
65536,
134217728,
1);
static MYSQL_THDVAR_UINT(max_read_buffer_size,
0,
"Maximum size of buffers used for read-ahead.",
NULL,
NULL,
1048576,
32768,
16777216,
1);
static MYSQL_THDVAR_UINT(max_write_buffer_size,
0,
"Maximum size of buffers used for bulk writes.",
NULL,
NULL,
8388608,
32768,
67108864,
1);
static MYSQL_THDVAR_UINT(compat_opt_year_as_int,
0,
"Control how new YEAR columns should be defined in DB2. 0=CHAR(4) (default), 1=SMALLINT.",
NULL,
NULL,
0,
0,
1,
1);
static MYSQL_THDVAR_UINT(compat_opt_blob_cols,
0,
"Control how new TEXT and BLOB columns should be defined in DB2. 0=CLOB/BLOB (default), 1=VARCHAR/VARBINARY",
NULL,
NULL,
0,
0,
1,
1);
static MYSQL_THDVAR_UINT(compat_opt_allow_zero_date_vals,
0,
"Allow substitute values to be used when storing a column with a 0000-00-00 date component. 0=No substitution (default), 1=Substitute '0001-01-01'",
NULL,
NULL,
0,
0,
1,
1);
static MYSQL_THDVAR_UINT(create_index_option,
0,
"Control whether additional indexes are created. 0=No (default), 1=Create additional *HEX-based index",
NULL,
NULL,
0,
0,
1,
1);
/* static MYSQL_THDVAR_UINT(discovery_mode,
0,
"Unsupported",
NULL,
NULL,
0,
0,
1,
1);
*/
// MYSQL_THDVAR_BOOL(name, opt, comment, check, update, def)
// MYSQL_THDVAR_UINT(name, opt, comment, check, update, def, min, max, blk)
static MYSQL_THDVAR_UINT(compat_opt_time_as_duration,
0,
"Control how new TIME columns should be defined in DB2. 0=time-of-day (default), 1=duration.",
NULL,
NULL,
0, /* FALSE */
0,
1,
0
);
// MYSQL_THDVAR_BOOL(name, opt, comment, check, update, def)
// MYSQL_THDVAR_UINT(name, opt, comment, check, update, def, min, max, blk)
static MYSQL_THDVAR_UINT(propagate_default_col_vals,
0,
"Should DEFAULT column values be propagated to the DB2 table definition.",
NULL,
NULL,
1, /* TRUE */
0,
1,
0
);
// MYSQL_THDVAR_BOOL(name, opt, comment, check, update, def)
// MYSQL_THDVAR_UINT(name, opt, comment, check, update, def, min, max, blk)
static MYSQL_THDVAR_UINT(async_enabled,
0,
"Should reads be done asynchronously when possible",
NULL,
NULL,
1, /* TRUE */
0,
1,
0
);
// MYSQL_THDVAR_BOOL(name, opt, comment, check, update, def)
// MYSQL_THDVAR_UINT(name, opt, comment, check, update, def, min, max, blk)
static MYSQL_THDVAR_UINT(transaction_unsafe,
0,
"Disable support for commitment control",
NULL,
NULL,
0, /* FALSE */
0,
1,
0
);
static struct st_mysql_sys_var* ibmdb2i_system_variables[] = {
MYSQL_SYSVAR(rdb_name),
MYSQL_SYSVAR(transaction_unsafe),
MYSQL_SYSVAR(lob_alloc_size),
MYSQL_SYSVAR(max_read_buffer_size),
MYSQL_SYSVAR(max_write_buffer_size),
MYSQL_SYSVAR(async_enabled),
MYSQL_SYSVAR(assume_exclusive_use),
MYSQL_SYSVAR(compat_opt_blob_cols),
MYSQL_SYSVAR(compat_opt_time_as_duration),
MYSQL_SYSVAR(compat_opt_allow_zero_date_vals),
MYSQL_SYSVAR(compat_opt_year_as_int),
MYSQL_SYSVAR(propagate_default_col_vals),
MYSQL_SYSVAR(create_index_option),
// MYSQL_SYSVAR(discovery_mode),
MYSQL_SYSVAR(system_trace_level),
NULL
};
// ================================================================
// ================================================================
// functions
/**
Create hash key for tracking open tables.
*/
static uchar* ibmdb2i_get_key(IBMDB2I_SHARE *share,size_t *length,
bool not_used __attribute__((unused)))
{
*length=share->table_name_length;
return (uchar*) share->table_name;
}
int ibmdb2i_close_connection(handlerton* hton, THD *thd)
{
DBUG_PRINT("ha_ibmdb2i::close_connection", ("Closing %d", (int) thd->thread_id));
db2i_ileBridge::getBridgeForThread(thd)->closeConnection(thd->thread_id);
db2i_ileBridge::destroyBridgeForThread(thd);
return 0;
}
static int ibmdb2i_init_func(void *p)
{
DBUG_ENTER("ibmdb2i_init_func");
utsname tempName;
uname(&tempName);
osVersion.v = atoi(tempName.version);
osVersion.r = atoi(tempName.release);
was_ILE_inited = false;
ibmdb2i_hton= (handlerton *)p;
(void) pthread_mutex_init(&ibmdb2i_mutex,MY_MUTEX_INIT_FAST);
(void) my_hash_init(&ibmdb2i_open_tables,table_alias_charset,32,0,0,
(my_hash_get_key) ibmdb2i_get_key,0,0);
ibmdb2i_hton->state= SHOW_OPTION_YES;
ibmdb2i_hton->create= ibmdb2i_create_handler;
ibmdb2i_hton->drop_database= ibmdb2i_drop_database;
ibmdb2i_hton->commit= ha_ibmdb2i::doCommit;
ibmdb2i_hton->rollback= ha_ibmdb2i::doRollback;
ibmdb2i_hton->savepoint_offset= 0;
ibmdb2i_hton->savepoint_set= ibmdb2i_savepoint_set;
ibmdb2i_hton->savepoint_rollback= ibmdb2i_savepoint_rollback;
ibmdb2i_hton->savepoint_release= ibmdb2i_savepoint_release;
ibmdb2i_hton->alter_table_flags=ibmdb2i_alter_table_flags;
ibmdb2i_hton->close_connection=ibmdb2i_close_connection;
int rc;
DBUG_PRINT("ibmdb2i_init_func",("(adc) going to initCharsetSupport"));
rc = initCharsetSupport();
DBUG_PRINT("ibmdb2i_init_func",("(adc) going to db2i_ileBridge::setup"));
if (!rc)
rc = db2i_ileBridge::setup();
if (!rc)
{
DBUG_PRINT("ibmdb2i_init_func",("(adc) going to toupper"));
int nameLen = strlen(ibmdb2i_rdb_name);
for (int i = 0; i < nameLen; ++i)
{
ibmdb2i_rdb_name[i] = my_toupper(system_charset_info, (uchar)ibmdb2i_rdb_name[i]);
}
DBUG_PRINT("ibmdb2i_init_func",("(adc) going to db2i_ileBridge::initILE"));
rc = db2i_ileBridge::initILE(ibmdb2i_rdb_name, (uint16*)(((char*)&ibmdb2i_system_trace)+2));
if (rc == 0)
{
was_ILE_inited = true;
}
}
DBUG_PRINT("ibmdb2i_init_func",("(adc) leaving"));
DBUG_RETURN(rc);
}
static int ibmdb2i_done_func(void *p)
{
int error = 0;
DBUG_ENTER("ibmdb2i_done_func");
if (ibmdb2i_open_tables.records)
error= 1;
if (was_ILE_inited)
db2i_ileBridge::exitILE();
db2i_ileBridge::takedown();
doneCharsetSupport();
my_hash_free(&ibmdb2i_open_tables);
pthread_mutex_destroy(&ibmdb2i_mutex);
DBUG_RETURN(error);
}
static handler* ibmdb2i_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root)
{
return new (mem_root) ha_ibmdb2i(hton, table);
}
static void ibmdb2i_drop_database(handlerton *hton, char* path)
{
DBUG_ENTER("ha_ibmdb2i::ibmdb2i_drop_database");
char queryBuffer[200];
String query(queryBuffer, sizeof(queryBuffer), system_charset_info);
query.length(0);
/* comment these line out. Issue 51484. Can not drop schema created on IBM i.
query.append(STRING_WITH_LEN(" DROP SCHEMA \""));
query.append(path+2, strchr(path+2, '/')-(path+2));
query.append('"');
*/
//Jian updated
char db2FromLibName[MAX_DB2_SCHEMANAME_LENGTH+1];
db2i_table::getDB2LibNameFromPath(path, db2FromLibName);
query.append(STRING_WITH_LEN(" DROP SCHEMA "));
query.append(db2FromLibName);
//end Jian
SqlStatementStream sqlStream(query);
db2i_ileBridge::getBridgeForThread()->execSQL(sqlStream.getPtrToData(),
sqlStream.getStatementCount(),
QMY_NONE,
FALSE,
TRUE);
DBUG_VOID_RETURN;
}
static void genSavepointName(const void* sv, char* out)
{
*(uint32*)out = *(uint32*)SAVEPOINT_PREFIX;
DBUG_ASSERT(sizeof(SAVEPOINT_PREFIX) == 4);
out += sizeof(SAVEPOINT_PREFIX);
longlong2str((longlong)sv, out, 10);
while (*out)
{
out += 0xF0;
++out;
}
}
/* Sets a transaction savepoint. */
static int ibmdb2i_savepoint_set(handlerton* hton, THD* thd, void* sv)
{
DBUG_ENTER("ibmdb2i_savepoint_set");
int rc = 0;
if (!THDVAR(thd ,transaction_unsafe))
{
char name[64];
genSavepointName(sv, name);
DBUG_PRINT("ibmdb2i_savepoint_set",("Setting %s", name));
rc = ha_ibmdb2i::doSavepointSet(thd, name);
}
DBUG_RETURN(rc);
}
/* Rollback a savepoint. */
static int ibmdb2i_savepoint_rollback(handlerton* hton, THD* thd, void* sv)
{
DBUG_ENTER("ibmdb2i_savepoint_rollback");
int rc = 0;
if (!THDVAR(thd,transaction_unsafe))
{
char name[64];
genSavepointName(sv, name);
DBUG_PRINT("ibmdb2i_savepoint_rollback",("Rolling back %s", name));
rc = ha_ibmdb2i::doSavepointRollback(thd, name);
}
DBUG_RETURN(rc);
}
/* Release a savepoint. */
static int ibmdb2i_savepoint_release(handlerton* hton, THD* thd, void* sv)
{
DBUG_ENTER("ibmdb2i_savepoint_release");
int rc = 0;
if (!THDVAR(thd,transaction_unsafe))
{
char name[64];
genSavepointName(sv, name);
DBUG_PRINT("ibmdb2i_savepoint_release",("Releasing %s", name));
rc = ha_ibmdb2i::doSavepointRelease(thd, name);
}
DBUG_RETURN(rc);
}
/* Thse flags allow for the online add and drop of an index via the CREATE INDEX,
DROP INDEX, and ALTER TABLE statements. These flags indicate that MySQL is not
required to lock the table before calling the storage engine to add or drop the
index(s). */
static alter_table_operations ibmdb2i_alter_table_flags(alter_table_operations flags)
{
return (HA_INPLACE_ADD_INDEX_NO_WRITE | HA_INPLACE_DROP_INDEX_NO_WRITE |
HA_INPLACE_ADD_UNIQUE_INDEX_NO_WRITE | HA_INPLACE_DROP_UNIQUE_INDEX_NO_WRITE |
HA_INPLACE_ADD_PK_INDEX_NO_WRITE | HA_INPLACE_DROP_PK_INDEX_NO_WRITE);
}
// ================================================================
// ================================================================
// Class implementation
const char **ha_ibmdb2i::bas_ext() const
{
return ha_ibmdb2i_exts;
}
uint8 ha_ibmdb2i::getCommitLevel(THD* thd)
{
if (!THDVAR(thd, transaction_unsafe))
{
switch (thd_tx_isolation(thd))
{
case ISO_READ_UNCOMMITTED:
return (accessIntent == QMY_READ_ONLY ? QMY_READ_UNCOMMITTED : QMY_REPEATABLE_READ);
case ISO_READ_COMMITTED:
return (accessIntent == QMY_READ_ONLY ? QMY_READ_COMMITTED : QMY_REPEATABLE_READ);
case ISO_REPEATABLE_READ:
return QMY_REPEATABLE_READ;
case ISO_SERIALIZABLE:
return QMY_SERIALIZABLE;
}
}
return QMY_NONE;
}
uint8 ha_ibmdb2i::getCommitLevel()
{
return getCommitLevel(ha_thd());
}
IBMDB2I_SHARE *ha_ibmdb2i::get_share(const char *table_name, TABLE *table)
{
IBMDB2I_SHARE *share;
uint length;
char *tmp_name;
pthread_mutex_lock(&ibmdb2i_mutex);
length=(uint) strlen(table_name);
if (!(share=(IBMDB2I_SHARE*)
my_hash_search(&ibmdb2i_open_tables,
(uchar*)table_name,
length)))
{
if (!(share=(IBMDB2I_SHARE *)
my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
&share, sizeof(*share),
&tmp_name, length+1,
NullS)))
{
pthread_mutex_unlock(&ibmdb2i_mutex);
return NULL;
}
share->use_count=0;
share->table_name_length=length;
share->table_name=tmp_name;
strmov(share->table_name,table_name);
if (my_hash_insert(&ibmdb2i_open_tables, (uchar*) share))
goto error;
thr_lock_init(&share->lock);
pthread_mutexattr_t mutexattr = MY_MUTEX_INIT_FAST;
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&share->mutex, &mutexattr);
share->db2Table = new db2i_table(table->s, table_name);
int32 rc = share->db2Table->initDB2Objects(table_name);
if (rc)
{
delete share->db2Table;
my_hash_delete(&ibmdb2i_open_tables, (uchar*) share);
thr_lock_delete(&share->lock);
my_errno = rc;
goto error;
}
memset(&share->cachedStats, 0, sizeof(share->cachedStats));
}
share->use_count++;
pthread_mutex_unlock(&ibmdb2i_mutex);
db2Table = share->db2Table;
return share;
error:
pthread_mutex_destroy(&share->mutex);
my_free((uchar*) share);
pthread_mutex_unlock(&ibmdb2i_mutex);
return NULL;
}
int ha_ibmdb2i::free_share(IBMDB2I_SHARE *share)
{
pthread_mutex_lock(&ibmdb2i_mutex);
if (!--share->use_count)
{
delete share->db2Table;
db2Table = NULL;
my_hash_delete(&ibmdb2i_open_tables, (uchar*) share);
thr_lock_delete(&share->lock);
pthread_mutex_destroy(&share->mutex);
my_free(share);
pthread_mutex_unlock(&ibmdb2i_mutex);
return 1;
}
pthread_mutex_unlock(&ibmdb2i_mutex);
return 0;
}
ha_ibmdb2i::ha_ibmdb2i(handlerton *hton, TABLE_SHARE *table_arg) :
handler(hton, table_arg),
// lock(0),
share(NULL),
currentRRN(0),
rrnAssocHandle(0),
lastDupKeyRRN(0),
lastDupKeyID(0),
returnDupKeysImmediately(false),
onDupUpdate(false),
db2Table(NULL),
activeHandle(0),
dataHandle(0),
indexHandles(NULL),
releaseRowNeeded(false),
activeFormat(NULL),
// keyBuf,
keyLen(0),
// multiRowWriteBuf,
// multiRowReadBuf,
activeReadBuf(NULL),
activeWriteBuf(NULL),
blobReadBuffers(NULL),
blobWriteBuffers(NULL),
last_rnd_init_rc(0),
last_index_init_rc(0),
last_start_bulk_insert_rc(0),
outstanding_start_bulk_insert(false),
incrementByValue(0),
default_identity_value(false),
autoIncLockAcquired(false),
got_auto_inc_values(false),
next_identity_value(0),
accessIntent(QMY_UPDATABLE),
readAccessIntent(0),
indexReadSizeEstimates(NULL),
// conversionBufferMemroot,
forceSingleRowRead(false),
readAllColumns(false),
invalidDataFound(false),
cachedBridge(NULL),
// curConnection,
activeReferences(0)
{
// activeReferences = 0;
ref_length = sizeof(currentRRN);
if (table_share && table_share->keys > 0)
{
indexHandles = (FILE_HANDLE*)my_malloc(table_share->keys * sizeof(FILE_HANDLE), MYF(MY_WME | MY_ZEROFILL));
}
clear_alloc_root(&conversionBufferMemroot);
}
ha_ibmdb2i::~ha_ibmdb2i()
{
DBUG_ASSERT(activeReferences == 0 || outstanding_start_bulk_insert);
if (indexHandles)
my_free(indexHandles);
if (indexReadSizeEstimates)
my_free(indexReadSizeEstimates);
cleanupBuffers();
}
int ha_ibmdb2i::open(const char *name, int mode, uint test_if_locked)
{
DBUG_ENTER("ha_ibmdb2i::open");
initBridge();
dataHandle = bridge()->findAndRemovePreservedHandle(name, &share);
if (share)
db2Table = share->db2Table;
if (!share && (!(share = get_share(name, table))))
DBUG_RETURN(my_errno);
thr_lock_data_init(&share->lock,&lock,NULL);
info(HA_STATUS_NO_LOCK | HA_STATUS_CONST | HA_STATUS_VARIABLE);
DBUG_RETURN(0);
}
int ha_ibmdb2i::close(void)
{
DBUG_ENTER("ha_ibmdb2i::close");
int32 rc = 0;
bool preserveShare = false;
db2i_ileBridge* bridge = db2i_ileBridge::getBridgeForThread();
if (dataHandle)
{
if (bridge->expectErrors(QMY_ERR_PEND_LOCKS)->deallocateFile(dataHandle, FALSE) == QMY_ERR_PEND_LOCKS)
{
bridge->preserveHandle(share->table_name, dataHandle, share);
preserveShare = true;
}
dataHandle = 0;
}
for (int idx = 0; idx < (int) table_share->keys; ++idx)
{
if (indexHandles[idx] != 0)
{
bridge->deallocateFile(indexHandles[idx], FALSE);
}
}
cleanupBuffers();
if (!preserveShare)
{
if (free_share(share))
share = NULL;
}
DBUG_RETURN(rc);
}
int ha_ibmdb2i::write_row(uchar * buf)
{
DBUG_ENTER("ha_ibmdb2i::write_row");
if (last_start_bulk_insert_rc)
DBUG_RETURN( last_start_bulk_insert_rc );
increment_statistics(&SSV::ha_write_count);
int rc = 0;
bool fileHandleNeedsRelease = false;
if (!activeHandle)
{
rc = useDataFile();
if (rc) DBUG_RETURN(rc);
fileHandleNeedsRelease = true;
}
if (!outstanding_start_bulk_insert)
rc = prepWriteBuffer(1, getFileForActiveHandle());
if (!rc)
{
char* writeBuffer = activeWriteBuf->addRow();
rc = prepareRowForWrite(writeBuffer,
writeBuffer+activeWriteBuf->getRowNullOffset(),
true);
if (rc == 0)
{
// If we are doing block inserts, if the MI is supposed to generate an auto_increment
// (i.e. identity column) value for this record, and if this is not the first record in
// the block, then store the value (that the MI will generate for the identity column)
// into the MySQL write buffer. We can predetermine the value because the file is locked.
if ((autoIncLockAcquired) && (default_identity_value) && (got_auto_inc_values))
{
if (unlikely((next_identity_value - 1) ==
maxValueForField(table->next_number_field)))
{
rc = QMY_ERR_MAXVALUE;
}
else
{
rc = table->next_number_field->store((longlong) next_identity_value, TRUE);
next_identity_value = next_identity_value + incrementByValue;
}
}
// If the buffer is full, or if we locked the file and this is the first or last row
// of a blocked insert, then flush the buffer.
if ((!rc && (activeWriteBuf->endOfBuffer())) ||
((autoIncLockAcquired) &&
((!got_auto_inc_values))) ||
(returnDupKeysImmediately))
rc = flushWrite(activeHandle, buf);
}
else
activeWriteBuf->deleteRow();
}
if (fileHandleNeedsRelease)
releaseActiveHandle();
DBUG_RETURN(rc);
}
/**
@brief
Helper function used by write_row and update_row to prepare the MySQL
row for insertion into DB2.
*/
int ha_ibmdb2i::prepareRowForWrite(char* data, char* nulls, bool honorIdentCols)
{
int rc = 0;
// set null map all to non nulls
memset(nulls,__NOT_NULL_VALUE_EBCDIC, table->s->fields);
default_identity_value = FALSE;
ulong sql_mode = ha_thd()->variables.sql_mode;
#if MYSQL_VERSION_ID >= 100328
MY_BITMAP *old_map= dbug_tmp_use_all_columns(table, &table->read_set);
#else
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
#endif
for (Field **field = table->field; *field && !rc; ++field)
{
int fieldIndex = (*field)->field_index;
if ((*field)->Field::is_null())
{
nulls[fieldIndex] = __NULL_VALUE_EBCDIC;
}
if (honorIdentCols && ((*field)->flags & AUTO_INCREMENT_FLAG) &&
*field == table->next_number_field)
// && ((!autoIncLockAcquired) || (!got_auto_inc_values)))
{
if (sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO)
{
if (!table->auto_increment_field_not_null)
{
nulls[fieldIndex] = __DEFAULT_VALUE_EBCDIC;
default_identity_value = TRUE;
}
}
else if ((*field)->val_int() == 0)
{
nulls[fieldIndex] = __DEFAULT_VALUE_EBCDIC;
default_identity_value = TRUE;
}
}
DB2Field& db2Field = db2Table->db2Field(fieldIndex);
if (nulls[fieldIndex] == __NOT_NULL_VALUE_EBCDIC ||
db2Field.isBlob())
{
rc = convertMySQLtoDB2(*field, db2Field, data + db2Field.getBufferOffset());
}
}
if (!rc && db2Table->hasBlobs())
rc = db2i_ileBridge::getBridgeForThread()->objectOverride(activeHandle,
activeWriteBuf->ptr());
#if MYSQL_VERSION_ID >= 100328
dbug_tmp_restore_column_map(&table->read_set, old_map);
#else
dbug_tmp_restore_column_map(table->read_set, old_map);
#endif
return rc;
}
int ha_ibmdb2i::update_row(const uchar * old_data, const uchar * new_data)
{
DBUG_ENTER("ha_ibmdb2i::update_row");
increment_statistics(&SSV::ha_update_count);
int rc;
bool fileHandleNeedsRelease = false;
if (!activeHandle)
{
rc = useFileByHandle(QMY_UPDATABLE, rrnAssocHandle);
if (rc) DBUG_RETURN(rc);
fileHandleNeedsRelease = true;
}
char* writeBuf = activeWriteBuf->addRow();
rc = prepareRowForWrite(writeBuf,
writeBuf+activeWriteBuf->getRowNullOffset(),
onDupUpdate);
char* lastDupKeyNamePtr = NULL;
uint32 lastDupKeyNameLen = 0;
if (!rc)
{
rc = db2i_ileBridge::getBridgeForThread()->updateRow(activeHandle,
currentRRN,
activeWriteBuf->ptr(),
&lastDupKeyRRN,
&lastDupKeyNamePtr,
&lastDupKeyNameLen);
}
if (lastDupKeyNameLen)
{
lastDupKeyID = getKeyFromName(lastDupKeyNamePtr, lastDupKeyNameLen);
rrnAssocHandle = activeHandle;
}
if (fileHandleNeedsRelease)
releaseActiveHandle();
activeWriteBuf->resetAfterWrite();
DBUG_RETURN(rc);
}
int ha_ibmdb2i::delete_row(const uchar * buf)
{
DBUG_ENTER("ha_ibmdb2i::delete_row");
increment_statistics(&SSV::ha_delete_count);
bool needReleaseFile = false;
int rc = 0;
if (!activeHandle) // In some circumstances, MySQL comes here after
{ // closing the active handle. We need to re-open.
rc = useFileByHandle(QMY_UPDATABLE, rrnAssocHandle);
needReleaseFile = true;
}
if (likely(!rc))
{
rc = db2i_ileBridge::getBridgeForThread()->deleteRow(activeHandle,
currentRRN);
invalidateCachedStats();
if (needReleaseFile)
releaseActiveHandle();
}
DBUG_RETURN(rc);
}
int ha_ibmdb2i::index_init(uint idx, bool sorted)
{
DBUG_ENTER("ha_ibmdb2i::index_init");
int& rc = last_index_init_rc;
rc = 0;
invalidDataFound=false;
tweakReadSet();
active_index=idx;
rc = useIndexFile(idx);
if (!rc)
{
// THD* thd = ha_thd();
// if (accessIntent == QMY_UPDATABLE &&
// thd_tx_isolation(thd) == ISO_REPEATABLE_READ &&
// !THDVAR(thd, transaction_unsafe))