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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 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
*/
#include <config.h>
#include <fcntl.h>
#include <sstream>
#include <drizzled/show.h>
#include <drizzled/lock.h>
#include <drizzled/session.h>
#include <drizzled/statement/alter_table.h>
#include <drizzled/charset.h>
#include <drizzled/gettext.h>
#include <drizzled/data_home.h>
#include <drizzled/sql_table.h>
#include <drizzled/table_proto.h>
#include <drizzled/optimizer/range.h>
#include <drizzled/time_functions.h>
#include <drizzled/records.h>
#include <drizzled/pthread_globals.h>
#include <drizzled/internal/my_sys.h>
#include <drizzled/internal/iocache.h>
#include <drizzled/plugin/storage_engine.h>
#include <drizzled/copy_field.h>
#include <drizzled/transaction_services.h>
#include <drizzled/filesort.h>
#include <drizzled/message.h>
#include <drizzled/message/alter_table.pb.h>
#include <drizzled/alter_column.h>
#include <drizzled/alter_info.h>
#include <drizzled/util/test.h>
#include <drizzled/open_tables_state.h>
#include <drizzled/table/cache.h>
#include <drizzled/create_field.h>
using namespace std;
namespace drizzled {
extern pid_t current_pid;
static int copy_data_between_tables(Session *session,
Table *from,Table *to,
List<CreateField> &create,
bool ignore,
uint32_t order_num,
Order *order,
ha_rows *copied,
ha_rows *deleted,
message::AlterTable &alter_table_message,
bool error_if_not_empty);
static bool prepare_alter_table(Session *session,
Table *table,
HA_CREATE_INFO *create_info,
const message::Table &original_proto,
message::Table &table_message,
message::AlterTable &alter_table_message,
AlterInfo *alter_info);
static Table *open_alter_table(Session *session, Table *table, identifier::Table &identifier);
static int apply_online_alter_keys_onoff(Session *session,
Table* table,
const message::AlterTable::AlterKeysOnOff &op);
static int apply_online_rename_table(Session *session,
Table *table,
plugin::StorageEngine *original_engine,
identifier::Table &original_table_identifier,
identifier::Table &new_table_identifier,
const message::AlterTable::RenameTable &alter_operation);
namespace statement {
AlterTable::AlterTable(Session *in_session, Table_ident *) :
CreateTable(in_session)
{
set_command(SQLCOM_ALTER_TABLE);
}
} // namespace statement
bool statement::AlterTable::execute()
{
TableList *first_table= (TableList *) lex().select_lex.table_list.first;
TableList *all_tables= lex().query_tables;
assert(first_table == all_tables && first_table != 0);
Select_Lex *select_lex= &lex().select_lex;
is_engine_set= not createTableMessage().engine().name().empty();
if (is_engine_set)
{
create_info().db_type=
plugin::StorageEngine::findByName(session(), createTableMessage().engine().name());
if (create_info().db_type == NULL)
{
my_error(createTableMessage().engine().name(), ER_UNKNOWN_STORAGE_ENGINE, MYF(0));
return true;
}
}
/* Must be set in the parser */
assert(select_lex->db);
/* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
message::table::shared_ptr original_table_message;
{
identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
if (not (original_table_message= plugin::StorageEngine::getTableMessage(session(), identifier)))
{
my_error(ER_BAD_TABLE_ERROR, identifier);
return true;
}
if (not create_info().db_type)
{
create_info().db_type=
plugin::StorageEngine::findByName(session(), original_table_message->engine().name());
if (not create_info().db_type)
{
my_error(ER_BAD_TABLE_ERROR, identifier);
return true;
}
}
}
if (not validateCreateTableOption())
return true;
if (session().inTransaction())
{
my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
return true;
}
if (session().wait_if_global_read_lock(0, 1))
return true;
bool res;
if (original_table_message->type() == message::Table::STANDARD )
{
identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
identifier::Table new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
lex().name.data() ? lex().name.data() : first_table->getTableName());
res= alter_table(&session(),
identifier,
new_identifier,
&create_info(),
*original_table_message,
createTableMessage(),
first_table,
&alter_info,
select_lex->order_list.size(),
(Order *) select_lex->order_list.first,
lex().ignore);
}
else
{
identifier::Table catch22(first_table->getSchemaName(), first_table->getTableName());
Table *table= session().open_tables.find_temporary_table(catch22);
assert(table);
{
identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
identifier::Table new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
lex().name.data() ? lex().name.data() : first_table->getTableName(),
table->getMutableShare()->getPath());
res= alter_table(&session(),
identifier,
new_identifier,
&create_info(),
*original_table_message,
createTableMessage(),
first_table,
&alter_info,
select_lex->order_list.size(),
(Order *) select_lex->order_list.first,
lex().ignore);
}
}
/*
Release the protection against the global read lock and wake
everyone, who might want to set a global read lock.
*/
session().startWaitingGlobalReadLock();
return res;
}
/**
Prepare column and key definitions for CREATE TABLE in ALTER Table.
This function transforms parse output of ALTER Table - lists of
columns and keys to add, drop or modify into, essentially,
CREATE TABLE definition - a list of columns and keys of the new
table. While doing so, it also performs some (bug not all)
semantic checks.
This function is invoked when we know that we're going to
perform ALTER Table via a temporary table -- i.e. fast ALTER Table
is not possible, perhaps because the ALTER statement contains
instructions that require change in table data, not only in
table definition or indexes.
@param[in,out] session thread handle. Used as a memory pool
and source of environment information.
@param[in] table the source table, open and locked
Used as an interface to the storage engine
to acquire additional information about
the original table.
@param[in,out] create_info A blob with CREATE/ALTER Table
parameters
@param[in,out] alter_info Another blob with ALTER/CREATE parameters.
Originally create_info was used only in
CREATE TABLE and alter_info only in ALTER Table.
But since ALTER might end-up doing CREATE,
this distinction is gone and we just carry
around two structures.
@return
Fills various create_info members based on information retrieved
from the storage engine.
Sets create_info->varchar if the table has a VARCHAR column.
Prepares alter_info->create_list and alter_info->key_list with
columns and keys of the new table.
@retval true error, out of memory or a semantical error in ALTER
Table instructions
@retval false success
*/
static bool prepare_alter_table(Session *session,
Table *table,
HA_CREATE_INFO *create_info,
const message::Table &original_proto,
message::Table &table_message,
message::AlterTable &alter_table_message,
AlterInfo *alter_info)
{
uint32_t used_fields= create_info->used_fields;
vector<string> drop_keys;
vector<string> drop_columns;
vector<string> drop_fkeys;
/* we use drop_(keys|columns|fkey) below to check that we can do all
operations we've been asked to do */
for (int operationnr= 0; operationnr < alter_table_message.operations_size();
operationnr++)
{
const message::AlterTable::AlterTableOperation &operation=
alter_table_message.operations(operationnr);
switch (operation.operation())
{
case message::AlterTable::AlterTableOperation::DROP_KEY:
drop_keys.push_back(operation.drop_name());
break;
case message::AlterTable::AlterTableOperation::DROP_COLUMN:
drop_columns.push_back(operation.drop_name());
break;
case message::AlterTable::AlterTableOperation::DROP_FOREIGN_KEY:
drop_fkeys.push_back(operation.drop_name());
break;
default:
break;
}
}
/* Let new create options override the old ones */
message::Table::TableOptions *table_options= table_message.mutable_options();
if (not (used_fields & HA_CREATE_USED_DEFAULT_CHARSET))
create_info->default_table_charset= table->getShare()->table_charset;
if (not (used_fields & HA_CREATE_USED_AUTO) && table->found_next_number_field)
{
/* Table has an autoincrement, copy value to new table */
table->cursor->info(HA_STATUS_AUTO);
create_info->auto_increment_value= table->cursor->stats.auto_increment_value;
if (create_info->auto_increment_value != original_proto.options().auto_increment_value())
table_options->set_has_user_set_auto_increment_value(false);
}
table->restoreRecordAsDefault(); /* Empty record for DEFAULT */
List<CreateField> new_create_list;
List<Key> new_key_list;
/* First collect all fields from table which isn't in drop_list */
Field *field;
for (Field **f_ptr= table->getFields(); (field= *f_ptr); f_ptr++)
{
/* Check if field should be dropped */
vector<string>::iterator it= drop_columns.begin();
while (it != drop_columns.end())
{
if (not system_charset_info->strcasecmp(field->field_name, it->c_str()))
{
/* Reset auto_increment value if it was dropped */
if (MTYP_TYPENR(field->unireg_check) == Field::NEXT_NUMBER &&
not (used_fields & HA_CREATE_USED_AUTO))
{
create_info->auto_increment_value= 0;
create_info->used_fields|= HA_CREATE_USED_AUTO;
}
break;
}
it++;
}
if (it != drop_columns.end())
{
drop_columns.erase(it);
continue;
}
/* Mark that we will read the field */
field->setReadSet();
CreateField *def;
/* Check if field is changed */
List<CreateField>::iterator def_it= alter_info->create_list.begin();
while ((def= def_it++))
{
if (def->change &&
! system_charset_info->strcasecmp(field->field_name, def->change))
break;
}
if (def)
{
/* Field is changed */
def->field= field;
if (! def->after)
{
new_create_list.push_back(def);
def_it.remove();
}
}
else
{
/*
This field was not dropped and not changed, add it to the list
for the new table.
*/
def= new CreateField(field, field);
new_create_list.push_back(def);
AlterInfo::alter_list_t::iterator alter(alter_info->alter_list.begin());
for (; alter != alter_info->alter_list.end(); alter++)
{
if (not system_charset_info->strcasecmp(field->field_name, alter->name))
break;
}
if (alter != alter_info->alter_list.end())
{
def->setDefaultValue(alter->def, NULL);
alter_info->alter_list.erase(alter);
}
}
}
CreateField *def;
List<CreateField>::iterator def_it= alter_info->create_list.begin();
while ((def= def_it++)) /* Add new columns */
{
if (def->change && ! def->field)
{
my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table->getMutableShare()->getTableName());
return true;
}
/*
If we have been given a field which has no default value, and is not null then we need to bail.
*/
if (not (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) and not def->change)
{
alter_info->error_if_not_empty= true;
}
if (! def->after)
{
new_create_list.push_back(def);
}
else if (def->after == first_keyword)
{
new_create_list.push_front(def);
}
else
{
CreateField *find;
List<CreateField>::iterator find_it= new_create_list.begin();
while ((find= find_it++)) /* Add new columns */
{
if (not system_charset_info->strcasecmp(def->after, find->field_name))
break;
}
if (not find)
{
my_error(ER_BAD_FIELD_ERROR, MYF(0), def->after, table->getMutableShare()->getTableName());
return true;
}
find_it.after(def); /* Put element after this */
/*
XXX: hack for Bug#28427.
If column order has changed, force OFFLINE ALTER Table
without querying engine capabilities. If we ever have an
engine that supports online ALTER Table CHANGE COLUMN
<name> AFTER <name1> (Falcon?), this fix will effectively
disable the capability.
TODO: detect the situation in compare_tables, behave based
on engine capabilities.
*/
if (alter_table_message.build_method() == message::AlterTable::BUILD_ONLINE)
{
my_error(*session->getQueryString(), ER_NOT_SUPPORTED_YET);
return true;
}
}
}
if (not alter_info->alter_list.empty())
{
my_error(ER_BAD_FIELD_ERROR, MYF(0), alter_info->alter_list.front().name, table->getMutableShare()->getTableName());
return true;
}
if (new_create_list.is_empty())
{
my_message(ER_CANT_REMOVE_ALL_FIELDS, ER(ER_CANT_REMOVE_ALL_FIELDS), MYF(0));
return true;
}
/*
Collect all keys which isn't in drop list. Add only those
for which some fields exists.
*/
KeyInfo *key_info= table->key_info;
for (uint32_t i= 0; i < table->getShare()->sizeKeys(); i++, key_info++)
{
const char *key_name= key_info->name;
vector<string>::iterator it= drop_keys.begin();
while (it != drop_keys.end())
{
if (not system_charset_info->strcasecmp(key_name, it->c_str()))
break;
it++;
}
if (it != drop_keys.end())
{
drop_keys.erase(it);
continue;
}
KeyPartInfo *key_part= key_info->key_part;
List<Key_part_spec> key_parts;
for (uint32_t j= 0; j < key_info->key_parts; j++, key_part++)
{
if (not key_part->field)
continue; /* Wrong field (from UNIREG) */
const char *key_part_name= key_part->field->field_name;
CreateField *cfield;
List<CreateField>::iterator field_it= new_create_list.begin();
while ((cfield= field_it++))
{
if (cfield->change)
{
if (not system_charset_info->strcasecmp(key_part_name, cfield->change))
break;
}
else if (not system_charset_info->strcasecmp(key_part_name, cfield->field_name))
break;
}
if (not cfield)
continue; /* Field is removed */
uint32_t key_part_length= key_part->length;
if (cfield->field) /* Not new field */
{
/*
If the field can't have only a part used in a key according to its
new type, or should not be used partially according to its
previous type, or the field length is less than the key part
length, unset the key part length.
We also unset the key part length if it is the same as the
old field's length, so the whole new field will be used.
BLOBs may have cfield->length == 0, which is why we test it before
checking whether cfield->length < key_part_length (in chars).
*/
if (! Field::type_can_have_key_part(cfield->field->type()) ||
! Field::type_can_have_key_part(cfield->sql_type) ||
(cfield->field->field_length == key_part_length) ||
(cfield->length &&
(cfield->length < key_part_length / key_part->field->charset()->mbmaxlen)))
key_part_length= 0; /* Use whole field */
}
key_part_length/= key_part->field->charset()->mbmaxlen;
key_parts.push_back(new Key_part_spec(str_ref(cfield->field_name), key_part_length));
}
if (key_parts.size())
{
KEY_CREATE_INFO key_create_info= default_key_create_info;
key_create_info.algorithm= key_info->algorithm;
if (key_info->flags & HA_USES_BLOCK_SIZE)
key_create_info.block_size= key_info->block_size;
if (key_info->flags & HA_USES_COMMENT)
key_create_info.comment= key_info->comment;
Key::Keytype key_type= key_info->flags & HA_NOSAME
? (is_primary_key(key_name) ? Key::PRIMARY : Key::UNIQUE)
: Key::MULTIPLE;
new_key_list.push_back(new Key(key_type, str_ref(key_name), &key_create_info, test(key_info->flags & HA_GENERATED_KEY), key_parts));
}
}
/* Copy over existing foreign keys */
for (int32_t j= 0; j < original_proto.fk_constraint_size(); j++)
{
vector<string>::iterator it= drop_fkeys.begin();
while (it != drop_fkeys.end())
{
if (! system_charset_info->strcasecmp(original_proto.fk_constraint(j).name().c_str(), it->c_str()))
{
break;
}
it++;
}
if (it != drop_fkeys.end())
{
drop_fkeys.erase(it);
continue;
}
message::Table::ForeignKeyConstraint *pfkey= table_message.add_fk_constraint();
*pfkey= original_proto.fk_constraint(j);
}
{
Key *key;
List<Key>::iterator key_it(alter_info->key_list.begin());
while ((key= key_it++)) /* Add new keys */
{
if (key->type == Key::FOREIGN_KEY)
{
if (((Foreign_key *)key)->validate(new_create_list))
{
return true;
}
Foreign_key *fkey= (Foreign_key*)key;
add_foreign_key_to_table_message(&table_message,
fkey->name.data(),
fkey->columns,
fkey->ref_table,
fkey->ref_columns,
fkey->delete_opt,
fkey->update_opt,
fkey->match_opt);
}
if (key->type != Key::FOREIGN_KEY)
new_key_list.push_back(key);
if (key->name.data() && is_primary_key(key->name.data()))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.data());
return true;
}
}
}
/* Fix names of foreign keys being added */
for (int j= 0; j < table_message.fk_constraint_size(); j++)
{
if (! table_message.fk_constraint(j).has_name())
{
std::string name(table->getMutableShare()->getTableName());
char number[20];
name.append("_ibfk_");
snprintf(number, sizeof(number), "%d", j+1);
name.append(number);
message::Table::ForeignKeyConstraint *pfkey= table_message.mutable_fk_constraint(j);
pfkey->set_name(name);
}
}
if (drop_keys.size())
{
my_error(ER_CANT_DROP_FIELD_OR_KEY,
MYF(0),
drop_keys.front().c_str());
return true;
}
if (drop_columns.size())
{
my_error(ER_CANT_DROP_FIELD_OR_KEY,
MYF(0),
drop_columns.front().c_str());
return true;
}
if (drop_fkeys.size())
{
my_error(ER_CANT_DROP_FIELD_OR_KEY,
MYF(0),
drop_fkeys.front().c_str());
return true;
}
if (not alter_info->alter_list.empty())
{
my_error(ER_CANT_DROP_FIELD_OR_KEY,
MYF(0),
alter_info->alter_list.front().name);
return true;
}
if (not table_message.options().has_comment()
&& table->getMutableShare()->hasComment())
{
table_options->set_comment(table->getMutableShare()->getComment());
}
if (table->getShare()->getType())
{
table_message.set_type(message::Table::TEMPORARY);
}
table_message.set_creation_timestamp(table->getShare()->getTableMessage()->creation_timestamp());
table_message.set_version(table->getShare()->getTableMessage()->version());
table_message.set_uuid(table->getShare()->getTableMessage()->uuid());
alter_info->create_list.swap(new_create_list);
alter_info->key_list.swap(new_key_list);
size_t num_engine_options= table_message.engine().options_size();
size_t original_num_engine_options= original_proto.engine().options_size();
for (size_t x= 0; x < original_num_engine_options; ++x)
{
bool found= false;
for (size_t y= 0; y < num_engine_options; ++y)
{
found= not table_message.engine().options(y).name().compare(original_proto.engine().options(x).name());
if (found)
break;
}
if (not found)
{
message::Engine::Option *opt= table_message.mutable_engine()->add_options();
opt->set_name(original_proto.engine().options(x).name());
opt->set_state(original_proto.engine().options(x).state());
}
}
drizzled::message::update(table_message);
return false;
}
/* table_list should contain just one table */
static int discard_or_import_tablespace(Session *session,
TableList *table_list,
bool discard)
{
/*
Note that DISCARD/IMPORT TABLESPACE always is the only operation in an
ALTER Table
*/
session->set_proc_info("discard_or_import_tablespace");
/*
We set this flag so that ha_innobase::open and ::external_lock() do
not complain when we lock the table
*/
session->setDoingTablespaceOperation(true);
Table* table= session->openTableLock(table_list, TL_WRITE);
if (not table)
{
session->setDoingTablespaceOperation(false);
return -1;
}
int error;
do {
error= table->cursor->ha_discard_or_import_tablespace(discard);
session->set_proc_info("end");
if (error)
break;
/* The ALTER Table is always in its own transaction */
error= TransactionServices::autocommitOrRollback(*session, false);
if (not session->endActiveTransaction())
error= 1;
if (error)
break;
TransactionServices::rawStatement(*session,
*session->getQueryString(),
*session->schema());
} while(0);
(void) TransactionServices::autocommitOrRollback(*session, error);
session->setDoingTablespaceOperation(false);
if (error == 0)
{
session->my_ok();
return 0;
}
table->print_error(error, MYF(0));
return -1;
}
/**
Manages enabling/disabling of indexes for ALTER Table
SYNOPSIS
alter_table_manage_keys()
table Target table
indexes_were_disabled Whether the indexes of the from table
were disabled
keys_onoff ENABLE | DISABLE | LEAVE_AS_IS
RETURN VALUES
false OK
true Error
*/
static bool alter_table_manage_keys(Session *session,
Table *table, int indexes_were_disabled,
const message::AlterTable &alter_table_message)
{
int error= 0;
if (alter_table_message.has_alter_keys_onoff()
&& alter_table_message.alter_keys_onoff().enable())
{
error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
}
if ((! alter_table_message.has_alter_keys_onoff() && indexes_were_disabled)
|| (alter_table_message.has_alter_keys_onoff()
&& ! alter_table_message.alter_keys_onoff().enable()))
{
error= table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
}
if (error == HA_ERR_WRONG_COMMAND)
{
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
table->getMutableShare()->getTableName());
error= 0;
}
else if (error)
{
table->print_error(error, MYF(0));
}
return(error);
}
static bool lockTableIfDifferent(Session &session,
identifier::Table &original_table_identifier,
identifier::Table &new_table_identifier,
Table *name_lock)
{
/* Check that we are not trying to rename to an existing table */
if (not (original_table_identifier == new_table_identifier))
{
if (original_table_identifier.isTmp())
{
if (session.open_tables.find_temporary_table(new_table_identifier))
{
my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
return false;
}
}
else
{
name_lock= session.lock_table_name_if_not_cached(new_table_identifier);
if (not name_lock)
{
my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
return false;
}
if (plugin::StorageEngine::doesTableExist(session, new_table_identifier))
{
/* Table will be closed by Session::executeCommand() */
my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
{
boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
session.unlink_open_table(name_lock);
}
return false;
}
}
}
return true;
}
/**
Alter table
SYNOPSIS
alter_table()
session Thread handle
new_db If there is a RENAME clause
new_name If there is a RENAME clause
create_info Information from the parsing phase about new
table properties.
table_list The table to change.
alter_info Lists of fields, keys to be changed, added
or dropped.
order_num How many ORDER BY fields has been specified.
order List of fields to order_st BY.
ignore Whether we have ALTER IGNORE Table
DESCRIPTION
This is a veery long function and is everything but the kitchen sink :)
It is used to alter a table and not only by ALTER Table but also
CREATE|DROP INDEX are mapped on this function.
When the ALTER Table statement just does a RENAME or ENABLE|DISABLE KEYS,
or both, then this function short cuts its operation by renaming
the table and/or enabling/disabling the keys. In this case, the FRM is
not changed, directly by alter_table. However, if there is a
RENAME + change of a field, or an index, the short cut is not used.
See how `create_list` is used to generate the new FRM regarding the
structure of the fields. The same is done for the indices of the table.
Important is the fact, that this function tries to do as little work as
possible, by finding out whether a intermediate table is needed to copy
data into and when finishing the altering to use it as the original table.
For this reason the function compare_tables() is called, which decides
based on all kind of data how similar are the new and the original
tables.
RETURN VALUES
false OK
true Error
*/
static bool internal_alter_table(Session *session,
Table *table,
identifier::Table &original_table_identifier,
identifier::Table &new_table_identifier,
HA_CREATE_INFO *create_info,
const message::Table &original_proto,
message::Table &create_proto,
message::AlterTable &alter_table_message,
TableList *table_list,
AlterInfo *alter_info,
uint32_t order_num,
Order *order,
bool ignore)
{
int error= 0;
char tmp_name[80];
char old_name[32];
ha_rows copied= 0;
ha_rows deleted= 0;
if (not original_table_identifier.isValid())
return true;
if (not new_table_identifier.isValid())
return true;
session->set_proc_info("init");
table->use_all_columns();
plugin::StorageEngine *new_engine;
plugin::StorageEngine *original_engine;
original_engine= table->getMutableShare()->getEngine();
if (not create_info->db_type)
{
create_info->db_type= original_engine;
}
new_engine= create_info->db_type;
create_proto.set_schema(new_table_identifier.getSchemaName());
create_proto.set_type(new_table_identifier.getType());
/**
@todo Have a check on the table definition for FK in the future
to remove the need for the cursor. (aka can_switch_engines())
*/
if (new_engine != original_engine &&
not table->cursor->can_switch_engines())
{
assert(0);
my_error(ER_ROW_IS_REFERENCED, MYF(0));
return true;
}
if (original_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED) ||
new_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED))
{
my_error(ER_ILLEGAL_HA, new_table_identifier);
return true;
}
session->set_proc_info("setup");
/* First we try for operations that do not require a copying
ALTER TABLE.
In future there should be more operations, currently it's just a couple.
*/
if ((alter_table_message.has_rename()
|| alter_table_message.has_alter_keys_onoff())
&& alter_table_message.operations_size() == 0)
{
/*
* test if no other bits except ALTER_RENAME and ALTER_KEYS_ONOFF are set
*/
bitset<32> tmp;
tmp.set();
tmp.reset(ALTER_RENAME);
tmp.reset(ALTER_KEYS_ONOFF);
tmp&= alter_info->flags;
if((not (tmp.any()) && not table->getShare()->getType())) // no need to touch frm
{
if (alter_table_message.has_alter_keys_onoff())
{
error= apply_online_alter_keys_onoff(session, table,
alter_table_message.alter_keys_onoff());
if (error == HA_ERR_WRONG_COMMAND)
{
error= EE_OK;
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
table->getAlias());
}
}
if (alter_table_message.has_rename())
{
error= apply_online_rename_table(session,
table,
original_engine,
original_table_identifier,
new_table_identifier,
alter_table_message.rename());
if (error == HA_ERR_WRONG_COMMAND)
{
error= EE_OK;
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
table->getAlias());
}
}
if (not error)
{
TransactionServices::allocateNewTransactionId();
TransactionServices::rawStatement(*session,
*session->getQueryString(),
*session->schema());
session->my_ok();
}
else if (error > EE_OK) // If we have already set the error, we pass along -1
{
table->print_error(error, MYF(0));
}
table_list->table= NULL;
return error;
}
}
if (alter_table_message.build_method() == message::AlterTable::BUILD_ONLINE)
{
my_error(*session->getQueryString(), ER_NOT_SUPPORTED_YET);
return true;
}
/* We have to do full alter table. */
new_engine= create_info->db_type;
if (prepare_alter_table(session, table, create_info, original_proto, create_proto, alter_table_message, alter_info))
{
return true;
}
set_table_default_charset(create_info, new_table_identifier.getSchemaName().c_str());
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
/* Create a temporary table with the new format */
/**
@note we make an internal temporary table unless the table is a temporary table. In last
case we just use it as is. Neither of these tables require locks in order to be
filled.
*/
identifier::Table new_table_as_temporary(original_table_identifier.getSchemaName(),
tmp_name,
create_proto.type() != message::Table::TEMPORARY ? message::Table::INTERNAL :
message::Table::TEMPORARY);
/*
Create a table with a temporary name.
We don't log the statement, it will be logged later.
*/
create_proto.set_name(new_table_as_temporary.getTableName());
create_proto.mutable_engine()->set_name(create_info->db_type->getName());
error= create_table(session,
new_table_as_temporary,
create_info, create_proto, alter_info, true, 0, false);
if (error != 0)
{
return true;
}
/* Open the table so we need to copy the data to it. */
Table *new_table= open_alter_table(session, table, new_table_as_temporary);
if (not new_table)
{
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
return true;
}
/* Copy the data if necessary. */
{
/* We must not ignore bad input! */
session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL; // calc cuted fields
session->cuted_fields= 0L;
session->set_proc_info("copy to tmp table");
copied= deleted= 0;
/* We don't want update TIMESTAMP fields during ALTER Table. */
new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
new_table->next_number_field= new_table->found_next_number_field;
error= copy_data_between_tables(session,
table,
new_table,
alter_info->create_list,
ignore,
order_num,
order,
&copied,
&deleted,
alter_table_message,
alter_info->error_if_not_empty);
/* We must not ignore bad input! */
assert(session->count_cuted_fields == CHECK_FIELD_ERROR_FOR_NULL);
}
/* Now we need to resolve what just happened with the data copy. */
if (error)
{
/*
No default value was provided for new fields.
*/
if (alter_info->error_if_not_empty && session->row_count)
{
my_error(ER_INVALID_ALTER_TABLE_FOR_NOT_NULL, MYF(0));
}
if (original_table_identifier.isTmp())
{
if (new_table)
{
/* close_temporary_table() frees the new_table pointer. */
session->open_tables.close_temporary_table(new_table);
}
else
{
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
}
return true;
}
else
{
if (new_table)
{
/*
Close the intermediate table that will be the new table.
Note that MERGE tables do not have their children attached here.
*/
new_table->intern_close_table();
if (new_table->hasShare())
{
delete new_table->getMutableShare();
}
delete new_table;
}
boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
return true;
}
}
// Temporary table and success
else if (original_table_identifier.isTmp())
{
/* Close lock if this is a transactional table */
if (session->open_tables.lock)
{
session->unlockTables(session->open_tables.lock);
session->open_tables.lock= 0;
}
/* Remove link to old table and rename the new one */
session->open_tables.close_temporary_table(table);
/* Should pass the 'new_name' as we store table name in the cache */
new_table->getMutableShare()->setIdentifier(new_table_identifier);
new_table_identifier.setPath(new_table_as_temporary.getPath());
if (rename_table(*session, new_engine, new_table_as_temporary, new_table_identifier) != 0)
{
return true;
}
}
// Normal table success
else
{
if (new_table)
{
/*
Close the intermediate table that will be the new table.
Note that MERGE tables do not have their children attached here.
*/
new_table->intern_close_table();
if (new_table->hasShare())
{
delete new_table->getMutableShare();
}
delete new_table;
}
{
boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* ALTER TABLE */
/*
Data is copied. Now we:
1) Wait until all other threads close old version of table.
2) Close instances of table open by this thread and replace them
with exclusive name-locks.
3) Rename the old table to a temp name, rename the new one to the
old name.
4) If we are under LOCK TABLES and don't do ALTER Table ... RENAME
we reopen new version of table.
5) Write statement to the binary log.
6) If we are under LOCK TABLES and do ALTER Table ... RENAME we
remove name-locks from list of open tables and table cache.
7) If we are not not under LOCK TABLES we rely on close_thread_tables()
call to remove name-locks from table cache and list of open table.
*/
session->set_proc_info("rename result table");
snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
files_charset_info->casedn_str(old_name);
wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
session->close_data_files_and_morph_locks(original_table_identifier);
assert(not error);
/*
This leads to the storage engine (SE) not being notified for renames in
rename_table(), because we just juggle with the FRM and nothing
more. If we have an intermediate table, then we notify the SE that
it should become the actual table. Later, we will recycle the old table.
However, in case of ALTER Table RENAME there might be no intermediate
table. This is when the old and new tables are compatible, according to
compare_table(). Then, we need one additional call to
*/
identifier::Table original_table_to_drop(original_table_identifier.getSchemaName(),
old_name, create_proto.type() != message::Table::TEMPORARY ? message::Table::INTERNAL :
message::Table::TEMPORARY);
drizzled::error_t rename_error= EE_OK;
if (rename_table(*session, original_engine, original_table_identifier, original_table_to_drop))
{
error= ER_ERROR_ON_RENAME;
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
}
else
{
if (rename_table(*session, new_engine, new_table_as_temporary, new_table_identifier) != 0)
{
/* Try to get everything back. */
rename_error= ER_ERROR_ON_RENAME;
plugin::StorageEngine::dropTable(*session, new_table_identifier);
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
rename_table(*session, original_engine, original_table_to_drop, original_table_identifier);
}
else
{
plugin::StorageEngine::dropTable(*session, original_table_to_drop);
}
}
if (rename_error)
{
/*
An error happened while we were holding exclusive name-lock on table
being altered. To be safe under LOCK TABLES we should remove placeholders
from list of open tables list and table cache.
*/
session->unlink_open_table(table);
return true;
}
}
session->set_proc_info("end");
TransactionServices::rawStatement(*session,
*session->getQueryString(),
*session->schema());
table_list->table= NULL;
}
/*
* Field::store() may have called my_error(). If this is
* the case, we must not send an ok packet, since
* Diagnostics_area::is_set() will fail an assert.
*/
if (session->is_error())
{
/* my_error() was called. Return true (which means error...) */
return true;
}
snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
(ulong) (copied + deleted), (ulong) deleted,
(ulong) session->cuted_fields);
session->my_ok(copied + deleted, 0, 0L, tmp_name);
return false;
}
static int apply_online_alter_keys_onoff(Session *session,
Table* table,
const message::AlterTable::AlterKeysOnOff &op)
{
int error= 0;
if (op.enable())
{
/*
wait_while_table_is_used() ensures that table being altered is
opened only by this thread and that Table::TableShare::version
of Table object corresponding to this table is 0.
The latter guarantees that no DML statement will open this table
until ALTER Table finishes (i.e. until close_thread_tables())
while the fact that the table is still open gives us protection
from concurrent DDL statements.
*/
{
boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* DDL wait for/blocker */
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
}
error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
/* COND_refresh will be signaled in close_thread_tables() */
}
else
{
{
boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* DDL wait for/blocker */
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
}
error= table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
/* COND_refresh will be signaled in close_thread_tables() */
}
return error;
}
static int apply_online_rename_table(Session *session,
Table *table,
plugin::StorageEngine *original_engine,
identifier::Table &original_table_identifier,
identifier::Table &new_table_identifier,
const message::AlterTable::RenameTable &alter_operation)
{
int error= 0;
boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* Lock to remove all instances of table from table cache before ALTER */
/*
Unlike to the above case close_cached_table() below will remove ALL
instances of Table from table cache (it will also remove table lock
held by this thread). So to make actual table renaming and writing
to binlog atomic we have to put them into the same critical section
protected by table::Cache::mutex() mutex. This also removes gap for races between
access() and rename_table() calls.
*/
if (not (original_table_identifier == new_table_identifier))
{
assert(alter_operation.to_name() == new_table_identifier.getTableName());
assert(alter_operation.to_schema() == new_table_identifier.getSchemaName());
session->set_proc_info("rename");
/*
Then do a 'simple' rename of the table. First we need to close all
instances of 'source' table.
*/
session->close_cached_table(table);
/*
Then, we want check once again that target table does not exist.
Actually the order of these two steps does not matter since
earlier we took name-lock on the target table, so we do them
in this particular order only to be consistent with 5.0, in which
we don't take this name-lock and where this order really matters.
@todo Investigate if we need this access() check at all.
*/
if (plugin::StorageEngine::doesTableExist(*session, new_table_identifier))
{
my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
error= -1;
}
else
{
if (rename_table(*session, original_engine, original_table_identifier, new_table_identifier))
{
error= -1;
}
}
}
return error;
}
bool alter_table(Session *session,
identifier::Table &original_table_identifier,
identifier::Table &new_table_identifier,
HA_CREATE_INFO *create_info,
const message::Table &original_proto,
message::Table &create_proto,
TableList *table_list,
AlterInfo *alter_info,
uint32_t order_num,
Order *order,
bool ignore)
{
bool error;
Table *table;
message::AlterTable *alter_table_message= session->lex().alter_table();
alter_table_message->set_catalog(original_table_identifier.getCatalogName());
alter_table_message->set_schema(original_table_identifier.getSchemaName());
alter_table_message->set_name(original_table_identifier.getTableName());
if (alter_table_message->operations_size()
&& (alter_table_message->operations(0).operation()
== message::AlterTable::AlterTableOperation::DISCARD_TABLESPACE
|| alter_table_message->operations(0).operation()
== message::AlterTable::AlterTableOperation::IMPORT_TABLESPACE))
{
bool discard= (alter_table_message->operations(0).operation() ==
message::AlterTable::AlterTableOperation::DISCARD_TABLESPACE);
/* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
return discard_or_import_tablespace(session, table_list, discard);
}
session->set_proc_info("init");
if (not (table= session->openTableLock(table_list, TL_WRITE_ALLOW_READ)))
return true;
session->set_proc_info("gained write lock on table");
/*
Check that we are not trying to rename to an existing table,
if one existed we get a lock, if we can't we error.
*/
{
Table *name_lock= NULL;
if (not lockTableIfDifferent(*session, original_table_identifier, new_table_identifier, name_lock))
{
return true;
}
error= internal_alter_table(session,
table,
original_table_identifier,
new_table_identifier,
create_info,
original_proto,
create_proto,
*alter_table_message,
table_list,
alter_info,
order_num,
order,
ignore);
if (name_lock)
{
boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
session->unlink_open_table(name_lock);
}
}
return error;
}
/* alter_table */
static int
copy_data_between_tables(Session *session,
Table *from, Table *to,
List<CreateField> &create,
bool ignore,
uint32_t order_num, Order *order,
ha_rows *copied,
ha_rows *deleted,
message::AlterTable &alter_table_message,
bool error_if_not_empty)
{
int error= 0;
CopyField *copy,*copy_end;
ulong found_count,delete_count;
uint32_t length= 0;
SortField *sortorder;
ReadRecord info;
TableList tables;
List<Item> fields;
List<Item> all_fields;
ha_rows examined_rows;
bool auto_increment_field_copied= 0;
uint64_t prev_insert_id;
/*
Turn off recovery logging since rollback of an alter table is to
delete the new table so there is no need to log the changes to it.
This needs to be done before external_lock
*/
/*
* LP Bug #552420
*
* Since open_temporary_table() doesn't invoke lockTables(), we
* don't get the usual automatic call to StorageEngine::startStatement(), so
* we manually call it here...
*/
to->getMutableShare()->getEngine()->startStatement(session);
copy= new CopyField[to->getShare()->sizeFields()];
if (to->cursor->ha_external_lock(session, F_WRLCK))
return -1;
/* We need external lock before we can disable/enable keys */
alter_table_manage_keys(session, to, from->cursor->indexes_are_disabled(),
alter_table_message);
/* We can abort alter table for any table type */
session->setAbortOnWarning(not ignore);
from->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
to->cursor->ha_start_bulk_insert(from->cursor->stats.records);
List<CreateField>::iterator it(create.begin());
copy_end= copy;
for (Field **ptr= to->getFields(); *ptr ; ptr++)
{
CreateField* def=it++;
if (def->field)
{
if (*ptr == to->next_number_field)
auto_increment_field_copied= true;
(copy_end++)->set(*ptr,def->field,0);
}
}
found_count=delete_count=0;
do
{
if (order)
{
if (to->getShare()->hasPrimaryKey() && to->cursor->primary_key_is_clustered())
{
char warn_buff[DRIZZLE_ERRMSG_SIZE];
snprintf(warn_buff, sizeof(warn_buff),
_("order_st BY ignored because there is a user-defined clustered "
"index in the table '%-.192s'"),
from->getMutableShare()->getTableName());
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
warn_buff);
}
else
{
FileSort filesort(*session);
from->sort.io_cache= new internal::io_cache_st;
tables.table= from;
tables.setTableName(from->getMutableShare()->getTableName());
tables.alias= tables.getTableName();
tables.setSchemaName(from->getMutableShare()->getSchemaName());
error= 1;
session->lex().select_lex.setup_ref_array(session, order_num);
if (setup_order(session, session->lex().select_lex.ref_pointer_array, &tables, fields, all_fields, order))
break;
sortorder= make_unireg_sortorder(order, &length, NULL);
if ((from->sort.found_records= filesort.run(from, sortorder, length, (optimizer::SqlSelect *) 0, HA_POS_ERROR, 1, examined_rows)) == HA_POS_ERROR)
break;
}
}
/* Tell handler that we have values for all columns in the to table */
to->use_all_columns();
error= info.init_read_record(session, from, NULL, 1, true);
if (error)
{
to->print_error(errno, MYF(0));
break;
}
if (ignore)
{
to->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
}
session->row_count= 0;
to->restoreRecordAsDefault(); // Create empty record
while (not (error=info.read_record(&info)))
{
if (session->getKilled())
{
session->send_kill_message();
error= 1;
break;
}
session->row_count++;
/* Return error if source table isn't empty. */
if (error_if_not_empty)
{
error= 1;
break;
}
if (to->next_number_field)
{
if (auto_increment_field_copied)
to->auto_increment_field_not_null= true;
else
to->next_number_field->reset();
}
for (CopyField *copy_ptr= copy; copy_ptr != copy_end ; copy_ptr++)
{
if (not copy->to_field->hasDefault() and copy->from_null_ptr and *copy->from_null_ptr & copy->from_bit)
{
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
ER_WARN_DATA_TRUNCATED, 1);
copy->to_field->reset();
error= 1;
break;
}
copy_ptr->do_copy(copy_ptr);
}
if (error)
{
break;
}
prev_insert_id= to->cursor->next_insert_id;
error= to->cursor->insertRecord(to->record[0]);
to->auto_increment_field_not_null= false;
if (error)
{
if (!ignore || to->cursor->is_fatal_error(error, HA_CHECK_DUP))
{
to->print_error(error, MYF(0));
break;
}
to->cursor->restore_auto_increment(prev_insert_id);
delete_count++;
}
else
{
found_count++;
}
}
info.end_read_record();
from->free_io_cache();
delete[] copy;
if (to->cursor->ha_end_bulk_insert() && error <= 0)
{
to->print_error(errno, MYF(0));
error= 1;
}
to->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
/*
Ensure that the new table is saved properly to disk so that we
can do a rename
*/
if (TransactionServices::autocommitOrRollback(*session, false))
error= 1;
if (not session->endActiveTransaction())
error= 1;
} while (0);
session->setAbortOnWarning(false);
from->free_io_cache();
*copied= found_count;
*deleted=delete_count;
to->cursor->ha_release_auto_increment();
if (to->cursor->ha_external_lock(session, F_UNLCK))
{
error= 1;
}
return error > 0 ? -1 : 0;
}
static Table *open_alter_table(Session *session, Table *table, identifier::Table &identifier)
{
/* Open the table so we need to copy the data to it. */
if (table->getShare()->getType())
{
TableList tbl;
tbl.setSchemaName(identifier.getSchemaName().c_str());
tbl.alias= identifier.getTableName().c_str();
tbl.setTableName(identifier.getTableName().c_str());
/* Table is in session->temporary_tables */
return session->openTable(&tbl, NULL, DRIZZLE_LOCK_IGNORE_FLUSH);
}
else
{
/* Open our intermediate table */
return session->open_temporary_table(identifier, false);
}
}
} /* namespace drizzled */
|