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
|
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* 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
*/
#ifndef _WIN32
#include <stdio.h>
#endif
#include "base/string_utilities.h"
#include "base/util_functions.h"
#include "base/log.h"
#include <boost/scoped_array.hpp>
#include "db_object_helpers.h"
#include "grtpp_undo_manager.h"
#include "grtpp_util.h"
#include "grt/common.h"
#include "grt/parse_utils.h"
#include "grts/structs.workbench.physical.h"
#include "grtdb/db_helpers.h"
#include "mysql-parser.h"
#include "MySQLParser.h" // For token types.
DEFAULT_LOG_DOMAIN("dbhelpers");
#ifndef _WIN32
#include <string.h>
#endif
using namespace bec;
using namespace grt;
template<class T>
class auto_array_ptr
{
T *_ptr;
public:
auto_array_ptr(T* ptr) : _ptr(ptr) {}
~auto_array_ptr() { delete[] _ptr; }
operator T* () { return _ptr; }
};
// Important: this functions works only for model objects, not live objects!
db_mgmt_RdbmsRef get_rdbms_for_db_object(const ::grt::ValueRef &object)
{
GrtObjectRef parent= GrtObjectRef::cast_from(object);
while (parent.is_valid() && !parent.is_instance("workbench.physical.Model"))
parent= parent->owner();
// do it the hard way to avoid havig to link to objimpl for model
//return workbench_physical_ModelRef::cast_from(parent)->rdbms();
if (parent.is_valid())
return db_mgmt_RdbmsRef::cast_from(parent.get_member("rdbms"));
return db_mgmt_RdbmsRef();
}
db_SimpleDatatypeRef CatalogHelper::get_datatype(grt::ListRef<db_SimpleDatatype> types, const std::string &name)
{
for (size_t c= types.count(), i= 0; i < c; i++)
{
if (base::string_compare(types[i]->name(), name, false)==0)
return types[i];
//for (size_t d= types[i]->synonyms().count(), j= 0; j < d; j++)
//{
// if (g_strcasecmp(types[i]->synonyms().get(j).c_str(), name.c_str())==0)
// return types[i];
//}
}
return db_SimpleDatatypeRef();
}
//--------------------------------------------------------------------------------------------------
bool CatalogHelper::is_type_valid_for_version(const db_SimpleDatatypeRef &type, const GrtVersionRef &target_version)
{
std::string validity = type->validity();
GrtVersionRef valid_version;
if (!validity.empty())
{
bool match = false;
switch (validity[0])
{
case '<':
if (validity[1] == '=')
{
valid_version = parse_version(type.get_grt(), validity.substr(2));
if (version_equal(target_version, valid_version) || version_greater(valid_version, target_version))
match = true;
}
else
{
valid_version = parse_version(type.get_grt(), validity.substr(1));
if (version_greater(valid_version, target_version))
match = true;
}
break;
case '>':
if (validity[1] == '=')
{
valid_version = parse_version(type.get_grt(), validity.substr(2));
if (version_equal(target_version, valid_version) || version_greater(target_version, valid_version))
match = true;
}
else
{
valid_version = parse_version(type.get_grt(), validity.substr(1));
if (version_greater(target_version, valid_version))
match = true;
}
break;
case '=':
valid_version = parse_version(type.get_grt(), validity.substr(1));
if (version_equal(target_version, valid_version))
match = true;
break;
}
return match;
}
return true;
}
std::string CatalogHelper::dbobject_to_dragdata(const db_DatabaseObjectRef &object)
{
return object.class_name()+":"+object.id();
}
db_DatabaseObjectRef CatalogHelper::dragdata_to_dbobject(const db_CatalogRef &catalog, const std::string &data)
{
if (data.find(':') != std::string::npos)
{
std::string oid= data.substr(data.find(':')+1);
return db_DatabaseObjectRef::cast_from(find_child_object(catalog, oid));
}
return db_DatabaseObjectRef();
}
std::string CatalogHelper::dbobject_list_to_dragdata(const std::list<db_DatabaseObjectRef> &objects)
{
std::string ret;
for (std::list<db_DatabaseObjectRef>::const_iterator iter= objects.begin(); iter != objects.end(); ++iter)
{
if (!ret.empty())
ret.append("\n");
ret.append(dbobject_to_dragdata(*iter));
}
return ret;
}
std::list<db_DatabaseObjectRef> CatalogHelper::dragdata_to_dbobject_list(const db_CatalogRef &catalog, const std::string &data)
{
std::list<db_DatabaseObjectRef> dbobjects;
std::vector<std::string> items= base::split(data, "\n");
for (std::vector<std::string>::const_iterator item= items.begin(); item != items.end(); ++item)
{
db_DatabaseObjectRef object= dragdata_to_dbobject(catalog, *item);
if (object.is_valid())
dbobjects.push_back(object);
}
return dbobjects;
}
//------------------------------------------------------------------------------------
std::set<std::string> SchemaHelper::get_foreign_key_names(const db_SchemaRef &schema)
{
std::set<std::string> used_names;
GRTLIST_FOREACH(db_Table, schema->tables(), table)
{
GRTLIST_FOREACH(db_ForeignKey, (*table)->foreignKeys(), fk)
{
used_names.insert((*fk)->name());
}
}
return used_names;
}
std::string SchemaHelper::get_unique_foreign_key_name(std::set<std::string> &used_names,
const std::string &prefix_,
int maxlength)
{
std::string prefix;
std::string the_name= prefix_;
int index= 0;
// truncate if too long, on byte count, because that's what matters when creating
// stuff on the server
if ((int) the_name.size() > maxlength-2)
{
const char *start = the_name.c_str();
const char *end = the_name.c_str()+maxlength-2;
the_name= the_name.substr(0, g_utf8_find_prev_char(start, end)-start);
}
prefix= the_name;
while (used_names.find(the_name) != used_names.end())
the_name= base::strfmt("%s%i", prefix.c_str(), index++);
if (the_name != prefix)
used_names.insert(the_name);
return the_name;
}
std::string SchemaHelper::get_unique_foreign_key_name(const db_SchemaRef &schema,
const std::string &prefix_,
int maxlength)
{
std::set<std::string> used_names;
std::string prefix;
std::string the_name= prefix_;
int index= 0;
// truncate if too long, on byte count, because that's what matters when creating
// stuff on the server
if ((int) the_name.size() > maxlength-2)
{
const char *start = the_name.c_str();
const char *end = the_name.c_str()+maxlength-2;
the_name= the_name.substr(0, g_utf8_find_prev_char(start, end)-start);
}
prefix= the_name;
GRTLIST_FOREACH(db_Table, schema->tables(), table)
{
GRTLIST_FOREACH(db_ForeignKey, (*table)->foreignKeys(), fk)
{
used_names.insert((*fk)->name());
if (the_name == prefix && index == 0)
index++;
}
}
// prefix is duplicated
if (index > 0)
{
do
{
the_name= base::strfmt("%s%i", prefix.c_str(), index++);
} while (used_names.find(the_name) != used_names.end());
}
return the_name;
}
//------------------------------------------------------------------------------------
db_TableRef TableHelper::create_associative_table(const db_SchemaRef &schema,
const db_TableRef &table1, const db_TableRef &table2,
bool mandatory1, bool mandatory2,
const db_mgmt_RdbmsRef &rdbms,
const grt::DictRef &global_options,
const grt::DictRef &options)
{
db_TableRef atable;
std::string name;
AutoUndo undo(schema->get_grt());
name= options.get_string("AuxTableTemplate", global_options.get_string("AuxTableTemplate", "%stable%_%dtable%"));
name= replace_variable(name, "%stable%", table1->name().c_str());
name= replace_variable(name, "%dtable%", table2->name().c_str());
atable= schema.get_grt()->create_object<db_Table>(table1.get_metaclass()->name());
atable->owner(schema);
atable->name(get_name_suggestion_for_list_object(schema->tables(), name, false));
atable->oldName(atable->name());
if (atable.has_member("tableEngine"))
atable->set_member("tableEngine", table1.get_member("tableEngine"));
if (atable.has_member("defaultCharacterSetName"))
atable->set_member("defaultCharacterSetName", table1.get_member("defaultCharacterSetName"));
if (atable.has_member("defaultCollationName"))
atable->set_member("defaultCollationName", table1.get_member("defaultCollationName"));
db_ForeignKeyRef first_fk = create_foreign_key_to_table(atable, table1, true, mandatory1, true, true, rdbms, global_options, options);
schema->tables().insert(atable);//Insert put newly added FK into schema to avoid name collision
create_foreign_key_to_table(atable, table2, true, mandatory2, true, true, rdbms, global_options, options);
// when the 1st FK is created, the PK and FK will have the same columns, so the index will be reused
// when the 2nd FK is created, the PK will have both columns so it can't be used by the 1st FK, so we have
// to create a new one for it
db_IndexRef index = create_index_for_fk(schema->get_grt(), first_fk, rdbms->maximumIdentifierLength());
first_fk->index(index);
atable->indices().insert(index);
atable->createDate(grt::StringRef(base::fmttime(0, DATETIME_FMT)));
atable->lastChangeDate(atable->createDate());
undo.end("Create Associative Table");
return atable;
}
//
//static std::string format_ident_with_table(const std::string &fmt, const db_TableRef &table)
//{
// return replace_variable(fmt, "%table%", table->name().c_str());
//}
static std::string format_ident_with_stable_dtable(const std::string &fmt, const db_TableRef &stable, const db_TableRef &dtable)
{
return replace_variable(replace_variable(fmt, "%stable%", stable->name().c_str()), "%dtable%", dtable->name().c_str());
}
static std::string format_ident_with_column(const std::string &fmt, const db_ColumnRef &column)
{
return replace_variable(replace_variable(fmt, "%table%", db_TableRef::cast_from(column->owner())->name().c_str()),
"%column%", column->name().c_str());
}
db_IndexRef TableHelper::create_index_for_fk(grt::GRT *grt, const db_ForeignKeyRef &fk, const size_t max_len)
{
std::string index_name(fk->name().c_str());
if (index_name.length() > (max_len-5))
index_name.resize(max_len-5);
index_name.append("_idx");
index_name = grt::get_name_suggestion_for_list_object(fk->owner()->indices(),index_name,false);
// add the corresponding index
db_IndexRef index= grt->create_object<db_Index>(db_TableRef::cast_from(fk->owner()).get_metaclass()->get_member_type("indices").content.object_class.c_str());
index->owner(fk->owner());
index->name(index_name);
index->oldName(fk->oldName());
index->indexType("INDEX");
for(size_t i= 0, sz= fk->columns().count(); i < sz; i++)
{
db_ColumnRef col(fk->columns().get(i));
db_IndexColumnRef index_col(grt->create_object<db_IndexColumn>(index.get_metaclass()->get_member_type("columns").content.object_class));
index_col->owner(index);
// "name", col->name().valueptr(),
index_col->descend(grt::IntegerRef(0));
//"columnLength", grt::IntegerRef(col->length().valueptr()).valueptr(),
index_col->columnLength(grt::IntegerRef(0));
index_col->referencedColumn(col);
index->columns().insert(index_col);
}
return index;
}
void TableHelper::reorder_foreign_key_for_index(const db_ForeignKeyRef &fk, const db_IndexRef &index)
{
// make the order of the columns in the FK match that of the index
size_t column_count = fk->columns().count();
if (fk->columns().count() != fk->referencedColumns().count())
{
log_error("Internal consistency error: number of items in fk->columns and fk->referencedColumns() for %s.%s.%s do not match\n",
fk->owner()->owner()->name().c_str(), fk->owner()->name().c_str(), fk->name().c_str());
return;
}
if (column_count > index->columns().count())
{
log_error("Internal consistency error: number of items in index for FK is less than columns in FK %s.%s.%s\n",
fk->owner()->owner()->name().c_str(), fk->owner()->name().c_str(), fk->name().c_str());
return;
}
for (size_t j= 0; j < column_count; j++)
{
if (index->columns()[j]->referencedColumn() != fk->columns()[j])
{
// if the column in the index is not the expected one, reorder the FK
// find the position of the column in the FK
for (size_t k = j+1; k < column_count; k++)
{
if (index->columns()[j]->referencedColumn() == fk->columns()[k])
{
// reorder the columns in the FK
fk->columns().reorder(k, j);
fk->referencedColumns().reorder(k, j);
break;
}
}
break;
}
}
}
db_IndexRef TableHelper::find_index_usable_by_fk(const db_ForeignKeyRef &fk, const db_IndexRef &other_than, bool allow_any_order)
{
size_t column_count = fk->columns().count();
db_TableRef table(db_TableRef::cast_from(fk->owner()));
if (column_count == 0)
return db_IndexRef();
for (size_t c= table->indices().count(), i= 0; i < c; i++)
{
db_IndexRef index(table->indices()[i]);
if (index == other_than)
continue;
// smaller indexes than the FK won't work
size_t index_column_count = index->columns().count();
if (index_column_count >= column_count)
{
bool index_usable;
if (allow_any_order)
{
index_usable = true;
for (size_t j= 0; j < column_count; j++)
{
bool ok= false;
// check whether this FK column is in the index
for (size_t k= 0; k < index_column_count; k++)
{
if (index->columns()[k]->referencedColumn() == fk->columns()[j])
{
ok= true;
break;
}
}
if (!ok)
{
index_usable= false;
break;
}
}
if (index_usable)
{
size_t c = 0;
// now check whether only columns from the prefix of the index are in use in the FK..
// example: FK (a, b) and INDEX (a, b, c) is OK
// FK (b, c) and INDEX (a, b, c) is not ok
for (size_t j= 0; j < column_count && c < column_count; j++)
{
if (fk->columns().get_index(index->columns()[j]->referencedColumn()) != grt::BaseListRef::npos)
c++; // jth column is in the FK, so it's OK so far
else
{
index_usable = false;
break; // jth column of the index is not in the FK, so this index can't be used
}
}
}
}
else
{
index_usable = false;
// make sure that the FK form prefix of the columns in the index
for (size_t j= 0; j < column_count; j++)
{
if (index->columns()[j]->referencedColumn() != fk->columns()[j])
{
index_usable = false;
break;
}
else
index_usable = true; // there's at least one match
}
}
if (index_usable)
return index;
}
}
return db_IndexRef();
}
void TableHelper::update_foreign_keys_from_column_notnull(const db_TableRef &table, const db_ColumnRef &column)
{
AutoUndo undo(table->get_grt());
// go through all foreign keys and update the ones that have this column
grt::ListRef<db_ForeignKey> fklist(table->foreignKeys());
for (size_t c= fklist.count(), i= 0; i < c; i++)
{
db_ForeignKeyRef fk(fklist[i]);
size_t notnull= 0;
bool flag= false;
for (size_t d= fk->columns().count(), j= 0; j < d; j++)
{
db_ColumnRef col(fk->columns()[j]);
if (*col->isNotNull())
notnull++;
if (col == column)
flag= true;
}
if (flag)
{
if (notnull == fk->columns().count())
{
// if the FK is optional, then it means the ref table is optional
fk->referencedMandatory(1);
}
else if (notnull == 0)
{
fk->referencedMandatory(0);
}
}
}
undo.end("Update FK Mandatory Flag");
}
std::string TableHelper::generate_foreign_key_name()
{
return std::string("fk_") + grt::get_guid();
}
db_ForeignKeyRef TableHelper::create_empty_foreign_key(grt::GRT *grt, const db_TableRef &table, const std::string &name)
{
db_ForeignKeyRef fk;
// create a new FK
fk= grt->create_object<db_ForeignKey>(table.get_metaclass()->get_member_type("foreignKeys").content.object_class);
fk->owner(table);
fk->name(name.empty()?generate_foreign_key_name():name);
grt::AutoUndo undo(grt);
table->foreignKeys().insert(fk);
/* don't always create an index for the FK, it should only be created if there are no other indexes that match it
the check for indexes should be done every time the FK columns are edited
db_IndexRef index(create_index_for_fk(grt, fk));
fk->index(index);
table->indices().insert(index);
*/
undo.end(_("Create Foreign Key"));
return fk;
}
void TableHelper::update_foreign_key_index(const db_ForeignKeyRef &fk)
{
//! todo: add undo group
db_TableRef table(fk->owner());
db_IndexRef index(fk->index());
if (index.is_valid())
{
db_IndexRef other;
if ((other = find_index_usable_by_fk(fk, index, true)).is_valid())
{
// the index from this FK is redundant, so remove it
fk->index(db_IndexRef());
table->indices().remove_value(index);
reorder_foreign_key_for_index(fk, other);
return;
}
// make sure that the columns in the index match the ones in the FK
// remove columns that are gone from the FK
for (ssize_t i = index->columns().count() - 1; i >= 0; --i)
{
if (fk->columns().get_index(index->columns()[i]) == grt::BaseListRef::npos)
index->columns().remove(i);
}
// recreate index columns
while (index->columns().count() > 0)
index->columns().remove(0);
ListRef<db_Column> fk_columns(fk->columns());
for (size_t n= 0, count= fk_columns.count(); n < count; ++n)
{
db_ColumnRef column(fk_columns.get(n));
db_IndexColumnRef index_column(fk->get_grt()->create_object<db_IndexColumn>(index.get_metaclass()->get_member_type("columns").content.object_class));
index_column->owner(index);
//"name", column->name().valueptr(),
index_column->referencedColumn(column);
index->columns().insert(index_column);
}
if (index->columns().count() == 0)
{
// the index is empty, remove it
fk->index(db_IndexRef());
table->indices().remove_value(index);
return;
}
}
else
create_index_for_fk_if_needed(fk);
}
bool TableHelper::create_index_for_fk_if_needed(db_ForeignKeyRef fk)
{
db_IndexRef index = find_index_usable_by_fk(fk, db_IndexRef(), true);
if (index.is_valid())
reorder_foreign_key_for_index(fk, index);
else if (fk->columns().count() > 0)
{
index = create_index_for_fk(fk.get_grt(), fk);
fk->index(index);
fk->owner()->indices().insert(index);
return true;
}
return false;
}
bool TableHelper::create_missing_indexes_for_foreign_keys(const db_TableRef &table)
{
bool created= false;
GRTLIST_FOREACH(db_ForeignKey, table->foreignKeys(), fk)
{
// check if an index already exists for the FK columns and if not, create one
if (!(*fk)->index().is_valid())
created = created || create_index_for_fk_if_needed(*fk);
else
reorder_foreign_key_for_index(*fk, (*fk)->index());
}
return created;
}
/**
****************************************************************************
* @brief Creates a foreign key in table, refering to ref_table
*
*
* Options:
* FKNameTemplate, FKColumnNameTemplate, db_ForeignKey:deleteRule, updateRule
*
* @param table the table to have the FK added to
* @param ref_table the table that the FK refers to
* @param mandatory if the relationship is mandatory in table
* @param ref_mandatory if the relationship is mandatory in referenced table
* @param many cardinality of the table in the relationship
* @param identifying whether the rel is identifying (FK is also made PK)
* @param options the options dictionary (see above)
*
* @return the created foreign key
****************************************************************************
*/
db_ForeignKeyRef TableHelper::create_foreign_key_to_table(const db_TableRef &table,
const db_TableRef &ref_table,
bool mandatory, bool ref_mandatory,
bool many, bool identifying,
const db_mgmt_RdbmsRef &rdbms,
const grt::DictRef &global_options,
const grt::DictRef &options)
{
db_ForeignKeyRef new_fk;
db_IndexRef pk= ref_table->primaryKey();
grt::GRT *grt= table.get_grt();
std::string name_format;
std::string column_name_format;
std::string scolumn_name;
std::string dcolumn_name;
size_t max_identifier_length= rdbms->maximumIdentifierLength();
// check if there is a PK
if (!pk.is_valid() || pk->columns().count() == 0)
return new_fk;
grt::AutoUndo undo(grt, !table->is_global());
name_format= options.get_string("FKNameTemplate", global_options.get_string("FKNameTemplate","FK%table%"));
column_name_format= options.get_string("FKColumnNameTemplate", global_options.get_string("FKColumnNameTemplate", "FK%table%%column%"));
name_format= format_ident_with_stable_dtable(name_format, table, ref_table);
name_format= format_ident_with_column(name_format, pk->columns().get(0)->referencedColumn());
column_name_format= format_ident_with_stable_dtable(column_name_format, table, ref_table);
// create a new FK
new_fk= grt->create_object<db_ForeignKey>(table.get_metaclass()->get_member_type("foreignKeys").content.object_class);
new_fk->oldName(new_fk->name());
new_fk->deleteRule(options.get_string("db.ForeignKey:deleteRule", global_options.get_string("db.ForeignKey:deleteRule", "NO ACTION")));
new_fk->updateRule(options.get_string("db.ForeignKey:updateRule", global_options.get_string("db.ForeignKey:updateRule", "NO ACTION")));
new_fk->mandatory(mandatory?1:0);
new_fk->referencedMandatory(ref_mandatory?1:0);
new_fk->many(many?1:0);
new_fk->referencedTable(ref_table);
for (size_t c= pk->columns().count(), i= 0; i < c; i++)
{
db_IndexColumnRef pk_column= pk->columns().get(i);
db_ColumnRef column= pk_column->referencedColumn();
db_ColumnRef new_fk_column;
// create the column that will be the FK in the other table
new_fk_column= grt->create_object<db_Column>(column.class_name());
new_fk_column->owner(table);
new_fk_column->name(get_name_suggestion_for_list_object(table->columns(), format_ident_with_column(column_name_format, column), false));
new_fk_column->oldName(new_fk_column->name());
new_fk_column->isNotNull(ref_mandatory?1:0);
ColumnHelper::copy_column(column, new_fk_column);
table->columns().insert(new_fk_column);
// add the new column to the FK
new_fk->columns().insert(new_fk_column);
new_fk->referencedColumns().insert(column);
if (identifying)
table->addPrimaryKeyColumn(new_fk_column);
if (scolumn_name.empty())
{
scolumn_name= column->name();
dcolumn_name= new_fk_column->name();
}
}
// substitute scolumn/dcolumn now that we know the created column name
name_format= replace_variable(replace_variable(name_format, "%scolumn%", scolumn_name), "%dcolumn%", dcolumn_name);
new_fk->name(SchemaHelper::get_unique_foreign_key_name(db_SchemaRef::cast_from(table->owner()),
name_format,
(int)max_identifier_length));
new_fk->oldName(new_fk->name());
new_fk->owner(table); // set the owner last
db_IndexRef index;
// check if an index already exists for the FK columns and if not, create one
if (!find_index_usable_by_fk(new_fk).is_valid())
{
index = create_index_for_fk(grt, new_fk, rdbms->maximumIdentifierLength());
new_fk->index(index);
// index is inserted later down
}
// add the FK to the source table
table->foreignKeys().insert(new_fk);
undo.set_description_for_last_action("Add Foreign Key to Table");
if (index.is_valid())
{
table->indices().insert(index);
undo.set_description_for_last_action("Add Index for FK to Table");
}
undo.end(_("Add Foreign Key"));
return new_fk;
}
db_ForeignKeyRef TableHelper::create_foreign_key_to_table(const db_TableRef &table, const std::vector<db_ColumnRef> &columns,
const db_TableRef &ref_table, const std::vector<db_ColumnRef> &refcolumns,
bool mandatory,
bool many,
const db_mgmt_RdbmsRef &rdbms,
const grt::DictRef &global_options,
const grt::DictRef &options)
{
db_ForeignKeyRef new_fk;
grt::GRT *grt= table.get_grt();
std::string name_format;
std::string scolumn_name;
std::string dcolumn_name;
size_t max_identifier_length= rdbms->maximumIdentifierLength();
bool ref_mandatory;
grt::AutoUndo undo(grt, table->is_global());
name_format= options.get_string("FKNameTemplate", global_options.get_string("FKNameTemplate","FK%table%"));
name_format= format_ident_with_stable_dtable(name_format, table, ref_table);
name_format= format_ident_with_column(name_format, refcolumns[0]);
// create a new FK
new_fk= grt->create_object<db_ForeignKey>(table.get_metaclass()->get_member_type("foreignKeys").content.object_class);
new_fk->deleteRule(options.get_string("db.ForeignKey:deleteRule", global_options.get_string("db.ForeignKey:deleteRule", "NO ACTION")));
new_fk->updateRule(options.get_string("db.ForeignKey:updateRule", global_options.get_string("db.ForeignKey:updateRule", "NO ACTION")));
new_fk->referencedTable(ref_table);
new_fk->mandatory(mandatory?1:0);
//new_fk->referencedMandatory(ref_mandatory?1:0); done later
new_fk->many(many?1:0);
ref_mandatory= false;
for (size_t c= refcolumns.size(), i= 0; i < c; i++)
{
db_ColumnRef column(refcolumns[i]);
db_ColumnRef fk_column(columns[i]);
if (column->isNotNull())
ref_mandatory= true;
// add the new column to the FK
new_fk->columns().insert(fk_column);
new_fk->referencedColumns().insert(column);
if (scolumn_name.empty())
{
scolumn_name= fk_column->name();
dcolumn_name= column->name();
}
}
// substitute scolumn/dcolumn now that we know the created column name
name_format= replace_variable(replace_variable(name_format, "%scolumn%", scolumn_name), "%dcolumn%", dcolumn_name);
new_fk->name(SchemaHelper::get_unique_foreign_key_name(db_SchemaRef::cast_from(table->owner()),
name_format,
(int)max_identifier_length));
new_fk->oldName(new_fk->name());
new_fk->referencedMandatory(ref_mandatory?1:0);
new_fk->owner(table);
// check if an index already exists for the FK columns and if not, create one
if (!find_index_usable_by_fk(new_fk).is_valid())
{
db_IndexRef index(create_index_for_fk(grt, new_fk,rdbms->maximumIdentifierLength()));
new_fk->index(index);
// add the FK to the source table
table->foreignKeys().insert(new_fk);
table->indices().insert(index);
}
else // add the FK to the source table
table->foreignKeys().insert(new_fk);
undo.end(_("Add Foreign Key"));
return new_fk;
}
bool TableHelper::rename_foreign_key(const db_TableRef &table, db_ForeignKeyRef &fk, const std::string &new_name)
{
std::string old_name;
// check if the name is already taken
if (find_named_object_in_list(table->foreignKeys(), new_name).is_valid())
return false;
old_name= fk->name();
grt::AutoUndo undo(table->get_grt());
fk->name(new_name);
// only rename the index if the old names match
if (fk->index().is_valid())
{
if (old_name == *fk->index()->name())
fk->index()->name(new_name);
// else
// g_warning("ForeignKey %s has no attached index", fk->name().c_str());
}
undo.end(_("Rename Foreign Key"));
return true;
}
bool TableHelper::is_identifying_foreign_key(const db_TableRef &table, const db_ForeignKeyRef &fk)
{
// check if the fk is part of the PK
if (table->primaryKey().is_valid())
{
for (size_t c= fk->columns().count(), i= 0; i < c; i++)
{
if (!table->isPrimaryKeyColumn(fk->columns().get(i)))
return false;
}
return true;
}
return false;
}
db_mysql_StorageEngineRef TableHelper::get_engine_by_name(grt::GRT *grt, const std::string &name)
{
grt::ListRef<db_mysql_StorageEngine> engines;
Module *module= grt->get_module("DbMySQL");
if (!module)
throw std::logic_error("module DbMySQL not found");
grt::BaseListRef args(grt);
engines= grt::ListRef<db_mysql_StorageEngine>::cast_from(module->call_function("getKnownEngines", args));
if (engines.is_valid())
{
for (grt::ListRef<db_mysql_StorageEngine>::const_iterator iter= engines.begin();
iter != engines.end(); ++iter)
{
if ((*iter)->name() == name)
return *iter;
}
}
return db_mysql_StorageEngineRef();
}
/**
* Returns a pointer to the position of the first empty line (NOT a line break!) or the first character after the
* maximum count, whichever comes first. If no line break was found and the string is shorter
* than the maximum length the result points to the terminating 0.
*/
static void split_comment(const std::string &comment, size_t db_comment_len,
std::string *comment_ret, std::string *leftover_ret)
{
size_t res;
// XXX: check for Unicode line breaks! especially asian languages may not use the ANSI new line.
const gchar* pointer_to_linebreak = NULL;
{ // find 1st occurrence of 2 consecutive newlines
std::string::size_type pos = comment.find("\n\n");
if (pos == std::string::npos)
pos = comment.find("\r\n\r\n");
if (pos != std::string::npos)
pointer_to_linebreak = comment.c_str() + pos;
}
// We need the number of characters which the string part includes, so convert to a char count.
if (pointer_to_linebreak != NULL)
res = g_utf8_pointer_to_offset(comment.c_str(), pointer_to_linebreak);
else
res = comment.size(); // it's wrong to use g_utf8_strlen here because the comment length must be measured in bytes, not unichars
// Don't break in the middle of a utf8 sequence
if (res > db_comment_len)
{
if (g_utf8_get_char_validated(comment.c_str() + db_comment_len, (gssize)(res - db_comment_len)) == (gunichar)-1)
res = g_utf8_pointer_to_offset(comment.c_str(), g_utf8_find_prev_char(comment.c_str(), comment.c_str() + db_comment_len));
else
res = db_comment_len;
}
if (comment_ret)
*comment_ret = comment.substr(0, res);
if (leftover_ret)
{
if (pointer_to_linebreak != NULL)
*leftover_ret = comment.substr(res+1);
else
*leftover_ret = comment.substr(res);
}
}
std::string TableHelper::get_sync_comment(const std::string &comment, const size_t max_len)
{
std::string ret;
if (comment.size() > max_len)
split_comment(comment, max_len, &ret, NULL);
else
ret = comment;
return ret;
};
std::string TableHelper::normalize_table_name_list(const std::string &schema, const std::string &table_name_list)
{
std::vector<std::string> names = base::split(table_name_list, ",");
for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it)
{
std::vector<std::string> tokens(base::split_qualified_identifier(base::trim(*it)));
if (tokens.size() == 1)
tokens.insert(tokens.begin(), schema);
for (std::vector<std::string>::iterator t = tokens.begin(); t != tokens.end(); ++t)
*t = base::quote_identifier(base::unquote_identifier(*t), '`');
*it = base::join(tokens, ".");
}
return base::join(names, ",");
}
void ColumnHelper::copy_column(const db_ColumnRef &from, db_ColumnRef &to)
{
to->userType(from->userType());
to->precision(from->precision());
to->scale(from->scale());
to->length(from->length());
to->characterSetName(from->characterSetName());
to->collationName(from->collationName());
while (to->flags().count() > 0)
to->flags().remove(0);
for (size_t c= from->flags().count(), i= 0; i < c; i++)
to->flags().insert(from->flags().get(i));
to->simpleType(from->simpleType());
to->structuredType(from->structuredType());
to->datatypeExplicitParams(from->datatypeExplicitParams());
}
ColumnTypeCompareResult ColumnHelper::compare_column_types(const db_ColumnRef &from, const db_ColumnRef &to)
{
// not to be used for foreign key column matching as the rules are different and DB dependant
std::string sfrom= from->formattedType();
std::string sto= to->formattedType();
if (sfrom != sto)
return COLUMNS_TYPES_DIFFER;
if (to->characterSetName() != from->characterSetName() )
return COLUMNS_CHARSETS_DIFFER;
if ( to->collationName() != from->collationName() )
return COLUMNS_COLLATIONS_DIFFER;
if (to->flags().count() != from->flags().count())
return COLUMNS_FLAGS_DIFFER;
for (size_t c= from->flags().count(), i= 0; i < c; i++)
if (to->flags().get_index(from->flags().get(i)) == BaseListRef::npos)
return COLUMNS_FLAGS_DIFFER;
//XXX compare db specific attribs
return COLUMNS_TYPES_EQUAL;
}
void ColumnHelper::set_default_value(db_ColumnRef column, const std::string &value)
{
column->defaultValueIsNull(base::string_compare(value, "NULL", false) ? 0 : 1);
column->defaultValue(value.c_str());
// If a default value of NULL was set then the column can no longer be a not-null column.
if (column->defaultValueIsNull())
column->isNotNull(false);
// Handling auto-increment columns is MySQL specific and done elsewhere.
}
//------------------------------------------------------------------------------------
void CatalogHelper::apply_defaults(db_mysql_ColumnRef column)
{
// for numeric types only
static std::map<std::string, int> def_precision_map;
if (def_precision_map.empty())
{
def_precision_map["INT"] = 11;
def_precision_map["TINYINT"] = 4;
def_precision_map["SMALLINT"] = 6;
def_precision_map["MEDIUMINT"] = 9;
def_precision_map["BIGINT"] = 20;
def_precision_map["FLOAT"] = 11;
def_precision_map["DOUBLE"] = 11;
def_precision_map["BIT"] = 1;
def_precision_map["CHAR"] = 1;
}
bool default_default_value= !column->isNotNull() && (strlen(column->defaultValue().c_str()) == 0);
if (!column->simpleType().is_valid())
return;
std::map<std::string, int>::const_iterator prec_map_it=
def_precision_map.find(column->simpleType()->name().c_str());
if (prec_map_it != def_precision_map.end())
{
if ((strcmp(column->simpleType()->name().c_str(), "CHAR") != 0))//length should be used for chars
column->length(grt::IntegerRef(-1));
if (column->precision() == bec::EMPTY_COLUMN_PRECISION)
{
if(column->flags().get_index("UNSIGNED") != grt::BaseListRef::npos)
column->precision(grt::IntegerRef(prec_map_it->second) - 1);
else
column->precision(grt::IntegerRef(prec_map_it->second));
}
if (default_default_value)
bec::ColumnHelper::set_default_value(column, "NULL");
}
else if ((strcmp(column->simpleType()->name().c_str(), "VARCHAR") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "CHAR") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "DECIMAL") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "BOOLEAN") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "BINARY") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "TINYBLOB") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "MEDIUMBLOB") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "BLOB") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "LONGBLOB") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "TINYTEXT") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "TEXT") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "MEDIUMTEXT") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "LONGTEXT") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "ENUM") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "SET") == 0))
{
if (default_default_value)
bec::ColumnHelper::set_default_value(column, "NULL");
}
else if ((strcmp(column->simpleType()->name().c_str(), "DATETIME") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "DATE") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "TIME") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "TIMESTAMP") == 0)
|| (strcmp(column->simpleType()->name().c_str(), "YEAR") == 0))
{
column->length(grt::IntegerRef(-1));
if (default_default_value)
bec::ColumnHelper::set_default_value(column, "NULL");
}
};
void CatalogHelper::apply_defaults(db_mysql_CatalogRef cat, std::string default_engine)
{
cat->defaultCharacterSetName("utf8");
cat->defaultCollationName("utf8_general_ci");
for(size_t i= 0, schemata_count= cat->schemata().count(); i < schemata_count; i++)
{
db_mysql_SchemaRef schema= cat->schemata().get(i);
if(strlen(schema->defaultCharacterSetName().c_str()) == 0)
schema->defaultCharacterSetName(cat->defaultCharacterSetName());
if(strlen(schema->defaultCollationName().c_str()) == 0)
schema->defaultCollationName(defaultCollationForCharset(schema->defaultCharacterSetName().c_str()));
for(size_t j= 0, tables_count= schema->tables().count(); j < tables_count; j++)
{
db_mysql_TableRef table= schema->tables().get(j);
if(strlen(table->defaultCharacterSetName().c_str()) == 0)
table->defaultCharacterSetName(schema->defaultCharacterSetName());
if(table->defaultCharacterSetName() == schema->defaultCharacterSetName())
{
if(strlen(table->defaultCollationName().c_str()) == 0)
table->defaultCollationName(schema->defaultCollationName());
}
else
{
if(strlen(table->defaultCollationName().c_str()) == 0)
table->defaultCollationName(defaultCollationForCharset(table->defaultCharacterSetName()));
}
if(strlen(table->tableEngine().c_str()) == 0)
table->tableEngine(default_engine.empty()?"InnoDB":default_engine);
for (size_t c= table->foreignKeys().count(), i= 0; i < c; i++)
{
db_ForeignKeyRef fk(table->foreignKeys()[i]);
if (fk->referencedTable().is_valid())
{
for (size_t d= fk->referencedColumns().count(), j= 0; j < d; j++)
{
db_mysql_ColumnRef col = db_mysql_ColumnRef::cast_from(fk->referencedColumns().get(j));
apply_defaults(col);
}
}
}
for(size_t k= 0, col_count= table->columns().count(); k < col_count; k++)
{
apply_defaults(table->columns().get(k));
}
}
}
}
//--------------------------------------------------------------------------------------------------
/**
* Compares the given typename with what is in the type list, including the synonyms and returns
* the type whose name or synonym matches.
*/
static db_SimpleDatatypeRef findType(const grt::ListRef<db_SimpleDatatype> &types,
const GrtVersionRef &target_version, const std::string &name)
{
for (size_t c = types.count(), i = 0; i < c; ++i)
{
bool type_found = base::same_string(types[i]->name(), name, false);
if (!type_found)
{
// Type has not the default name, but maybe one of the synonyms.
for (ListRef<internal::String>::const_iterator synonym = types[i]->synonyms().begin(); synonym != types[i]->synonyms().end(); ++synonym)
{
if (base::same_string(*synonym, name, false))
{
type_found = true;
break;
}
}
}
if (type_found)
{
if (!target_version.is_valid() || CatalogHelper::is_type_valid_for_version(types[i], target_version))
return types[i];
}
}
return db_SimpleDatatypeRef();
}
//--------------------------------------------------------------------------------------------------
static bool parse_type(const std::string &type,
const GrtVersionRef &targetVersion,
const grt::ListRef<db_SimpleDatatype> &typeList,
db_SimpleDatatypeRef &simpleType,
int &precision,
int &scale,
int &length,
std::string &explicitParams)
{
// No char sets necessary for parsing data types as there's no repertoire necessary/allowed in any
// data type part. Neither do we need an sql mode (string lists in enum defs only allow
// single quoted text).
//
// Note: we parse here more than the pure data type name + precision/scale (namely additional parameters
// like charsets etc.). That doesn't affect the main task here, however. Additionally stuff
// is simply ignored for now (but it must be a valid definition).
MySQLRecognizer recognizer(bec::version_to_int(targetVersion), "", std::set<std::string>());
recognizer.parse(type.c_str(), type.size(), true, PuDataType);
if (!recognizer.error_info().empty())
return false;
MySQLRecognizerTreeWalker walker = recognizer.tree_walker();
// A type name can consist of up to 3 parts (e.g. "national char varying").
std::string type_name = walker.token_text();
switch (walker.token_type())
{
case DOUBLE_SYMBOL:
walker.next();
if (walker.token_type() == PRECISION_SYMBOL)
walker.next(); // Simply ignore syntactic sugar.
break;
case NATIONAL_SYMBOL:
walker.next();
type_name += " " + walker.token_text();
walker.next();
if (walker.token_type() == VARYING_SYMBOL)
{
type_name += " " + walker.token_text();
walker.next();
}
break;
case NCHAR_SYMBOL:
walker.next();
if (walker.token_type() == VARCHAR_SYMBOL || walker.token_type() == VARYING_SYMBOL)
{
type_name += " " + walker.token_text();
walker.next();
}
break;
case CHAR_SYMBOL:
walker.next();
if (walker.token_type() == VARYING_SYMBOL)
{
type_name += " " + walker.token_text();
walker.next();
}
break;
case LONG_SYMBOL:
walker.next();
switch (walker.token_type())
{
case CHAR_SYMBOL: // LONG CHAR VARYING
if (walker.look_ahead(1) == VARYING_SYMBOL) // Otherwise we may get e.g. LONG CHAR SET...
{
type_name += " " + walker.token_text();
walker.next();
type_name += " " + walker.token_text();
walker.next();
}
break;
case VARBINARY_SYMBOL:
case VARCHAR_SYMBOL:
type_name += " " + walker.token_text();
walker.next();
}
break;
default:
walker.next();
}
simpleType = findType(typeList, targetVersion, type_name);
if (!simpleType.is_valid()) // Should always be valid at this point or we have messed up our type list.
return false;
if (!walker.is(OPEN_PAR_SYMBOL))
return true;
walker.next();
// We use the simple type properties for char length to learn if we have a length here or a precision.
// We could indicate that in the grammar instead, however the handling in WB is a bit different
// than what the server grammar would suggest (e.g. the length is also used for integer types, in the grammar).
if (simpleType->characterMaximumLength() != bec::EMPTY_TYPE_MAXIMUM_LENGTH
|| simpleType->characterOctetLength() != bec::EMPTY_TYPE_OCTET_LENGTH)
{
if (walker.token_type() != INT_NUMBER)
return false;
length = base::atoi<int>(walker.token_text().c_str());
return true;
}
if (!walker.is(INT_NUMBER))
{
// ENUM or SET.
do
{
if (!explicitParams.empty())
explicitParams += ", ";
explicitParams += walker.token_text(true);
walker.next();
walker.skip_if(COMMA_SYMBOL); // We normalize the whitespace around the commas.
} while (!walker.is(CLOSE_PAR_SYMBOL));
explicitParams = "(" + explicitParams + ")";
return true;
}
// Finally all cases with either precision, scale or both.
precision = base::atoi<int>(walker.token_text().c_str());
walker.next();
if (walker.token_type() != COMMA_SYMBOL)
return true;
walker.next();
scale = base::atoi<int>(walker.token_text().c_str());
return true;
}
//--------------------------------------------------------------------------------------------------
bool bec::parse_type_definition(const std::string &type,
const GrtVersionRef &targetVersion,
const grt::ListRef<db_SimpleDatatype> &typeList,
const grt::ListRef<db_UserDatatype>& user_types,
const grt::ListRef<db_SimpleDatatype>& default_type_list,
db_SimpleDatatypeRef &simpleType,
db_UserDatatypeRef& userType,
int &precision,
int &scale,
int &length,
std::string &datatypeExplicitParams)
{
if (user_types.is_valid())
{
std::string::size_type argp = type.find('(');
std::string typeName = type;
if (argp != std::string::npos)
typeName = type.substr(0, argp);
// 1st check if this is a user defined type
for (size_t c = user_types.count(), i = 0; i < c; i++)
{
db_UserDatatypeRef utype(user_types[i]);
if (base::string_compare(utype->name(), typeName, false) == 0)
{
userType = utype;
break;
}
}
}
if (userType.is_valid())
{
// If the type spec has an argument, we replace the arguments from the type definition
// with the one provided by the user.
std::string finalType = userType->sqlDefinition();
std::string::size_type tp;
bool overriden_args = false;
if ((tp = type.find('(')) != std::string::npos) // Are there user specified args?
{
std::string::size_type p = finalType.find('(');
if (p != std::string::npos) // Strip the original args.
finalType = finalType.substr(0, p);
// Extract the user specified args and append to the specification.
finalType.append(type.substr(tp));
overriden_args = true;
}
// Parse user type definition.
if (!parse_type(finalType, targetVersion, typeList.is_valid() ? typeList : default_type_list,
simpleType, precision, scale, length, datatypeExplicitParams))
return false;
simpleType = db_SimpleDatatypeRef();
if (!overriden_args)
{
precision = bec::EMPTY_COLUMN_PRECISION;
scale = bec::EMPTY_COLUMN_SCALE;
length = bec::EMPTY_COLUMN_LENGTH;
datatypeExplicitParams = "";
}
}
else
{
if (!parse_type(type, targetVersion, typeList.is_valid() ? typeList : default_type_list,
simpleType, precision, scale, length, datatypeExplicitParams))
return false;
userType = db_UserDatatypeRef();
}
return true;
}
//--------------------------------------------------------------------------------------------------
std::string bec::get_default_collation_for_charset(const db_SchemaRef &schema, const std::string &character_set)
{
if (schema->owner().is_valid())
{
db_CatalogRef catalog(db_CatalogRef::cast_from(schema->owner()));
db_CharacterSetRef charset = find_named_object_in_list(catalog->characterSets(), character_set);
if (charset.is_valid())
{
return charset->defaultCollation();
}
}
else
log_warning("While checking diff, catalog ref was found to be invalid\n");
return "";
}
std::string bec::get_default_collation_for_charset(const db_TableRef &table, const std::string &character_set)
{
if (table->owner().is_valid())
return bec::get_default_collation_for_charset(db_SchemaRef::cast_from(table->owner()), character_set);
else
log_warning("While checking diff, table ref was found to be invalid\n");
return "";
}
std::string bec::TableHelper::generate_comment_text(const std::string& comment_text, size_t comment_lenght)
{
if (comment_text.size() > comment_lenght)
{
std::string comment, leftover;
split_comment(comment_text, comment_lenght, &comment, &leftover);
if (!comment.empty())
comment = "'"+base::escape_sql_string(comment)+"'";
if (!leftover.empty())
{
base::replace(leftover, "*/", "*\\/");
comment.append(" /* comment truncated */ /*")
.append(leftover)
.append("*/");
}
return comment;
}
else if (!comment_text.empty())
return "'"+base::escape_sql_string(comment_text)+"'";
return "";
}
|