info.c 81.2 KB
Newer Older
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

/* Module:          info.c
 *
 * Description:     This module contains routines related to
 *                  ODBC informational functions.
 *
 * Classes:         n/a
 *
 * API functions:   SQLGetInfo, SQLGetTypeInfo, SQLGetFunctions, 
 *                  SQLTables, SQLColumns, SQLStatistics, SQLSpecialColumns,
 *                  SQLPrimaryKeys, SQLForeignKeys, 
 *                  SQLProcedureColumns(NI), SQLProcedures(NI), 
 *                  SQLTablePrivileges(NI), SQLColumnPrivileges(NI)
 *
 * Comments:        See "notice.txt" for copyright and license information.
 *
 */

#include <string.h>
#include <stdio.h>
#include "psqlodbc.h"
#include <windows.h>
#include <sql.h> 
#include <sqlext.h>
#include "tuple.h"
#include "pgtypes.h"

#include "environ.h"
#include "connection.h"
#include "statement.h"
#include "qresult.h"
#include "bind.h"
#include "misc.h"
#include "pgtypes.h"

//      -       -       -       -       -       -       -       -       -

RETCODE SQL_API SQLGetInfo(
        HDBC      hdbc,
        UWORD     fInfoType,
        PTR       rgbInfoValue,
        SWORD     cbInfoValueMax,
        SWORD FAR *pcbInfoValue)
{
ConnectionClass *conn = (ConnectionClass *) hdbc;
char *p;

    if ( ! conn) 
       return SQL_INVALID_HANDLE;

    /* CC: Some sanity checks */
    if ((NULL == (char *)rgbInfoValue) ||
        (cbInfoValueMax == 0))

        /* removed: */
        /* || (NULL == pcbInfoValue) */

        /* pcbInfoValue is ignored for non-character output. */
        /* some programs (at least Microsoft Query) seem to just send a NULL, */
        /* so let them get away with it... */

        return SQL_INVALID_HANDLE;


    switch (fInfoType) {
    case SQL_ACCESSIBLE_PROCEDURES: /* ODBC 1.0 */
        // can the user call all functions returned by SQLProcedures?
        // I assume access permissions could prevent this in some cases(?)
        // anyway, SQLProcedures doesn't exist yet.
        *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_ACCESSIBLE_TABLES: /* ODBC 1.0 */
        // is the user guaranteed "SELECT" on every table?
        *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_ACTIVE_CONNECTIONS: /* ODBC 1.0 */
        // how many simultaneous connections do we support?
        *((WORD *)rgbInfoValue) = MAX_CONNECTIONS;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_ACTIVE_STATEMENTS: /* ODBC 1.0 */
        // no limit on the number of active statements.
        *((WORD *)rgbInfoValue) = (WORD)0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_ALTER_TABLE: /* ODBC 2.0 */
        // what does 'alter table' support? (bitmask)
        // postgres doesn't seem to let you drop columns.
        *((DWORD *)rgbInfoValue) = SQL_AT_ADD_COLUMN;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_BOOKMARK_PERSISTENCE: /* ODBC 2.0 */
        // through what operations do bookmarks persist? (bitmask)
        // bookmarks don't exist yet, so they're not very persistent.
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_COLUMN_ALIAS: /* ODBC 2.0 */
        // do we support column aliases?  guess not.
        *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_CONCAT_NULL_BEHAVIOR: /* ODBC 1.0 */
        // how does concatenation work with NULL columns?
        // not sure how you do concatentation, but this way seems
        // more reasonable
        *((WORD *)rgbInfoValue) = SQL_CB_NON_NULL;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

        // which types of data-conversion do we support?
        // currently we don't support any, except converting a type
        // to itself.
    case SQL_CONVERT_BIGINT:
    case SQL_CONVERT_BINARY:
    case SQL_CONVERT_BIT:
    case SQL_CONVERT_CHAR:
    case SQL_CONVERT_DATE:
    case SQL_CONVERT_DECIMAL:
    case SQL_CONVERT_DOUBLE:
    case SQL_CONVERT_FLOAT:
    case SQL_CONVERT_INTEGER:
    case SQL_CONVERT_LONGVARBINARY:
    case SQL_CONVERT_LONGVARCHAR:
    case SQL_CONVERT_NUMERIC:
    case SQL_CONVERT_REAL:
    case SQL_CONVERT_SMALLINT:
    case SQL_CONVERT_TIME:
    case SQL_CONVERT_TIMESTAMP:
    case SQL_CONVERT_TINYINT:
    case SQL_CONVERT_VARBINARY:
    case SQL_CONVERT_VARCHAR: /* ODBC 1.0 */
        // only return the type we were called with (bitmask)
        *((DWORD *)rgbInfoValue) = fInfoType;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_CONVERT_FUNCTIONS: /* ODBC 1.0 */
        // which conversion functions do we support? (bitmask)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_CORRELATION_NAME: /* ODBC 1.0 */
        // I don't know what a correlation name is, so I guess we don't
        // support them.

        // *((WORD *)rgbInfoValue) = (WORD)SQL_CN_NONE;

        // well, let's just say we do--otherwise Query won't work.
        *((WORD *)rgbInfoValue) = (WORD)SQL_CN_ANY;
        if(pcbInfoValue) { *pcbInfoValue = 2; }

        break;

    case SQL_CURSOR_COMMIT_BEHAVIOR: /* ODBC 1.0 */
        // postgres definitely closes cursors when a transaction ends,
        // but you shouldn't have to re-prepare a statement after
        // commiting a transaction (I don't think)
        *((WORD *)rgbInfoValue) = (WORD)SQL_CB_CLOSE;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_CURSOR_ROLLBACK_BEHAVIOR: /* ODBC 1.0 */
        // see above
        *((WORD *)rgbInfoValue) = (WORD)SQL_CB_CLOSE;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_DATA_SOURCE_NAME: /* ODBC 1.0 */
		p = CC_get_DSN(conn);
		if (pcbInfoValue) *pcbInfoValue = strlen(p);
		strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax);
        break;

    case SQL_DATA_SOURCE_READ_ONLY: /* ODBC 1.0 */
        if (pcbInfoValue) *pcbInfoValue = 1;
		sprintf((char *)rgbInfoValue, "%c", CC_is_readonly(conn) ? 'Y' : 'N');
        break;

    case SQL_DATABASE_NAME: /* Support for old ODBC 1.0 Apps */
        // case SQL_CURRENT_QUALIFIER:
        // this tag doesn't seem to be in ODBC 2.0, and it conflicts
        // with a valid tag (SQL_TIMEDATE_ADD_INTERVALS).

		p = CC_get_database(conn);
		if (pcbInfoValue) *pcbInfoValue = strlen(p);
		strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax);
		break;

    case SQL_DBMS_NAME: /* ODBC 1.0 */
        if (pcbInfoValue) *pcbInfoValue = 10;
        strncpy_null((char *)rgbInfoValue, DBMS_NAME, (size_t)cbInfoValueMax);
        break;

    case SQL_DBMS_VER: /* ODBC 1.0 */
        if (pcbInfoValue) *pcbInfoValue = 25;
        strncpy_null((char *)rgbInfoValue, DBMS_VERSION, (size_t)cbInfoValueMax);
        break;

    case SQL_DEFAULT_TXN_ISOLATION: /* ODBC 1.0 */
        // are dirty reads, non-repeatable reads, and phantoms possible? (bitmask)
        // by direct experimentation they are not.  postgres forces
        // the newer transaction to wait before doing something that
        // would cause one of these problems.
        *((DWORD *)rgbInfoValue) = SQL_TXN_SERIALIZABLE;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_DRIVER_NAME: /* ODBC 1.0 */
        // this should be the actual filename of the driver
        p = DRIVER_FILE_NAME;
        if (pcbInfoValue)  *pcbInfoValue = strlen(p);
        strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax);
        break;

    case SQL_DRIVER_ODBC_VER:
        /* I think we should return 02.00--at least, that is the version of the */
        /* spec I'm currently referring to. */
        if (pcbInfoValue) *pcbInfoValue = 5;
        strncpy_null((char *)rgbInfoValue, "02.00", (size_t)cbInfoValueMax);
        break;

    case SQL_DRIVER_VER: /* ODBC 1.0 */
        p = POSTGRESDRIVERVERSION;
        if (pcbInfoValue) *pcbInfoValue = strlen(p);
        strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax);
        break;

    case SQL_EXPRESSIONS_IN_ORDERBY: /* ODBC 1.0 */
        // can you have expressions in an 'order by' clause?
        // not sure about this.  say no for now.
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_FETCH_DIRECTION: /* ODBC 1.0 */
        // which fetch directions are supported? (bitmask)
        // I guess these apply to SQLExtendedFetch?
        *((DWORD *)rgbInfoValue) = SQL_FETCH_NEXT ||
                                   SQL_FETCH_FIRST ||
                                   SQL_FETCH_LAST ||
                                   SQL_FETCH_PRIOR ||
                                   SQL_FETCH_ABSOLUTE;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_FILE_USAGE: /* ODBC 2.0 */
        // we are a two-tier driver, not a file-based one.
        *((WORD *)rgbInfoValue) = (WORD)SQL_FILE_NOT_SUPPORTED;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_GETDATA_EXTENSIONS: /* ODBC 2.0 */
        // (bitmask)
        *((DWORD *)rgbInfoValue) = (SQL_GD_ANY_COLUMN | SQL_GD_ANY_ORDER | SQL_GD_BOUND);
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_GROUP_BY: /* ODBC 2.0 */
        // how do the columns selected affect the columns you can group by?
        *((WORD *)rgbInfoValue) = SQL_GB_GROUP_BY_EQUALS_SELECT;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_IDENTIFIER_CASE: /* ODBC 1.0 */
        // are identifiers case-sensitive (yes)
        *((WORD *)rgbInfoValue) = SQL_IC_SENSITIVE;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_IDENTIFIER_QUOTE_CHAR: /* ODBC 1.0 */
        // the character used to quote "identifiers" (what are they?)
        // the manual index lists 'owner names' and 'qualifiers' as
        // examples of identifiers.  it says return a blank for no
        // quote character, we'll try that...
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, " ", (size_t)cbInfoValueMax);
        break;

    case SQL_KEYWORDS: /* ODBC 2.0 */
        // do this later
        conn->errormsg = "SQL_KEYWORDS parameter to SQLGetInfo not implemented.";
        conn->errornumber = CONN_NOT_IMPLEMENTED_ERROR;
        return SQL_ERROR;
        break;

    case SQL_LIKE_ESCAPE_CLAUSE: /* ODBC 2.0 */
        // is there a character that escapes '%' and '_' in a LIKE clause?
        // not as far as I can tell
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_LOCK_TYPES: /* ODBC 2.0 */
        // which lock types does SQLSetPos support? (bitmask)
        // SQLSetPos doesn't exist yet
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_MAX_BINARY_LITERAL_LEN: /* ODBC 2.0 */
        // the maximum length of a query is 2k, so maybe we should
        // set the maximum length of all these literals to that value?
        // for now just use zero for 'unknown or no limit'

        // maximum length of a binary literal
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_MAX_CHAR_LITERAL_LEN: /* ODBC 2.0 */
        // maximum length of a character literal
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_MAX_COLUMN_NAME_LEN: /* ODBC 1.0 */
        // maximum length of a column name
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_COLUMNS_IN_GROUP_BY: /* ODBC 2.0 */
        // maximum number of columns in a 'group by' clause
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_COLUMNS_IN_INDEX: /* ODBC 2.0 */
        // maximum number of columns in an index
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_COLUMNS_IN_ORDER_BY: /* ODBC 2.0 */
        // maximum number of columns in an ORDER BY statement
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_COLUMNS_IN_SELECT: /* ODBC 2.0 */
        // I think you get the idea by now
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_COLUMNS_IN_TABLE: /* ODBC 2.0 */
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_CURSOR_NAME_LEN: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_INDEX_SIZE: /* ODBC 2.0 */
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_MAX_OWNER_NAME_LEN: /* ODBC 1.0 */
        // the maximum length of a table owner's name.  (0 == none)
        // (maybe this should be 8)
        *((WORD *)rgbInfoValue) = (WORD)0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_PROCEDURE_NAME_LEN: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_QUALIFIER_NAME_LEN: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_ROW_SIZE: /* ODBC 2.0 */
        // the maximum size of one row
        // here I do know a definite value
        *((DWORD *)rgbInfoValue) = 8192;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_MAX_ROW_SIZE_INCLUDES_LONG: /* ODBC 2.0 */
        // does the preceding value include LONGVARCHAR and LONGVARBINARY
        // fields?
        *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax);
        break;

    case SQL_MAX_STATEMENT_LEN: /* ODBC 2.0 */
        // there should be a definite value here (2k?)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_MAX_TABLE_NAME_LEN: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_TABLES_IN_SELECT: /* ODBC 2.0 */
        *((WORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MAX_USER_NAME_LEN:
        *(SWORD FAR *)rgbInfoValue = 0;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_MULT_RESULT_SETS: /* ODBC 1.0 */
        // do we support multiple result sets?
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax);
        break;

    case SQL_MULTIPLE_ACTIVE_TXN: /* ODBC 1.0 */
        // do we support multiple simultaneous transactions?
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax);
        break;

    case SQL_NEED_LONG_DATA_LEN: /* ODBC 2.0 */
        if (pcbInfoValue) *pcbInfoValue = 1;
		/*	Dont need the length, SQLPutData can handle any size and multiple calls */
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_NON_NULLABLE_COLUMNS: /* ODBC 1.0 */
        // I think you can have NOT NULL columns with one of dal Zotto's
        // patches, but for now we'll say no.
        *((WORD *)rgbInfoValue) = (WORD)SQL_NNC_NULL;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_NULL_COLLATION: /* ODBC 2.0 */
        // where are nulls sorted?
        *((WORD *)rgbInfoValue) = (WORD)SQL_NC_END;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_NUMERIC_FUNCTIONS: /* ODBC 1.0 */
        // what numeric functions are supported? (bitmask)
        // I'm not sure if any of these are actually supported
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_ODBC_API_CONFORMANCE: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = SQL_OAC_LEVEL1; /* well, almost */
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_ODBC_SAG_CLI_CONFORMANCE: /* ODBC 1.0 */
        // can't find any reference to SAG in the ODBC reference manual
        // (although it's in the index, it doesn't actually appear on
        // the pages referenced)
        *((WORD *)rgbInfoValue) = SQL_OSCC_NOT_COMPLIANT;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_ODBC_SQL_CONFORMANCE: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = SQL_OSC_CORE;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_ODBC_SQL_OPT_IEF: /* ODBC 1.0 */
        // do we support the "Integrity Enhancement Facility" (?)
        // (something to do with referential integrity?)
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_ORDER_BY_COLUMNS_IN_SELECT: /* ODBC 2.0 */
        // do the columns sorted by have to be in the list of
        // columns selected?
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax);
        break;

    case SQL_OUTER_JOINS: /* ODBC 1.0 */
        // do we support outer joins?
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax);
        break;

    case SQL_OWNER_TERM: /* ODBC 1.0 */
        // what we call an owner
        if (pcbInfoValue) *pcbInfoValue = 5;
        strncpy_null((char *)rgbInfoValue, "owner", (size_t)cbInfoValueMax);
        break;

    case SQL_OWNER_USAGE: /* ODBC 2.0 */
        // in which statements can "owners be used"?  (what does that mean?
        // specifying 'owner.table' instead of just 'table' or something?)
        // (bitmask)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_POS_OPERATIONS: /* ODBC 2.0 */
        // what functions does SQLSetPos support? (bitmask)
        // SQLSetPos does not exist yet
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_POSITIONED_STATEMENTS: /* ODBC 2.0 */
        // what 'positioned' functions are supported? (bitmask)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_PROCEDURE_TERM: /* ODBC 1.0 */
        // what do we call a procedure?
        if (pcbInfoValue) *pcbInfoValue = 9;
        strncpy_null((char *)rgbInfoValue, "procedure", (size_t)cbInfoValueMax);
        break;

    case SQL_PROCEDURES: /* ODBC 1.0 */
        // do we support procedures?
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax);
        break;

    case SQL_QUALIFIER_LOCATION: /* ODBC 2.0 */
        // where does the qualifier go (before or after the table name?)
        // we don't really use qualifiers, so...
        *((WORD *)rgbInfoValue) = SQL_QL_START;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_QUALIFIER_NAME_SEPARATOR: /* ODBC 1.0 */
        // not really too sure what a qualifier is supposed to do either
        // (specify the name of a database in certain cases?), so nix
        // on that, too.
        if (pcbInfoValue) *pcbInfoValue = 0;
        strncpy_null((char *)rgbInfoValue, "", (size_t)cbInfoValueMax);
        break;

    case SQL_QUALIFIER_TERM: /* ODBC 1.0 */
        // what we call a qualifier
        if (pcbInfoValue) *pcbInfoValue = 0;
        strncpy_null((char *)rgbInfoValue, "", (size_t)cbInfoValueMax);
        break;

    case SQL_QUALIFIER_USAGE: /* ODBC 2.0 */
        // where can qualifiers be used? (bitmask)
        // nowhere
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_QUOTED_IDENTIFIER_CASE: /* ODBC 2.0 */
        // are "quoted" identifiers case-sensitive?
        // well, we don't really let you quote identifiers, so...
        *((WORD *)rgbInfoValue) = SQL_IC_SENSITIVE;
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_ROW_UPDATES: /* ODBC 1.0 */
        // not quite sure what this means
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax);
        break;

    case SQL_SCROLL_CONCURRENCY: /* ODBC 1.0 */
        // what concurrency options are supported? (bitmask)
        // taking a guess here
        *((DWORD *)rgbInfoValue) = SQL_SCCO_OPT_ROWVER;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_SCROLL_OPTIONS: /* ODBC 1.0 */
        // what options are supported for scrollable cursors? (bitmask)
        // not too sure about this one, either...
        *((DWORD *)rgbInfoValue) = SQL_SO_KEYSET_DRIVEN;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_SEARCH_PATTERN_ESCAPE: /* ODBC 1.0 */
        // this is supposed to be the character that escapes '_' or '%'
        // in LIKE clauses.  as far as I can tell postgres doesn't have one
        // (backslash generates an error).  returning an empty string means
        // no escape character is supported.
        if (pcbInfoValue) *pcbInfoValue = 0;
        strncpy_null((char *)rgbInfoValue, "", (size_t)cbInfoValueMax);
        break;

    case SQL_SERVER_NAME: /* ODBC 1.0 */
		p = CC_get_server(conn);
		if (pcbInfoValue)  *pcbInfoValue = strlen(p);
		strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax);
        break;

    case SQL_SPECIAL_CHARACTERS: /* ODBC 2.0 */
        // what special characters can be used in table and column names, etc.?
        // probably more than just this...
        if (pcbInfoValue) *pcbInfoValue = 1;
        strncpy_null((char *)rgbInfoValue, "_", (size_t)cbInfoValueMax);
        break;

    case SQL_STATIC_SENSITIVITY: /* ODBC 2.0 */
        // can changes made inside a cursor be detected? (or something like that)
        // (bitmask)
        // only applies to SQLSetPos, which doesn't exist yet.
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_STRING_FUNCTIONS: /* ODBC 1.0 */
        // what string functions exist? (bitmask)
        // not sure if any of these exist, either
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_SUBQUERIES: /* ODBC 2.0 */
		/* postgres 6.3 supports subqueries */
        *((DWORD *)rgbInfoValue) = (SQL_SQ_QUANTIFIED |
			                        SQL_SQ_IN |
                                    SQL_SQ_EXISTS |
								    SQL_SQ_COMPARISON);
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_SYSTEM_FUNCTIONS: /* ODBC 1.0 */
        // what system functions are supported? (bitmask)
        // none of these seem to be supported, either
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_TABLE_TERM: /* ODBC 1.0 */
        // what we call a table
        if (pcbInfoValue) *pcbInfoValue = 5;
        strncpy_null((char *)rgbInfoValue, "table", (size_t)cbInfoValueMax);
        break;

    case SQL_TIMEDATE_ADD_INTERVALS: /* ODBC 2.0 */
        // what resolutions are supported by the "TIMESTAMPADD scalar
        // function" (whatever that is)? (bitmask)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_TIMEDATE_DIFF_INTERVALS: /* ODBC 2.0 */
        // what resolutions are supported by the "TIMESTAMPDIFF scalar
        // function" (whatever that is)? (bitmask)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_TIMEDATE_FUNCTIONS: /* ODBC 1.0 */
        // what time and date functions are supported? (bitmask)
        *((DWORD *)rgbInfoValue) = 0;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_TXN_CAPABLE: /* ODBC 1.0 */
        *((WORD *)rgbInfoValue) = (WORD)SQL_TC_ALL;
        // Postgres can deal with create or drop table statements in a transaction
        if(pcbInfoValue) { *pcbInfoValue = 2; }
        break;

    case SQL_TXN_ISOLATION_OPTION: /* ODBC 1.0 */
        // what transaction isolation options are available? (bitmask)
        // only the default--serializable transactions.
        *((DWORD *)rgbInfoValue) = SQL_TXN_SERIALIZABLE;
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_UNION: /* ODBC 2.0 */
		/*  unions with all supported in postgres 6.3 */
        *((DWORD *)rgbInfoValue) = (SQL_U_UNION | SQL_U_UNION_ALL);
        if(pcbInfoValue) { *pcbInfoValue = 4; }
        break;

    case SQL_USER_NAME: /* ODBC 1.0 */
		p = CC_get_username(conn);
        if (pcbInfoValue) *pcbInfoValue = strlen(p);
        strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax);
        break;

    default:
        /* unrecognized key */
        conn->errormsg = "Unrecognized key passed to SQLGetInfo.";
        conn->errornumber = CONN_NOT_IMPLEMENTED_ERROR;
        return SQL_ERROR;
    }

    return SQL_SUCCESS;
}

//      -       -       -       -       -       -       -       -       -


RETCODE SQL_API SQLGetTypeInfo(
        HSTMT   hstmt,
        SWORD   fSqlType)
{
StatementClass *stmt = (StatementClass *) hstmt;
TupleNode *row;
int i;
Int4 type;

	mylog("**** in SQLGetTypeInfo: fSqlType = %d\n", fSqlType);

	if( ! stmt) {
		return SQL_INVALID_HANDLE;
	}

	stmt->manual_result = TRUE;
	stmt->result = QR_Constructor();
	if( ! stmt->result) {
		return SQL_ERROR;
	}

	extend_bindings(stmt, 15);

	QR_set_num_fields(stmt->result, 15);
	QR_set_field_info(stmt->result, 0, "TYPE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 1, "DATA_TYPE", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 2, "PRECISION", PG_TYPE_INT4, 4);
	QR_set_field_info(stmt->result, 3, "LITERAL_PREFIX", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 4, "LITERAL_SUFFIX", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 5, "CREATE_PARAMS", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 6, "NULLABLE", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 7, "CASE_SENSITIVE", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 8, "SEARCHABLE", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 9, "UNSIGNED_ATTRIBUTE", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 10, "MONEY", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 11, "AUTO_INCREMENT", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 12, "LOCAL_TYPE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 13, "MINIMUM_SCALE", PG_TYPE_INT2, 2);
	QR_set_field_info(stmt->result, 14, "MAXIMUM_SCALE", PG_TYPE_INT2, 2);

    // cycle through the types
    for(i=0, type = pgtypes_defined[0]; type; type = pgtypes_defined[++i]) {

		if(fSqlType == SQL_ALL_TYPES || fSqlType == pgtype_to_sqltype(type)) {

			row = (TupleNode *)malloc(sizeof(TupleNode) + (15 - 1)*sizeof(TupleField));

			/*	These values can't be NULL */
			set_tuplefield_string(&row->tuple[0], pgtype_to_name(type));
			set_tuplefield_int2(&row->tuple[1], pgtype_to_sqltype(type));
			set_tuplefield_int2(&row->tuple[6], pgtype_nullable(type));
			set_tuplefield_int2(&row->tuple[7], pgtype_case_sensitive(type));
			set_tuplefield_int2(&row->tuple[8], pgtype_searchable(type));
			set_tuplefield_int2(&row->tuple[10], pgtype_money(type));

			/*	Localized data-source dependent data type name (always NULL) */
			set_tuplefield_null(&row->tuple[12]);	

			/*	These values can be NULL */
			set_nullfield_int4(&row->tuple[2], pgtype_precision(type));
			set_nullfield_string(&row->tuple[3], pgtype_literal_prefix(type));
			set_nullfield_string(&row->tuple[4], pgtype_literal_suffix(type));
			set_nullfield_string(&row->tuple[5], pgtype_create_params(type));
			set_nullfield_int2(&row->tuple[9], pgtype_unsigned(type));
			set_nullfield_int2(&row->tuple[11], pgtype_auto_increment(type));
			set_nullfield_int2(&row->tuple[13], pgtype_scale(type));
			set_nullfield_int2(&row->tuple[14], pgtype_scale(type));

			QR_add_tuple(stmt->result, row);
		}
    }

    stmt->status = STMT_FINISHED;
    stmt->currTuple = -1;

    return SQL_SUCCESS;
}

//      -       -       -       -       -       -       -       -       -

RETCODE SQL_API SQLGetFunctions(
        HDBC      hdbc,
        UWORD     fFunction,
        UWORD FAR *pfExists)
{
    if (fFunction == SQL_API_ALL_FUNCTIONS) {


#ifdef GETINFO_LIE
        int i;
        memset(pfExists, 0, sizeof(UWORD)*100);

        pfExists[SQL_API_SQLALLOCENV] = TRUE;
        pfExists[SQL_API_SQLFREEENV] = TRUE;
        for (i = SQL_API_SQLALLOCCONNECT; i <= SQL_NUM_FUNCTIONS; i++)
            pfExists[i] = TRUE;
        for (i = SQL_EXT_API_START; i <= SQL_EXT_API_LAST; i++)
            pfExists[i] = TRUE;
#else
        memset(pfExists, 0, sizeof(UWORD)*100);

        // ODBC core functions
        pfExists[SQL_API_SQLALLOCCONNECT]     = TRUE;
        pfExists[SQL_API_SQLALLOCENV]         = TRUE;
        pfExists[SQL_API_SQLALLOCSTMT]        = TRUE;
        pfExists[SQL_API_SQLBINDCOL]          = TRUE;  
        pfExists[SQL_API_SQLCANCEL]           = TRUE;
        pfExists[SQL_API_SQLCOLATTRIBUTES]    = TRUE;
        pfExists[SQL_API_SQLCONNECT]          = TRUE;
        pfExists[SQL_API_SQLDESCRIBECOL]      = TRUE;  // partial
        pfExists[SQL_API_SQLDISCONNECT]       = TRUE;
        pfExists[SQL_API_SQLERROR]            = TRUE;
        pfExists[SQL_API_SQLEXECDIRECT]       = TRUE;
        pfExists[SQL_API_SQLEXECUTE]          = TRUE;
        pfExists[SQL_API_SQLFETCH]            = TRUE;
        pfExists[SQL_API_SQLFREECONNECT]      = TRUE;
        pfExists[SQL_API_SQLFREEENV]          = TRUE;
        pfExists[SQL_API_SQLFREESTMT]         = TRUE;
        pfExists[SQL_API_SQLGETCURSORNAME]    = FALSE;
        pfExists[SQL_API_SQLNUMRESULTCOLS]    = TRUE;
        pfExists[SQL_API_SQLPREPARE]          = TRUE;  // complete?
        pfExists[SQL_API_SQLROWCOUNT]         = TRUE;
        pfExists[SQL_API_SQLSETCURSORNAME]    = FALSE;
        pfExists[SQL_API_SQLSETPARAM]         = FALSE;
        pfExists[SQL_API_SQLTRANSACT]         = TRUE;

        // ODBC level 1 functions
        pfExists[SQL_API_SQLBINDPARAMETER]    = TRUE;
        pfExists[SQL_API_SQLCOLUMNS]          = TRUE;
        pfExists[SQL_API_SQLDRIVERCONNECT]    = TRUE;
        pfExists[SQL_API_SQLGETCONNECTOPTION] = TRUE;  // partial
        pfExists[SQL_API_SQLGETDATA]          = TRUE;
        pfExists[SQL_API_SQLGETFUNCTIONS]     = TRUE;  // sadly,  I still
                                                       // had to think about
                                                       // this one
        pfExists[SQL_API_SQLGETINFO]          = TRUE;
        pfExists[SQL_API_SQLGETSTMTOPTION]    = TRUE;  // very partial
        pfExists[SQL_API_SQLGETTYPEINFO]      = TRUE;
        pfExists[SQL_API_SQLPARAMDATA]        = TRUE;
        pfExists[SQL_API_SQLPUTDATA]          = TRUE;
        pfExists[SQL_API_SQLSETCONNECTOPTION] = TRUE;  // partial
        pfExists[SQL_API_SQLSETSTMTOPTION]    = TRUE;
        pfExists[SQL_API_SQLSPECIALCOLUMNS]   = TRUE;
        pfExists[SQL_API_SQLSTATISTICS]       = TRUE;
        pfExists[SQL_API_SQLTABLES]           = TRUE;

        // ODBC level 2 functions
        pfExists[SQL_API_SQLBROWSECONNECT]    = FALSE;
        pfExists[SQL_API_SQLCOLUMNPRIVILEGES] = FALSE;
        pfExists[SQL_API_SQLDATASOURCES]      = FALSE;  // only implemented by DM
        pfExists[SQL_API_SQLDESCRIBEPARAM]    = FALSE;
        pfExists[SQL_API_SQLDRIVERS]          = FALSE;
        pfExists[SQL_API_SQLEXTENDEDFETCH]    = TRUE;   // partial?
        pfExists[SQL_API_SQLFOREIGNKEYS]      = TRUE;
        pfExists[SQL_API_SQLMORERESULTS]      = TRUE;
        pfExists[SQL_API_SQLNATIVESQL]        = TRUE;
        pfExists[SQL_API_SQLNUMPARAMS]        = TRUE;
        pfExists[SQL_API_SQLPARAMOPTIONS]     = FALSE;
        pfExists[SQL_API_SQLPRIMARYKEYS]      = TRUE;
        pfExists[SQL_API_SQLPROCEDURECOLUMNS] = FALSE;
        pfExists[SQL_API_SQLPROCEDURES]       = FALSE;
        pfExists[SQL_API_SQLSETPOS]           = FALSE;
        pfExists[SQL_API_SQLSETSCROLLOPTIONS] = FALSE;
        pfExists[SQL_API_SQLTABLEPRIVILEGES]  = FALSE;
#endif
    } else {
#ifdef GETINFO_LIE
        *pfExists = TRUE;
#else
        switch(fFunction) {
        case SQL_API_SQLALLOCCONNECT:     *pfExists = TRUE; break;
        case SQL_API_SQLALLOCENV:         *pfExists = TRUE; break;
        case SQL_API_SQLALLOCSTMT:        *pfExists = TRUE; break;
        case SQL_API_SQLBINDCOL:          *pfExists = TRUE; break;
        case SQL_API_SQLCANCEL:           *pfExists = TRUE; break;
        case SQL_API_SQLCOLATTRIBUTES:    *pfExists = TRUE; break;
        case SQL_API_SQLCONNECT:          *pfExists = TRUE; break;
        case SQL_API_SQLDESCRIBECOL:      *pfExists = TRUE; break;  // partial
        case SQL_API_SQLDISCONNECT:       *pfExists = TRUE; break;
        case SQL_API_SQLERROR:            *pfExists = TRUE; break;
        case SQL_API_SQLEXECDIRECT:       *pfExists = TRUE; break;
        case SQL_API_SQLEXECUTE:          *pfExists = TRUE; break;
        case SQL_API_SQLFETCH:            *pfExists = TRUE; break;
        case SQL_API_SQLFREECONNECT:      *pfExists = TRUE; break;
        case SQL_API_SQLFREEENV:          *pfExists = TRUE; break;
        case SQL_API_SQLFREESTMT:         *pfExists = TRUE; break;
        case SQL_API_SQLGETCURSORNAME:    *pfExists = FALSE; break;
        case SQL_API_SQLNUMRESULTCOLS:    *pfExists = TRUE; break;
        case SQL_API_SQLPREPARE:          *pfExists = TRUE; break;
        case SQL_API_SQLROWCOUNT:         *pfExists = TRUE; break;
        case SQL_API_SQLSETCURSORNAME:    *pfExists = FALSE; break;
        case SQL_API_SQLSETPARAM:         *pfExists = FALSE; break;
        case SQL_API_SQLTRANSACT:         *pfExists = TRUE; break;

            // ODBC level 1 functions
        case SQL_API_SQLBINDPARAMETER:    *pfExists = TRUE; break;
        case SQL_API_SQLCOLUMNS:          *pfExists = TRUE; break;
        case SQL_API_SQLDRIVERCONNECT:    *pfExists = TRUE; break;
        case SQL_API_SQLGETCONNECTOPTION: *pfExists = TRUE; break;  // partial
        case SQL_API_SQLGETDATA:          *pfExists = TRUE; break;
        case SQL_API_SQLGETFUNCTIONS:     *pfExists = TRUE; break;
        case SQL_API_SQLGETINFO:          *pfExists = TRUE; break;
        case SQL_API_SQLGETSTMTOPTION:    *pfExists = TRUE; break;  // very partial
        case SQL_API_SQLGETTYPEINFO:      *pfExists = TRUE; break;
        case SQL_API_SQLPARAMDATA:        *pfExists = TRUE; break;
        case SQL_API_SQLPUTDATA:          *pfExists = TRUE; break;
        case SQL_API_SQLSETCONNECTOPTION: *pfExists = TRUE; break;  // partial
        case SQL_API_SQLSETSTMTOPTION:    *pfExists = TRUE; break;
        case SQL_API_SQLSPECIALCOLUMNS:   *pfExists = TRUE; break;
        case SQL_API_SQLSTATISTICS:       *pfExists = TRUE; break;
        case SQL_API_SQLTABLES:           *pfExists = TRUE; break;

            // ODBC level 2 functions
        case SQL_API_SQLBROWSECONNECT:    *pfExists = FALSE; break;
        case SQL_API_SQLCOLUMNPRIVILEGES: *pfExists = FALSE; break;
        case SQL_API_SQLDATASOURCES:      *pfExists = FALSE; break;  // only implemented by DM
        case SQL_API_SQLDESCRIBEPARAM:    *pfExists = FALSE; break;
        case SQL_API_SQLDRIVERS:          *pfExists = FALSE; break;
        case SQL_API_SQLEXTENDEDFETCH:    *pfExists = TRUE; break;   // partial?
        case SQL_API_SQLFOREIGNKEYS:      *pfExists = TRUE; break;
        case SQL_API_SQLMORERESULTS:      *pfExists = TRUE; break;
        case SQL_API_SQLNATIVESQL:        *pfExists = TRUE; break;
        case SQL_API_SQLNUMPARAMS:        *pfExists = TRUE; break;
        case SQL_API_SQLPARAMOPTIONS:     *pfExists = FALSE; break;
        case SQL_API_SQLPRIMARYKEYS:      *pfExists = TRUE; break;
        case SQL_API_SQLPROCEDURECOLUMNS: *pfExists = FALSE; break;
        case SQL_API_SQLPROCEDURES:       *pfExists = FALSE; break;
        case SQL_API_SQLSETPOS:           *pfExists = FALSE; break;
        case SQL_API_SQLSETSCROLLOPTIONS: *pfExists = FALSE; break;
        case SQL_API_SQLTABLEPRIVILEGES:  *pfExists = FALSE; break;
        }
#endif
    }

    return SQL_SUCCESS;
}



RETCODE SQL_API SQLTables(
                          HSTMT       hstmt,
                          UCHAR FAR * szTableQualifier,
                          SWORD       cbTableQualifier,
                          UCHAR FAR * szTableOwner,
                          SWORD       cbTableOwner,
                          UCHAR FAR * szTableName,
                          SWORD       cbTableName,
                          UCHAR FAR * szTableType,
                          SWORD       cbTableType)
{
StatementClass *stmt = (StatementClass *) hstmt;
StatementClass *tbl_stmt;
TupleNode *row;
HSTMT htbl_stmt;
RETCODE result;
char *tableType;
char tables_query[MAX_STATEMENT_LEN];
char table_name[MAX_INFO_STRING], table_owner[MAX_INFO_STRING];
SDWORD table_name_len, table_owner_len;

mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt);

	if( ! stmt)
		return SQL_INVALID_HANDLE;

	stmt->manual_result = TRUE;
	stmt->errormsg_created = TRUE;

	result = SQLAllocStmt( stmt->hdbc, &htbl_stmt);
	if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errornumber = STMT_NO_MEMORY_ERROR;
		stmt->errormsg = "Couldn't allocate statement for SQLTables result.";
		return SQL_ERROR;
	}
	tbl_stmt = (StatementClass *) htbl_stmt;

	// **********************************************************************
	//	Create the query to find out the tables
	// **********************************************************************

	strcpy(tables_query, "select relname, usename from pg_class, pg_user where relkind = 'r' ");

	my_strcat(tables_query, " and usename like '%.*s'", szTableOwner, cbTableOwner);
	my_strcat(tables_query, " and relname like '%.*s'", szTableName, cbTableName);

	//	make_string mallocs memory
	tableType = make_string(szTableType, cbTableType, NULL);
	if (tableType && ! strstr(tableType, "SYSTEM TABLE")) 	// is SYSTEM TABLE not present?
		strcat(tables_query, " and relname not like '" POSTGRES_SYS_PREFIX "%' and relname not like '" INSIGHT_SYS_PREFIX "%'");

	if (tableType)
		free(tableType);

	strcat(tables_query, " and relname !~ '^Inv[0-9]+' and int4out(usesysid) = int4out(relowner) order by relname");

	// **********************************************************************

	result = SQLExecDirect(htbl_stmt, tables_query, strlen(tables_query));
	if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = SC_create_errormsg(htbl_stmt);
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
		return SQL_ERROR;
	}

    result = SQLBindCol(htbl_stmt, 1, SQL_C_CHAR,
                        table_name, MAX_INFO_STRING, &table_name_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = tbl_stmt->errormsg;
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(htbl_stmt, 2, SQL_C_CHAR,
                        table_owner, MAX_INFO_STRING, &table_owner_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = tbl_stmt->errormsg;
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

	stmt->result = QR_Constructor();
	if(!stmt->result) {
		stmt->errormsg = "Couldn't allocate memory for SQLTables result.";
		stmt->errornumber = STMT_NO_MEMORY_ERROR;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
		return SQL_ERROR;
	}

    // the binding structure for a statement is not set up until
    // a statement is actually executed, so we'll have to do this ourselves.
	extend_bindings(stmt, 5);
	
    // set the field names
	QR_set_num_fields(stmt->result, 5);
	QR_set_field_info(stmt->result, 0, "TABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 1, "TABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 2, "TABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 3, "TABLE_TYPE", PG_TYPE_TEXT, MAX_INFO_STRING);
	QR_set_field_info(stmt->result, 4, "REMARKS", PG_TYPE_TEXT, 254);

    // add the tuples
	result = SQLFetch(htbl_stmt);
	while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) {
		row = (TupleNode *)malloc(sizeof(TupleNode) + (5 - 1) * sizeof(TupleField));

		set_tuplefield_string(&row->tuple[0], "");

        // I have to hide the table owner from Access, otherwise it
        // insists on referring to the table as 'owner.table'.
        // (this is valid according to the ODBC SQL grammar, but
        // Postgres won't support it.)
        // set_tuplefield_string(&row->tuple[1], table_owner);

		set_tuplefield_string(&row->tuple[1], "");
		set_tuplefield_string(&row->tuple[2], table_name);

		mylog("SQLTables: table_name = '%s'\n", table_name);

        // careful: this is case-sensitive
		if(strncmp(table_name, POSTGRES_SYS_PREFIX, strlen(POSTGRES_SYS_PREFIX)) == 0 || 
			strncmp(table_name, INSIGHT_SYS_PREFIX, strlen(INSIGHT_SYS_PREFIX)) == 0) {
			set_tuplefield_string(&row->tuple[3], "SYSTEM TABLE");
		} else {
			set_tuplefield_string(&row->tuple[3], "TABLE");
		}

		set_tuplefield_string(&row->tuple[4], "");

		QR_add_tuple(stmt->result, row);

		result = SQLFetch(htbl_stmt);
    }
	if(result != SQL_NO_DATA_FOUND) {
		stmt->errormsg = SC_create_errormsg(htbl_stmt);
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
		return SQL_ERROR;
	}

    // also, things need to think that this statement is finished so
    // the results can be retrieved.
	stmt->status = STMT_FINISHED;

    // set up the current tuple pointer for SQLFetch
	stmt->currTuple = -1;

	SQLFreeStmt(htbl_stmt, SQL_DROP);
	mylog("SQLTables(): EXIT,  stmt=%u\n", stmt);
	return SQL_SUCCESS;
}

RETCODE SQL_API SQLColumns(
                           HSTMT        hstmt,
                           UCHAR FAR *  szTableQualifier,
                           SWORD        cbTableQualifier,
                           UCHAR FAR *  szTableOwner,
                           SWORD        cbTableOwner,
                           UCHAR FAR *  szTableName,
                           SWORD        cbTableName,
                           UCHAR FAR *  szColumnName,
                           SWORD        cbColumnName)
{
StatementClass *stmt = (StatementClass *) hstmt;
TupleNode *row;
HSTMT hcol_stmt;
StatementClass *col_stmt;
char columns_query[MAX_STATEMENT_LEN];
RETCODE result;
char table_owner[MAX_INFO_STRING], table_name[MAX_INFO_STRING], field_name[MAX_INFO_STRING], field_type_name[MAX_INFO_STRING];
Int2 field_number, field_length, mod_length;
Int4 field_type;
SDWORD table_owner_len, table_name_len, field_name_len,
    field_type_len, field_type_name_len, field_number_len,
	field_length_len, mod_length_len;

mylog("**** SQLColumns(): ENTER, stmt=%u\n", stmt);

	if( ! stmt)
		return SQL_INVALID_HANDLE;

	stmt->manual_result = TRUE;
	stmt->errormsg_created = TRUE;

	// **********************************************************************
	//	Create the query to find out the columns (Note: pre 6.3 did not have the atttypmod field)
	// **********************************************************************
	sprintf(columns_query, "select u.usename, c.relname, a.attname, a.atttypid,t.typname, a.attnum, a.attlen, %s from pg_user u, pg_class c, pg_attribute a, pg_type t where "
		"int4out(u.usesysid) = int4out(c.relowner) and c.oid= a.attrelid and a.atttypid = t.oid and (a.attnum > 0)",
		PROTOCOL_62(&(stmt->hdbc->connInfo)) ? "a.attlen" : "a.atttypmod");

	my_strcat(columns_query, " and c.relname like '%.*s'", szTableName, cbTableName);
	my_strcat(columns_query, " and u.usename like '%.*s'", szTableOwner, cbTableOwner);
	my_strcat(columns_query, " and a.attname like '%.*s'", szColumnName, cbColumnName);

    // give the output in the order the columns were defined
    // when the table was created
    strcat(columns_query, " order by attnum");
	// **********************************************************************

    result = SQLAllocStmt( stmt->hdbc, &hcol_stmt);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errornumber = STMT_NO_MEMORY_ERROR;
		stmt->errormsg = "Couldn't allocate statement for SQLColumns result.";
        return SQL_ERROR;
    }
	col_stmt = (StatementClass *) hcol_stmt;

    result = SQLExecDirect(hcol_stmt, columns_query,
                           strlen(columns_query));
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = SC_create_errormsg(hcol_stmt);
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 1, SQL_C_CHAR,
                        table_owner, MAX_INFO_STRING, &table_owner_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 2, SQL_C_CHAR,
                        table_name, MAX_INFO_STRING, &table_name_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 3, SQL_C_CHAR,
                        field_name, MAX_INFO_STRING, &field_name_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 4, SQL_C_DEFAULT,
                        &field_type, 4, &field_type_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 5, SQL_C_CHAR,
                        field_type_name, MAX_INFO_STRING, &field_type_name_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 6, SQL_C_DEFAULT,
                        &field_number, MAX_INFO_STRING, &field_number_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 7, SQL_C_DEFAULT,
                        &field_length, MAX_INFO_STRING, &field_length_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(hcol_stmt, 8, SQL_C_DEFAULT,
                        &mod_length, MAX_INFO_STRING, &mod_length_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = col_stmt->errormsg;
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    stmt->result = QR_Constructor();
    if(!stmt->result) {
		stmt->errormsg = "Couldn't allocate memory for SQLColumns result.";
        stmt->errornumber = STMT_NO_MEMORY_ERROR;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    // the binding structure for a statement is not set up until
    // a statement is actually executed, so we'll have to do this ourselves.
    extend_bindings(stmt, 12);

    // set the field names
    QR_set_num_fields(stmt->result, 12);
    QR_set_field_info(stmt->result, 0, "TABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 1, "TABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 2, "TABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 3, "COLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 4, "DATA_TYPE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 5, "TYPE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 6, "PRECISION", PG_TYPE_INT4, 4);
    QR_set_field_info(stmt->result, 7, "LENGTH", PG_TYPE_INT4, 4);
    QR_set_field_info(stmt->result, 8, "SCALE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 9, "RADIX", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 10, "NULLABLE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 11, "REMARKS", PG_TYPE_TEXT, 254);

    result = SQLFetch(hcol_stmt);
    while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) {
        row = (TupleNode *)malloc(sizeof(TupleNode) +
                                  (12 - 1) * sizeof(TupleField));

        set_tuplefield_string(&row->tuple[0], "");
        // see note in SQLTables()
        //      set_tuplefield_string(&row->tuple[1], table_owner);
        set_tuplefield_string(&row->tuple[1], "");
        set_tuplefield_string(&row->tuple[2], table_name);
        set_tuplefield_string(&row->tuple[3], field_name);

		/*	Replace an unknown postgres type with SQL_CHAR type */
		/*	Leave the field_type_name with "unknown" */
		if (pgtype_to_sqltype(field_type) == PG_UNKNOWN)
	        set_tuplefield_int2(&row->tuple[4], SQL_CHAR);
		else
	        set_tuplefield_int2(&row->tuple[4], pgtype_to_sqltype(field_type));

        set_tuplefield_string(&row->tuple[5], field_type_name);


		/*	Some Notes about Postgres Data Types:

			VARCHAR - the length is stored in the pg_attribute.atttypmod field
			BPCHAR  - the length is also stored as varchar is
			NAME    - the length is fixed and stored in pg_attribute.attlen field (32 on my system)

		*/
        if((field_type == PG_TYPE_VARCHAR) ||
		   (field_type == PG_TYPE_NAME) ||
		   (field_type == PG_TYPE_BPCHAR)) {

			if (field_type == PG_TYPE_NAME)
				mod_length = field_length;	// the length is in attlen
			else if (mod_length >= 4)
				mod_length -= 4;			// the length is in atttypmod - 4

			if (mod_length > MAX_VARCHAR_SIZE || mod_length <= 0)
				mod_length = MAX_VARCHAR_SIZE;

			mylog("SQLColumns: field type is VARCHAR,NAME: field_type = %d, mod_length = %d\n", field_type, mod_length);

            set_tuplefield_int4(&row->tuple[7], mod_length);
			set_tuplefield_int4(&row->tuple[6], mod_length);
        } else {
			mylog("SQLColumns: field type is OTHER: field_type = %d, pgtype_length = %d\n", field_type, pgtype_length(field_type));

            set_tuplefield_int4(&row->tuple[7], pgtype_length(field_type));
			set_tuplefield_int4(&row->tuple[6], pgtype_precision(field_type));

        }

		set_nullfield_int2(&row->tuple[8], pgtype_scale(field_type));
		set_nullfield_int2(&row->tuple[9], pgtype_radix(field_type));
		set_tuplefield_int2(&row->tuple[10], pgtype_nullable(field_type));
		set_tuplefield_string(&row->tuple[11], "");

        QR_add_tuple(stmt->result, row);

        result = SQLFetch(hcol_stmt);
    }
    if(result != SQL_NO_DATA_FOUND) {
		stmt->errormsg = SC_create_errormsg(hcol_stmt);
		stmt->errornumber = col_stmt->errornumber;
		SQLFreeStmt(hcol_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    // also, things need to think that this statement is finished so
    // the results can be retrieved.
    stmt->status = STMT_FINISHED;

    // set up the current tuple pointer for SQLFetch
    stmt->currTuple = -1;

	SQLFreeStmt(hcol_stmt, SQL_DROP);
	mylog("SQLColumns(): EXIT,  stmt=%u\n", stmt);
    return SQL_SUCCESS;
}

RETCODE SQL_API SQLSpecialColumns(
                                  HSTMT        hstmt,
                                  UWORD        fColType,
                                  UCHAR FAR *  szTableQualifier,
                                  SWORD        cbTableQualifier,
                                  UCHAR FAR *  szTableOwner,
                                  SWORD        cbTableOwner,
                                  UCHAR FAR *  szTableName,
                                  SWORD        cbTableName,
                                  UWORD        fScope,
                                  UWORD        fNullable)
{
TupleNode *row;
StatementClass *stmt = (StatementClass *) hstmt;

mylog("**** SQLSpecialColumns(): ENTER,  stmt=%u\n", stmt);

    if( ! stmt) {
        return SQL_INVALID_HANDLE;
    }
	stmt->manual_result = TRUE;
    stmt->result = QR_Constructor();
    extend_bindings(stmt, 8);

    QR_set_num_fields(stmt->result, 8);
    QR_set_field_info(stmt->result, 0, "SCOPE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 1, "COLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 2, "DATA_TYPE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 3, "TYPE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 4, "PRECISION", PG_TYPE_INT4, 4);
    QR_set_field_info(stmt->result, 5, "LENGTH", PG_TYPE_INT4, 4);
    QR_set_field_info(stmt->result, 6, "SCALE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 7, "PSEUDO_COLUMN", PG_TYPE_INT2, 2);

    /* use the oid value for the rowid */
    if(fColType == SQL_BEST_ROWID) {

        row = (TupleNode *)malloc(sizeof(TupleNode) + (8 - 1) * sizeof(TupleField));

        set_tuplefield_int2(&row->tuple[0], SQL_SCOPE_SESSION);
        set_tuplefield_string(&row->tuple[1], "oid");
        set_tuplefield_int2(&row->tuple[2], pgtype_to_sqltype(PG_TYPE_OID));
        set_tuplefield_string(&row->tuple[3], "OID");
        set_tuplefield_int4(&row->tuple[4], pgtype_precision(PG_TYPE_OID));
        set_tuplefield_int4(&row->tuple[5], pgtype_length(PG_TYPE_OID));
        set_tuplefield_int2(&row->tuple[6], pgtype_scale(PG_TYPE_OID));
        set_tuplefield_int2(&row->tuple[7], SQL_PC_PSEUDO);

        QR_add_tuple(stmt->result, row);

    } else if(fColType == SQL_ROWVER) {
        /* can columns automatically update? */
        /* for now assume no. */
        /* return an empty result. */
    }

    stmt->status = STMT_FINISHED;
    stmt->currTuple = -1;

	mylog("SQLSpecialColumns(): EXIT,  stmt=%u\n", stmt);
    return SQL_SUCCESS;
}

RETCODE SQL_API SQLStatistics(
                              HSTMT         hstmt,
                              UCHAR FAR *   szTableQualifier,
                              SWORD         cbTableQualifier,
                              UCHAR FAR *   szTableOwner,
                              SWORD         cbTableOwner,
                              UCHAR FAR *   szTableName,
                              SWORD         cbTableName,
                              UWORD         fUnique,
                              UWORD         fAccuracy)
{
StatementClass *stmt = (StatementClass *) hstmt;
char index_query[MAX_STATEMENT_LEN];
HSTMT hindx_stmt;
RETCODE result;
char *table_name;
char index_name[MAX_INFO_STRING];
short fields_vector[8];
SDWORD index_name_len, fields_vector_len;
TupleNode *row;
int i;
HSTMT hcol_stmt;
StatementClass *col_stmt, *indx_stmt;
char column_name[MAX_INFO_STRING];
char **column_names = 0;
Int4 column_name_len;
int total_columns = 0;
char error = TRUE;

mylog("**** SQLStatistics(): ENTER,  stmt=%u\n", stmt);

    if( ! stmt) {
        return SQL_INVALID_HANDLE;
    }

	stmt->manual_result = TRUE;
	stmt->errormsg_created = TRUE;

    stmt->result = QR_Constructor();
    if(!stmt->result) {
        stmt->errormsg = "Couldn't allocate memory for SQLStatistics result.";
        stmt->errornumber = STMT_NO_MEMORY_ERROR;
        return SQL_ERROR;
    }

    // the binding structure for a statement is not set up until
    // a statement is actually executed, so we'll have to do this ourselves.
    extend_bindings(stmt, 13);

    // set the field names
    QR_set_num_fields(stmt->result, 13);
    QR_set_field_info(stmt->result, 0, "TABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 1, "TABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 2, "TABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 3, "NON_UNIQUE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 4, "INDEX_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 5, "INDEX_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 6, "TYPE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 7, "SEQ_IN_INDEX", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 8, "COLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 9, "COLLATION", PG_TYPE_CHAR, 1);
    QR_set_field_info(stmt->result, 10, "CARDINALITY", PG_TYPE_INT4, 4);
    QR_set_field_info(stmt->result, 11, "PAGES", PG_TYPE_INT4, 4);
    QR_set_field_info(stmt->result, 12, "FILTER_CONDITION", PG_TYPE_TEXT, MAX_INFO_STRING);

    // there are no unique indexes in postgres, so return nothing
    // if those are requested
    if(fUnique != SQL_INDEX_UNIQUE) {
        // only use the table name... the owner should be redundant, and
        // we never use qualifiers.
		table_name = make_string(szTableName, cbTableName, NULL);
		if ( ! table_name) {
            stmt->errormsg = "No table name passed to SQLStatistics.";
            stmt->errornumber = STMT_INTERNAL_ERROR;
            return SQL_ERROR;
        }

		// we need to get a list of the field names first,
		// so we can return them later.
		result = SQLAllocStmt( stmt->hdbc, &hcol_stmt);
		if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
			stmt->errormsg = "SQLAllocStmt failed in SQLStatistics for columns.";
			stmt->errornumber = STMT_NO_MEMORY_ERROR;
			goto SEEYA;
		}

		col_stmt = (StatementClass *) hcol_stmt;

		result = SQLColumns(hcol_stmt, "", 0, "", 0, 
					table_name, (SWORD) strlen(table_name), "", 0);
		if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
				stmt->errormsg = col_stmt->errormsg;        // "SQLColumns failed in SQLStatistics.";
				stmt->errornumber = col_stmt->errornumber;  // STMT_EXEC_ERROR;
				SQLFreeStmt(hcol_stmt, SQL_DROP);
				goto SEEYA;
		}
		result = SQLBindCol(hcol_stmt, 4, SQL_C_CHAR,
					column_name, MAX_INFO_STRING, &column_name_len);
		if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
			stmt->errormsg = col_stmt->errormsg;
			stmt->errornumber = col_stmt->errornumber;
			SQLFreeStmt(hcol_stmt, SQL_DROP);
			goto SEEYA;

		}

		result = SQLFetch(hcol_stmt);
		while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) {
			total_columns++;

			column_names = 
			(char **)realloc(column_names, 
					 total_columns * sizeof(char *));
			column_names[total_columns-1] = 
			(char *)malloc(strlen(column_name)+1);
			strcpy(column_names[total_columns-1], column_name);

			result = SQLFetch(hcol_stmt);
		}
		if(result != SQL_NO_DATA_FOUND || total_columns == 0) {
				stmt->errormsg = SC_create_errormsg(hcol_stmt); // "Couldn't get column names in SQLStatistics.";
				stmt->errornumber = col_stmt->errornumber;
				SQLFreeStmt(hcol_stmt, SQL_DROP);
   				goto SEEYA;

		}
		
		SQLFreeStmt(hcol_stmt, SQL_DROP);

        // get a list of indexes on this table
        result = SQLAllocStmt( stmt->hdbc, &hindx_stmt);
        if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
			stmt->errormsg = "SQLAllocStmt failed in SQLStatistics for indices.";
			stmt->errornumber = STMT_NO_MEMORY_ERROR;
			goto SEEYA;

        }
		indx_stmt = (StatementClass *) hindx_stmt;

		sprintf(index_query, "select c.relname, i.indkey from pg_index i, pg_class c, pg_class d where c.oid = i.indexrelid and d.relname = '%s' and d.oid = i.indrelid", 
			table_name);

        result = SQLExecDirect(hindx_stmt, index_query, strlen(index_query));
        if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
			stmt->errormsg = SC_create_errormsg(hindx_stmt); // "Couldn't execute index query (w/SQLExecDirect) in SQLStatistics.";
			stmt->errornumber = indx_stmt->errornumber;
			SQLFreeStmt(hindx_stmt, SQL_DROP);
  			goto SEEYA;

        }

        result = SQLBindCol(hindx_stmt, 1, SQL_C_CHAR,
                            index_name, MAX_INFO_STRING, &index_name_len);
        if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
			stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics.";
			stmt->errornumber = indx_stmt->errornumber;
			SQLFreeStmt(hindx_stmt, SQL_DROP);
   			goto SEEYA;

        }
        // bind the vector column
        result = SQLBindCol(hindx_stmt, 2, SQL_C_DEFAULT,
                            fields_vector, 16, &fields_vector_len);
        if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
			stmt->errormsg = indx_stmt->errormsg;  // "Couldn't bind column in SQLStatistics.";
			stmt->errornumber = indx_stmt->errornumber;
			SQLFreeStmt(hindx_stmt, SQL_DROP);
			goto SEEYA;

        }

        result = SQLFetch(hindx_stmt);
        while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) {
			i = 0;
			// add a row in this table for each field in the index
			while(i < 8 && fields_vector[i] != 0) {

				row = (TupleNode *)malloc(sizeof(TupleNode) + 
							  (13 - 1) * sizeof(TupleField));

				// no table qualifier
				set_tuplefield_string(&row->tuple[0], "");
				// don't set the table owner, else Access tries to use it
				set_tuplefield_string(&row->tuple[1], "");
				set_tuplefield_string(&row->tuple[2], table_name);

				// Postgres95 indices always allow non-unique values.
				set_tuplefield_int2(&row->tuple[3], TRUE);
				
				// no index qualifier
				set_tuplefield_string(&row->tuple[4], "");
				set_tuplefield_string(&row->tuple[5], index_name);

				// check this--what does it mean for an index
				// to be clustered?  (none of mine seem to be--
				// we can and probably should find this out from
				// the pg_index table)
				set_tuplefield_int2(&row->tuple[6], SQL_INDEX_HASHED);
				set_tuplefield_int2(&row->tuple[7], (Int2) (i+1));

				if(fields_vector[i] < 0 || fields_vector[i] > total_columns)
					set_tuplefield_string(&row->tuple[8], "UNKNOWN");
				else
					set_tuplefield_string(&row->tuple[8], column_names[fields_vector[i]-1]);

				set_tuplefield_string(&row->tuple[9], "A");
				set_tuplefield_null(&row->tuple[10]);
				set_tuplefield_null(&row->tuple[11]);
				set_tuplefield_null(&row->tuple[12]);

				QR_add_tuple(stmt->result, row);
				i++;
		    }

            result = SQLFetch(hindx_stmt);
        }
        if(result != SQL_NO_DATA_FOUND) {
			stmt->errormsg = SC_create_errormsg(hindx_stmt); // "SQLFetch failed in SQLStatistics.";
			stmt->errornumber = indx_stmt->errornumber;
			SQLFreeStmt(hindx_stmt, SQL_DROP);
			goto SEEYA;
        }

		SQLFreeStmt(hindx_stmt, SQL_DROP);
    }

    // also, things need to think that this statement is finished so
    // the results can be retrieved.
    stmt->status = STMT_FINISHED;

    // set up the current tuple pointer for SQLFetch
    stmt->currTuple = -1;

	error = FALSE;

SEEYA:
	/* These things should be freed on any error ALSO! */
	free(table_name);
    for(i = 0; i < total_columns; i++) {
		free(column_names[i]);
    }
    free(column_names);

	mylog("SQLStatistics(): EXIT, %s, stmt=%u\n", error ? "error" : "success", stmt);

	if (error)
		return SQL_ERROR;
	else
		return SQL_SUCCESS;
}

RETCODE SQL_API SQLColumnPrivileges(
                                    HSTMT        hstmt,
                                    UCHAR FAR *  szTableQualifier,
                                    SWORD        cbTableQualifier,
                                    UCHAR FAR *  szTableOwner,
                                    SWORD        cbTableOwner,
                                    UCHAR FAR *  szTableName,
                                    SWORD        cbTableName,
                                    UCHAR FAR *  szColumnName,
                                    SWORD        cbColumnName)
{
    return SQL_ERROR;
}

RETCODE
getPrimaryKeyString(StatementClass *stmt, char *szTableName, SWORD cbTableName, char *svKey, int *nKey)
{
HSTMT htbl_stmt;
StatementClass *tbl_stmt;
RETCODE result;
char tables_query[MAX_STATEMENT_LEN];
char attname[MAX_INFO_STRING];
SDWORD attname_len;
int nk = 0;

	if (nKey != NULL)
		*nKey = 0;

	svKey[0] = '\0';

	stmt->errormsg_created = TRUE;

    result = SQLAllocStmt( stmt->hdbc, &htbl_stmt);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errornumber = STMT_NO_MEMORY_ERROR;
		stmt->errormsg = "Couldn't allocate statement for Primary Key result.";
        return SQL_ERROR;
    }
	tbl_stmt = (StatementClass *) htbl_stmt;

	tables_query[0] = '\0';
	if ( ! my_strcat(tables_query, "select distinct on attnum a2.attname, a2.attnum from pg_attribute a1, pg_attribute a2, pg_class c, pg_index i where c.relname = '%.*s_key' AND c.oid = i.indexrelid AND a1.attrelid = c.oid AND a2.attrelid = c.oid AND (i.indkey[0] = a1.attnum OR i.indkey[1] = a1.attnum OR i.indkey[2] = a1.attnum OR i.indkey[3] = a1.attnum OR i.indkey[4] = a1.attnum OR i.indkey[5] = a1.attnum OR i.indkey[6] = a1.attnum OR i.indkey[7] = a1.attnum) order by a2.attnum",
			szTableName, cbTableName)) {

		stmt->errormsg = "No Table specified to getPrimaryKeyString.";
	    stmt->errornumber = STMT_INTERNAL_ERROR;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
		return SQL_ERROR;
	}

	mylog("getPrimaryKeyString: tables_query='%s'\n", tables_query);

    result = SQLExecDirect(htbl_stmt, tables_query, strlen(tables_query));
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = SC_create_errormsg(htbl_stmt);
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(htbl_stmt, 1, SQL_C_CHAR,
                        attname, MAX_INFO_STRING, &attname_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = tbl_stmt->errormsg;
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLFetch(htbl_stmt);
    while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) {

		if (strlen(svKey) > 0)
			strcat(svKey, "+");
		strcat(svKey, attname);

        result = SQLFetch(htbl_stmt);
		nk++;
    }

    if(result != SQL_NO_DATA_FOUND) {
		stmt->errormsg = SC_create_errormsg(htbl_stmt);
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

	SQLFreeStmt(htbl_stmt, SQL_DROP);

	if (nKey != NULL)
		*nKey = nk;

	mylog(">> getPrimaryKeyString: returning nKey=%d, svKey='%s'\n", nk, svKey);
	return result;
}

RETCODE
getPrimaryKeyArray(StatementClass *stmt, char *szTableName, SWORD cbTableName, char keyArray[][MAX_INFO_STRING], int *nKey)
{
RETCODE result;
char svKey[MAX_KEYLEN], *svKeyPtr;
int i = 0;

	result = getPrimaryKeyString(stmt, szTableName, cbTableName, svKey, nKey);
	if (result != SQL_SUCCESS && result != SQL_NO_DATA_FOUND)
		//  error passed from above
		return result;

	//	If no keys, return NO_DATA_FOUND
	if (svKey[0] == '\0') {
		mylog("!!!!!! getPrimaryKeyArray: svKey was null\n");
		return SQL_NO_DATA_FOUND;
	}

	// mylog(">> primarykeyArray: nKey=%d, svKey='%s'\n",  *nKey, svKey);

	svKeyPtr = strtok(svKey, "+");
	while (svKeyPtr != NULL && i < MAX_KEYPARTS) {
		strcpy(keyArray[i++], svKeyPtr);
		svKeyPtr = strtok(NULL, "+");
	}

	/*
	for (i = 0; i < *nKey; i++)
		mylog(">> keyArray[%d] = '%s'\n", i, keyArray[i]);
	*/

	return result;
}


RETCODE SQL_API SQLPrimaryKeys(
                               HSTMT         hstmt,
                               UCHAR FAR *   szTableQualifier,
                               SWORD         cbTableQualifier,
                               UCHAR FAR *   szTableOwner,
                               SWORD         cbTableOwner,
                               UCHAR FAR *   szTableName,
                               SWORD         cbTableName)
{
StatementClass *stmt = (StatementClass *) hstmt;
TupleNode *row;
RETCODE result;
char svKey[MAX_KEYLEN], *ptr;
int seq = 1, nkeys = 0;

mylog("**** SQLPrimaryKeys(): ENTER, stmt=%u\n", stmt);

    if( ! stmt) {
        return SQL_INVALID_HANDLE;
    }
	stmt->manual_result = TRUE;

	result = getPrimaryKeyString(stmt, szTableName, cbTableName, svKey, &nkeys);

	mylog(">> PrimaryKeys: getPrimaryKeyString() returned %d, nkeys=%d, svKey = '%s'\n", result, nkeys, svKey);

	if (result != SQL_SUCCESS && result != SQL_NO_DATA_FOUND) {
		//	error msg passed from above
		return result;
	}

	//	I'm not sure if this is correct to return when there are no keys or
	//	if an empty result set would be better.
	if (nkeys == 0) {
		stmt->errornumber = STMT_INFO_ONLY;
		stmt->errormsg = "No primary keys for this table.";
		return SQL_SUCCESS_WITH_INFO;
	}

    stmt->result = QR_Constructor();
    if(!stmt->result) {
        stmt->errormsg = "Couldn't allocate memory for SQLPrimaryKeys result.";
        stmt->errornumber = STMT_NO_MEMORY_ERROR;
        return SQL_ERROR;
    }


    // the binding structure for a statement is not set up until
    // a statement is actually executed, so we'll have to do this ourselves.
    extend_bindings(stmt, 6);
	
    // set the field names
    QR_set_num_fields(stmt->result, 6);
    QR_set_field_info(stmt->result, 0, "TABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 1, "TABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 2, "TABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 3, "COLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 4, "KEY_SEQ", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 5, "PK_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);

    // add the tuples
	ptr = strtok(svKey, "+");
    while( ptr != NULL) {
        row = (TupleNode *)malloc(sizeof(TupleNode) + (6 - 1) * sizeof(TupleField));

        set_tuplefield_string(&row->tuple[0], "");

        // I have to hide the table owner from Access, otherwise it
        // insists on referring to the table as 'owner.table'.
        // (this is valid according to the ODBC SQL grammar, but
        // Postgres won't support it.)

		mylog(">> primaryKeys: ptab = '%s', seq = %d\n", ptr, seq);

        set_tuplefield_string(&row->tuple[1], "");
        set_tuplefield_string(&row->tuple[2], szTableName);
        set_tuplefield_string(&row->tuple[3], ptr);
		set_tuplefield_int2(&row->tuple[4], (Int2) (seq++));
		set_tuplefield_null(&row->tuple[5]);

        QR_add_tuple(stmt->result, row);

		ptr = strtok(NULL, "+");
	}

    // also, things need to think that this statement is finished so
    // the results can be retrieved.
    stmt->status = STMT_FINISHED;

    // set up the current tuple pointer for SQLFetch
    stmt->currTuple = -1;

	mylog("SQLPrimaryKeys(): EXIT, stmt=%u\n", stmt);
    return SQL_SUCCESS;
}

RETCODE SQL_API SQLForeignKeys(
                               HSTMT         hstmt,
                               UCHAR FAR *   szPkTableQualifier,
                               SWORD         cbPkTableQualifier,
                               UCHAR FAR *   szPkTableOwner,
                               SWORD         cbPkTableOwner,
                               UCHAR FAR *   szPkTableName,
                               SWORD         cbPkTableName,
                               UCHAR FAR *   szFkTableQualifier,
                               SWORD         cbFkTableQualifier,
                               UCHAR FAR *   szFkTableOwner,
                               SWORD         cbFkTableOwner,
                               UCHAR FAR *   szFkTableName,
                               SWORD         cbFkTableName)
{
StatementClass *stmt = (StatementClass *) hstmt;
TupleNode *row;
HSTMT htbl_stmt;
StatementClass *tbl_stmt;
RETCODE result;
char tables_query[MAX_STATEMENT_LEN];
char relname[MAX_INFO_STRING], attnames[MAX_INFO_STRING], frelname[MAX_INFO_STRING];
SDWORD relname_len, attnames_len, frelname_len;
char *pktab, *fktab;
char fkey = FALSE;
char primaryKey[MAX_KEYPARTS][MAX_INFO_STRING];
char *attnamePtr;
int pkeys, seq;

mylog("**** SQLForeignKeys(): ENTER, stmt=%u\n", stmt);

	memset(primaryKey, 0, sizeof(primaryKey));

    if( ! stmt) {
        return SQL_INVALID_HANDLE;
    }
	stmt->manual_result = TRUE;
	stmt->errormsg_created = TRUE;

    result = SQLAllocStmt( stmt->hdbc, &htbl_stmt);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errornumber = STMT_NO_MEMORY_ERROR;
		stmt->errormsg = "Couldn't allocate statement for SQLForeignKeys result.";
        return SQL_ERROR;
    }

	tbl_stmt = (StatementClass *) htbl_stmt;

	pktab = make_string(szPkTableName, cbPkTableName, NULL);
	fktab = make_string(szFkTableName, cbFkTableName, NULL);

	if (pktab && fktab) {
        //	Get the primary key of the table listed in szPkTable
		result = getPrimaryKeyArray(stmt, pktab, (SWORD) strlen(pktab), primaryKey, &pkeys);
		if (result != SQL_SUCCESS && result != SQL_NO_DATA_FOUND) {
			//	error msg passed from above
			SQLFreeStmt(htbl_stmt, SQL_DROP);
			free(pktab); free(fktab);
			return result;
		}
		if (pkeys == 0) {
			stmt->errornumber = STMT_INFO_ONLY;
			stmt->errormsg = "No primary keys for this table.";
			SQLFreeStmt(htbl_stmt, SQL_DROP);
			free(pktab); free(fktab);
			return SQL_SUCCESS_WITH_INFO;
		}

	    sprintf(tables_query, "select relname, attnames, frelname from %s where relname='%s' AND frelname='%s'", KEYS_TABLE, fktab, pktab);
		free(pktab); free(fktab);
	}
    else if (pktab) {
        //	Get the primary key of the table listed in szPkTable
		result = getPrimaryKeyArray(stmt, pktab, (SWORD) strlen(pktab), primaryKey, &pkeys);
		if (result != SQL_SUCCESS && result != SQL_NO_DATA_FOUND) {
			//	error msg passed from above
			SQLFreeStmt(htbl_stmt, SQL_DROP);
			free(pktab);
			return result;
		}
		if (pkeys == 0) {
			stmt->errornumber = STMT_INFO_ONLY;
			stmt->errormsg = "No primary keys for this table.";
			SQLFreeStmt(htbl_stmt, SQL_DROP);
			free(pktab);
			return SQL_SUCCESS_WITH_INFO;
		}

	    sprintf(tables_query, "select relname, attnames, frelname from %s where frelname='%s'", KEYS_TABLE, pktab);
		free(pktab);
    }
    else if (fktab) {
		//	This query could involve multiple calls to getPrimaryKey()
		//	so put that off till we know what pktables we need.
		fkey = TRUE;

	    sprintf(tables_query, "select relname, attnames, frelname from %s where relname='%s'", KEYS_TABLE, fktab);
		free(fktab);
    }
	else {
		stmt->errormsg = "No tables specified to SQLForeignKeys.";
		stmt->errornumber = STMT_INTERNAL_ERROR;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
		return SQL_ERROR;
	}

    result = SQLExecDirect(htbl_stmt, tables_query, strlen(tables_query));
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = SC_create_errormsg(htbl_stmt);
		stmt->errornumber = tbl_stmt->errornumber;
    	SQLFreeStmt(htbl_stmt, SQL_DROP);
	    return SQL_ERROR;
    }

    result = SQLBindCol(htbl_stmt, 1, SQL_C_CHAR,
                        relname, MAX_INFO_STRING, &relname_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = tbl_stmt->errormsg;
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }
    result = SQLBindCol(htbl_stmt, 2, SQL_C_CHAR,
                        attnames, MAX_INFO_STRING, &attnames_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = tbl_stmt->errormsg;
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    result = SQLBindCol(htbl_stmt, 3, SQL_C_CHAR,
                        frelname, MAX_INFO_STRING, &frelname_len);
    if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
		stmt->errormsg = tbl_stmt->errormsg;
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    stmt->result = QR_Constructor();
    if(!stmt->result) {
		stmt->errormsg = "Couldn't allocate memory for SQLForeignKeys result.";
        stmt->errornumber = STMT_NO_MEMORY_ERROR;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

    // the binding structure for a statement is not set up until
    // a statement is actually executed, so we'll have to do this ourselves.
    extend_bindings(stmt, 13);

    // set the field names
    QR_set_num_fields(stmt->result, 13);
    QR_set_field_info(stmt->result, 0, "PKTABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 1, "PKTABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 2, "PKTABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 3, "PKCOLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 4, "FKTABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 5, "FKTABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 6, "FKTABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 7, "FKCOLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 8, "KEY_SEQ", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 9, "UPDATE_RULE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 10, "DELETE_RULE", PG_TYPE_INT2, 2);
    QR_set_field_info(stmt->result, 11, "FK_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
    QR_set_field_info(stmt->result, 12, "PK_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);

    // add the tuples
    result = SQLFetch(htbl_stmt);

    while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) {

		if (fkey == TRUE) {
			result = getPrimaryKeyArray(stmt, frelname, (SWORD) strlen(frelname), primaryKey, &pkeys);

			//  mylog(">> getPrimaryKeyArray: frelname = '%s', pkeys = %d, result = %d\n", frelname, pkeys, result);

			//	If an error occurs or for some reason there is no primary key for a
			//	table that is a foreign key, then skip that one.
			if ((result != SQL_SUCCESS && result != SQL_NO_DATA_FOUND) || pkeys == 0) {
		        result = SQLFetch(htbl_stmt);
				continue;
			}

			/*
			for (i = 0; i< pkeys; i++)
				mylog(">> fkey: pkeys=%d, primaryKey[%d] = '%s'\n", pkeys, i, primaryKey[i]);
			mylog(">> !!!!!!!!! pkeys = %d\n", pkeys);
			*/
		}

		// mylog(">> attnames='%s'\n", attnames);

		attnamePtr = strtok(attnames, "+");
		seq = 0;

		while (attnamePtr != NULL && seq < pkeys) {

	        row = (TupleNode *)malloc(sizeof(TupleNode) + (13 - 1) * sizeof(TupleField));

			set_tuplefield_null(&row->tuple[0]);

			// I have to hide the table owner from Access, otherwise it
			// insists on referring to the table as 'owner.table'.
			// (this is valid according to the ODBC SQL grammar, but
			// Postgres won't support it.)

			mylog(">> foreign keys: pktab='%s' patt='%s' fktab='%s' fatt='%s' seq=%d\n", 
				frelname, primaryKey[seq], relname, attnamePtr, (seq+1));

			set_tuplefield_string(&row->tuple[1], "");
			set_tuplefield_string(&row->tuple[2], frelname);
			set_tuplefield_string(&row->tuple[3], primaryKey[seq]);
			set_tuplefield_null(&row->tuple[4]);
			set_tuplefield_string(&row->tuple[5], "");
			set_tuplefield_string(&row->tuple[6], relname);
			set_tuplefield_string(&row->tuple[7], attnamePtr);
			set_tuplefield_int2(&row->tuple[8], (Int2) (++seq));
			set_tuplefield_null(&row->tuple[9]);
			set_tuplefield_null(&row->tuple[10]);
			set_tuplefield_null(&row->tuple[11]);
			set_tuplefield_null(&row->tuple[12]);

			QR_add_tuple(stmt->result, row);

			attnamePtr = strtok(NULL, "+");
		}
        result = SQLFetch(htbl_stmt);
    }

    if(result != SQL_NO_DATA_FOUND) {
		stmt->errormsg = SC_create_errormsg(htbl_stmt);
		stmt->errornumber = tbl_stmt->errornumber;
		SQLFreeStmt(htbl_stmt, SQL_DROP);
        return SQL_ERROR;
    }

	SQLFreeStmt(htbl_stmt, SQL_DROP);

    // also, things need to think that this statement is finished so
    // the results can be retrieved.
    stmt->status = STMT_FINISHED;

    // set up the current tuple pointer for SQLFetch
    stmt->currTuple = -1;

	mylog("SQLForeignKeys(): EXIT, stmt=%u\n", stmt);
    return SQL_SUCCESS;
}



RETCODE SQL_API SQLProcedureColumns(
                                    HSTMT         hstmt,
                                    UCHAR FAR *   szProcQualifier,
                                    SWORD         cbProcQualifier,
                                    UCHAR FAR *   szProcOwner,
                                    SWORD         cbProcOwner,
                                    UCHAR FAR *   szProcName,
                                    SWORD         cbProcName,
                                    UCHAR FAR *   szColumnName,
                                    SWORD         cbColumnName)
{
    return SQL_ERROR;
}

RETCODE SQL_API SQLProcedures(
                              HSTMT          hstmt,
                              UCHAR FAR *    szProcQualifier,
                              SWORD          cbProcQualifier,
                              UCHAR FAR *    szProcOwner,
                              SWORD          cbProcOwner,
                              UCHAR FAR *    szProcName,
                              SWORD          cbProcName)
{
    return SQL_ERROR;
}

RETCODE SQL_API SQLTablePrivileges(
                                   HSTMT           hstmt,
                                   UCHAR FAR *     szTableQualifier,
                                   SWORD           cbTableQualifier,
                                   UCHAR FAR *     szTableOwner,
                                   SWORD           cbTableOwner,
                                   UCHAR FAR *     szTableName,
                                   SWORD           cbTableName)
{
    return SQL_ERROR;
}