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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.BooleanValuesTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.lang;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import junit.framework.Test;
import org.apache.derby.iapi.types.HarmonySerialBlob;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
import org.apache.derbyTesting.junit.XML;
/**
* <p>
* Test Derby's expanding support for BOOLEAN values.
* </p>
*/
public class BooleanValuesTest extends GeneratedColumnsHelper
{
///////////////////////////////////////////////////////////////////////////////////
//
// CONSTANTS
//
///////////////////////////////////////////////////////////////////////////////////
private static final String ILLEGAL_GET = "22005";
private static final String ILLEGAL_XML_SELECTION = "42Z71";
private static final String BAD_CAST = "22018";
private static final String NOT_UNION_COMPATIBLE = "42X61";
private static final String BAD_CONVERSION = "42846";
private static final String ILLEGAL_INSERT = "42821";
private static final String BAD_DEFAULT = "42894";
private static final String ILLEGAL_UPDATE = "XCL12";
private static final String NON_BOOLEAN_OPERAND = "42Y94";
///////////////////////////////////////////////////////////////////////////////////
//
// STATE
//
///////////////////////////////////////////////////////////////////////////////////
private boolean _supportsXML;
///////////////////////////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
///////////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance.
*/
public BooleanValuesTest(String name)
{
super(name);
_supportsXML = XML.classpathMeetsXMLReqs();
}
///////////////////////////////////////////////////////////////////////////////////
//
// JUnit BEHAVIOR
//
///////////////////////////////////////////////////////////////////////////////////
/**
* Construct top level suite in this JUnit test
*/
public static Test suite()
{
Test result = (BaseTestSuite)TestConfiguration.defaultSuite(
BooleanValuesTest.class);
return result;
}
protected void setUp()
throws Exception
{
super.setUp();
Connection conn = getConnection();
if ( !routineExists( conn, "MAKESIMPLEBLOB" ) )
{
goodStatement
(
conn,
"create function makeSimpleBlob( ) returns blob\n" +
"language java parameter style java no sql deterministic\n" +
"external name 'org.apache.derbyTesting.functionTests.tests.lang.BooleanValuesTest.makeSimpleBlob'\n"
);
}
if ( !tableExists( conn, "ALL_TYPES" ) )
{
StringBuffer buffer;
//
// create table
//
buffer = new StringBuffer();
buffer.append
(
"create table all_types\n" +
"(\n" +
" key_col int,\n" +
" bigint_col BIGINT,\n" +
" blob_col BLOB(2147483647),\n" +
" char_col CHAR(10),\n" +
" char_for_bit_data_col CHAR (10) FOR BIT DATA,\n" +
" clob_col CLOB(2147483647),\n" +
" date_col DATE,\n" +
" decimal_col DECIMAL(5,2),\n" +
" real_col REAL,\n" +
" double_col DOUBLE,\n" +
" int_col INTEGER,\n" +
" long_varchar_col LONG VARCHAR,\n" +
" long_varchar_for_bit_data_col LONG VARCHAR FOR BIT DATA,\n" +
" numeric_col NUMERIC(5,2), \n" +
" smallint_col SMALLINT,\n" +
" time_col TIME,\n" +
" timestamp_col TIMESTAMP,\n" +
" varchar_col VARCHAR(10),\n" +
" varchar_for_bit_data_col VARCHAR (10) FOR BIT DATA\n"
);
if ( _supportsXML )
{
buffer.append( " ,xml_col XML\n" );
}
buffer.append( ")\n" );
goodStatement( conn, buffer.toString() );
//
// populate table
//
buffer = new StringBuffer();
buffer.append
(
"insert into all_types\n" +
"(\n" +
" key_col,\n" +
" bigint_col,\n" +
" blob_col,\n" +
" char_col,\n" +
" char_for_bit_data_col,\n" +
" clob_col,\n" +
" date_col,\n" +
" decimal_col,\n" +
" real_col,\n" +
" double_col,\n" +
" int_col,\n" +
" long_varchar_col,\n" +
" long_varchar_for_bit_data_col,\n" +
" numeric_col, \n" +
" smallint_col,\n" +
" time_col,\n" +
" timestamp_col,\n" +
" varchar_col,\n" +
" varchar_for_bit_data_col\n"
);
if ( _supportsXML )
{
buffer.append( " ,xml_col\n" );
}
buffer.append
(
")\n" +
"values\n" +
"(\n" +
" 0,\n" +
" 0,\n" +
" makeSimpleBlob(),\n" +
" '0',\n" +
" X'DE',\n" +
" '0',\n" +
" date('1994-02-23'),\n" +
" 0.00,\n" +
" 0.0,\n" +
" 0.0,\n" +
" 0,\n" +
" '0',\n" +
" X'DE',\n" +
" 0.00, \n" +
" 0,\n" +
" time('15:09:02'),\n" +
" timestamp('1962-09-23 03:23:34.234'),\n" +
" '0',\n" +
" X'DE'\n"
);
if ( _supportsXML )
{
buffer.append( " , xmlparse( document '<?xml version=\"1.0\" encoding=\"UTF-8\"?> <html/>' preserve whitespace )\n" );
}
buffer.append
(
"),\n" +
"(\n" +
" 1,\n" +
" 1,\n" +
" makeSimpleBlob(),\n" +
" '1',\n" +
" X'DE',\n" +
" '1',\n" +
" date('1994-02-23'),\n" +
" 1.00,\n" +
" 1.0,\n" +
" 1.0,\n" +
" 1,\n" +
" '1',\n" +
" X'DE',\n" +
" 1.00, \n" +
" 1,\n" +
" time('15:09:02'),\n" +
" timestamp('1962-09-23 03:23:34.234'),\n" +
" '1',\n" +
" X'DE'\n"
);
if ( _supportsXML )
{
buffer.append( " , xmlparse( document '<?xml version=\"1.0\" encoding=\"UTF-8\"?> <html/>' preserve whitespace )\n" );
}
buffer.append
(
"),\n" +
"(\n" +
" 2,\n" +
" 2,\n" +
" makeSimpleBlob(),\n" +
" '2',\n" +
" X'DE',\n" +
" '2',\n" +
" date('1994-02-23'),\n" +
" 2.00,\n" +
" 2.0,\n" +
" 2.0,\n" +
" 2,\n" +
" '2',\n" +
" X'DE',\n" +
" 2.00, \n" +
" 2,\n" +
" time('15:09:02'),\n" +
" timestamp('1962-09-23 03:23:34.234'),\n" +
" '2',\n" +
" X'DE'\n"
);
if ( _supportsXML )
{
buffer.append( " , xmlparse( document '<?xml version=\"1.0\" encoding=\"UTF-8\"?> <html/>' preserve whitespace )\n" );
}
buffer.append
(
"),\n" +
"(\n" +
" 3,\n" +
" 3,\n" +
" makeSimpleBlob(),\n" +
" 'baffle',\n" +
" X'DE',\n" +
" 'baffle',\n" +
" date('1994-02-23'),\n" +
" 3.00,\n" +
" 3.0,\n" +
" 3.0,\n" +
" 3,\n" +
" 'baffle',\n" +
" X'DE',\n" +
" 3.00, \n" +
" 3,\n" +
" time('15:09:02'),\n" +
" timestamp('1962-09-23 03:23:34.234'),\n" +
" 'baffle',\n" +
" X'DE'\n"
);
if ( _supportsXML )
{
buffer.append( " , xmlparse( document '<?xml version=\"1.0\" encoding=\"UTF-8\"?> <html/>' preserve whitespace )\n" );
}
buffer.append
(
")\n"
);
goodStatement( conn, buffer.toString() );
}
if ( !tableExists( conn, "BOOLEANSTRING" ) )
{
//
// create table
//
goodStatement( conn, "create table booleanString( keyCol int, stringCol varchar( 20 ) )" );
//
// populate it
//
goodStatement
(
conn,
"insert into booleanString( keyCol, stringCol )\n" +
"values ( 0, 'false' ), ( 1, 'true' ), ( 2, null ), ( 3, 'unknown' ), ( 10, ' false ' ), ( 11, ' true ' ), ( 12, null ), ( 13, ' unknown ' )\n"
);
}
if ( !tableExists( conn, "T_4704" ) )
{
//
// create table
//
goodStatement( conn, "create table t_4704( keyCol int, stringCol varchar( 20 ) not null )" );
//
// populate it
//
goodStatement
(
conn,
"insert into t_4704( keyCol, stringCol )\n" +
"values ( 0, 'false' ), ( 1, 'true' ), ( 2, 'unknown' )\n"
);
}
if ( !tableExists( conn, "STRING_TYPES" ) )
{
//
// create table
//
goodStatement
(
conn,
"create table string_types\n" +
"(\n" +
" key_col int,\n" +
" char_col CHAR(10),\n" +
" clob_col CLOB(2147483647),\n" +
" long_varchar_col LONG VARCHAR,\n" +
" varchar_col VARCHAR(10)\n" +
")\n"
);
//
// populate it
//
goodStatement
(
conn,
"insert into string_types\n" +
"( key_col, char_col, clob_col, long_varchar_col, varchar_col )\n" +
"values\n" +
"( 0, 'false', 'false', 'false', 'false' ),\n" +
"( 1, 'true', 'true', 'true', 'true' ),\n" +
"( 2, 'unknown', 'unknown', 'unknown', 'unknown' ),\n" +
"( 3, null, null, null, null ),\n" +
"( 4, ' false ', ' false ', ' false ', ' false ' ),\n" +
"( 5, ' true ', ' true ', ' true ', ' true ' ),\n" +
"( 6, ' unknown ', ' unknown ', ' unknown ', ' unknown ' ),\n" +
"( 7, null, null, null, null ),\n" +
"( 10, 'FALSE', 'FALSE', 'FALSE', 'FALSE' ),\n" +
"( 11, 'TRUE', 'TRUE', 'TRUE', 'TRUE' ),\n" +
"( 12, 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN' ),\n" +
"( 13, NULL, NULL, NULL, NULL ),\n" +
"( 14, ' FALSE ', ' FALSE ', ' FALSE ', ' FALSE ' ),\n" +
"( 15, ' TRUE ', ' TRUE ', ' TRUE ', ' TRUE ' ),\n" +
"( 16, ' UNKNOWN ', ' UNKNOWN ', ' UNKNOWN ', ' UNKNOWN ' ),\n" +
"( 17, NULL, NULL, NULL, NULL ),\n" +
"( 20, 'arg', 'arg', 'arg', 'arg' ),\n" +
"( 21, '0', '0', '0', '0' ),\n" +
"( 22, '1', '1', '1', '1' ),\n" +
"( 23, '2', '2', '2', '2' )\n"
);
}
if ( !routineExists( conn, "BOOLEANVALUE" ) )
{
//
// create function
//
goodStatement
(
conn,
"create function booleanValue( b boolean )\n" +
"returns varchar( 100 ) language java parameter style java no sql\n" +
"external name 'org.apache.derbyTesting.functionTests.tests.lang.BooleanValuesTest.booleanValue'\n"
);
}
if ( !tableExists( conn, "BOOLEAN_TABLE" ) )
{
//
// create table
//
goodStatement
(
conn,
"create table boolean_table\n" +
"(\n" +
" key_col int,\n" +
" boolean_col boolean\n" +
")\n"
);
}
if ( !tableExists( conn, "T_4889" ) )
{
//
// create table
//
goodStatement
(
conn,
"create table t_4889\n" +
"(\n" +
" key_col int,\n" +
" setter_col varchar( 20 ),\n" +
" boolean_col boolean\n" +
")\n"
);
}
}
///////////////////////////////////////////////////////////////////////////////////
//
// TESTS
//
///////////////////////////////////////////////////////////////////////////////////
/**
* <p>
* Verify the number of datatypes supported by Derby. If this number changes,
* then you need to add a new datatype to this test:
* </p>
*
* <ul>
* <li>Add a new column to ALL_TYPES and corresponding rows (see setUp())</li>
* <li>Add the new datatype to one of the tests below</li>
* <li>Add a new bad union case to test_06_unions()</li>
* <li>Add a new bad explicit cast to test_09_explicitCasts()</li>
* </ul>
*/
public void test_01_datatypeCount() throws Exception
{
Connection conn = getConnection();
ResultSet rs = conn.getMetaData().getTypeInfo();
int actualTypeCount = 0;
while ( rs.next() ) { actualTypeCount++; }
rs.close();
assertEquals( 22, actualTypeCount );
}
/**
* <p>
* Test that ResultSet.getBoolean() behaves well for the datatypes for
* which the JDBC spec defines correct values: CHAR, VARCHAR, TINYINT,
* SMALLINT, INTEGER, BIGINT.
* </p>
*/
public void test_02_defined() throws Exception
{
Connection conn = getConnection();
vet_getBoolean( conn, "BIGINT_COL" );
vet_getBoolean( conn, "CHAR_COL" );
vet_getBoolean( conn, "INT_COL" );
vet_getBoolean( conn, "SMALLINT_COL" );
vet_getBoolean( conn, "VARCHAR_COL" );
}
/**
* <p>
* Verifies Derby's behavior is the same for embedded and client
* drivers on datatypes for which the JDBC spec does not define results
* but which Derby handles.
* </p>
*/
public void test_03_undefinedButLegal() throws Exception
{
Connection conn = getConnection();
vet_getBoolean( conn, "DECIMAL_COL" );
vet_getBoolean( conn, "REAL_COL" );
vet_getBoolean( conn, "DOUBLE_COL" );
vet_getBoolean( conn, "LONG_VARCHAR_COL" );
vet_getBoolean( conn, "NUMERIC_COL" );
}
/**
* <p>
* Verify Derby's behavior is the same for embedded and client
* drivers on datatypes for which the JDBC spec does not define results
* and which Derby does not handle.
* </p>
*/
public void test_04_undefinedAndIllegal() throws Exception
{
Connection conn = getConnection();
vet_getBooleanIsIllegal( conn, "BLOB_COL" );
vet_getBooleanIsIllegal( conn, "CHAR_FOR_BIT_DATA_COL" );
vet_getBooleanIsIllegal( conn, "CLOB_COL" );
vet_getBooleanIsIllegal( conn, "DATE_COL" );
vet_getBooleanIsIllegal( conn, "LONG_VARCHAR_FOR_BIT_DATA_COL" );
vet_getBooleanIsIllegal( conn, "TIME_COL" );
vet_getBooleanIsIllegal( conn, "TIMESTAMP_COL" );
vet_getBooleanIsIllegal( conn, "VARCHAR_FOR_BIT_DATA_COL" );
}
/**
* <p>
* Regression tests for outliers. If this behavior changes,
* hopefully the new behavior will fit into one of the categories above.
* </p>
*/
public void test_05_undefinedIllegalOutliers() throws Exception
{
//
// We don't test the XML datatype on JDK 1.4 because the Xalan
// packages are in the wrong location there.
//
if ( _supportsXML )
{
expectCompilationError
(
ILLEGAL_XML_SELECTION,
makeQuery( "XML_COL" )
);
}
}
/**
* <p>
* Test that ResultSet.getBoolean() returns the correct value. Expects to
* select a result set with the following rows:
* </p>
*
* <pre>
* 0 0
* 1 1
* 2 2
* 3 for string columns, something other than a number
* </pre>
*/
private void vet_getBoolean( Connection conn, String columnName ) throws Exception
{
PreparedStatement ps = chattyPrepare( conn, makeQuery( columnName ) );
ResultSet rs = ps.executeQuery();
// 0 => getBoolean() = false
rs.next();
assertFalse( rs.getBoolean( columnName ) );
// 1 => getBoolean() = true
rs.next();
assertTrue( rs.getBoolean( columnName ) );
// 2 => getBoolean() = true
//
// If the value is not 0 or 1, the JDBC spec leaves it up to the implementation.
// For Derby, the answer is ( value != 0 )
rs.next();
assertTrue( rs.getBoolean( columnName ) );
// non-number => getBoolean() = true
//
// If the value is not 0 or 1, the JDBC spec leaves it up to the implementation.
// For Derby, the answer is ( value != 0 )
rs.next();
assertTrue( rs.getBoolean( columnName ) );
rs.close();
ps.close();
}
private String makeQuery( String columnName )
{
return "select key_col, " + columnName + " from all_types order by key_col";
}
/**
* <p>
* Test that ResultSet.getBoolean() raises an error on datatypes which
* Derby doesn't handle.
* </p>
*/
private void vet_getBooleanIsIllegal( Connection conn, String columnName ) throws Exception
{
vet_getBooleanException( conn, columnName, ILLEGAL_GET );
}
/**
* <p>
* Test that ResultSet.getBoolean() raises the expected error.
* </p>
*/
private void vet_getBooleanException( Connection conn, String columnName, String sqlstate ) throws Exception
{
PreparedStatement ps = chattyPrepare( conn, makeQuery( columnName ) );
ResultSet rs = ps.executeQuery();
rs.next();
try {
rs.getBoolean( columnName );
fail( "getBoolean() on " + columnName + " should not have succeeded." );
}
catch (SQLException se)
{
assertSQLState( "getBoolean() on " + columnName, sqlstate, se );
}
}
/**
* <p>
* Test that unions fail if one but not both sides of the union is BOOLEAN. The rules for union
* compatibility are found in the SQL Standard, part 2, section 7.3 (<query expression>),
* syntax rule 20.b.ii. That in turn, refers you to section 9.3 (Result of data type combinations).
* See, for instance, <a href="https://issues.apache.org/jira/browse/DERBY-4692">DERBY-4692</a>.This enforces the rules found
* </p>
*/
public void test_06_unions() throws Exception
{
Connection conn = getConnection();
vetBadImplicitCasts( conn, "bigint_col" );
vetBadImplicitCasts( conn, "blob_col" );
vetBadImplicitCasts( conn, "char_col" );
vetBadImplicitCasts( conn, "char_for_bit_data_col" );
vetBadImplicitCasts( conn, "clob_col" );
vetBadImplicitCasts( conn, "date_col" );
vetBadImplicitCasts( conn, "decimal_col" );
vetBadImplicitCasts( conn, "real_col" );
vetBadImplicitCasts( conn, "double_col" );
vetBadImplicitCasts( conn, "int_col" );
vetBadImplicitCasts( conn, "long_varchar_col" );
vetBadImplicitCasts( conn, "long_varchar_for_bit_data_col" );
vetBadImplicitCasts( conn, "numeric_col" );
vetBadImplicitCasts( conn, "smallint_col" );
vetBadImplicitCasts( conn, "time_col" );
vetBadImplicitCasts( conn, "timestamp_col" );
vetBadImplicitCasts( conn, "varchar_col" );
vetBadImplicitCasts( conn, "varchar_for_bit_data_col" );
if ( _supportsXML ) { vetBadImplicitCasts( conn, "xml_col" ); }
}
private void vetBadImplicitCasts( Connection conn, String columnName ) throws Exception
{
vetBadImplicitCastToBoolean( conn, columnName );
vetBadImplicitCastFromBoolean( conn, columnName );
}
private void vetBadImplicitCastToBoolean( Connection conn, String columnName ) throws Exception
{
String query =
"select isindex from sys.sysconglomerates where conglomeratename = 'foo'\n" +
"union\n" +
"select " + columnName + " from all_types\n";
expectCompilationError( NOT_UNION_COMPATIBLE, query );
}
private void vetBadImplicitCastFromBoolean( Connection conn, String columnName ) throws Exception
{
String query =
"select " + columnName + " from all_types\n" +
"union\n" +
"select isindex from sys.sysconglomerates\n";
expectCompilationError( NOT_UNION_COMPATIBLE, query );
}
/**
* <p>
* Verify that boolean literals work.
* </p>
*/
public void test_07_booleanLiterals() throws Exception
{
Connection conn = getConnection();
assertResults
(
conn,
"values ( true )",
new String[][]
{
{ "true" },
},
false
);
assertResults
(
conn,
"values ( false )",
new String[][]
{
{ "false" },
},
false
);
}
/**
* <p>
* Verify that explicit casts to and from string work as expected.
* </p>
*/
public void test_08_stringCasts() throws Exception
{
Connection conn = getConnection();
assertResults( conn, "values ( cast( 'true' as boolean ) )", new String[][] { { "true" } }, false );
assertResults( conn, "values ( cast( 'false' as boolean ) )", new String[][] { { "false" } }, false );
assertResults( conn, "values ( cast( null as boolean ) )", new String[][] { { null } }, false );
assertResults( conn, "values ( cast( 'unknown' as boolean ) )", new String[][] { { null } }, false );
assertResults( conn, "values ( cast( ' true ' as boolean ) )", new String[][] { { "true" } }, false );
assertResults( conn, "values ( cast( ' false ' as boolean ) )", new String[][] { { "false" } }, false );
assertResults( conn, "values ( cast( ' unknown ' as boolean ) )", new String[][] { { null } }, false );
expectCompilationError( BAD_CAST, "values ( cast( 'arglebargle' as boolean ) )" );
expectCompilationError( BAD_CAST, "values ( cast( '1' as boolean ) )" );
expectCompilationError( BAD_CAST, "values ( cast( '0' as boolean ) )" );
expectCompilationError( BAD_CAST, "values ( cast( '2' as boolean ) )" );
assertResults
(
conn,
"select keyCol, cast( stringCol as boolean) from booleanString order by keyCol",
new String[][]
{
{ "0", "false" },
{ "1", "true" },
{ "2", null },
{ "3", null },
{ "10", "false" },
{ "11", "true" },
{ "12", null },
{ "13", null },
},
false
);
goodStatement( conn, "update booleanString set stringCol = 'arglebargle'" );
expectExecutionError( conn, BAD_CAST, "select keyCol, cast( stringCol as boolean) from booleanString order by keyCol" );
goodStatement( conn, "update booleanString set stringCol = '0'" );
expectExecutionError( conn, BAD_CAST, "select keyCol, cast( stringCol as boolean) from booleanString order by keyCol" );
goodStatement( conn, "update booleanString set stringCol = '1'" );
expectExecutionError( conn, BAD_CAST, "select keyCol, cast( stringCol as boolean) from booleanString order by keyCol" );
goodStatement( conn, "update booleanString set stringCol = '2'" );
expectExecutionError( conn, BAD_CAST, "select keyCol, cast( stringCol as boolean) from booleanString order by keyCol" );
}
/**
* <p>
* Verify that most explicit casts to and from boolean are not allowed.
* </p>
*/
public void test_09_explicitCasts() throws Exception
{
Connection conn = getConnection();
vetBadExplicitCasts( conn, "bigint_col", "BIGINT", "0" );
vetBadExplicitCasts( conn, "blob_col", "BLOB(2147483647)", "makeSimpleBlob()" );
// CHAR ok
vetBadExplicitCasts( conn, "char_for_bit_data_col", "CHAR (10) FOR BIT DATA", "X'DE'" );
// CLOB ok
vetBadExplicitCasts( conn, "date_col", "DATE", "date('1994-02-23')" );
vetBadExplicitCasts( conn, "decimal_col", "DECIMAL(5,2)", "0.00" );
vetBadExplicitCasts( conn, "real_col", "REAL", "0.0" );
vetBadExplicitCasts( conn, "double_col", "DOUBLE", "0.0" );
vetBadExplicitCasts( conn, "int_col", "INTEGER", "0" );
// LONG VARCHAR ok
vetBadExplicitCasts( conn, "long_varchar_for_bit_data_col", "LONG VARCHAR FOR BIT DATA", "X'DE'" );
vetBadExplicitCasts( conn, "numeric_col", "NUMERIC(5,2)", "0.00" );
vetBadExplicitCasts( conn, "smallint_col", "SMALLINT", "0" );
vetBadExplicitCasts( conn, "time_col", "TIME", "time('15:09:02')" );
vetBadExplicitCasts( conn, "timestamp_col", "TIMESTAMP", "timestamp('1962-09-23 03:23:34.234')" );
// VARCHAR ok
vetBadExplicitCasts( conn, "varchar_for_bit_data_col", "VARCHAR (10) FOR BIT DATA", "X'DE'" );
if ( _supportsXML ) { vetBadExplicitCasts( conn, "xml_col", "XML", "xmlparse( document '<?xml version=\"1.0\" encoding=\"UTF-8\"?> <html/>' preserve whitespace )" ); }
assertResults
(
conn,
"select key_col, cast( char_col as boolean ), cast( clob_col as boolean ), cast( long_varchar_col as boolean ), cast( varchar_col as boolean )\n" +
"from string_types where key_col < 18 order by key_col\n",
new String[][]
{
{ "0", "false", "false", "false", "false", },
{ "1", "true", "true", "true", "true", },
{ "2", null, null, null, null, },
{ "3", null, null, null, null, },
{ "4", "false", "false", "false", "false", },
{ "5", "true", "true", "true", "true", },
{ "6", null, null, null, null, },
{ "7", null, null, null, null, },
{ "10", "false", "false", "false", "false", },
{ "11", "true", "true", "true", "true", },
{ "12", null, null, null, null, },
{ "13", null, null, null, null, },
{ "14", "false", "false", "false", "false", },
{ "15", "true", "true", "true", "true", },
{ "16", null, null, null, null, },
{ "17", null, null, null, null, },
},
false
);
assertResults
(
conn,
"values ( cast( true as boolean) )\n",
new String[][]
{
{ "true" },
},
false
);
vetBadStringCast( conn, "char_col" );
vetBadStringCast( conn, "clob_col" );
vetBadStringCast( conn, "long_varchar_col" );
vetBadStringCast( conn, "varchar_col" );
vet4704();
}
private void vetBadStringCast( Connection conn, String columnName ) throws Exception
{
for ( int i = 20; i < 24; i++ )
{
expectExecutionError( conn, BAD_CAST, "select cast( " + columnName + " as boolean ) from string_types where key_col = " + i );
}
}
private void vetBadExplicitCasts( Connection conn, String columnName, String dataType, String literal ) throws Exception
{
vetBadExplicitCastToBoolean( conn, columnName, literal );
vetBadExplicitCastFromBoolean( conn, dataType );
}
private void vetBadExplicitCastToBoolean( Connection conn, String columnName, String literal ) throws Exception
{
expectCompilationError( BAD_CONVERSION, "values ( cast( " + literal + " as boolean ) )" );
expectCompilationError( BAD_CONVERSION, "select cast( " + columnName + " as boolean ) from all_types\n" );
}
private void vetBadExplicitCastFromBoolean( Connection conn, String dataType ) throws Exception
{
expectCompilationError( BAD_CONVERSION, "values ( cast( true as " + dataType + " ) )" );
expectCompilationError( BAD_CONVERSION, "select cast( isindex as " + dataType + " ) from sys.sysconglomerates" );
}
/**
* Regression test case for DERBY-4704. When casting non-nullable VARCHAR
* columns to BOOLEAN, the result column was marked as non-nullable, even
* though the VARCHAR could contain the value 'UNKNOWN', in which case
* the cast should return NULL.
*/
public void vet4704()
throws SQLException
{
Statement s = createStatement();
ResultSet rs = s.executeQuery( "select keyCol, stringCol from t_4704 order by keyCol" );
JDBC.assertNullability(rs, new boolean[] { true, false });
rs = s.executeQuery( "select keyCol, cast(stringCol as boolean) from t_4704 order by keyCol" );
JDBC.assertNullability(rs, new boolean[] { true, true });
JDBC.assertFullResultSet
(
rs,
new String[][]
{
{ "0", "false" },
{ "1", "true" },
{ "2", null },
}
);
}
public void test_10_nullabilityOfCastFromLiteral() throws SQLException {
Statement s = createStatement();
ResultSet rs = s.executeQuery("values cast('true' as boolean)");
JDBC.assertNullability(rs, new boolean[] { false });
JDBC.assertSingleValueResultSet(rs, "true");
rs = s.executeQuery("values cast('false' as boolean)");
JDBC.assertNullability(rs, new boolean[] { false });
JDBC.assertSingleValueResultSet(rs, "false");
rs = s.executeQuery("values cast('unknown' as boolean)");
JDBC.assertNullability(rs, new boolean[] { true });
JDBC.assertSingleValueResultSet(rs, null);
rs = s.executeQuery(
"values (cast('true' as boolean),\n" +
" cast('false' as boolean),\n" +
" cast('unknown' as boolean))");
JDBC.assertNullability(rs, new boolean[] { false, false, true });
JDBC.assertFullResultSet(
rs, new String[][] {{ "true", "false", null }});
rs = s.executeQuery(
"values (cast ('true' as boolean)),\n" +
" (cast ('false' as boolean)),\n" +
" (cast ('unknown' as boolean))");
JDBC.assertNullability(rs, new boolean[] { true });
JDBC.assertFullResultSet(
rs, new String[][] { {"true"}, {"false"}, {null} });
}
/**
* <p>
* Verify that boolean function parameters behave well.
* </p>
*/
public void test_11_booleanFunctionParameters() throws Exception
{
Connection conn = getConnection();
assertResults
(
conn,
"values ( booleanValue( true ) ), ( booleanValue( false ) ), ( booleanValue( null ) )",
new String[][]
{
{ "True value", },
{ "False value", },
{ "Null value", },
},
false
);
PreparedStatement ps = chattyPrepare( conn, "values ( booleanValue( ? ) )" );
ps.setBoolean( 1, true );
assertScalarResult( ps, "True value" );
ps.setBoolean( 1, false );
assertScalarResult( ps, "False value" );
ps.setNull( 1, Types.BOOLEAN );
assertScalarResult( ps, "Null value" );
ps.close();
}
private void assertScalarResult( PreparedStatement ps, String result ) throws Exception
{
ResultSet rs = ps.executeQuery();
assertResults( rs, new String[][] {{ result } }, false );
rs.close();
}
/**
* <p>
* Verify that tables with boolean columns behave well.
* </p>
*/
public void test_12_booleanColumns() throws Exception
{
Connection conn = getConnection();
goodStatement( conn, "insert into boolean_table( key_col, boolean_col ) values ( 0, true ), ( 1, false ), ( 2, null )" );
assertResults
(
conn,
"select * from boolean_table order by boolean_col",
new String[][]
{
{ "1", "false" },
{ "0", "true" },
{ "2", null },
},
false
);
goodStatement( conn, "delete from boolean_table" );
vetBadInsert( "bigint_col" );
vetBadInsert( "blob_col" );
vetBadInsert( "char_for_bit_data_col" );
vetBadInsert( "date_col" );
vetBadInsert( "decimal_col" );
vetBadInsert( "real_col" );
vetBadInsert( "double_col" );
vetBadInsert( "int_col" );
vetBadInsert( "long_varchar_for_bit_data_col" );
vetBadInsert( "numeric_col" );
vetBadInsert( "smallint_col" );
vetBadInsert( "time_col" );
vetBadInsert( "timestamp_col" );
vetBadInsert( "varchar_for_bit_data_col" );
vetStringInsert( conn, "char_col" );
vetStringInsert( conn, "clob_col" );
vetStringInsert( conn, "long_varchar_col" );
vetStringInsert( conn, "varchar_col" );
}
private void vetBadInsert( String columnName ) throws Exception
{
expectCompilationError
(
ILLEGAL_INSERT,
"insert into boolean_table( key_col, boolean_col ) select key_col, " + columnName + " from all_types"
);
}
private void vetStringInsert( Connection conn, String columnName ) throws Exception
{
goodStatement( conn, "insert into boolean_table( key_col, boolean_col ) select key_col, " + columnName + " from string_types where key_col < 18" );
assertResults
(
conn,
"select * from boolean_table order by key_col",
new String[][]
{
{ "0", "false" },
{ "1", "true" },
{ "2", null },
{ "3", null },
{ "4", "false" },
{ "5", "true" },
{ "6", null },
{ "7", null },
{ "10", "false" },
{ "11", "true" },
{ "12", null },
{ "13", null },
{ "14", "false" },
{ "15", "true" },
{ "16", null },
{ "17", null },
},
false
);
for ( int i = 20; i < 24; i++ )
{
expectExecutionError
( conn, BAD_CAST, "insert into boolean_table( key_col, boolean_col ) select key_col, " + columnName + " from string_types where key_col = " + i );
}
goodStatement( conn, "delete from boolean_table" );
}
/**
* <p>
* Verify that boolean nulls sort at the end with or without an index,
* the behavior of other datatypes.
* </p>
*/
public void test_13_sortOrder() throws Exception
{
Connection conn = getConnection();
goodStatement( conn, "create table booleanUnindexed( a boolean )" );
goodStatement( conn, "create table booleanIndexed( a boolean )" );
goodStatement( conn, "create index bi on booleanIndexed( a )" );
goodStatement( conn, "insert into booleanUnindexed( a ) values ( true ), ( null ), ( false )" );
goodStatement( conn, "insert into booleanIndexed( a ) values ( true ), ( null ), ( false )" );
String[][] expectedResults = new String[][]
{
{ "false" },
{ "true" },
{ null },
};
assertResults
(
conn,
"select * from booleanUnindexed order by a",
expectedResults,
false
);
assertResults
(
conn,
"select * from booleanIndexed order by a",
expectedResults,
false
);
goodStatement( conn, "drop table booleanUnindexed" );
goodStatement( conn, "drop table booleanIndexed" );
}
/**
* <p>
* Verify that you can declare literal defaults for boolean columns.
* </p>
*/
public void test_14_defaults() throws Exception
{
Connection conn = getConnection();
goodStatement( conn, "create table booleanDefaults( a int, b boolean default true, c boolean default false, d boolean default null )" );
goodStatement( conn, "insert into booleanDefaults( a ) values ( 0 )" );
assertResults
(
conn,
"select * from booleanDefaults order by a",
new String[][]
{
{ "0", "true", "false", null }
},
false
);
goodStatement( conn, "alter table booleanDefaults add column e boolean default true" );
assertResults
(
conn,
"select * from booleanDefaults order by a",
new String[][]
{
{ "0", "true", "false", null, "true" }
},
false
);
goodStatement( conn, "drop table booleanDefaults" );
expectCompilationError( BAD_DEFAULT, "create table badDefault( a int, b boolean default 0 )" );
expectCompilationError( BAD_DEFAULT, "create table badDefault( a int, b boolean default 9.99 )" );
expectCompilationError( BAD_DEFAULT, "create table badDefault( a int, b boolean default 5e1 )" );
expectCompilationError( BAD_DEFAULT, "create table badDefault( a int, b boolean default 'false' )" );
expectCompilationError( BAD_DEFAULT, "create table badDefault( a int, b boolean default X'DE' )" );
}
/**
* <p>
* Verify that you can have boolean expressions in the SELECT list.
* </p>
*/
public void test_15_selectList() throws Exception
{
Connection conn = getConnection();
assertResults
(
conn,
"select columnnumber > 0 from sys.syscolumns where columnname = 'TABLENAME'",
new String[][]
{
{ "true" }
},
false
);
assertResults
(
conn,
"values 3 > 10",
new String[][]
{
{ "false" }
},
false
);
}
/**
* <p>
* Verify that you can alter a table and add boolean columns.
* </p>
*/
public void test_16_alterTable() throws Exception
{
Connection conn = getConnection();
goodStatement( conn, "create table booleanAlter( a int )" );
goodStatement( conn, "insert into booleanAlter( a ) values ( 0 ), ( 1 ), ( 2 )" );
goodStatement( conn, "alter table booleanAlter add column b boolean" );
goodStatement( conn, "alter table booleanAlter add column c boolean default true" );
goodStatement( conn, "alter table booleanAlter add column d boolean default false" );
goodStatement( conn, "alter table booleanAlter add column e boolean default null" );
assertResults
(
conn,
"select * from booleanAlter order by a",
new String[][]
{
{ "0", null, "true", "false", null },
{ "1", null, "true", "false", null },
{ "2", null, "true", "false", null },
},
false
);
goodStatement( conn, "alter table booleanAlter drop column b" );
assertResults
(
conn,
"select * from booleanAlter order by a",
new String[][]
{
{ "0", "true", "false", null },
{ "1", "true", "false", null },
{ "2", "true", "false", null },
},
false
);
goodStatement( conn, "drop table booleanAlter" );
}
/**
* <p>
* Verify that you can create boolean-valued generated columns.
* </p>
*/
public void test_17_generatedColumns() throws Exception
{
Connection conn = getConnection();
goodStatement
(
conn,
"create table tree\n" +
"(\n" +
" treeID int primary key generated always as identity,\n" +
" latinName varchar( 100 ),\n" +
" commonName varchar( 100 ),\n" +
" waterPerWeek int,\n" +
" minimumSunNeeded int,\n" +
" maximumSunTolerated int,\n" +
" windTolerated int,\n" +
" isHardy boolean generated always as( (waterPerWeek < 10) and (minimumSunNeeded < 5) and (maximumSunTolerated > 90) and (windTolerated > 100) )\n" +
")\n"
);
generatedMinion( conn );
// repeat the experiment with an implicitly typed generated column
goodStatement
(
conn,
"create table tree\n" +
"(\n" +
" treeID int primary key generated always as identity,\n" +
" latinName varchar( 100 ),\n" +
" commonName varchar( 100 ),\n" +
" waterPerWeek int,\n" +
" minimumSunNeeded int,\n" +
" maximumSunTolerated int,\n" +
" windTolerated int,\n" +
" isHardy generated always as( (waterPerWeek < 10) and (minimumSunNeeded < 5) and (maximumSunTolerated > 90) and (windTolerated > 100) )\n" +
")\n"
);
generatedMinion( conn );
}
private void generatedMinion( Connection conn ) throws Exception
{
goodStatement( conn, "create index hardyTrees on tree( treeID, isHardy )" );
goodStatement
(
conn,
"insert into tree( latinName, commonName, waterPerWeek, minimumSunNeeded, maximumSunTolerated, windTolerated )\n" +
"values\n" +
"( 'tristania conferta', 'brisbane boxwood', 7, 4, 95, 120 ),\n" +
"( 'acer rubrum', 'red maple', 20, 20, 95, 120 )\n"
);
assertResults
(
conn,
"select * from tree order by treeID",
new String[][]
{
{ "1", "tristania conferta", "brisbane boxwood", "7", "4", "95", "120", "true" },
{ "2", "acer rubrum", "red maple", "20", "20", "95", "120", "false" },
},
false
);
assertResults
(
conn,
"select * from tree where isHardy order by treeID",
new String[][]
{
{ "1", "tristania conferta", "brisbane boxwood", "7", "4", "95", "120", "true" },
},
false
);
goodStatement( conn, "drop table tree" );
}
/**
* <p>
* Verify views with boolean columns.
* </p>
*/
public void test_18_views() throws Exception
{
Connection conn = getConnection();
goodStatement
(
conn,
"create table item\n" +
"(\n" +
" itemID int primary key generated always as identity,\n" +
" itemName varchar( 100 ),\n" +
" quantityOnHand int,\n" +
" quantityOrdered int\n" +
")\n"
);
goodStatement
(
conn,
"create view orderView( itemID, needsRestocking )\n" +
" as select itemID, quantityOrdered > quantityOnHand from item\n"
);
goodStatement
(
conn,
"insert into item( itemName, quantityOnHand, quantityOrdered )\n" +
"values\n" +
"( 'Bracelet', 100, 200 ),\n" +
"( 'Glasses', 200, 100 )\n"
);
assertResults
(
conn,
"select * from orderView order by itemID",
new String[][]
{
{ "1", "true" },
{ "2", "false" },
},
false
);
goodStatement( conn, "drop view orderView" );
goodStatement( conn, "drop table item" );
}
/**
* <p>
* Verify booleans in foreign keys.
* </p>
*/
public void test_19_foreignKeys() throws Exception
{
Connection conn = getConnection();
goodStatement
(
conn,
"create table keys\n" +
"(\n" +
" userName varchar( 100 ),\n" +
" publicVersion boolean,\n" +
" keyValue varchar( 1000 ),\n" +
" primary key( publicVersion, userName )\n" +
")\n"
);
goodStatement
(
conn,
"insert into keys( userName, publicVersion, keyValue )\n" +
"values\n" +
"( 'Robert', true, 'abcdefghij' ),\n" +
"( 'Robert', false, 'mnopqrstuv' )\n"
);
goodStatement
(
conn,
"create table messages\n" +
"(\n" +
" messageID int primary key generated always as identity,\n" +
" userName varchar( 100 ),\n" +
" publicVersion boolean,\n" +
" messageText clob,\n" +
" foreign key ( publicVersion, userName ) references keys( publicVersion, userName )\n" +
")\n"
);
goodStatement
(
conn,
"insert into messages( userName, publicVersion, messageText )\n" +
"values\n" +
"( 'Robert', false, 'Twas brillig' )\n"
);
assertResults
(
conn,
"select * from messages order by messageID",
new String[][]
{
{ "1", "Robert", "false", "Twas brillig" },
},
false
);
goodStatement( conn, "drop table messages" );
goodStatement( conn, "drop table keys" );
}
/**
* <p>
* Verify booleans in check constraints.
* </p>
*/
public void test_20_checkConstraints() throws Exception
{
Connection conn = getConnection();
goodStatement
(
conn,
"create table remes\n" +
"(\n" +
" remesID int primary key generated always as identity,\n" +
" name varchar( 100 ),\n" +
" isInsect boolean,\n" +
" isArachnid boolean,\n" +
" check ( isInsect or isArachnid )\n" +
")\n"
);
goodStatement
(
conn,
"insert into remes( name, isInsect, isArachnid )\n" +
"values\n" +
"( 'black widow', false, true ),\n" +
"( 'house fly', true, false )\n"
);
assertResults
(
conn,
"select * from remes order by remesID",
new String[][]
{
{ "1", "black widow", "false", "true" },
{ "2", "house fly", "true", "false" },
},
false
);
goodStatement( conn, "drop table remes" );
}
/**
* <p>
* Verify triggers on boolean columns.
* </p>
*/
public void test_21_triggers() throws Exception
{
Connection conn = getConnection();
goodStatement
(
conn,
"create table alumnus\n" +
"(\n" +
" alumnusID int primary key generated always as identity,\n" +
" name varchar( 100 ),\n" +
" living boolean\n" +
")\n"
);
goodStatement
(
conn,
"create table livenessChange\n" +
"(\n" +
" eventID int primary key generated always as identity,\n" +
" alumnusID int, \n" +
" living boolean\n" +
")\n"
);
goodStatement
(
conn,
"create trigger trig_after_update_row_trigger\n" +
"after update of living\n" +
"on alumnus\n" +
"referencing new as newRow\n" +
"for each row\n" +
"insert into livenessChange( alumnusID, living ) values ( newRow.alumnusID, newRow.living )\n"
);
goodStatement( conn, "insert into alumnus( name, living ) values ( 'Fred', true ), ( 'Monica', true )" );
goodStatement( conn, "update alumnus set living = false where name = 'Fred'" );
assertResults
(
conn,
"select * from livenessChange order by eventID",
new String[][]
{
{ "1", "1", "false" },
},
false
);
goodStatement( conn, "drop table alumnus" );
goodStatement( conn, "drop table livenessChange" );
}
/**
* Test that the binary operators return the expected values for all
* possible combinations of operands.
*/
public void test_22_binary_operators() throws SQLException {
setAutoCommit(false);
// Create a table with two columns containing all combinations of
// legal boolean values.
Statement s = createStatement();
s.execute("create table combos(b1 boolean, b2 boolean)");
assertUpdateCount(s, 9,
"insert into combos select * from "
+ "(values true, false, cast(null as boolean)) v1, "
+ "(values true, false, cast(null as boolean)) v2");
// Equals operator (=)
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 = b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "true" },
{ "false", "true" , "false" },
{ "false", null , null },
{ "true" , "false", "false" },
{ "true" , "true" , "true" },
{ "true" , null , null },
{ null , "false", null },
{ null , "true" , null },
{ null , null , null },
});
// Not equals operator (<>)
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 <> b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "false" },
{ "false", "true" , "true" },
{ "false", null , null },
{ "true" , "false", "true" },
{ "true" , "true" , "false" },
{ "true" , null , null },
{ null , "false", null },
{ null , "true" , null },
{ null , null , null },
});
// Less than operator (<)
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 < b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "false" },
{ "false", "true" , "true" },
{ "false", null , null },
{ "true" , "false", "false" },
{ "true" , "true" , "false" },
{ "true" , null , null },
{ null , "false", null },
{ null , "true" , null },
{ null , null , null },
});
// Greater than operator (>)
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 > b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "false" },
{ "false", "true" , "false" },
{ "false", null , null },
{ "true" , "false", "true" },
{ "true" , "true" , "false" },
{ "true" , null , null },
{ null , "false", null },
{ null , "true" , null },
{ null , null , null },
});
// Less than or equals operator (<=)
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 <= b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "true" },
{ "false", "true" , "true" },
{ "false", null , null },
{ "true" , "false", "false" },
{ "true" , "true" , "true" },
{ "true" , null , null },
{ null , "false", null },
{ null , "true" , null },
{ null , null , null },
});
// Greater than or equals operator (>=)
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 >= b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "true" },
{ "false", "true" , "false" },
{ "false", null , null },
{ "true" , "false", "true" },
{ "true" , "true" , "true" },
{ "true" , null , null },
{ null , "false", null },
{ null , "true" , null },
{ null , null , null },
});
// AND operator
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, b1 and b2 from combos order by 1, 2"),
new String[][] {
{ "false", "false", "false" },
{ "false", "true" , "false" },
{ "false", null , "false" },
{ "true" , "false", "false" },
{ "true" , "true" , "true" },
{ "true" , null , null },
{ null , "false", "false" },
{ null , "true" , null },
{ null , null , null },
});
// OR operator
JDBC.assertFullResultSet(
s.executeQuery(
"select b1, b2, (b1 or b2) from combos order by 1, 2"),
new String[][] {
{ "false", "false", "false" },
{ "false", "true" , "true" },
{ "false", null , null },
{ "true" , "false", "true" },
{ "true" , "true" , "true" },
{ "true" , null , "true" },
{ null , "false", null },
{ null , "true" , "true" },
{ null , null , null },
});
}
/**
* Verify that SELECT DISTINCT works as expected on boolean columns.
*/
public void test_23_select_distinct() throws SQLException {
setAutoCommit(false);
Statement s = createStatement();
assertUpdateCount(s, 6, "insert into boolean_table(boolean_col) "
+ "values true, true, false, false, null, null");
JDBC.assertUnorderedResultSet(
s.executeQuery("select distinct boolean_col from boolean_table"),
new String[][] { {"true"}, {"false"}, {null} });
}
/**
* <p>
* Verify fix for DERBY-4890.
* </p>
*/
public void test_4890() throws Exception
{
Connection conn = getConnection();
goodStatement( conn, "delete from boolean_table" );
PreparedStatement ps = chattyPrepare( conn, "values ( cast ( ? as boolean ), cast ( ? as boolean ) )" );
ps.setString( 1, "true" );
ps.setString( 2, "false" );
ResultSet rs = ps.executeQuery();
rs.next();
assertTrue( rs.getBoolean( 1 ) );
assertFalse( rs.getBoolean( 2 ) );
rs.close();
ps.close();
ps = chattyPrepare
(
conn,
"insert into boolean_table( key_col, boolean_col ) values ( 1, ? ), ( 2, ? )"
);
ps.setString( 1, "true" );
ps.setString( 2, "false" );
ps.execute();
assertResults
(
conn,
"select * from boolean_table order by key_col",
new String[][]
{
{ "1", "true" },
{ "2", "false" },
},
false
);
goodStatement( conn, "delete from boolean_table" );
}
/**
* <p>
* Verify fix for DERBY-4890.
* </p>
*/
public void test_4889() throws Exception
{
Connection conn = getConnection();
minion_4889( conn, 0, false );
minion_4889( conn, 1, true );
minion_4889( conn, 2, true );
}
private void minion_4889( Connection conn, int value, boolean expectedBooleanResult )
throws Exception
{
goodStatement( conn, "delete from t_4889" );
PreparedStatement ps = chattyPrepare
(
conn,
"insert into t_4889( key_col, setter_col, boolean_col ) values ( ?, ?, ? )"
);
ps.setInt( 1, 1 );
ps.setString( 2, "setByte" );
ps.setByte( 3, (byte) value );
ps.execute();
ps.setInt( 1, 2 );
ps.setString( 2, "setShort" );
ps.setShort( 3, (short) value );
ps.execute();
ps.setInt( 1, 3 );
ps.setString( 2, "setInt" );
ps.setInt( 3, value );
ps.execute();
ps.setInt( 1, 4 );
ps.setString( 2, "setLong" );
ps.setLong( 3, (long) value );
ps.execute();
ps.setInt( 1, 5 );
ps.setString( 2, "setObject( Byte )" );
ps.setObject( 3, (byte) value );
ps.execute();
ps.setInt( 1, 6 );
ps.setString( 2, "setObject( Short )" );
ps.setObject( 3, (short) value );
ps.execute();
ps.setInt( 1, 7 );
ps.setString( 2, "setObject( Integer )" );
ps.setObject( 3, value );
ps.execute();
ps.setInt( 1, 8 );
ps.setString( 2, "setObject( Long )" );
ps.setObject( 3, (long) value );
ps.execute();
String stringValue = Boolean.toString( (value != 0) );
assertResults
(
conn,
"select * from t_4889 order by key_col",
new String[][]
{
{ "1", "setByte", stringValue },
{ "2", "setShort", stringValue },
{ "3", "setInt", stringValue },
{ "4", "setLong", stringValue },
{ "5", "setObject( Byte )", stringValue },
{ "6", "setObject( Short )", stringValue },
{ "7", "setObject( Integer )", stringValue },
{ "8", "setObject( Long )", stringValue },
},
false
);
}
/**
* Verify fix for DERBY-4964 - failure to convert string to boolean with
* the client driver.
*/
public void test_4964() throws SQLException {
PreparedStatement ps = prepareStatement("values cast(? as boolean)");
// These should work
test_4964_minion(ps, null, null);
test_4964_minion(ps, "unknown", null);
test_4964_minion(ps, "UNKNOWN", null);
test_4964_minion(ps, "unKnoWn", null);
test_4964_minion(ps, " unknown ", null);
test_4964_minion(ps, "true", Boolean.TRUE);
test_4964_minion(ps, "TRUE", Boolean.TRUE);
test_4964_minion(ps, "TrUe", Boolean.TRUE);
test_4964_minion(ps, " true ", Boolean.TRUE);
test_4964_minion(ps, "false", Boolean.FALSE);
test_4964_minion(ps, "FALSE", Boolean.FALSE);
test_4964_minion(ps, "FaLsE", Boolean.FALSE);
test_4964_minion(ps, "FaLsE", Boolean.FALSE);
test_4964_minion(ps, " false ", Boolean.FALSE);
// These should fail
test_4964_minion(ps, "0", BAD_CAST);
test_4964_minion(ps, "1", BAD_CAST);
test_4964_minion(ps, "2", BAD_CAST);
test_4964_minion(ps, "null", BAD_CAST);
test_4964_minion(ps, "true true", BAD_CAST);
test_4964_minion(ps, "false false", BAD_CAST);
}
/**
* Set a boolean parameter using a string value and verify that we get
* the expected result.
*
* @param ps a prepared statement that takes a boolean parameter
* @param input input string for the parameter
* @param expectedValue the expected result; either the expected Boolean return
* value if the operation is expected to succeed, or the SQLState of the
* exception if it is expected to fail
*/
private void test_4964_minion(
PreparedStatement ps, String input, Object expectedValue)
throws SQLException
{
// If the expected value is a string, it denotes the SQLState of an
// expected failure
boolean shouldFail = expectedValue instanceof String;
Object[][] rows = { { expectedValue } };
// test setString(int, String)
try {
ps.setString(1, input);
JDBC.assertFullResultSet(ps.executeQuery(), rows, false);
assertFalse(shouldFail);
} catch (SQLException sqle) {
if (shouldFail) {
assertSQLState((String) expectedValue, sqle);
} else {
throw sqle;
}
}
// test setObject(int, Object)
try {
ps.setObject(1, input);
JDBC.assertFullResultSet(ps.executeQuery(), rows, false);
assertFalse(shouldFail);
} catch (SQLException sqle) {
if (shouldFail) {
assertSQLState((String) expectedValue, sqle);
} else {
throw sqle;
}
}
// test setObject(int, Object, int) with various target types
int[] types = { Types.BIT, Types.BOOLEAN, Types.CHAR, Types.VARCHAR };
for (int i = 0; i < types.length; i++) {
try {
ps.setObject(1, input, types[i]);
JDBC.assertFullResultSet(ps.executeQuery(), rows, false);
assertFalse(shouldFail);
} catch (SQLException sqle) {
if (shouldFail) {
assertSQLState((String) expectedValue, sqle);
} else {
throw sqle;
}
}
}
}
/**
* Verify fix for DERBY-4965 - conversion from boolean to char results
* in 1/0 instead of true/false.
*/
public void test_4965() throws SQLException {
String[] stringTypes = { "CHAR(10)", "VARCHAR(10)", "LONG VARCHAR" };
for (int i = 0; i < stringTypes.length; i++) {
PreparedStatement ps = prepareStatement(
"values cast(? as " + stringTypes[i] + ")");
// Test setBoolean()
ps.setBoolean(1, true);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "true");
ps.setBoolean(1, false);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "false");
// Test setObject(int, Object)
ps.setObject(1, Boolean.TRUE);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "true");
ps.setObject(1, Boolean.FALSE);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "false");
// Test setObject(int, Object, int)
int[] targetTypes = {
Types.BIT, Types.BOOLEAN,
Types.CHAR, Types.VARCHAR, Types.LONGVARCHAR,
};
for (int j = 0; j < targetTypes.length; j++) {
ps.setObject(1, Boolean.TRUE, targetTypes[j]);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "true");
ps.setObject(1, Boolean.FALSE, targetTypes[j]);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "false");
}
}
String[] intTypes = { "SMALLINT", "INTEGER", "BIGINT" };
for (int i = 0; i < intTypes.length; i++) {
PreparedStatement ps = prepareStatement(
"values cast(? as " + intTypes[i] + ")");
// Test setBoolean()
ps.setBoolean(1, true);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "1");
ps.setBoolean(1, false);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "0");
// Test setObject(int, Object)
ps.setObject(1, Boolean.TRUE);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "1");
ps.setObject(1, Boolean.FALSE);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "0");
// Test setObject(int, Object, int)
int[] targetTypes = {
Types.BIT, Types.BOOLEAN,
Types.SMALLINT, Types.INTEGER, Types.BIGINT,
};
for (int j = 0; j < targetTypes.length; j++) {
ps.setObject(1, Boolean.TRUE, targetTypes[j]);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "1");
ps.setObject(1, Boolean.FALSE, targetTypes[j]);
JDBC.assertSingleValueResultSet(ps.executeQuery(), "0");
}
}
}
/**
* Verify fix for DERBY-5042, where updateBoolean() and updateObject()
* would fail on a BOOLEAN column when using the client driver.
*/
public void test_5042_updateBoolean() throws SQLException {
setAutoCommit(false);
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
s.execute("create table derby5042(b boolean, i int, c char(10))");
ResultSet rs = s.executeQuery("select * from derby5042");
// Test updateBoolean() on various column types
rs.moveToInsertRow();
rs.updateBoolean("B", true); // Used to fail with client driver
rs.updateBoolean("I", true);
rs.updateBoolean("C", true);
rs.insertRow();
// Test updateObject() with a java.lang.Boolean on various column types
rs.moveToInsertRow();
rs.updateObject("B", Boolean.FALSE); // Used to fail with client driver
rs.updateObject("I", Boolean.FALSE);
rs.updateObject("C", Boolean.FALSE);
rs.insertRow();
rs.close();
JDBC.assertFullResultSet(
s.executeQuery("select * from derby5042 order by 1,2,3"),
new String[][]{
{"false", "0", "false"},
{"true", "1", "true"}});
}
/**
* Verify fix for DERBY-5063 - updateBytes() should fail when invoked
* on boolean columns.
*/
public void test_5063_updateBytes() throws SQLException {
setAutoCommit(false);
Statement s = createStatement();
s.execute("create table derby5063(b boolean)");
PreparedStatement ps = prepareStatement("select b from derby5063",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
byte[] bytes = "abc".getBytes();
// Test updateBytes()
ResultSet rs = ps.executeQuery();
rs.moveToInsertRow();
try {
rs.updateBytes(1, bytes);
fail("updateBytes should fail");
} catch (SQLException sqle) {
assertSQLState(ILLEGAL_UPDATE, sqle);
}
rs.close();
// setObject() should also fail when the argument is a byte array
rs = ps.executeQuery();
rs.moveToInsertRow();
try {
rs.updateObject(1, bytes);
fail("updateObject should fail");
} catch (SQLException sqle) {
assertSQLState(ILLEGAL_UPDATE, sqle);
}
rs.close();
}
/**
* Verify that you can use CREATE TABLE AS SELECT to create
* empty tables with BOOLEAN columns.
*/
public void test_5918() throws Exception
{
setAutoCommit(false);
Statement s = createStatement();
s.execute("create table derby5918_1(b boolean)");
s.execute("create table derby5918_2 as select * from derby5918_1 with no data");
s.executeUpdate("insert into derby5918_2 values ( true )");
assertResults
(
getConnection(),
"select * from derby5918_2",
new String[][]
{
{ "true" },
},
false
);
}
/**
* Verify that you can use AND and OR expressions as BOOLEAN values.
* Regression test case for DERBY-5972.
*/
public void test_5972() throws SQLException {
// Disable auto-commit so that tearDown() can roll back any changes.
setAutoCommit(false);
Statement s = createStatement();
// Test boolean expressions in select list. Used to fail with syntax
// errors with OR expressions.
JDBC.assertSingleValueResultSet(
s.executeQuery("select true and false from sysibm.sysdummy1"),
"false");
JDBC.assertSingleValueResultSet(
s.executeQuery("select true or false from sysibm.sysdummy1"),
"true");
assertCompileError(NON_BOOLEAN_OPERAND,
"select 1 and 2 from sysibm.sysdummy1");
assertCompileError(NON_BOOLEAN_OPERAND,
"select 1 or 2 from sysibm.sysdummy1");
// Test boolean expressions in VALUES statements. Used to fail with
// syntax errors with OR expressions.
JDBC.assertSingleValueResultSet(
s.executeQuery("values true and false"), "false");
JDBC.assertSingleValueResultSet(
s.executeQuery("values true or false"), "true");
assertCompileError(NON_BOOLEAN_OPERAND, "values 1 and 2");
assertCompileError(NON_BOOLEAN_OPERAND, "values 1 or 2");
// Test boolean expressions as parameters in function calls. Used to
// result in syntax errors with OR expressions.
JDBC.assertSingleValueResultSet(s.executeQuery(
"values booleanValue(true and false)"), "False value");
JDBC.assertSingleValueResultSet(s.executeQuery(
"values booleanValue(true or false)"), "True value");
assertCompileError(NON_BOOLEAN_OPERAND, "values booleanValue(1 and 2)");
assertCompileError(NON_BOOLEAN_OPERAND, "values booleanValue(1 or 2)");
// Test boolean expressions in UPDATE statements. Used to fail with
// syntax errors with OR expressions.
s.execute("create table d5972(b boolean)");
assertUpdateCount(s, 0, "update d5972 set b = true and false");
assertUpdateCount(s, 0, "update d5972 set b = true or false");
assertCompileError(NON_BOOLEAN_OPERAND, "update d5972 set b = 1 and 2");
assertCompileError(NON_BOOLEAN_OPERAND, "update d5972 set b = 1 or 2");
// Used to work correctly. Verify for completeness.
JDBC.assertSingleValueResultSet(s.executeQuery(
"values case when true and false then 1 else 0 end"), "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"values case when true or false then 1 else 0 end"), "1");
assertCompileError(NON_BOOLEAN_OPERAND,
"values case when 1 and 2 then 1 else 0 end");
assertCompileError(NON_BOOLEAN_OPERAND,
"values case when 1 or 2 then 1 else 0 end");
}
/**
* Some BOOLEAN expressions used to be transformed to non-equivalent
* IN lists. Verify that they now return the correct results.
* Regression test case for DERBY-6363.
*/
public void test_6363() throws SQLException {
Statement s = createStatement();
s.execute("create table d6363(a int, b char)");
s.execute("insert into d6363 values (1, 'a'), (2, 'b'), (3, 'a'), "
+ "(4, 'b'), (5, 'a'), (6, 'b')");
JDBC.assertFullResultSet(s.executeQuery(
"select a, ((b = 'a' or b = 'b') and a < 4), "
+ "((b = 'a' or b = 'c' or b = 'b') and a < 4), "
+ "((b = 'a' or (b = 'c' or b = 'b')) and a < 4), "
+ "((b = 'a' or b in ('c', 'b')) and a < 4), "
+ "(a < 4 and (b = 'a' or b = 'b')) "
+ "from d6363 order by a"),
new String[][] {
{ "1", "true", "true", "true", "true", "true" },
{ "2", "true", "true", "true", "true", "true" },
{ "3", "true", "true", "true", "true", "true" },
{ "4", "false", "false", "false", "false", "false" },
{ "5", "false", "false", "false", "false", "false" },
{ "6", "false", "false", "false", "false", "false" },
});
JDBC.assertFullResultSet(s.executeQuery(
"select a, b, "
+ "case when ((b = 'a' or b = 'b') and a < 4) "
+ "then 'x' else '-' end, "
+ "case when (a < 4 and (b = 'a' or b = 'b')) "
+ "then 'y' else '-' end "
+ "from d6363 order by a"),
new String[][] {
{ "1", "a", "x", "y" },
{ "2", "b", "x", "y" },
{ "3", "a", "x", "y" },
{ "4", "b", "-", "-" },
{ "5", "a", "-", "-" },
{ "6", "b", "-", "-" },
});
}
///////////////////////////////////////////////////////////////////////////////////
//
// SQL ROUTINES
//
///////////////////////////////////////////////////////////////////////////////////
public static Blob makeSimpleBlob() throws SQLException
{
return new HarmonySerialBlob( new byte[] { 1 } );
}
public static String booleanValue( Boolean b )
{
if ( b == null ) { return "Null value"; }
else if ( b.booleanValue() ) { return "True value"; }
else { return "False value"; }
}
///////////////////////////////////////////////////////////////////////////////////
//
// MINIONS
//
///////////////////////////////////////////////////////////////////////////////////
/** Return true if the SQL routine exists */
private boolean routineExists( Connection conn, String functionName ) throws Exception
{
PreparedStatement ps = chattyPrepare( conn, "select count (*) from sys.sysaliases where alias = ?" );
ps.setString( 1, functionName );
ResultSet rs = ps.executeQuery();
rs.next();
boolean retval = rs.getInt( 1 ) > 0 ? true : false;
rs.close();
ps.close();
return retval;
}
/** Return true if the table exists */
private boolean tableExists( Connection conn, String tableName ) throws Exception
{
PreparedStatement ps = chattyPrepare( conn, "select count (*) from sys.systables where tablename = ?" );
ps.setString( 1, tableName );
ResultSet rs = ps.executeQuery();
rs.next();
boolean retval = rs.getInt( 1 ) > 0 ? true : false;
rs.close();
ps.close();
return retval;
}
}
|