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 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
|
/*
* Copyright (c) 2007, 2016, 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
*/
#include "editor_table.h"
#include "sqlide/recordset_table_inserts_storage.h"
#include "grtui/inserts_export_form.h"
#include "db_object_helpers.h"
#include "grtpp_util.h"
#include "grt/clipboard.h"
#include "grt/validation_manager.h"
#include "grtdb/db_object_helpers.h"
#include "base/string_utilities.h"
#include "base/file_utilities.h"
#include "objimpl/db.query/db_query_EditableResultset.h"
#include "base/log.h"
#include "mforms/box.h"
#include "mforms/form.h"
#include "mforms/menubar.h"
#include "mforms/record_grid.h"
#include "mforms/toolbar.h"
#include "mforms/utilities.h"
#include <algorithm>
#undef min
using namespace bec;
using namespace grt;
using namespace base;
DEFAULT_LOG_DOMAIN("TableEditorBE")
/*
The big mess of user datatypes, synonyms and column flags.
- A column can have a datatype, a list of flags specific for the type (eg INT UNSIGNED ZEROFILL)
and other parameters.
- Some datatypes have aliases or synonyms, eg: INTEGER is just an alias for INT in MySQL
- Users can define User Defined Datatypes and use them in columns. If a column type is set to a UDT
no other parameters can be changed. Flags like UNSIGNED cannot be changed for the column that
uses the UDT anymore. However, the UDT definition can be changed and if that happens, all uses
of that UDT will be updated automatically.
- Originally, server aliases were handled by a "synonyms" list for each simple datatype.
So, INT had a list of synonyms that contained INTEGER. Both were basically treated as if they were
the same, the same kind of parameters and flags could be toggled for either.
- At some point, for a reason I don't know, someone removed the synonyms list and made
such aliases be implemented as UDTs.
- That caused all sorts of problems, mostly because aliases/synonyms can have parameters changed
at will in the column definition while UDTs can't.
- Now, to bring back sanity to this whole mess, we have to either:
- revert the removal of aliases/synonyms and make things pretty again; or
- introduce a hack to distinguish between real UDTs and UDTs that were corrupted into aliases
- So, to distinguish between real UDTs and corrupt-UDTs you should check if the object id.
*/
/****************************************************************************
* @brief Column list backend for table editors. Internal use.
*
****************************************************************************
*/
TableColumnsListBE::TableColumnsListBE(TableEditorBE *owner)
: _owner(owner)
{
_editing_placeholder_row= -1;
}
static bool sort_simple_type(const db_SimpleDatatypeRef &a, const db_SimpleDatatypeRef &b)
{
int i= strcmp(a->group()->name().c_str(), b->group()->name().c_str());
if (i == 0)
i= strcmp(a->name().c_str(), b->name().c_str());
return i < 0;
}
std::vector<std::string> TableColumnsListBE::get_datatype_names()
{
std::vector<std::string> types;
// TODO replace this hard coded list with a dynamically generated top-used types list (same for charset).
types.push_back("INT");
types.push_back("VARCHAR()");
types.push_back("DECIMAL()");
types.push_back("DATETIME");
types.push_back("BLOB");
GrtVersionRef target_version(_owner->get_catalog()->version());
std::vector<db_SimpleDatatypeRef> sorted_types;
grt::ListRef<db_SimpleDatatype> stypes(_owner->get_catalog()->simpleDatatypes());
for (grt::ListRef<db_SimpleDatatype>::const_iterator iter= stypes.begin(); iter != stypes.end(); ++iter)
{
if (target_version.is_valid() && !bec::CatalogHelper::is_type_valid_for_version(*iter, target_version))
{
if(!base::same_string((*iter)->name(), "json", false))
continue;
}
sorted_types.push_back(*iter);
}
// sort by group and name
std::sort(sorted_types.begin(), sorted_types.end(), sort_simple_type);
// insert grouped by group
db_DatatypeGroupRef group;
for (std::vector<db_SimpleDatatypeRef>::const_iterator iter= sorted_types.begin(); iter != sorted_types.end(); ++iter)
{
if (group != (*iter)->group())
{
group= (*iter)->group();
types.push_back("-");
}
std::string tmp;
if ((*iter)->parameterFormatType() == 1 ||
(*iter)->parameterFormatType() == 2 ||
(*iter)->parameterFormatType() == 3 ||
(*iter)->parameterFormatType() == 4 ||
(*iter)->parameterFormatType() == 10)
tmp = *(*iter)->name()+"()";
else
tmp = (*iter)->name();
if (types.empty() || types.back() != tmp)
types.push_back(tmp);
}
// insert UDTs
types.push_back("-");
grt::ListRef<db_UserDatatype> utypes(_owner->get_catalog()->userDatatypes());
for (grt::ListRef<db_UserDatatype>::const_iterator iter= utypes.begin(); iter != utypes.end(); ++iter)
types.push_back(*(*iter)->name());
/*
types.push_back("BIT");
types.push_back("TINYINT");
types.push_back("SMALLINT");
types.push_back("MEDIUMINT");
types.push_back("INT");
types.push_back("BIGINT");
types.push_back("FLOAT");
types.push_back("DOUBLE");
types.push_back("-");
types.push_back("DATE");
types.push_back("TIME");
types.push_back("TIMESTAMP");
types.push_back("YEAR");
types.push_back("-");
types.push_back("CHAR()");
types.push_back("VARCHAR()");
types.push_back("BINARY()");
types.push_back("TINYBLOB");
types.push_back("BLOB");
types.push_back("MEDIUMBLOB");
types.push_back("LONGBLOB");
types.push_back("TINYTEXT");
types.push_back("TEXT");
types.push_back("MEDIUMTEXT");
types.push_back("LONGTEXT");
types.push_back("ENUM()");
types.push_back("SET()");
*/
return types;
}
bec::ColumnNamesSet TableColumnsListBE::get_column_names_completion_list() const
{
bec::ColumnNamesSet column_names;
db_SchemaRef schema = db_SchemaRef::cast_from(_owner->get_table()->owner());
if (schema.is_valid())
{
grt::ListRef<db_Table> tables = schema->tables();
for (ssize_t i = tables.count() - 1; i >=0; --i)
{
grt::ListRef<db_Column> columns = tables[i]->columns();
for (ssize_t j = columns.count() - 1; j >=0; --j)
{
column_names.insert(columns[j]->name().c_str());
}
}
}
return column_names;
}
/**
****************************************************************************
* @brief Get info for a table column
*
* @param node - identifier of the column node
* @param name - returns name of column
* @param type - returns formatted datatype of the column
* @param ispk - returns true if column is part of a PK
* @param notnull - returns true if column is NOT NULL
* @param flags - returns comma separated list of additional flags
* @param defvalue - returns default value of column
* @param defnull - returns true if default value of column is NULL
* @param charset - returns character set name of column
* @param collation - returns collation name for column
*
* @return false if row is invalid
****************************************************************************
*/
bool TableColumnsListBE::get_row(const NodeId &node,
std::string &name,
std::string &type,
bool &ispk,
bool ¬null,
bool &isunique,
bool &isbinary,
bool &isunsigned,
bool &iszerofill,
std::string &flags,
std::string &defvalue,
std::string &charset,
std::string &collation,
std::string &comment
)
{
if (node[0] < real_count())
{
db_ColumnRef col= _owner->get_table()->columns().get(node[0]);
name= col->name();
type= _owner->format_column_type(col);
ispk= _owner->get_table()->isPrimaryKeyColumn(col) != 0;
notnull= (col->isNotNull() != 0);
isbinary= get_column_flag(node, "BINARY") != 0;
isunsigned= get_column_flag(node, "UNSIGNED") != 0;
iszerofill= get_column_flag(node, "ZEROFILL") != 0;
flags= ""; //XXX
defvalue= col->defaultValue();
charset= col->characterSetName();
collation= col->collationName();
comment= col->comment();
return true;
}
return false;
}
void TableColumnsListBE::reorder(const NodeId &node, size_t nindex)
{
if (node[0] < real_count())
{
AutoUndoEdit undo(_owner);
_owner->get_table()->columns().reorder(node[0], nindex);
update_primary_index_order();
_owner->update_change_date();
_owner->freeze_refresh_on_object_change();
(*_owner->get_table()->signal_refreshDisplay())("column");
_owner->thaw_refresh_on_object_change(true);
undo.end(strfmt(_("Reorder Column '%s.%s'"), _owner->get_name().c_str(), _owner->get_table()->columns()[node[0]]->name().c_str()));
if (nindex < node[0])
_owner->do_partial_ui_refresh(::bec::TableEditorBE::RefreshColumnMoveUp);
else
_owner->do_partial_ui_refresh(::bec::TableEditorBE::RefreshColumnMoveDown);
}
}
void TableColumnsListBE::reorder_many(const std::vector<size_t> &rows, size_t targetIndex)
{
if (!rows.empty())
{
std::vector<size_t> indices(rows);
std::sort(indices.begin(), indices.end());
AutoUndoEdit undo(_owner);
for (size_t i = 0; i < indices.size(); i++)
{
// Watch out how the backend does the reorder. If the source index is smaller than the target
// index we will end up one node after the one we intended (the node is first removed
// moving so all following indices one index down).
_owner->get_table()->columns().reorder(indices[i],
(indices[i] < targetIndex) ? targetIndex - 1 : targetIndex);
// Update all those indices in our list between the current index and the target index
// if the current index is below that value. This is because remaining indices move down in the list
// if a node is removed from there and inserted after them in the list.
if (indices[i] < targetIndex)
{
for (size_t j = i + 1; j < indices.size(); j++)
if (indices[j] > indices[i] && indices[j] < targetIndex)
indices[j]--;
}
else
// If the current is above the target index then increase the latter to ensure
// that the multiple indices stay in the same order.
targetIndex++;
}
update_primary_index_order();
_owner->update_change_date();
(*_owner->get_table()->signal_refreshDisplay())("column");
undo.end(strfmt(_("Reorder Columns in '%s'"), _owner->get_name().c_str()));
_owner->do_partial_ui_refresh(::bec::TableEditorBE::RefreshColumnList);
}
}
void TableColumnsListBE::update_primary_index_order()
{
if (_owner->get_table()->primaryKey().is_valid())
{
ListRef<db_Column> columns(_owner->get_table()->columns());
ListRef<db_IndexColumn> icolumns(_owner->get_table()->primaryKey()->columns());
if (icolumns.count() > 1)
{
size_t pos= 0;
// update the PRIMARY index columns order to match the order they appear in the table
for (size_t c= real_count(), i= 0; i < c; i++)
{
for (size_t ic= icolumns.count(), ii= pos; ii < ic; ii++)
{
if (icolumns[ii]->referencedColumn() == columns[i])
{
if (ii != pos)
{
// out of place, reorder
icolumns->reorder(ii, pos);
}
pos++;
break;
}
}
if (pos >= icolumns.count())
break;
}
}
}
}
bool TableColumnsListBE::set_column_type(const NodeId &node, const GrtObjectRef &type)
{
if (type.is_instance(db_UserDatatype::static_class_name()))
{
db_UserDatatypeRef utype(db_UserDatatypeRef::cast_from(type));
AutoUndoEdit undo(_owner);
if (node[0] >= real_count())
_owner->add_column(grt::get_name_suggestion_for_list_object(_owner->get_table()->columns(), utype->name(), false));
bool flag= set_field(node, Type, utype->name());
undo.end(strfmt(_("Add Column to '%s'"), _owner->get_name().c_str()));
return flag;
}
return false;
}
IconId TableColumnsListBE::get_field_icon(const NodeId &node, int column, IconSize size)
{
if (node[0] < real_count())
{
if (column == Type)
{
return 0;
}
else if (column == Name)
{
db_ColumnRef col= _owner->get_table()->columns().get(node[0]);
if (_owner->get_table()->isPrimaryKeyColumn(col))
return IconManager::get_instance()->get_icon_id(col, Icon11, "pk");
else if (_owner->get_table()->isForeignKeyColumn(col))
{
if (*col->isNotNull())
return IconManager::get_instance()->get_icon_id(col, Icon11, "fknn");
else
return IconManager::get_instance()->get_icon_id(col, Icon11, "fk");
}
else
{
if (*col->isNotNull())
return IconManager::get_instance()->get_icon_id(col, Icon11, "nn");
else
return IconManager::get_instance()->get_icon_id(col, Icon11);
}
}
}
return 0;
}
void TableColumnsListBE::refresh()
{
}
size_t TableColumnsListBE::real_count()
{
return _owner->get_table()->columns().count();
}
size_t TableColumnsListBE::count()
{
return _owner->get_table()->columns().count() + 1;
}
bool TableColumnsListBE::set_column_type_from_string(db_ColumnRef &col, const std::string &type)
{
bool flag= _owner->parse_column_type(type, col);
if (flag)
{
if (col->simpleType().is_valid())
{
// Remove flags that don't exist in new datatype.
if (col->flags().count() > 0)
{
grt::StringListRef valid_flags(col->simpleType()->flags());
for (ssize_t i = col->flags().count() - 1; i >= 0; i--)
{
if (valid_flags.get_index(col->flags().get(i)) == BaseListRef::npos)
col->flags().remove(i);
}
}
}
else if (col->userType().is_valid())
{
col->flags().remove_all();
// Read top of file for note on column flag handling
// Don't set flags from userType or we have trouble when userType is updated, that was
// incorrect behavior.
}
}
else
log_warning("%s is not a valid column type", type.c_str());
return flag;
}
bool TableColumnsListBE::has_unique_index(const db_ColumnRef &col)
{
db_TableRef table(_owner->get_table());
for (size_t c= table->indices().count(), i= 0; i < c; i++)
{
db_IndexRef index(table->indices()[i]);
if (index->indexType() == "UNIQUE" && index->columns().count() == 1 &&
index->columns()[0]->referencedColumn() == col)
return true;
}
return false;
}
bool TableColumnsListBE::make_unique_index(const db_ColumnRef &col, bool flag)
{
if (flag == has_unique_index(col))
return false;
db_TableRef table(_owner->get_table());
if (flag)
{
db_IndexRef index= _owner->get_grt()->create_object<db_Index>(table->indices().content_class_name());
index->name(*col->name()+"_UNIQUE");
index->owner(table);
index->indexType("UNIQUE");
index->unique(1);
db_IndexColumnRef icolumn=
_owner->get_grt()->create_object<db_IndexColumn>(index->columns().content_class_name());
icolumn->owner(index);
icolumn->referencedColumn(col);
index->columns().insert(icolumn);
AutoUndoEdit undo(_owner);
_owner->update_change_date();
table->indices().insert(index);
undo.end(strfmt(_("Add Unique Index for '%s'.'%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
else
{
bool found = false;
AutoUndoEdit undo(_owner);
for (size_t c= table->indices().count(), i= 0; i < c; i++)
{
db_IndexRef index(table->indices()[i]);
if (index->indexType() == "UNIQUE" && index->columns().count() == 1 &&
index->columns()[0]->referencedColumn() == col)
{
table->indices().remove(i);
found = true;
break;
}
}
_owner->update_change_date();
if (found)
undo.end(strfmt(_("Remove Unique Index for '%s'.'%s'"), _owner->get_name().c_str(), col->name().c_str()));
else
return false;
}
return true;
}
bool TableColumnsListBE::set_field(const NodeId &node, ColumnId column, const std::string &value)
{
RefreshUI::Blocker __centry(*_owner);
std::string old;
// either the row was edited or cancelled (sent "" as value)
if (node[0] == count()-1)
{
// not a placeholder anymore
_editing_placeholder_row= -1;
}
// if the user enters a new column name in the editing placeholder row
if (node[0] == count()-1 && ((ColumnListColumns)column == Name || (ColumnListColumns)column == Type)
&& !value.empty())
{
AutoUndoEdit undo(_owner);
// add new column
std::string col_name;
if ((ColumnListColumns)column == Name)
{
col_name= base::trim_right(value);
_owner->add_column(col_name);
db_ColumnRef col(_owner->get_table()->columns()[node[0]]);
if (node[0] == 0)
{
_owner->get_table()->addPrimaryKeyColumn(col);
set_column_type_from_string(col, grt::StringRef::cast_from(_owner->get_grt_manager()->get_app_option("DefaultPkColumnType")));
}
else
set_column_type_from_string(col, grt::StringRef::cast_from(_owner->get_grt_manager()->get_app_option("DefaultColumnType")));
}
else
{
std::string name;
_editing_placeholder_row= node[0];
get_field(node, Name, name);
_editing_placeholder_row= -1;
_owner->add_column(name);
db_ColumnRef col(_owner->get_table()->columns()[node[0]]);
col_name= col->name();
if (node[0] == 0)
_owner->get_table()->addPrimaryKeyColumn(col);
set_column_type_from_string(col, grt::StringRef(value));
}
undo.end(strfmt(_("Add column '%s.%s'"), _owner->get_name().c_str(), col_name.c_str()));
return true;
}
else if (node[0] >= real_count())
return false;
db_ColumnRef col;
col = _owner->get_table()->columns().get(node[0]);
get_field(node, column, old);
switch ((ColumnListColumns)column)
{
case Name:
{
std::string value_= base::trim_right(value);
if (old != value_)
_owner->rename_column(col, value_);
}
return true;
case Type:
{
bool result = true;
if (value != old)
{
AutoUndoEdit undo(_owner);
if (set_column_type_from_string(col, value))
{
_owner->update_change_date();
undo.end(strfmt(_("Set Type of '%s.%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
else
{
result = _owner->showErrorMessage(value);
undo.cancel();
}
_owner->do_partial_ui_refresh(::bec::TableEditorBE::RefreshColumnCollation);
}
return result;
}
case IsPK:
return false;
case IsNotNull:
return false;
case Flags:
return false;
case Default:
if (old != value)
{
AutoUndoEdit undo(_owner, col, "defaultValue");
std::string value_= base::trim_right(value);
db_SimpleDatatypeRef sData;
if (col->userType().is_valid()) //Get SimpleData from userType when in modeling.
sData = col->userType()->actualType();
else if(col->simpleType().is_valid()) //When in SQLIDE we use simpleType directly.
sData = col->simpleType();
//Change true to 1 and false to 0 only when user selected numeric type and it's natural number (INT based like TINY/MEDIUM/BIG).
if (!value_.empty() && sData.is_valid() && sData->group().is_valid() && sData->group()->name() == "numeric" && sData->numericScale() == 0)
{
if (same_string(value_, "true", false))
value_ = "1";
else if (same_string(value_, "false", false))
value_ = "0";
}
bec::ColumnHelper::set_default_value(col, value_.empty() ? "" : quote_value_if_needed(col, value_));
_owner->update_change_date();
undo.end(strfmt(_("Set Default Value of '%s.%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
return true;
case CharsetCollation:
if (old != value)
{
std::string charset, collation;
_owner->parse_charset_collation(value, charset, collation);
if (charset != *col->characterSetName() || collation != *col->collationName())
{
AutoUndoEdit undo(_owner);
col->characterSetName(charset);
col->collationName(collation);
_owner->update_change_date();
undo.end(strfmt(_("Set Collation/Charset of '%s.%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
}
return true;
case Charset:
if (old != value)
{
AutoUndoEdit undo(_owner);
col->characterSetName(value);
_owner->update_change_date();
undo.end(
strfmt(_("Set Character Set of '%s.%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
return true;
case Collation:
if (old != value)
{
AutoUndoEdit undo(_owner);
col->collationName(value);
_owner->update_change_date();
undo.end(
strfmt(_("Set Collation of '%s.%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
return true;
case HasCharset:
return false;
case Comment:
if (old != value)
{
AutoUndoEdit undo(_owner, col, "comment");
col->comment(value);
_owner->update_change_date();
undo.end(strfmt(_("Set Comment of '%s.%s'"), _owner->get_name().c_str(), col->name().c_str()));
}
return true;
case LastColumn:
return false;
default:
return false;
}
return false;
}
void TableColumnsListBE::reset_placeholder()
{
_editing_placeholder_row = -1;
}
bool TableColumnsListBE::set_field(const NodeId &node, ColumnId column, ssize_t value)
{
RefreshUI::Blocker __centry(*_owner);
db_ColumnRef col;
// only entering a new name can create a new column
if (node[0] == count()-1)
{
// hack to get notifications of starting to edit placeholder row
if (value == 1)
_editing_placeholder_row= node[0];
else
_editing_placeholder_row= -1;
return false;
}
col= _owner->get_table()->columns().get(node[0]);
switch ((ColumnListColumns)column)
{
case Name:
case Type:
return false;
case IsPK:
if (*_owner->get_table()->isPrimaryKeyColumn(col) != (value != 0))
{
AutoUndoEdit undo(_owner);
if (value != 0)
{
_owner->get_table()->addPrimaryKeyColumn(col);
if (col->defaultValueIsNull())
bec::ColumnHelper::set_default_value(col, "");
}
else
_owner->get_table()->removePrimaryKeyColumn(col);
bool nvalue= _owner->get_table()->isPrimaryKeyColumn(col) != 0;
_owner->update_change_date();
if (nvalue)
undo.end(strfmt(_("Set '%s.%s' PK"), _owner->get_name().c_str(), (*col->name()).c_str()));
else
undo.end(strfmt(_("Unset '%s.%s' PK"), _owner->get_name().c_str(), (*col->name()).c_str()));
}
return true;
case IsNotNull:
{
FreezeRefresh frz(_owner);
AutoUndoEdit undo(_owner);
col->isNotNull(value != 0);
// When setting the not-null flag then having a default value of NULL is meaningless.
// Remove that if it is set.
if (col->defaultValueIsNull() && col->isNotNull())
bec::ColumnHelper::set_default_value(col, "");
TableHelper::update_foreign_keys_from_column_notnull(_owner->get_table(), col);
_owner->update_change_date();
(*_owner->get_table()->signal_refreshDisplay())("column");
undo.end(strfmt(_("Set '%s.%s' NOT NULL"), _owner->get_name().c_str(), (*col->name()).c_str()));
}
return true;
case IsBinary:
{
FreezeRefresh frz(_owner);
return set_column_flag(node, "BINARY", value!=0);
}
case IsUnsigned:
{
FreezeRefresh frz(_owner);
return set_column_flag(node, "UNSIGNED", value!=0);
}
case IsZerofill:
{
FreezeRefresh frz(_owner);
return set_column_flag(node, "ZEROFILL", value!=0);
}
case IsUnique:
{
FreezeRefresh frz(_owner);
return make_unique_index(col, value!=0);
}
case Flags:
case Default:
return false;
case CharsetCollation:
case Charset:
case Collation:
case Comment:
case HasCharset:
return false;
case LastColumn:
return false;
}
return false;
}
bool TableColumnsListBE::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
if (node[0] < real_count())
{
db_ColumnRef col= _owner->get_table()->columns().get(node[0]);
switch ((ColumnListColumns)column)
{
case Name:
value= col->name();
return true;
case Type:
value= grt::StringRef(_owner->format_column_type(col));
return true;
case IsPK:
value= grt::IntegerRef(_owner->get_table()->isPrimaryKeyColumn(col) ? 1 : 0);
return true;
case IsNotNull:
value= col->isNotNull();
return true;
case IsBinary:
value= grt::IntegerRef(get_column_flag(node, "BINARY"));
return true;
case IsUnsigned:
value= grt::IntegerRef(get_column_flag(node, "UNSIGNED"));
return true;
case IsZerofill:
value= grt::IntegerRef(get_column_flag(node, "ZEROFILL"));
return true;
case IsUnique:
value= grt::IntegerRef(has_unique_index(col) ? 1 : 0);
return true;
case Flags:
value= grt::StringRef("");
return true;
case Default:
value= col->defaultValue();
return true;
case CharsetCollation:
value= grt::StringRef(_owner->format_charset_collation(col->characterSetName(), col->collationName()));
return true;
case Charset:
value= col->characterSetName();
return true;
case Collation:
value= col->collationName();
return true;
case HasCharset:
value= grt::IntegerRef(0);
/* moved to db specific code
if (col->simpleType().is_valid())
{
if (col->simpleType()->group()->name() == "string" || col->simpleType()->group()->name() == "text")
value= grt::IntegerRef(1);
}*/
return true;
case Comment:
value= col->comment();
return true;
case LastColumn:
return false;
}
}
else if (node[0] == count()-1)
{
if (column == Name && _editing_placeholder_row == node[0])
{
if (node[0] == 0)
{
value= grt::StringRef(replace_variable(_owner->get_grt_manager()->get_app_option_string("PkColumnNameTemplate"),
"%table%", _owner->get_name().c_str()));
}
else
{
std::string templ= replace_variable(_owner->get_grt_manager()->get_app_option_string("ColumnNameTemplate"),
"%table%", _owner->get_name().c_str());
value= grt::StringRef(grt::get_name_suggestion_for_list_object(_owner->get_table()->columns(), templ, false));
}
}
else if (column == Type && _editing_placeholder_row == node[0])
{
if (node[0] == 0)
value= grt::StringRef::cast_from(_owner->get_grt_manager()->get_app_option("DefaultPkColumnType"));
else
value= grt::StringRef::cast_from(_owner->get_grt_manager()->get_app_option("DefaultColumnType"));
}
else
{
value = grt::StringRef("");
return false;
}
return true;
}
return false;
}
/** Returns list of flags that are available for column's type
*/
std::vector<std::string> TableColumnsListBE::get_datatype_flags(const ::bec::NodeId &node, bool all)
{
db_ColumnRef col;
std::vector<std::string> retval;
if (node.is_valid())
{
if (node[0] < real_count())
col= _owner->get_table()->columns().get(node[0]);
if(!col.is_valid() || !col->simpleType().is_valid())
return retval;
grt::StringListRef datatype_flags;
if (col->simpleType().is_valid())
datatype_flags= col->simpleType()->flags();
else if (col->userType().is_valid() && col->userType()->actualType().is_valid())
{
// Return flags only for type aliases. UDTs cannot have their types changed.
// See note at top of file for more about UDTs vs aliases.
// the method to identify a type alias is a hack
if (g_str_has_prefix(col->userType().id().c_str(), "com.mysql.rdbms.mysql.userdatatype."))
datatype_flags= col->userType()->actualType()->flags();
}
if(!datatype_flags.is_valid())
return retval;
size_t flags_count= datatype_flags.count();
for(size_t i= 0; i < flags_count; i++)
{
std::string s= datatype_flags.get(i).c_str();
if (!all)
{
if (s == "UNSIGNED" || s == "ZEROFILL" || s == "BINARY") continue;
}
retval.push_back(s);
}
}
return retval;
}
bool TableColumnsListBE::set_column_flag(const ::bec::NodeId &node, const std::string& flag_name, int is_set)
{
db_ColumnRef col;
std::vector<std::string> retval;
if (node.is_valid())
{
if (node[0] < real_count())
col= _owner->get_table()->columns().get(node[0]);
if(!col.is_valid())
return false;
bool found= false;
grt::StringListRef col_flags= col->flags();
size_t flags_count= col_flags.count();
for(size_t i= 0; i < flags_count; i++)
{
grt::StringRef existing_flag= col_flags.get(i);
if(flag_name.compare(existing_flag.c_str()) == 0)
{
found= true;
if(!is_set)
{
AutoUndoEdit undo(_owner);
col_flags.remove(i);
_owner->update_change_date();
(*_owner->get_table()->signal_refreshDisplay())("column");
undo.end(strfmt(_("Unset %s of '%s.%s'"), flag_name.c_str(), _owner->get_name().c_str(), col->name().c_str()));
}
break;
}
}
std::vector<std::string> allowed_flags(get_datatype_flags(node, true));
if (!found && is_set && std::find(allowed_flags.begin(), allowed_flags.end(), flag_name) != allowed_flags.end())
{
AutoUndoEdit undo(_owner);
col_flags.insert(grt::StringRef(flag_name));
_owner->update_change_date();
(*_owner->get_table()->signal_refreshDisplay())("column");
undo.end(strfmt(_("Set %s of '%s.%s'"), flag_name.c_str(), _owner->get_name().c_str(), col->name().c_str()));
return true;
}
}
return false;
}
int TableColumnsListBE::get_column_flag(const ::bec::NodeId &node, const std::string& flag_name)
{
db_ColumnRef col;
std::vector<std::string> retval;
if (node.is_valid())
{
if (node[0] < real_count())
col= _owner->get_table()->columns().get(node[0]);
if(!col.is_valid())
return 0;
if (col->simpleType().is_valid())
{
grt::StringListRef col_flags= col->flags();
size_t flags_count= col_flags.count();
for(size_t i= 0; i < flags_count; i++)
{
grt::StringRef existing_flag= col_flags.get(i);
if(g_ascii_strcasecmp(flag_name.c_str(), existing_flag.c_str()) == 0)
return 1;
}
}else if (col->userType().is_valid())
return std::string(col->userType()->flags()).find(flag_name) != std::string::npos;
}
return 0;
}
std::string TableColumnsListBE::quote_value_if_needed(const db_ColumnRef &column, const std::string &value)
{
std::string datatypeName;
std::string datatypeGroupName;
if (column->userType().is_valid() && column->userType()->actualType().is_valid())
datatypeName= column->userType()->actualType()->name();
else if (column->simpleType().is_valid())
{
datatypeName= column->simpleType()->name();
datatypeGroupName= column->simpleType()->group()->name();
}
// Only quote (var)char and do it only if not already quoted and not given the special values 0/NULL.
// Non-text types (like datetime) shouldn't be auto-quoted.
// Using the "text" and "string" groups here includes more than we'd need (e.g. TEXT blobs), but as long
// as this function is only used for default values (which blobs cannot have) we are fine.
bool isChar = (g_ascii_strcasecmp(datatypeGroupName.c_str(), "string") == 0) ||
(g_ascii_strcasecmp(datatypeGroupName.c_str(), "text") == 0) ||
(g_ascii_strcasecmp(datatypeName.c_str(), "ENUM") == 0) ||
(g_ascii_strcasecmp(datatypeName.c_str(), "SET") == 0);
if (isChar && (value != "NULL") && (value != "0") && (value[0] != '\''))
return std::string("'").append(escape_sql_string(value)).append("'");
return value;
}
//----------------------------------------------------------------------
MenuItemList TableColumnsListBE::get_popup_items_for_nodes(const std::vector<NodeId> &nodes)
{
MenuItemList items;
//struct WBPUBLICBACKEND_PUBLIC_FUNC MenuItem
//{
// std::string caption;
// std::string shortcut;
// std::string name;
// MenuItemType type;
//
// bool enabled;
// bool checked;
//
// MenuItem() : type(MenuAction), enabled(true), checked(false) {}
//};
MenuItem sep;
sep.type = MenuSeparator;
MenuItem item;
item.caption = "Move Up";
item.name = "moveUpToolStripMenuItem";
item.enabled = nodes.size() == 1;
items.push_back(item);
item.caption = "Move Down";
item.name = "moveDownToolStripMenuItem";
item.enabled = nodes.size() == 1;
items.push_back(item);
items.push_back(sep);
item.caption = "Copy";
item.name = "copyColumnToolStripMenuItem";
item.enabled = nodes.size() > 0;
items.push_back(item);
item.caption = "Cut";
item.name = "cutColumnToolStripMenuItem";
item.enabled = nodes.size() > 0;
items.push_back(item);
item.caption = "Paste";
item.name = "pasteColumnToolStripMenuItem";
item.enabled = false;
bec::Clipboard *clip = _owner->get_grt_manager()->get_clipboard();
if (clip->is_data())
{
std::list<grt::ObjectRef> data = clip->get_data();
bool ok = true;
for (std::list<grt::ObjectRef>::iterator i = data.begin(); i != data.end(); ++i)
{
if (!db_ColumnRef::can_wrap(*i))
ok = false;
}
item.enabled = ok;
}
items.push_back(item);
item.caption = "Delete Selected";
item.name = "deleteSelectedColumnsToolStripMenuItem";
item.enabled = nodes.size() > 0;
items.push_back(item);
items.push_back(sep);
item.caption = "Refresh";
item.name = "refreshGridToolStripMenuItem";
item.enabled = true;
items.push_back(item);
items.push_back(sep);
item.caption = "Clear Default";
item.name = "clearDefaultToolStripMenuItem";
item.enabled = nodes.size() > 0;
items.push_back(item);
item.caption = "Default NULL";
item.name = "defaultNULLToolStripMenuItem";
item.enabled = nodes.size() > 0;
items.push_back(item);
return items;
}
bool TableColumnsListBE::activate_popup_item_for_nodes(const std::string &name, const std::vector<NodeId> &orig_nodes)
{
std::vector<NodeId> nodes(orig_nodes);
std::sort(nodes.begin(), nodes.end());
if (name == "moveUpToolStripMenuItem")
{
if (nodes.size() == 1)
{
size_t row = nodes.front()[0];
reorder(nodes.front(), row-1);
}
}
else if (name == "moveDownToolStripMenuItem")
{
if (nodes.size() == 1)
{
size_t row = nodes.front()[0];
reorder(nodes.front(), row+1);
}
}
else if (name == "copyColumnToolStripMenuItem")
{
bec::Clipboard *clip = _owner->get_grt_manager()->get_clipboard();
clip->clear();
for (std::vector<NodeId>::const_iterator iter= nodes.begin();
iter != nodes.end(); ++iter)
{
if ((*iter)[0] < real_count())
{
db_ColumnRef col(_owner->get_table()->columns().get((*iter)[0]));
clip->append_data(col);
}
}
clip->set_content_description("Table Column");
}
else if (name == "cutColumnToolStripMenuItem")
{
bec::Clipboard *clip = _owner->get_grt_manager()->get_clipboard();
clip->clear();
for (std::vector<NodeId>::const_iterator iter= nodes.begin();
iter != nodes.end(); ++iter)
{
if ((*iter)[0] < real_count())
{
db_ColumnRef col(_owner->get_table()->columns().get((*iter)[0]));
clip->append_data(col);
}
}
clip->set_content_description("Table Column");
for (std::vector<NodeId>::reverse_iterator iter= nodes.rbegin();
iter != nodes.rend(); ++iter)
delete_node(*iter);
}
else if (name == "pasteColumnToolStripMenuItem")
{
bec::Clipboard *clip = _owner->get_grt_manager()->get_clipboard();
if (clip->is_data())
{
AutoUndoEdit undo(_owner);
std::list<grt::ObjectRef> data = clip->get_data();
ssize_t insert_after = nodes.empty() ? -1 : nodes[0][0];
if (insert_after >= (ssize_t)real_count())
insert_after = -1;
for (std::list<grt::ObjectRef>::const_iterator i = data.begin(); i != data.end(); ++i)
{
db_ColumnRef column(db_ColumnRef::cast_from(*i));
_owner->duplicate_column(column, insert_after < 0 ? -1 : insert_after++);
}
refresh();
_owner->update_change_date();
undo.end(base::strfmt("Paste columns to table %s", _owner->get_name().c_str()));
}
}
else if (name == "deleteSelectedColumnsToolStripMenuItem")
{
for (std::vector<NodeId>::reverse_iterator iter= nodes.rbegin();
iter != nodes.rend(); ++iter)
{
delete_node(*iter);
}
}
else if (name == "refreshGridToolStripMenuItem")
{
refresh();
_owner->do_partial_ui_refresh(TableEditorBE::RefreshColumnList);
}
else if (name == "clearDefaultToolStripMenuItem")
{
AutoUndoEdit undo(_owner);
bool changed= false;
for (std::vector<NodeId>::reverse_iterator iter= nodes.rbegin();
iter != nodes.rend(); ++iter)
{
if ((*iter)[0] < real_count())
{
db_ColumnRef col(_owner->get_table()->columns().get((*iter)[0]));
if (col.is_valid())
{
bec::ColumnHelper::set_default_value(col, "");
_owner->update_change_date();
changed= true;
}
}
}
if (changed)
{
undo.end(_("Clear Column Default"));
_owner->do_partial_ui_refresh(TableEditorBE::RefreshColumnList);
}
else
undo.cancel();
}
else if (name == "defaultNULLToolStripMenuItem")
{
AutoUndoEdit undo(_owner);
bool changed= false;
for (std::vector<NodeId>::reverse_iterator iter= nodes.rbegin();
iter != nodes.rend(); ++iter)
{
if ((*iter)[0] < real_count())
{
db_ColumnRef col(_owner->get_table()->columns().get((*iter)[0]));
if (col.is_valid())
{
bec::ColumnHelper::set_default_value(col, "NULL");
_owner->update_change_date();
changed= true;
}
}
}
if (changed)
{
undo.end(_("Set Column Default to NULL"));
_owner->do_partial_ui_refresh(TableEditorBE::RefreshColumnList);
}
else
undo.cancel();
}
else
return false;
return true;
}
bool TableColumnsListBE::can_delete_node(const NodeId &node)
{
return node.is_valid() && node[0] < real_count();
}
bool TableColumnsListBE::delete_node(const NodeId &node)
{
if (!can_delete_node(node))
return false;
_owner->remove_column(node);
return true;
}
//----------------------------------------------------------------------
IndexColumnsListBE::IndexColumnsListBE(IndexListBE *owner)
: _owner(owner)
{
}
void IndexColumnsListBE::refresh()
{
}
size_t IndexColumnsListBE::count()
{
return _owner->get_owner()->get_table()->columns().count();
}
db_IndexColumnRef IndexColumnsListBE::get_index_column(const db_ColumnRef &column)
{
if (column.is_valid() && _owner->get_selected_index().is_valid())
{
grt::ListRef<db_IndexColumn> index_columns(_owner->get_selected_index()->columns());
for (size_t c= index_columns.count(), i= 0; i < c; i++)
{
// because of the way the index editing works, it's impossible
// to have 2 index columns referring to the same table column
if (index_columns[i]->referencedColumn() == column)
return index_columns[i];
}
}
return db_IndexColumnRef();
}
size_t IndexColumnsListBE::get_index_column_index(const db_ColumnRef &column)
{
if (column.is_valid() && _owner->get_selected_index().is_valid())
{
grt::ListRef<db_IndexColumn> index_columns(_owner->get_selected_index()->columns());
for (size_t c = index_columns.count(), i = 0; i < c; i++)
{
// because of the way the index editing works, it's impossible
// to have 2 index columns referring to the same table column
if (index_columns[i]->referencedColumn() == column)
return i;
}
}
return -1;
}
void IndexColumnsListBE::set_index_column_order(const db_IndexColumnRef &column, size_t order)
{
grt::ListRef<db_IndexColumn> index_columns(_owner->get_selected_index()->columns());
size_t index= index_columns.get_index(column); // Check, we may get a BaseListRef::npos (-1) index
g_return_if_fail(index != grt::BaseListRef::npos);
index_columns.reorder(index, order);
}
void IndexColumnsListBE::set_column_enabled(const NodeId &node, bool flag)
{
if (get_column_enabled(node) != flag)
{
if (flag)
_owner->add_column(_owner->get_owner()->get_table()->columns()[node[0]]);
else
_owner->remove_column(node);
}
}
bool IndexColumnsListBE::get_column_enabled(const NodeId &node)
{
db_ColumnRef column(_owner->get_owner()->get_table()->columns()[node[0]]);
return get_index_column(column).is_valid();
}
size_t IndexColumnsListBE::get_max_order_index()
{
size_t order = 0;
if (_owner)
{
const db_IndexRef index = _owner->get_selected_index();
if ( index.is_valid() )
order = index->columns().count();
}
return order;
}
bool IndexColumnsListBE::set_field(const NodeId &node, ColumnId column, ssize_t value)
{
db_IndexColumnRef icolumn;
if (node[0] >= count())
return false;
icolumn= get_index_column(_owner->get_owner()->get_table()->columns()[node[0]]);
if (!_owner->index_editable(_owner->get_selected_index())
&& column != OrderIndex && !(icolumn->referencedColumn()->simpleType().is_valid() && column == Length && icolumn->referencedColumn()->simpleType()->group()->name() == "string")) // index order can be changed for PKs
return false; // index length can be changed for PK if it's text
switch (column)
{
case Name:
return false;
case Descending:
if (icolumn.is_valid())
{
AutoUndoEdit undo(_owner->get_owner());
set_column_enabled(node, true);
icolumn->descend(value!=0);
_owner->get_owner()->update_change_date();
undo.end(strfmt(_("Set Storage Order of Index Column '%s.%s.%s'"),
_owner->get_owner()->get_name().c_str(), _owner->get_selected_index()->name().c_str(), icolumn->name().c_str()));
}
return true;
case Length:
if (icolumn.is_valid())
{
AutoUndoEdit undo(_owner->get_owner());
icolumn->columnLength(value);
_owner->get_owner()->update_change_date();
undo.end(
strfmt(_("Set Length of Index Column '%s.%s.%s'"),
_owner->get_owner()->get_name().c_str(), _owner->get_selected_index()->name().c_str(), icolumn->name().c_str()));
}
return true;
case OrderIndex:
if (icolumn.is_valid())
{
if (value >= 1 && (size_t)value <= get_max_order_index())
{
AutoUndoEdit undo(_owner->get_owner());
set_index_column_order(icolumn, value - 1);
_owner->get_owner()->update_change_date();
undo.end(
strfmt(_("Reorder for Index Column '%s.%s.%s'"),
_owner->get_owner()->get_name().c_str(), _owner->get_selected_index()->name().c_str(), icolumn->name().c_str()));
}
}
return true;
}
return false;
}
bool IndexColumnsListBE::set_field(const NodeId &node, ColumnId column, const std::string &value)
{
db_IndexColumnRef icolumn;
if (node[0] >= count())
return false;
if (!_owner->index_editable(_owner->get_selected_index())
&& column != OrderIndex) // index order can be changed for PKs
return false;
switch (column)
{
case Name:
case Descending:
case Length:
return false;
case OrderIndex:
{
int order= 0;
if (sscanf(value.c_str(), "%i", &order) != 1)
return false;
return set_field(node, column, order);
}
}
return false;
}
bool IndexColumnsListBE::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
db_TableRef table= _owner->get_owner()->get_table();
db_ColumnRef dbcolumn;
if (node[0] < table->columns().count())
dbcolumn= table->columns()[node[0]];
switch (column)
{
case Name:
{
if (dbcolumn.is_valid())
value= dbcolumn->name();
else
value= grt::StringRef("");
}
return true;
case Descending:
{
db_IndexColumnRef icolumn= get_index_column(dbcolumn);
if (icolumn.is_valid())
value= icolumn->descend();
else
value= grt::IntegerRef(0);
}
return true;
case Length:
{
db_IndexColumnRef icolumn= get_index_column(dbcolumn);
if (icolumn.is_valid())
value= icolumn->columnLength();
else
value= grt::IntegerRef(0);
}
return true;
case OrderIndex:
{
ssize_t i = get_index_column_index(dbcolumn);
if (i < 0)
value = grt::StringRef("");
else
value = grt::StringRef(base::to_string(i + 1));
return true;
}
}
return false;
}
//----------------------------------------------------------------------
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4355) // 'this' : used in base member initializer list
// that's ok as we just store pointer for later use
#endif
IndexListBE::IndexListBE(TableEditorBE *owner)
: _column_list(this), _owner(owner)
{
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
db_IndexRef IndexListBE::get_selected_index()
{
if (_selected.is_valid() && _selected[0] < real_count())
return _owner->get_table()->indices().get(_selected[0]);
return db_IndexRef();
}
bool IndexListBE::index_editable(const db_IndexRef &index)
{
if (index.is_valid())
{
// check if index is PRIMARY (primary key index) or FOREIGN (fk index)
// FK indices can now have the type changed, as they dont depend on indexType for
// identifying them as FK index anymore --alfredo 10/04/16
if (strcmp(index->indexType().c_str(), "PRIMARY") == 0)
return false;
}
return true;
}
db_ForeignKeyRef IndexListBE::index_belongs_to_fk(const db_IndexRef &index)
{
// check if the index belongs to a FK
if (index.is_valid())
{
ListRef<db_ForeignKey> fks(db_TableRef::cast_from(index->owner())->foreignKeys());
for (size_t c= fks.count(), i= 0; i < c; i++)
if (fks[i]->index() == index)
return fks[i];
}
return db_ForeignKeyRef();
}
MenuItemList IndexListBE::get_popup_items_for_nodes(const std::vector<NodeId> &nodes)
{
db_IndexRef index;
if (!nodes.empty() && nodes[0][0] < _owner->get_table()->indices().count())
index = _owner->get_table()->indices()[nodes[0][0]];
MenuItemList items;
MenuItem item;
item.caption = _("Delete Selected");
item.name = "deleteIndices";
item.enabled = index.is_valid() && nodes.size() > 0 && /*!index_belongs_to_fk(index) && */index_editable(index);
items.push_back(item);
return items;
}
bool IndexListBE::activate_popup_item_for_nodes(const std::string &name, const std::vector<NodeId> &unsorted_nodes)
{
std::vector<NodeId> nodes(unsorted_nodes);
std::sort(nodes.begin(), nodes.end());
if (name == "deleteIndices")
{
for (std::vector<NodeId>::reverse_iterator node= nodes.rbegin(); node != nodes.rend(); ++node)
{
if ((*node)[0] < _owner->get_table()->indices().count())
{
db_IndexRef index(_owner->get_table()->indices().get((*node)[0]));
db_ForeignKeyRef fk;
if (index.is_valid() && (fk = index_belongs_to_fk(index)).is_valid())
{
// don't let the index to be deleted unless there's another suitable index
if (bec::TableHelper::find_index_usable_by_fk(fk, index, false).is_valid())
;
else
{
mforms::Utilities::show_message("Cannot Delete Index",
base::strfmt("The index '%s' belongs to the Foreign Key '%s'.\nYou must delete the Foreign Key to delete the index.", index->name().c_str(), fk->name().c_str()),
"OK", "", "");
continue;
}
}
}
_owner->remove_index(*node, true);
}
}
else
return false;
return true;
}
bool IndexListBE::can_delete_node(const NodeId &node)
{
return node.is_valid() && node[0] < real_count();
}
bool IndexListBE::delete_node(const NodeId &node)
{
if (!can_delete_node(node))
return false;
_owner->remove_index(node, false);
return true;
}
NodeId IndexListBE::add_column(const db_ColumnRef &column, const db_IndexRef &aIndex)
{
db_IndexRef index= aIndex.is_valid() ? aIndex : get_selected_index();
if (!index.is_valid())
return NodeId();
if (/*!index_editable(index) || */index_belongs_to_fk(index).is_valid())
return NodeId();
// special handling for PK columns
if (strcmp(index->indexType().c_str(), "PRIMARY") == 0)
{
AutoUndoEdit undo(_owner);
_owner->get_table()->addPrimaryKeyColumn(column);
_owner->update_change_date();
undo.end(
strfmt(_("Add column '%s' to primary key from '%s'"),
column->name().c_str(), _owner->get_name().c_str()));
}
else
{
std::string column_struct= index.get_metaclass()->get_member_type("columns").content.object_class;
db_IndexColumnRef icolumn=
_owner->get_grt()->create_object<db_IndexColumn>(column_struct);
icolumn->owner(index);
icolumn->referencedColumn(column);
AutoUndoEdit undo(_owner);
index->columns().insert(icolumn);
_owner->update_change_date();
undo.end(
strfmt(_("Add column '%s' to index '%s.%s'"),
column->name().c_str(), _owner->get_name().c_str(), index->name().c_str()));
}
_column_list.refresh();
return NodeId(index->columns().count() - 1);
}
void IndexListBE::remove_column(const NodeId &node)
{
RefreshUI::Blocker __centry(*_owner);
db_IndexRef index= get_selected_index();
if (/*!index_editable(index) || */index_belongs_to_fk(index).is_valid())
return;
db_ColumnRef column(_owner->get_table()->columns().get(node[0]));
// special handling for PK columns
if (strcmp(index->indexType().c_str(), "PRIMARY") == 0)
{
AutoUndoEdit undo(_owner);
_owner->get_table()->removePrimaryKeyColumn(column);
_owner->update_change_date();
undo.end(
strfmt(_("Remove column '%s' from primary key from '%s'"),
column->name().c_str(), _owner->get_name().c_str()));
}
else
{
for (size_t c= index->columns().count(), i= 0; i < c; i++)
{
if (index->columns().get(i)->referencedColumn() == column)
{
AutoUndoEdit undo(_owner);
index->columns().remove(i);
_owner->update_change_date();
undo.end(
strfmt(_("Remove column '%s' from index '%s.%s'"),
column->name().c_str(), _owner->get_name().c_str(), index->name().c_str()));
_column_list.refresh();
break;
}
}
}
}
void IndexListBE::refresh()
{
}
size_t IndexListBE::count()
{
return real_count()+1;
}
size_t IndexListBE::real_count()
{
return _owner->get_table()->indices().count();
}
bool IndexListBE::set_field(const NodeId &node, ColumnId column, const std::string &value)
{
if (!node.is_valid())
return false;
db_IndexRef index;
std::vector<std::string> index_types;
//if (node[0] == count() && column == Name)
if (node[0] == real_count()) // a field added for new index placeholder
{
if (value.empty()) return false;
if (column == Name)
{
_owner->add_index(value);
return true;
}
else
_owner->add_index(("index" + base::to_string(count())).c_str());
}
index= _owner->get_table()->indices().get(node[0]);
if (!index_editable(index) && column != Comment) // Allow editing comments on PKs.
return false;
switch (column)
{
case Name:
if (index->name() != value)
{
AutoUndoEdit undo(_owner, index, "name");
index->name(value);
_owner->update_change_date();
undo.end(strfmt(_("Rename Index '%s.%s'"), _owner->get_name().c_str(), index->name().c_str()));
bec::ValidationManager::validate_instance(index, CHECK_NAME);
}
return true;
case Type:
index_types= _owner->get_index_types();
if (std::find(index_types.begin(), index_types.end(), value) == index_types.end())
return false;
// fk indices or primary indices cant be created by user
if (value == "PRIMARY")
return false;
if (index->indexType() != value)
{
AutoUndoEdit undo(_owner);
index->indexType(value);
index->unique(value == "UNIQUE");
_owner->update_change_date();
undo.end(strfmt(_("Set Type of Index '%s.%s'"), _owner->get_name().c_str(), index->name().c_str()));
}
return true;
case Comment:
if (index->comment() != value)
{
AutoUndoEdit undo(_owner, index, "comment");
index->comment(value);
_owner->update_change_date();
undo.end(strfmt(_("Set Comment of Index '%s.%s'"), _owner->get_name().c_str(), index->name().c_str()));
}
return true;
}
return false;
}
bool IndexListBE::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
db_IndexRef index;
if (node[0] < real_count())
index= _owner->get_table()->indices().get(node[0]);
switch (column)
{
case Name:
if (node[0] < real_count())
value= index->name();
else
value= grt::StringRef("");
return true;
case Type:
if (node[0] < real_count())
value= index->indexType();
else
value= grt::StringRef("");
return true;
case Comment:
if (node[0] < real_count())
value= index->comment();
else
value= grt::StringRef("");
return true;
}
return false;
}
void IndexListBE::select_index(const NodeId &node)
{
_selected= node;
_column_list.refresh();
}
//----------------------------------------------------------------------
FKConstraintColumnsListBE::FKConstraintColumnsListBE(FKConstraintListBE *owner)
: _owner(owner)
{
}
void FKConstraintColumnsListBE::refresh()
{
_referenced_columns.clear();
db_ForeignKeyRef fk(_owner->get_selected_fk());
if (fk.is_valid())
{
for (size_t i= fk->columns().count(); i >= 1; --i)
{
db_ColumnRef column(fk->columns()[i-1]);
bool ok= false;
if (column.is_valid() && i-1 < fk->referencedColumns().count())
{
db_ColumnRef refcolumn(fk->referencedColumns()[i-1]);
if (refcolumn.is_valid())
ok= true;
_referenced_columns[column.id()]= refcolumn;
}
if (!ok)
{
// invalid! remove this entry
fk->columns().remove(i-1);
if (i-1 < fk->referencedColumns().count())
fk->referencedColumns().remove(i-1);
_owner->get_owner()->get_grt()->make_output_visible();
_owner->get_owner()->get_grt()->send_warning("Removed corrupt column definition for Foreign Key "+*fk->name());
}
}
}
}
static std::set<std::string> get_indexed_column_ids(const db_TableRef &table)
{
std::set<std::string> set;
for (size_t c = table->indices().count(), i = 0; i < c; i++)
{
db_IndexRef index(table->indices()[i]);
for (size_t cc = index->columns().count(), j= 0; j < cc; j++)
set.insert(index->columns()[j]->referencedColumn().id());
}
return set;
}
/** Return list of candidate referenced column names for the FK.
*
* @param node of the source column in the FK column list.
* @return List of possible column names.
*/
std::vector<std::string> FKConstraintColumnsListBE::get_ref_columns_list(const NodeId &node, bool filtered)
{
db_ForeignKeyRef fk(_owner->get_selected_fk());
if (fk.is_valid() && fk->referencedTable().is_valid() && (size_t)node[0] < _owner->get_owner()->get_table()->columns().count())
{
std::vector<std::string> names, names2;
db_TableRef table(fk->referencedTable());
grt::ListRef<db_Column> columns(table->columns());
std::set<std::string> indexed_column_ids(get_indexed_column_ids(table));
db_ColumnRef srccolumn(_owner->get_owner()->get_table()->columns()[node[0]]);
for (size_t c= columns.count(), i= 0; i < c; i++)
{
if (!filtered)
names.push_back(columns[i]->name());
else if (_owner->get_owner()->check_column_referenceable_by_fk(columns[i], srccolumn))
{
// column must have index to be in a Fk
if (table->isPrimaryKeyColumn(columns[i]))
names.push_back(columns[i]->name());
else if (indexed_column_ids.find(columns[i]->id()) != indexed_column_ids.end())
names2.push_back(columns[i]->name());
}
}
names.insert(names.end(), names2.begin(), names2.end());
if (fk->referencedTable()->isStub())
names.push_back("Specify Column...");
return names;
}
return std::vector<std::string>();
}
size_t FKConstraintColumnsListBE::count()
{
if (_owner->get_selected_fk().is_valid())
return _owner->get_owner()->get_table()->columns().count();
return 0;
}
bool FKConstraintColumnsListBE::set_fk_column_pair(const db_ColumnRef &column, const db_ColumnRef &refcolumn)
{
_referenced_columns[column.id()]= refcolumn;
db_ForeignKeyRef fk(_owner->get_selected_fk());
AutoUndoEdit undo(_owner->get_owner());
bool changed_real_fk= false;
// check if the srccolumn is already in FK
size_t index= fk->columns().get_index(column);
if (index != grt::BaseListRef::npos)
{
if (!refcolumn.is_valid())
{
// remove real
size_t table_column_index= _owner->get_owner()->get_table()->columns().get_index(column);
if (table_column_index != grt::BaseListRef::npos)
{
_owner->remove_column(table_column_index);
changed_real_fk= true;
}
}
else
{
// update real
fk->referencedColumns().set(index, refcolumn);
changed_real_fk= true;
}
}
else
{
// if everything is ok, create it for real
if (column.is_valid() && refcolumn.is_valid())
{
_owner->add_column(column, refcolumn, fk);
changed_real_fk= true;
}
}
if (changed_real_fk)
{
TableHelper::update_foreign_key_index(fk);
_owner->get_owner()->update_change_date();
undo.end(strfmt(_("Set Ref. Column for FK '%s.%s'"),
_owner->get_owner()->get_name().c_str(), fk->name().c_str()));
}
else
undo.cancel();
return changed_real_fk;
}
bool FKConstraintColumnsListBE::set_column_is_fk(const NodeId &node, bool flag)
{
if (get_column_is_fk(node) != flag)
{
if (flag)
{
// disallow
if (flag && get_ref_columns_list(node).empty())
return false;
// guess some column from the ref table
db_ForeignKeyRef fk(_owner->get_selected_fk());
//std::string type;
db_ColumnRef srccolumn(_owner->get_owner()->get_table()->columns()[node[0]]);
db_ColumnRef refcolumn;
//type= ColumnHelper::format_column_type(srccolumn, 0, true);
if (fk.is_valid() && fk->referencedTable().is_valid())
{
db_TableRef table(fk->referencedTable());
grt::ListRef<db_Column> columns(table->columns());
for (size_t c= columns.count(), i= 0; i < c; i++)
{
if (srccolumn != columns[i])
{
//if (ColumnHelper::format_column_type(columns[i], 0, true) == type)
if (_owner->get_owner()->check_column_referenceable_by_fk(columns[i], srccolumn))
{
if (table->isPrimaryKeyColumn(columns[i]))
{
refcolumn= columns[i];
break;
}
else
{
if (!refcolumn.is_valid())
refcolumn= columns[i];
}
}
}
}
}
// create it virtually only unless whole definition is valid
set_fk_column_pair(srccolumn, refcolumn);
}
else
{
db_ColumnRef srccolumn(_owner->get_owner()->get_table()->columns()[node[0]]);
// remove it virtually
if (_referenced_columns.find(srccolumn.id()) != _referenced_columns.end())
_referenced_columns.erase(srccolumn.id());
_owner->remove_column(node);
}
return true;
}
return false;
}
ssize_t FKConstraintColumnsListBE::get_fk_column_index(const NodeId &node)
{
db_TableRef table= _owner->get_owner()->get_table();
db_ForeignKeyRef fk(_owner->get_selected_fk());
if (fk.is_valid() && node[0] < table->columns().count())
{
db_ColumnRef column= table->columns()[node[0]];
// find index in list of columns for the FK
for (size_t c= fk->columns().count(), i= 0; i < c; i++)
{
if (fk->columns().get(i) == column)
return i;
}
}
return -1;
}
bool FKConstraintColumnsListBE::get_column_is_fk(const NodeId &node)
{
if (node[0] < _owner->get_owner()->get_table()->columns().count())
{
db_ColumnRef srccolumn(_owner->get_owner()->get_table()->columns()[node[0]]);
return get_fk_column_index(node) >= 0 || _referenced_columns.find(srccolumn.id()) != _referenced_columns.end();
}
return false;
}
bool FKConstraintColumnsListBE::set_field(const NodeId &node, ColumnId column, const std::string &value)
{
db_ForeignKeyRef fk(_owner->get_selected_fk());
db_ColumnRef tcolumn;
switch (column)
{
case Enabled:
return false;
case Column:
// this is not changeable
return false;
case RefColumn:
if (fk.is_valid() && fk->referencedTable().is_valid())
{
std::auto_ptr<AutoUndoEdit> undo;
tcolumn= grt::find_named_object_in_list(fk->referencedTable()->columns(), value);
if (tcolumn.is_valid() == false)
return false;
// special handling for stub tables
if (fk->referencedTable()->isStub() && !tcolumn.is_valid() && !_owner->get_owner()->is_editing_live_object())
{
std::string column_name;
// ask for a column name and add it to the stub table
if (mforms::Utilities::request_input(_("Specify Referenced Column"), _("Referenced Column Name:"),
"", column_name))
{
tcolumn = grt::find_named_object_in_list(fk->referencedTable()->columns(), column_name);
if (!tcolumn.is_valid())
{
undo = std::auto_ptr<AutoUndoEdit>(new AutoUndoEdit(_owner->get_owner()));
// create the column
tcolumn = grt::copy_object(_owner->get_owner()->get_table()->columns()[node[0]]);
fk->referencedTable()->columns().insert(tcolumn);
tcolumn->owner(fk->referencedTable());
tcolumn->name(column_name);
fk->referencedTable()->addPrimaryKeyColumn(tcolumn);
}
}
else
return false;
}
ssize_t index = get_fk_column_index(node);
// if columns is not enabled yet, enable it
if (index < 0)
{
set_field(node, Enabled, 1);
index = get_fk_column_index(node);
if (index < 0)
return false;
}
// check if column is acceptable
std::vector<std::string> allowed_list = get_ref_columns_list(node);
if (std::find(allowed_list.begin(), allowed_list.end(), value) == allowed_list.end())
{
std::set<std::string> indexed_column_ids(get_indexed_column_ids(fk->referencedTable()));
if (indexed_column_ids.find(tcolumn->id()) == indexed_column_ids.end())
{
mforms::Utilities::show_message("Create Foreign Key",
base::strfmt("Selected column %s must be indexed and be of a compatible type for a Foreign Key to be created.", tcolumn->name().c_str()),
"OK");
}
else
{
db_TableRef reftable = fk->referencedTable();
std::string hint;
hint = base::strfmt("\nHint: source column has type %s%s,\nColumn %s of referenced table is %s%s.",
_owner->get_owner()->get_table()->columns()[node[0]]->flags().get_index("UNSIGNED") != grt::BaseListRef::npos ? "UNSIGNED " : "",
_owner->get_owner()->get_table()->columns()[node[0]]->formattedType().c_str(),
value.c_str(),
tcolumn->flags().get_index("UNSIGNED") != grt::BaseListRef::npos ? "UNSIGNED " : "",
tcolumn->formattedType().c_str());
// show a warning
mforms::Utilities::show_message("Create Foreign Key",
base::strfmt("The selected columns do not have compatible types.\n%s",
hint.c_str()),
"OK");
}
return false;
}
set_fk_column_pair(fk->columns()[index], tcolumn);
if (undo.get())
{
undo->end(base::strfmt("Set Column of Foreign Key %s.%s.%s", _owner->get_owner()->get_schema()->name().c_str(),
_owner->get_owner()->get_name().c_str(), fk->name().c_str()));
}
}
return true;
}
return false;
}
bool FKConstraintColumnsListBE::set_field(const NodeId &node, ColumnId column, ssize_t value)
{
db_ForeignKeyRef fk(_owner->get_selected_fk());
switch (column)
{
case Enabled:
if (fk.is_valid())
{
AutoUndoEdit undo(_owner->get_owner());
if (set_column_is_fk(node, value!=0))
{
_owner->get_owner()->update_change_date();
undo.end(value ?
strfmt(_("Add Column to FK '%s.%s'"), _owner->get_owner()->get_name().c_str(), fk->name().c_str()) :
strfmt(_("Remove Column from FK '%s.%s'"), _owner->get_owner()->get_name().c_str(), fk->name().c_str()));
}
else
{
undo.cancel();
db_ColumnRef tcolumn;
if (node[0] < _owner->get_owner()->get_table()->columns().count())
tcolumn = fk->owner()->columns()[node[0]];
db_TableRef reftable = fk->referencedTable();
if (reftable.is_valid() && tcolumn.is_valid() && get_ref_columns_list(node).empty())
{
std::string hint;
if (reftable->primaryKey().is_valid() && reftable->primaryKey()->columns().count() > 0)
hint = base::strfmt("\nHint: source column has type %s%s,\nPK of referenced table is %s%s.",
tcolumn->flags().get_index("UNSIGNED") != grt::BaseListRef::npos ? "UNSIGNED " : "",
tcolumn->formattedType().c_str(),
reftable->primaryKey()->columns()[0]->referencedColumn()->flags().get_index("UNSIGNED") != grt::BaseListRef::npos ? "UNSIGNED " : "",
reftable->primaryKey()->columns()[0]->referencedColumn()->formattedType().c_str());
// show a warning
mforms::Utilities::show_message("Create Foreign Key",
base::strfmt("Referenced table has no candidate columns with a compatible type for %s.%s.%s",
tcolumn->owner()->name().c_str(),
tcolumn->name().c_str(),
hint.c_str()),
"OK");
}
}
return true;
}
return false;
case Column:
// this is not changeable
return false;
case RefColumn:
return false;
}
return false;
}
//--------------------------------------------------------------------------------------------------
bool FKConstraintColumnsListBE::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
switch (column)
{
case Enabled:
value= grt::IntegerRef(get_column_is_fk(node)?1:0);
return true;
case Column:
if (node[0] == count())
value= grt::StringRef("");
else
value= _owner->get_owner()->get_table()->columns()[node[0]]->name();
return true;
case RefColumn:
{
db_ForeignKeyRef fk(_owner->get_selected_fk());
db_ColumnRef tcolumn;
ssize_t index = get_fk_column_index(node);
if (fk.is_valid() && index >= 0 && index < (ssize_t)fk->referencedColumns().count())
{
tcolumn= fk->referencedColumns().get(index);
if (tcolumn.is_valid())
value= tcolumn->name();
else
value= grt::StringRef("");
}
else
value= grt::StringRef("");
}
return true;
}
return false;
}
//----------------------------------------------------------------------
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4355) // 'this' : used in base member initializer list
// that's ok as we just store pointer for later use
#endif
FKConstraintListBE::FKConstraintListBE(TableEditorBE *owner)
: _column_list(this), _owner(owner), _editing_placeholder_row(-1)
{
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
NodeId FKConstraintListBE::add_column(const db_ColumnRef &column, const db_ColumnRef &refcolumn,
const db_ForeignKeyRef &aFk)
{
db_ForeignKeyRef fk= aFk.is_valid() ? aFk : get_selected_fk();
if (fk.is_valid())
{
AutoUndoEdit undo(_owner);
fk->columns().insert(column);
fk->referencedColumns().insert(refcolumn);
// make sure that there's an index that matches the columns in the FK or
// that the one created for it is up to date
TableHelper::update_foreign_key_index(fk);
_owner->update_change_date();
undo.end(
strfmt(_("Add Column to FK '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
_column_list.refresh();
// force a refresh of the columns list in the table figure
//not needed anymore ((db_ColumnRef)column)->name(column->name());
// force refresh of relationships
//not needed anymore fk->referencedTable(fk->referencedTable());
return NodeId(fk->columns().count() - 1);
}
return NodeId();
}
void FKConstraintListBE::remove_column(const NodeId &node)
{
db_ForeignKeyRef fk= get_selected_fk();
db_ColumnRef column(_owner->get_table()->columns().get(node[0]));
const size_t i= fk->columns().get_index(column);
if (i == BaseListRef::npos)
{
// column is not set
return;
}
AutoUndoEdit undo(_owner);
fk->columns().remove(i);
if (fk->referencedColumns().count() > i)
{
fk->referencedColumns().remove(i);
}
TableHelper::update_foreign_key_index(fk);
_owner->update_change_date();
undo.end(
strfmt(_("Remove Column From FK '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
_column_list.refresh();
// force a refresh of the columns list in the table figure
//not needed anymore column->name(column->name());
}
void FKConstraintListBE::select_fk(const NodeId &node)
{
_selected_fk= node;
if (_owner->is_editing_live_object())
{
db_ForeignKeyRef fkey= get_selected_fk();
if (fkey.is_valid())
{
db_TableRef ref_table= fkey->referencedTable();
if (ref_table.is_valid())
{
std::string schema_name= *ref_table->owner()->name();
std::string ref_table_name= *ref_table->name();
_owner->on_expand_live_table_stub(_owner, schema_name, ref_table_name);
}
}
}
_column_list.refresh();
}
db_ForeignKeyRef FKConstraintListBE::get_selected_fk()
{
if (_selected_fk.is_valid() && _selected_fk[0] < real_count())
return _owner->get_table()->foreignKeys().get(_selected_fk[0]);
else
return db_ForeignKeyRef();
}
void FKConstraintListBE::refresh()
{
}
size_t FKConstraintListBE::count()
{
return real_count() + 1;
}
size_t FKConstraintListBE::real_count()
{
return _owner->get_table()->foreignKeys().count();
}
bool FKConstraintListBE::set_field(const NodeId &node, ColumnId column, ssize_t value)
{
db_ForeignKeyRef fk;
if (node[0] == count() - 1)
_editing_placeholder_row = node[0];
else
_editing_placeholder_row = -1;
if (node[0] >= real_count())
return false;
fk= _owner->get_table()->foreignKeys().get(node[0]);
switch (column)
{
case ModelOnly:
if ((*fk->modelOnly()!=0) != (value!=0))
{
AutoUndoEdit undo(_owner, fk, "modelOnly");
fk->modelOnly(value!=0);
undo.end(strfmt(_("Toggle SQL generation for '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
}
return true;
default:
break;
}
return false;
}
static bool check_if_null(TableEditorBE *_owner, db_ForeignKeyRef &fk, std::string caption)
{
bool one_col_is_not_null = false;
for (size_t i = 0; i < fk->columns().count(); i++)
{
if (fk->columns()[i]->isNotNull())
{
one_col_is_not_null = true;
break;
}
}
if (one_col_is_not_null)
{
int ret_val = mforms::Utilities::show_warning(caption,
_("You can't use a SET NULL action if one of the referencing columns is set to NOT NULL.\n"
"Would you like to revert the change or remove the NOT NULL from the column attribute?"),
_("Remove NOT NULL"), _("Revert"));
if (ret_val == mforms::ResultCancel)
return false;
AutoUndoEdit undo(_owner);
for (size_t i =0; i < fk->columns().count(); i++)
{
fk->columns()[i]->isNotNull(false);
}
_owner->update_change_date();
(*_owner->get_table()->signal_refreshDisplay())("column");
undo.end(
strfmt(_("Remove NOT NULL for columns '%s'"), fk->owner()->name().c_str()));
_owner->do_ui_refresh();
}
return true;
}
bool FKConstraintListBE::set_field(const NodeId &node, ColumnId column, const std::string &value)
{
db_ForeignKeyRef fk;
if (node[0] == real_count() && column == Name)
{
if (value.empty()) return false;
_editing_placeholder_row = -1;
_owner->add_fk(value);
return true;
}
else if (node[0] >= real_count())
return false;
fk= _owner->get_table()->foreignKeys().get(node[0]);
switch (column)
{
case Name:
if (fk->name() != value)
{
AutoUndoEdit undo(_owner, fk, "name");
TableHelper::rename_foreign_key(_owner->get_table(), fk, value);
_owner->update_change_date();
undo.end(_("Rename FK"));
}
return true;
case OnDelete:
if (fk->deleteRule() != value)
{
if (value.find("SET NULL") != std::string::npos)
{
if (!check_if_null(_owner, fk, _("On Delete Action")))
return false;
}
AutoUndoEdit undo(_owner);
fk->deleteRule(value);
_owner->update_change_date();
undo.end(
strfmt(_("Change ON DELETE for FK '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
}
return true;
case OnUpdate:
if (fk->updateRule() != value)
{
if (value.find("SET NULL") != std::string::npos)
{
if (!check_if_null(_owner, fk, _("On Update Action")))
return false;
}
AutoUndoEdit undo(_owner);
fk->updateRule(value);
_owner->update_change_date();
undo.end(
strfmt(_("Change ON UPDATE for FK '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
}
return true;
case RefTable:
{
std::vector<std::string> parts= base::split_qualified_identifier(value);
std::string table;
db_SchemaRef schema;
if (!parts.empty())
{
if (value == "Specify Manually...")
{
std::string table_name;
if (mforms::Utilities::request_input(_("Specify Referenced Table Manually"), _("Qualified Table Name:"),
"", table_name))
{
parts= base::split_qualified_identifier(table_name);
}
else
{
parts.clear();
}
if (parts.empty())
return false;
}
if (_owner->is_editing_live_object())
{
if (parts.size()==1)
parts.insert(parts.begin(), *_owner->get_schema()->name());
if (!_owner->on_expand_live_table_stub(_owner, parts[0], parts[1]))
return false;
}
if (parts.size()==1)
{
table= parts[0];
schema= _owner->get_schema();
}
else
{
table= parts[1];
schema= _owner->get_schema_with_name(parts[0]);
}
db_TableRef dbtable;
if (schema.is_valid())
{
dbtable= grt::find_named_object_in_list(schema->tables(), table, "name");
if (!dbtable.is_valid())
return false;
}
if (!dbtable.is_valid() || dbtable != fk->referencedTable())
{
AutoUndoEdit undo(_owner);
{
std::string schema_name = schema.is_valid() ? *schema->name() : parts[0];
log_info("Creating stub schema and table %s.%s for foreign key\n", schema_name.c_str(), table.c_str());
dbtable = _owner->create_stub_table(schema_name, table);
if (!dbtable.is_valid())
return false;
}
fk->referencedTable(dbtable);
// reset the currently set columns
fk->columns().remove_all();
fk->referencedColumns().remove_all();
get_columns()->refresh();
TableHelper::update_foreign_key_index(fk);
_owner->update_change_date();
bec::ValidationManager::validate_instance(_owner->get_table(), "chk_fk_lgc");
bec::ValidationManager::validate_instance(dbtable, "chk_fk_lgc");
undo.end(strfmt(_("Change Ref. Table for FK '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
}
}
}
return true;
case Comment:
if (fk->comment() != value)
{
AutoUndoEdit undo(_owner, fk, "comment");
fk->comment(value);
_owner->update_change_date();
undo.end(strfmt(_("Change Comment for FK '%s.%s'"), _owner->get_name().c_str(), fk->name().c_str()));
}
return true;
case Index:
// cannot change index, its auto-created/maintained
return false;
case ModelOnly: // handled as int
return false;
}
return false;
}
bool FKConstraintListBE::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
db_ForeignKeyRef fk;
if (node[0] < real_count())
fk= _owner->get_table()->foreignKeys().get(node[0]);
switch (column)
{
case Name:
if (fk.is_valid())
value= fk->name();
else if (_editing_placeholder_row == node[0])
{
std::string temp = replace_string(_owner->get_grt_manager()->get_app_option_string("FKNameTemplate"),
"%stable%", _owner->get_name().c_str());
value = grt::StringRef(get_name_suggestion_for_list_object(_owner->get_table()->foreignKeys(), replace_string(temp, "%dtable%", ""), true));
}
else
value= grt::StringRef("");
return true;
case OnDelete:
if (fk.is_valid())
{
if (fk->deleteRule().empty())
value = grt::StringRef("RESTRICT"); // empty means default means RESTRICT
else
value= fk->deleteRule();
}
else
value= grt::StringRef("");
return true;
case OnUpdate:
if (fk.is_valid())
{
if (fk->updateRule().empty())
value = grt::StringRef("RESTRICT"); // empty means default means RESTRICT
else
value= fk->updateRule();
}
else
value= grt::StringRef("");
return true;
case RefTable:
if (fk.is_valid() && fk->referencedTable().is_valid())
value= grt::StringRef("`" + *fk->referencedTable()->owner()->name() + "`.`" + *fk->referencedTable()->name() + "`");
else
value= grt::StringRef("");
return true;
case Comment:
if (fk.is_valid())
value= fk->comment();
else
value= grt::StringRef("");
return true;
case Index:
if (fk.is_valid())
{
if (fk->index().is_valid())
value = fk->index()->name();
else
value = grt::StringRef(""); // a FK with no index means it just didn't have one created for it
//value = grt::StringRef("INVALID");
}
else
value = grt::StringRef("");
return true;
case ModelOnly:
if (fk.is_valid())
{
value= grt::IntegerRef(*fk->modelOnly());
return true;
}
}
return false;
}
bool FKConstraintListBE::can_delete_node(const NodeId &node)
{
return node.is_valid() && node[0] < real_count();
}
bool FKConstraintListBE::delete_node(const NodeId &node)
{
if (!can_delete_node(node))
return false;
_owner->remove_fk(node);
return true;
}
MenuItemList FKConstraintListBE::get_popup_items_for_nodes(const std::vector<NodeId> &nodes)
{
MenuItemList items;
MenuItem item;
item.caption = _("Delete selected");
item.name = "deleteSelectedFKs";
item.enabled = (nodes.size() >= 1);
items.push_back(item);
return items;
}
bool FKConstraintListBE::activate_popup_item_for_nodes(const std::string &name, const std::vector<NodeId>& nodes)
{
std::vector<NodeId> sorted_nodes(nodes);
std::sort(sorted_nodes.begin(), sorted_nodes.end());
if (name == "deleteSelectedFKs")
{
for (ssize_t i = sorted_nodes.size() - 1; i >= 0; --i)
delete_node(sorted_nodes[i]);
}
else
return false;
return true;
}
//----------------------------------------------------------------------
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable:4355) // 'this' : used in base member initializer list
// that's ok as we just store pointer for later use
#endif
TableEditorBE::TableEditorBE(GRTManager *grtm, const db_TableRef &table)
: DBObjectEditorBE(grtm, table), _fk_list(this)
{
_inserts_panel = NULL;
_inserts_grid = NULL;
if (table.class_name() == "db.Table")
throw std::logic_error("table object is abstract");
scoped_connect(get_catalog()->signal_changed(), boost::bind(&TableEditorBE::catalogChanged, this, _1, _2));
}
#ifdef _WIN32
#pragma warning(pop)
#endif
void TableEditorBE::set_name(const std::string &name)
{
if (name != get_name())
{
RefreshUI::Blocker __centry(*this);
AutoUndoEdit undo(this, get_object(), "name");
bec::ValidationManager::validate_instance(get_table(), CHECK_NAME);
std::string name_= base::trim_right(name);
get_dbobject()->name(name_);
undo.end(strfmt(_("Rename Table to '%s'"), name_.c_str()));
}
}
NodeId TableEditorBE::add_column(const std::string &name)
{
db_ColumnRef column;
column= get_grt()->create_object<db_Column>(get_table().get_metaclass()->get_member_type("columns").content.object_class);
column->name(name);
column->owner(get_table());
AutoUndoEdit undo(this);
get_table()->addColumn(column);
update_change_date();
undo.end(strfmt(_("Add Column '%s' to '%s'"), name.c_str(), get_name().c_str()));
get_columns()->refresh();
column_count_changed();
bec::ValidationManager::validate_instance(column, CHECK_NAME);
bec::ValidationManager::validate_instance(get_table(), "columns-count");
return NodeId(get_table()->columns().count()-1);
}
NodeId TableEditorBE::duplicate_column(const db_ColumnRef &column, ssize_t insert_after)
{
db_ColumnRef new_column = shallow_copy_object(column);
new_column->oldName(""); //It's new column, so it shouldn't have any old name set.
std::string name = new_column->name();
std::string new_name = name;
for (int i = 1;; i++)
{
if (!find_named_object_in_list(get_table()->columns(), new_name).is_valid())
break;
new_name = strfmt("%s_copy%i", name.c_str(), i);
}
if (new_name != *new_column->name())
new_column->name(new_name);
new_column->owner(get_table());
get_table()->addColumn(new_column);
if (insert_after >= 0)
get_table()->columns()->reorder(get_table()->columns()->get_index(new_column), insert_after);
bec::ValidationManager::validate_instance(new_column, CHECK_NAME);
bec::ValidationManager::validate_instance(get_table(), "columns-count");
column_count_changed();
return NodeId(get_table()->columns().count() - 1);
}
db_ColumnRef TableEditorBE::get_column_with_name(const std::string &name)
{
return grt::find_named_object_in_list<db_Column>(get_table()->columns(), name, "name");
}
void TableEditorBE::rename_column(const db_ColumnRef &column, const std::string &name)
{
std::string old_name= column->name();
AutoUndoEdit undo(this);
((db_ColumnRef)column)->name(name);
update_change_date();
undo.end(strfmt(_("Rename '%s.%s' to '%s'"), get_name().c_str(), old_name.c_str(), name.c_str()));
bec::ValidationManager::validate_instance(column, CHECK_NAME);
column_count_changed();
}
void TableEditorBE::remove_column(const NodeId &node)
{
db_TableRef table = get_table();
if (node[0] >= table->columns().count())
return;
db_ColumnRef column= get_table()->columns()[node[0]];
AutoUndoEdit undo(this);
table->removeColumn(column);
undo.end(strfmt(_("Remove '%s.%s'"), get_name().c_str(), column->name().c_str()));
get_columns()->refresh();
bec::ValidationManager::validate_instance(get_table(), "columns-count");
column_count_changed();
}
NodeId TableEditorBE::add_fk(const std::string &name)
{
if(!get_table()->columns().count())
{
mforms::Utilities::show_warning("FK Creation", "Cannot add FK on empty table, add some columns first", "OK");
return NodeId();
}
ListRef<db_ForeignKey> fklist= get_table()->foreignKeys();
db_ForeignKeyRef fk;
AutoUndoEdit undo(this);
fk= TableHelper::create_empty_foreign_key(get_grt(), get_table(), name);
fk->updateRule(StringRef::cast_from(_grtm->get_app_option
("db.ForeignKey:updateRule")));
fk->deleteRule(StringRef::cast_from(_grtm->get_app_option("db.ForeignKey:deleteRule")));
update_change_date();
undo.end(strfmt(_("Add Foreign Key '%s' to '%s'"), name.c_str(), get_name().c_str()));
_fk_list.refresh();
bec::ValidationManager::validate_instance(fk, CHECK_NAME);
return NodeId(fklist.count() - 1);
}
bool TableEditorBE::remove_fk(const NodeId &fk)
{
ListRef<db_ForeignKey> fklist= get_table()->foreignKeys();
if(fk[0] >= fklist.count())
return false;
const db_TableRef ref_table = fklist[fk[0]]->referencedTable();
AutoUndoEdit undo(this);
std::string name= fklist[fk[0]]->name();
// needs to execute from GRT thread so that side-effects get executed properly
get_table()->removeForeignKey(fklist[fk[0]], false);
// run_from_grt(boost::bind(&db_Table::removeForeignKey, get_table().content(), fklist[fk[0]], 0));
//bec::TableHelper::remove_foreign_key(get_table(), fklist[fk[0]], false, get_undo_manager());
update_change_date();
undo.end(strfmt(_("Remove Foreign Key '%s'.'%s'"), get_name().c_str(), name.c_str()));
_fk_list.refresh();
// There might be no referenced table yet.
if (ref_table.is_valid())
bec::ValidationManager::validate_instance(ref_table, "chk_fk_lgc");
bec::ValidationManager::validate_instance(get_table(), "chk_fk_lgc");
return true;
}
NodeId TableEditorBE::add_index(const std::string &name)
{
if(!get_table()->columns().count())
{
mforms::Utilities::show_warning("Index Creation", "Cannot add Index on empty table, add some columns first", "OK");
return NodeId();
}
ListRef<db_Index> indices= get_table()->indices();
db_IndexRef index;
if (indices.content_class_name() == "db.Index") throw std::logic_error("internal bug");
index= get_grt()->create_object<db_Index>(indices.content_class_name());
index->name(name);
index->owner(get_table());
std::vector<std::string> types;
types= get_index_types();
index->indexType(types[0]);
AutoUndoEdit undo(this);
update_change_date();
indices.insert(index);
undo.end(strfmt(_("Add Index '%s' to '%s'"), name.c_str(), get_name().c_str()));
get_indexes()->refresh();
bec::ValidationManager::validate_instance(index, CHECK_NAME);
bec::ValidationManager::validate_instance(get_table(), CHECK_EFFICIENCY);
return NodeId(indices.count() - 1);
}
bool TableEditorBE::remove_index(const NodeId &index, bool delete_even_if_foreign)
{
if(index[0] >= get_table()->indices().count())
return false;
db_IndexRef indexobj(get_table()->indices()[index[0]]);
db_ForeignKeyRef fk;
// do not allow removal of PRIMARY or FOREIGN indices by user
if (!get_indexes()->index_editable(indexobj) ||
((fk = get_indexes()->index_belongs_to_fk(indexobj)).is_valid() && !delete_even_if_foreign))
return false;
AutoUndoEdit undo(this);
get_table()->indices().remove_value(indexobj);
get_indexes()->refresh();
if (fk.is_valid())
fk->index(db_IndexRef());
update_change_date();
undo.end(strfmt(_("Remove Index '%s'.'%s'"), indexobj->name().c_str(), get_name().c_str()));
bec::ValidationManager::validate_instance(get_table(), CHECK_EFFICIENCY);
return true;
}
NodeId TableEditorBE::add_index_with_columns(const std::vector<NodeId> &columns)
{
AutoUndoEdit undo(this);
NodeId id= add_index(get_name_suggestion_for_list_object(get_table()->indices(), "index"));
db_TableRef table= get_table();
db_IndexRef index= table->indices()[id[0]];
ListRef<db_Column> clist= table->columns();
for (std::vector<NodeId>::const_iterator i= columns.begin();
i != columns.end(); ++i)
{
db_ColumnRef column= clist[(*i)[0]];
get_indexes()->add_column(column, index);
}
update_change_date();
undo.end(strfmt(_("Add Index '%s' to '%s'"), index->name().c_str(), get_name().c_str()));
bec::ValidationManager::validate_instance(index, CHECK_NAME);
return id;
}
NodeId TableEditorBE::add_fk_with_columns(const std::vector<NodeId> &columns)
{
AutoUndoEdit undo(this);
NodeId id= add_fk(get_name_suggestion_for_list_object(get_table()->foreignKeys(), "fk"));
db_TableRef table= get_table();
db_ForeignKeyRef fk= table->foreignKeys()[id[0]];
ListRef<db_Column> clist= table->columns();
for (std::vector<NodeId>::const_iterator i= columns.begin();
i != columns.end(); ++i)
{
db_ColumnRef column= clist[(*i)[0]];
_fk_list.add_column(column, db_ColumnRef(), fk);
}
update_change_date();
undo.end(strfmt(_("Add Foreign Key '%s' to '%s'"), fk->name().c_str(), get_name().c_str()));
bec::ValidationManager::validate_instance(fk, CHECK_NAME);
return id;
}
void TableEditorBE::undo_called(grt::UndoAction *action, grt::UndoAction *expected)
{
if (action == expected)
do_ui_refresh();
}
bool TableEditorBE::showErrorMessage(const std::string &type)
{
bool ret = false;
std::string key = base::tolower(type);
if (key == "json")
{
GrtVersionRef version = get_catalog()->version();
bool versionCheck = bec::is_supported_mysql_version_at_least(version, 5, 7, 7);
if (!versionCheck)
{
mforms::Utilities::show_message(_("Type not supported"),
"The JSON data is available not before MySQL version 5.7.7. In order to use it you have first to set this or a higher version for your model (see Model -> Model Options ...).",
_("Ok"));
ret = true;
}
}
//else if
return ret;
}
void TableEditorBE::catalogChanged(const std::string& member, const grt::ValueRef& value)
{
if (member == "version")
{
GrtVersionRef version = GrtVersionRef::cast_from(value);
GrtVersionRef actualVersion = get_catalog()->version();
if (!bec::version_greater(version, actualVersion))
return;
if (bec::is_supported_mysql_version_at_least(actualVersion, 5, 7, 7))
return;
bool messageShown = false;
int ret = mforms::ResultOther;
grt::ListRef<db_Schema> schemas = get_catalog()->schemata();
for (ssize_t i = schemas.count() - 1; i >= 0; --i)
{
if (!schemas[i].is_valid())
continue;
grt::ListRef<db_Table> tables = schemas[i]->tables();
for (ssize_t j = tables.count() - 1; j >= 0; --j)
{
grt::ListRef<db_Column> columns = tables[j]->columns();
for (ssize_t k = columns.count() - 1; k >= 0; --k)
{
if (!(columns[k].is_valid() && columns[k]->simpleType().is_valid()))
continue;
if (!base::same_string(columns[k]->simpleType()->name(), "json", false))
continue;
if (!messageShown)
{
ret = mforms::Utilities::show_message(_("Model downgrade"),
"You have at least one column definition with a data type that is not supported by this server version (JSON)."
"If you continue this data type will be converted to TEXT. Do you want to apply this change, ignore the incompatibility or cancel the version change?",
_("Apply"), _("Cancel"), _("Ignore"));
messageShown = true;
if (ret == mforms::ResultCancel)
{
grt::UndoManager *um = get_grt()->get_undo_manager();
assert(um != NULL);
UndoAction* action = um->get_latest_undo_action();
if (action != NULL && action->description() == "version")
action->undo(um);
return;
}
else if (ret == mforms::ResultOther)
return;
}
columns[k]->setParseType("text", get_catalog()->simpleDatatypes());
}
}
}
}
}
bool TableEditorBE::parse_column_type(const std::string &str, db_ColumnRef &column)
{
db_CatalogRef catalog(get_catalog());
bool flag= column->setParseType(str, catalog->simpleDatatypes()) == 1;
if (flag)
{
grt::UndoManager *um = get_grt()->get_undo_manager();
// call _refresh_ui when this parse column type action is undone
// XXX: everytime we parse a column type 2 new connections are added without removing the old ones!
scoped_connect(um->signal_undo(), boost::bind(&TableEditorBE::undo_called, this, _1, um->get_latest_undo_action()));
scoped_connect(um->signal_redo(), boost::bind(&TableEditorBE::undo_called, this, _1, um->get_latest_undo_action()));
}
return flag;
}
std::string TableEditorBE::format_column_type(db_ColumnRef &column)
{
return column->formattedRawType();
}
//--------------------------------------------------------------------------------------------------
void TableEditorBE::inserts_column_resized(int column)
{
int width = _inserts_grid->get_column_width(column);
grt::IntegerListRef widths;
if (grt::IntegerListRef::can_wrap(get_table()->customData().get("InsertsColumnWidths")))
widths = grt::IntegerListRef::cast_from(get_table()->customData().get("InsertsColumnWidths"));
else
{
widths = grt::IntegerListRef(_grtm->get_grt());
get_table()->customData().set("InsertsColumnWidths", widths);
}
while (column >= (int)widths.count())
widths.insert(grt::IntegerRef(0));
widths.set(column, grt::IntegerRef(width));
}
void TableEditorBE::restore_inserts_columns()
{
grt::IntegerListRef widths;
if (grt::IntegerListRef::can_wrap(get_table()->customData().get("InsertsColumnWidths")))
widths = grt::IntegerListRef::cast_from(get_table()->customData().get("InsertsColumnWidths"));
for (int i = 0; i < _inserts_grid->get_column_count(); i++)
{
bool flag = false;
if (widths.is_valid() && i < (int)widths.count())
{
int width = (int)widths[i];
if (width > 0)
{
_inserts_grid->set_column_width(i, width);
flag = true;
}
}
if (!flag && (int)get_table()->columns().count() > i)
{
// set a default
db_ColumnRef column(get_table()->columns()[i]);
if (column.is_valid() && column->simpleType().is_valid())
{
std::string type_group = column->simpleType()->group()->name();
if (type_group == "string")
{
_inserts_grid->set_column_width(i, std::min((int)column->length() * 15, 200));
}
else if (type_group == "numeric")
_inserts_grid->set_column_width(i, 80);
else
_inserts_grid->set_column_width(i, 150);
}
else
_inserts_grid->set_column_width(i, 100);
}
}
}
void TableEditorBE::open_field_editor(int row, int column)
{
Recordset::Ref rset(get_inserts_model());
if (rset)
{
std::string type;
db_ColumnRef tcolumn(get_table()->columns()[column]);
if (tcolumn.is_valid())
{
if (tcolumn->simpleType().is_valid())
type = tcolumn->simpleType()->name();
else if (tcolumn->userType().is_valid() && tcolumn->userType()->actualType().is_valid())
type = tcolumn->userType()->actualType()->name();
}
rset->open_field_data_editor(row, column, type);
}
}
void TableEditorBE::update_selection_for_menu_extra(mforms::ContextMenu *menu, const std::vector<int> &rows, int column)
{
mforms::MenuItem *item = menu->find_item("edit_cell");
if (item != NULL && !rows.empty())
{
if (item->signal_clicked()->empty())
item->signal_clicked()->connect(boost::bind(&TableEditorBE::open_field_editor, this, rows[0], column));
}
}
// used in unit-tests
Recordset::Ref TableEditorBE::get_inserts_model()
{
if (!_inserts_model)
{
if (get_table().class_name() == "db.Table")
throw std::logic_error("table object is abstract");
_inserts_storage = Recordset_table_inserts_storage::create(_grtm);
_inserts_storage->table(get_table());
_inserts_model = Recordset::create(_grtm);
_inserts_model->update_selection_for_menu_extra = boost::bind(&TableEditorBE::update_selection_for_menu_extra, this, _1, _2, _3);
_inserts_model->set_inserts_editor(true);
_inserts_model->data_storage(_inserts_storage);
_inserts_model->refresh();
}
return _inserts_model;
}
mforms::View *TableEditorBE::get_inserts_panel()
{
if (!_inserts_panel)
{
mforms::ToolBar *tbar = get_inserts_model()->get_toolbar();
tbar->find_item("record_export")->signal_activated()->connect(boost::bind(&TableEditorBE::show_export_wizard, this, (mforms::Form*)0));
if (tbar->find_item("record_import"))
tbar->find_item("record_import")->signal_activated()->connect(boost::bind(&TableEditorBE::show_import_wizard, this));
_inserts_grid = mforms::RecordGrid::create(get_inserts_model());
restore_inserts_columns();
_inserts_grid->signal_column_resized()->connect(boost::bind(&TableEditorBE::inserts_column_resized, this, _1));
_inserts_panel = mforms::manage(new mforms::Box(false));
_inserts_panel->add(mforms::manage(tbar), false, true);
_inserts_panel->add(mforms::manage(_inserts_grid), true, true);
}
return _inserts_panel;
}
//--------------------------------------------------------------------------------------------------
void TableEditorBE::show_export_wizard(mforms::Form *owner)
{
if (_inserts_model && _inserts_model->count() > 0)
{
grt::ValueRef option(_grtm->get_app_option("TableEditor:LastExportDirectory"));
std::string path = option.is_valid() ? grt::StringRef::cast_from(option) : "";
option = _grtm->get_app_option("TableEditor:LastExportExtension");
std::string extension = option.is_valid() ? grt::StringRef::cast_from(option) : "";
InsertsExportForm exporter(owner, _inserts_model, extension);
exporter.set_title(strfmt(_("Export Inserts for %s"), get_name().c_str()));
if (!path.empty())
{
path = bec::make_path(path, get_name());
exporter.set_path(path);
}
path = exporter.run();
if (path.empty())
_grtm->replace_status_text(_("Export inserts canceled"));
else
{
_grtm->replace_status_text(strfmt(_("Exported inserts to %s"), path.c_str()));
_grtm->set_app_option("TableEditor:LastExportDirectory", grt::StringRef(exporter.get_directory()));
extension = base::extension(path);
if (!extension.empty() && extension[0] == '.')
extension = extension.substr(1);
if (!extension.empty())
_grtm->set_app_option("TableEditor:LastExportExtension", grt::StringRef(extension));
}
}
else
mforms::Utilities::show_message("Export Data", "No data to be exported.", "OK");
}
//--------------------------------------------------------------------------------------------------
void TableEditorBE::show_import_wizard()
{
grt::BaseListRef args(_grtm->get_grt());
db_TableRef table(get_table());
if (table.is_valid() && table->columns().count() > 0)
{
args.ginsert(grtwrap_editablerecordset(table, _inserts_model));
grt::Module *module = _grtm->get_grt()->get_module("SQLIDEUtils");
if (module)
{
try
{
module->call_function("importRecordsetDataFromFile", args);
}
catch (grt::module_error &exc)
{
mforms::Utilities::show_error("Import Standard INSERTs", "Error during data import:\n" + exc.inner, "OK");
}
catch (std::exception &exc)
{
mforms::Utilities::show_error("Import Standard INSERTs", "Error during data import:\n" + std::string(exc.what()), "OK");
}
}
else
log_error("Can't find module SQLIDEUtils for record importer");
}
}
//--------------------------------------------------------------------------------------------------
MySQLEditor::Ref TableEditorBE::get_sql_editor()
{
MySQLEditor::Ref sql_editor= DBObjectEditorBE::get_sql_editor();
if (sql_editor)
sql_editor->restrict_content_to(MySQLEditor::ContentTypeTrigger);
return sql_editor;
}
std::string TableEditorBE::get_title()
{
return base::strfmt("%s - Table", get_name().c_str());
}
bool TableEditorBE::can_close()
{
if (_inserts_grid && _inserts_model->has_pending_changes())
{
int ret = mforms::Utilities::show_message("Close Table Editor",
base::strfmt("There are unsaved changes to the INSERTs data for %s. "
"If you do not save, these changes will be discarded.", get_name().c_str()),
"Save Changes", "Cancel", "Don't Save");
if (ret == mforms::ResultOk)
_inserts_model->apply_changes();
else if (ret == mforms::ResultOther)
_inserts_model->rollback();
else
return false;
}
return DBObjectEditorBE::can_close();
}
void TableEditorBE::column_count_changed()
{
if (_inserts_model)
_inserts_model->refresh();
if (_inserts_grid)
_inserts_grid->update_columns();
}
|