1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1999-2002
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char revid[] = "$Id: tcl_db.c,v 1.1.1.1 2003/11/20 22:13:54 toshok Exp $";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#endif
#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/db_am.h"
#include "dbinc/tcl_db.h"
/*
* Prototypes for procedures defined later in this file:
*/
static int tcl_DbAssociate __P((Tcl_Interp *,
int, Tcl_Obj * CONST*, DB *));
static int tcl_DbClose __P((Tcl_Interp *,
int, Tcl_Obj * CONST*, DB *, DBTCL_INFO *));
static int tcl_DbDelete __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_DbGet __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *, int));
static int tcl_DbKeyRange __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_DbPut __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_DbStat __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_DbTruncate __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_DbCursor __P((Tcl_Interp *,
int, Tcl_Obj * CONST*, DB *, DBC **));
static int tcl_DbJoin __P((Tcl_Interp *,
int, Tcl_Obj * CONST*, DB *, DBC **));
static int tcl_DbGetjoin __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_DbCount __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB *));
static int tcl_second_call __P((DB *, const DBT *, const DBT *, DBT *));
/*
* _DbInfoDelete --
*
* PUBLIC: void _DbInfoDelete __P((Tcl_Interp *, DBTCL_INFO *));
*/
void
_DbInfoDelete(interp, dbip)
Tcl_Interp *interp;
DBTCL_INFO *dbip;
{
DBTCL_INFO *nextp, *p;
/*
* First we have to close any open cursors. Then we close
* our db.
*/
for (p = LIST_FIRST(&__db_infohead); p != NULL; p = nextp) {
nextp = LIST_NEXT(p, entries);
/*
* Check if this is a cursor info structure and if
* it is, if it belongs to this DB. If so, remove
* its commands and info structure.
*/
if (p->i_parent == dbip && p->i_type == I_DBC) {
(void)Tcl_DeleteCommand(interp, p->i_name);
_DeleteInfo(p);
}
}
(void)Tcl_DeleteCommand(interp, dbip->i_name);
_DeleteInfo(dbip);
}
/*
*
* PUBLIC: int db_Cmd __P((ClientData, Tcl_Interp *, int, Tcl_Obj * CONST*));
*
* db_Cmd --
* Implements the "db" widget.
*/
int
db_Cmd(clientData, interp, objc, objv)
ClientData clientData; /* DB handle */
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
{
static char *dbcmds[] = {
#if CONFIG_TEST
"keyrange",
"pget",
"rpcid",
"test",
#endif
"associate",
"close",
"count",
"cursor",
"del",
"get",
"get_join",
"get_type",
"is_byteswapped",
"join",
"put",
"stat",
"sync",
"truncate",
NULL
};
enum dbcmds {
#if CONFIG_TEST
DBKEYRANGE,
DBPGET,
DBRPCID,
DBTEST,
#endif
DBASSOCIATE,
DBCLOSE,
DBCOUNT,
DBCURSOR,
DBDELETE,
DBGET,
DBGETJOIN,
DBGETTYPE,
DBSWAPPED,
DBJOIN,
DBPUT,
DBSTAT,
DBSYNC,
DBTRUNCATE
};
DB *dbp;
DBC *dbc;
DBTCL_INFO *dbip;
DBTCL_INFO *ip;
DBTYPE type;
Tcl_Obj *res;
int cmdindex, isswapped, result, ret;
char newname[MSG_SIZE];
Tcl_ResetResult(interp);
dbp = (DB *)clientData;
dbip = _PtrToInfo((void *)dbp);
memset(newname, 0, MSG_SIZE);
result = TCL_OK;
if (objc <= 1) {
Tcl_WrongNumArgs(interp, 1, objv, "command cmdargs");
return (TCL_ERROR);
}
if (dbp == NULL) {
Tcl_SetResult(interp, "NULL db pointer", TCL_STATIC);
return (TCL_ERROR);
}
if (dbip == NULL) {
Tcl_SetResult(interp, "NULL db info pointer", TCL_STATIC);
return (TCL_ERROR);
}
/*
* Get the command name index from the object based on the dbcmds
* defined above.
*/
if (Tcl_GetIndexFromObj(interp,
objv[1], dbcmds, "command", TCL_EXACT, &cmdindex) != TCL_OK)
return (IS_HELP(objv[1]));
res = NULL;
switch ((enum dbcmds)cmdindex) {
#if CONFIG_TEST
case DBKEYRANGE:
result = tcl_DbKeyRange(interp, objc, objv, dbp);
break;
case DBPGET:
result = tcl_DbGet(interp, objc, objv, dbp, 1);
break;
case DBRPCID:
/*
* No args for this. Error if there are some.
*/
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return (TCL_ERROR);
}
/*
* !!! Retrieve the client ID from the dbp handle directly.
* This is for testing purposes only. It is dbp-private data.
*/
res = Tcl_NewLongObj(dbp->cl_id);
break;
case DBTEST:
result = tcl_EnvTest(interp, objc, objv, dbp->dbenv);
break;
#endif
case DBASSOCIATE:
result = tcl_DbAssociate(interp, objc, objv, dbp);
break;
case DBCLOSE:
result = tcl_DbClose(interp, objc, objv, dbp, dbip);
break;
case DBDELETE:
result = tcl_DbDelete(interp, objc, objv, dbp);
break;
case DBGET:
result = tcl_DbGet(interp, objc, objv, dbp, 0);
break;
case DBPUT:
result = tcl_DbPut(interp, objc, objv, dbp);
break;
case DBCOUNT:
result = tcl_DbCount(interp, objc, objv, dbp);
break;
case DBSWAPPED:
/*
* No args for this. Error if there are some.
*/
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return (TCL_ERROR);
}
_debug_check();
ret = dbp->get_byteswapped(dbp, &isswapped);
res = Tcl_NewIntObj(isswapped);
break;
case DBGETTYPE:
/*
* No args for this. Error if there are some.
*/
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return (TCL_ERROR);
}
_debug_check();
ret = dbp->get_type(dbp, &type);
if (type == DB_BTREE)
res = Tcl_NewStringObj("btree", strlen("btree"));
else if (type == DB_HASH)
res = Tcl_NewStringObj("hash", strlen("hash"));
else if (type == DB_RECNO)
res = Tcl_NewStringObj("recno", strlen("recno"));
else if (type == DB_QUEUE)
res = Tcl_NewStringObj("queue", strlen("queue"));
else {
Tcl_SetResult(interp,
"db gettype: Returned unknown type\n", TCL_STATIC);
result = TCL_ERROR;
}
break;
case DBSTAT:
result = tcl_DbStat(interp, objc, objv, dbp);
break;
case DBSYNC:
/*
* No args for this. Error if there are some.
*/
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return (TCL_ERROR);
}
_debug_check();
ret = dbp->sync(dbp, 0);
res = Tcl_NewIntObj(ret);
if (ret != 0) {
Tcl_SetObjResult(interp, res);
result = TCL_ERROR;
}
break;
case DBCURSOR:
snprintf(newname, sizeof(newname),
"%s.c%d", dbip->i_name, dbip->i_dbdbcid);
ip = _NewInfo(interp, NULL, newname, I_DBC);
if (ip != NULL) {
result = tcl_DbCursor(interp, objc, objv, dbp, &dbc);
if (result == TCL_OK) {
dbip->i_dbdbcid++;
ip->i_parent = dbip;
Tcl_CreateObjCommand(interp, newname,
(Tcl_ObjCmdProc *)dbc_Cmd,
(ClientData)dbc, NULL);
res =
Tcl_NewStringObj(newname, strlen(newname));
_SetInfoData(ip, dbc);
} else
_DeleteInfo(ip);
} else {
Tcl_SetResult(interp,
"Could not set up info", TCL_STATIC);
result = TCL_ERROR;
}
break;
case DBJOIN:
snprintf(newname, sizeof(newname),
"%s.c%d", dbip->i_name, dbip->i_dbdbcid);
ip = _NewInfo(interp, NULL, newname, I_DBC);
if (ip != NULL) {
result = tcl_DbJoin(interp, objc, objv, dbp, &dbc);
if (result == TCL_OK) {
dbip->i_dbdbcid++;
ip->i_parent = dbip;
Tcl_CreateObjCommand(interp, newname,
(Tcl_ObjCmdProc *)dbc_Cmd,
(ClientData)dbc, NULL);
res =
Tcl_NewStringObj(newname, strlen(newname));
_SetInfoData(ip, dbc);
} else
_DeleteInfo(ip);
} else {
Tcl_SetResult(interp,
"Could not set up info", TCL_STATIC);
result = TCL_ERROR;
}
break;
case DBGETJOIN:
result = tcl_DbGetjoin(interp, objc, objv, dbp);
break;
case DBTRUNCATE:
result = tcl_DbTruncate(interp, objc, objv, dbp);
break;
}
/*
* Only set result if we have a res. Otherwise, lower
* functions have already done so.
*/
if (result == TCL_OK && res)
Tcl_SetObjResult(interp, res);
return (result);
}
/*
* tcl_db_stat --
*/
static int
tcl_DbStat(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
DB_BTREE_STAT *bsp;
DB_HASH_STAT *hsp;
DB_QUEUE_STAT *qsp;
void *sp;
Tcl_Obj *res, *flaglist, *myobjv[2];
DBTYPE type;
u_int32_t flag;
int result, ret;
char *arg;
result = TCL_OK;
flag = 0;
if (objc > 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?-faststat?");
return (TCL_ERROR);
}
if (objc == 3) {
arg = Tcl_GetStringFromObj(objv[2], NULL);
if (strcmp(arg, "-faststat") == 0)
flag = DB_FAST_STAT;
else {
Tcl_SetResult(interp,
"db stat: unknown arg", TCL_STATIC);
return (TCL_ERROR);
}
}
_debug_check();
ret = dbp->stat(dbp, &sp, flag);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db stat");
if (result == TCL_ERROR)
return (result);
(void)dbp->get_type(dbp, &type);
/*
* Have our stats, now construct the name value
* list pairs and free up the memory.
*/
res = Tcl_NewObj();
/*
* MAKE_STAT_LIST assumes 'res' and 'error' label.
*/
if (type == DB_HASH) {
hsp = (DB_HASH_STAT *)sp;
MAKE_STAT_LIST("Magic", hsp->hash_magic);
MAKE_STAT_LIST("Version", hsp->hash_version);
MAKE_STAT_LIST("Page size", hsp->hash_pagesize);
MAKE_STAT_LIST("Number of keys", hsp->hash_nkeys);
MAKE_STAT_LIST("Number of records", hsp->hash_ndata);
MAKE_STAT_LIST("Fill factor", hsp->hash_ffactor);
MAKE_STAT_LIST("Buckets", hsp->hash_buckets);
if (flag != DB_FAST_STAT) {
MAKE_STAT_LIST("Free pages", hsp->hash_free);
MAKE_STAT_LIST("Bytes free", hsp->hash_bfree);
MAKE_STAT_LIST("Number of big pages",
hsp->hash_bigpages);
MAKE_STAT_LIST("Big pages bytes free",
hsp->hash_big_bfree);
MAKE_STAT_LIST("Overflow pages", hsp->hash_overflows);
MAKE_STAT_LIST("Overflow bytes free",
hsp->hash_ovfl_free);
MAKE_STAT_LIST("Duplicate pages", hsp->hash_dup);
MAKE_STAT_LIST("Duplicate pages bytes free",
hsp->hash_dup_free);
}
} else if (type == DB_QUEUE) {
qsp = (DB_QUEUE_STAT *)sp;
MAKE_STAT_LIST("Magic", qsp->qs_magic);
MAKE_STAT_LIST("Version", qsp->qs_version);
MAKE_STAT_LIST("Page size", qsp->qs_pagesize);
MAKE_STAT_LIST("Extent size", qsp->qs_extentsize);
MAKE_STAT_LIST("Number of records", qsp->qs_nkeys);
MAKE_STAT_LIST("Record length", qsp->qs_re_len);
MAKE_STAT_LIST("Record pad", qsp->qs_re_pad);
MAKE_STAT_LIST("First record number", qsp->qs_first_recno);
MAKE_STAT_LIST("Last record number", qsp->qs_cur_recno);
if (flag != DB_FAST_STAT) {
MAKE_STAT_LIST("Number of pages", qsp->qs_pages);
MAKE_STAT_LIST("Bytes free", qsp->qs_pgfree);
}
} else { /* BTREE and RECNO are same stats */
bsp = (DB_BTREE_STAT *)sp;
MAKE_STAT_LIST("Magic", bsp->bt_magic);
MAKE_STAT_LIST("Version", bsp->bt_version);
MAKE_STAT_LIST("Number of keys", bsp->bt_nkeys);
MAKE_STAT_LIST("Number of records", bsp->bt_ndata);
MAKE_STAT_LIST("Minimum keys per page", bsp->bt_minkey);
MAKE_STAT_LIST("Fixed record length", bsp->bt_re_len);
MAKE_STAT_LIST("Record pad", bsp->bt_re_pad);
MAKE_STAT_LIST("Page size", bsp->bt_pagesize);
if (flag != DB_FAST_STAT) {
MAKE_STAT_LIST("Levels", bsp->bt_levels);
MAKE_STAT_LIST("Internal pages", bsp->bt_int_pg);
MAKE_STAT_LIST("Leaf pages", bsp->bt_leaf_pg);
MAKE_STAT_LIST("Duplicate pages", bsp->bt_dup_pg);
MAKE_STAT_LIST("Overflow pages", bsp->bt_over_pg);
MAKE_STAT_LIST("Pages on freelist", bsp->bt_free);
MAKE_STAT_LIST("Internal pages bytes free",
bsp->bt_int_pgfree);
MAKE_STAT_LIST("Leaf pages bytes free",
bsp->bt_leaf_pgfree);
MAKE_STAT_LIST("Duplicate pages bytes free",
bsp->bt_dup_pgfree);
MAKE_STAT_LIST("Bytes free in overflow pages",
bsp->bt_over_pgfree);
}
}
/*
* Construct a {name {flag1 flag2 ... flagN}} list for the
* dbp flags. These aren't access-method dependent, but they
* include all the interesting flags, and the integer value
* isn't useful from Tcl--return the strings instead.
*/
myobjv[0] = Tcl_NewStringObj("Flags", strlen("Flags"));
myobjv[1] = _GetFlagsList(interp, dbp->flags, __db_inmemdbflags);
flaglist = Tcl_NewListObj(2, myobjv);
if (flaglist == NULL) {
result = TCL_ERROR;
goto error;
}
if ((result =
Tcl_ListObjAppendElement(interp, res, flaglist)) != TCL_OK)
goto error;
Tcl_SetObjResult(interp, res);
error:
free(sp);
return (result);
}
/*
* tcl_db_close --
*/
static int
tcl_DbClose(interp, objc, objv, dbp, dbip)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
DBTCL_INFO *dbip; /* Info pointer */
{
static char *dbclose[] = {
"-nosync", "--", NULL
};
enum dbclose {
TCL_DBCLOSE_NOSYNC,
TCL_DBCLOSE_ENDARG
};
u_int32_t flag;
int endarg, i, optindex, result, ret;
char *arg;
result = TCL_OK;
endarg = 0;
flag = 0;
if (objc > 4) {
Tcl_WrongNumArgs(interp, 2, objv, "?-nosync?");
return (TCL_ERROR);
}
i = 2;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbclose,
"option", TCL_EXACT, &optindex) != TCL_OK) {
arg = Tcl_GetStringFromObj(objv[i], NULL);
if (arg[0] == '-')
return (IS_HELP(objv[i]));
else
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbclose)optindex) {
case TCL_DBCLOSE_NOSYNC:
flag = DB_NOSYNC;
break;
case TCL_DBCLOSE_ENDARG:
endarg = 1;
break;
}
/*
* If, at any time, parsing the args we get an error,
* bail out and return.
*/
if (result != TCL_OK)
return (result);
if (endarg)
break;
}
_DbInfoDelete(interp, dbip);
_debug_check();
/* Paranoia. */
dbp->api_internal = NULL;
ret = (dbp)->close(dbp, flag);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db close");
return (result);
}
/*
* tcl_db_put --
*/
static int
tcl_DbPut(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
static char *dbputopts[] = {
#if CONFIG_TEST
"-nodupdata",
#endif
"-append",
"-auto_commit",
"-nooverwrite",
"-partial",
"-txn",
NULL
};
enum dbputopts {
#if CONFIG_TEST
DBGET_NODUPDATA,
#endif
DBPUT_APPEND,
DBPUT_AUTO_COMMIT,
DBPUT_NOOVER,
DBPUT_PART,
DBPUT_TXN
};
static char *dbputapp[] = {
"-append", NULL
};
enum dbputapp { DBPUT_APPEND0 };
DBT key, data;
DBTYPE type;
DB_TXN *txn;
Tcl_Obj **elemv, *res;
void *dtmp, *ktmp;
db_recno_t recno;
u_int32_t flag;
int auto_commit, elemc, end, freekey, freedata;
int i, optindex, result, ret;
char *arg, msg[MSG_SIZE];
txn = NULL;
result = TCL_OK;
flag = 0;
if (objc <= 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?-args? key data");
return (TCL_ERROR);
}
freekey = freedata = 0;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
/*
* If it is a QUEUE or RECNO database, the key is a record number
* and must be setup up to contain a db_recno_t. Otherwise the
* key is a "string".
*/
(void)dbp->get_type(dbp, &type);
/*
* We need to determine where the end of required args are. If we
* are using a QUEUE/RECNO db and -append, then there is just one
* req arg (data). Otherwise there are two (key data).
*
* We preparse the list to determine this since we need to know
* to properly check # of args for other options below.
*/
end = objc - 2;
if (type == DB_QUEUE || type == DB_RECNO) {
i = 2;
while (i < objc - 1) {
if (Tcl_GetIndexFromObj(interp, objv[i++], dbputapp,
"option", TCL_EXACT, &optindex) != TCL_OK)
continue;
switch ((enum dbputapp)optindex) {
case DBPUT_APPEND0:
end = objc - 1;
break;
}
}
}
Tcl_ResetResult(interp);
/*
* Get the command name index from the object based on the options
* defined above.
*/
i = 2;
auto_commit = 0;
while (i < end) {
if (Tcl_GetIndexFromObj(interp, objv[i],
dbputopts, "option", TCL_EXACT, &optindex) != TCL_OK)
return (IS_HELP(objv[i]));
i++;
switch ((enum dbputopts)optindex) {
#if CONFIG_TEST
case DBGET_NODUPDATA:
FLAG_CHECK(flag);
flag = DB_NODUPDATA;
break;
#endif
case DBPUT_TXN:
if (i > (end - 1)) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"Put: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
case DBPUT_AUTO_COMMIT:
auto_commit = 1;
break;
case DBPUT_APPEND:
FLAG_CHECK(flag);
flag = DB_APPEND;
break;
case DBPUT_NOOVER:
FLAG_CHECK(flag);
flag = DB_NOOVERWRITE;
break;
case DBPUT_PART:
if (i > (end - 1)) {
Tcl_WrongNumArgs(interp, 2, objv,
"?-partial {offset length}?");
result = TCL_ERROR;
break;
}
/*
* Get sublist as {offset length}
*/
result = Tcl_ListObjGetElements(interp, objv[i++],
&elemc, &elemv);
if (elemc != 2) {
Tcl_SetResult(interp,
"List must be {offset length}", TCL_STATIC);
result = TCL_ERROR;
break;
}
data.flags = DB_DBT_PARTIAL;
result = _GetUInt32(interp, elemv[0], &data.doff);
if (result != TCL_OK)
break;
result = _GetUInt32(interp, elemv[1], &data.dlen);
/*
* NOTE: We don't check result here because all we'd
* do is break anyway, and we are doing that. If you
* add code here, you WILL need to add the check
* for result. (See the check for save.doff, a few
* lines above and copy that.)
*/
break;
}
if (result != TCL_OK)
break;
}
if (auto_commit)
flag |= DB_AUTO_COMMIT;
if (result == TCL_ERROR)
return (result);
/*
* If we are a recno db and we are NOT using append, then the 2nd
* last arg is the key.
*/
if (type == DB_QUEUE || type == DB_RECNO) {
key.data = &recno;
key.ulen = key.size = sizeof(db_recno_t);
key.flags = DB_DBT_USERMEM;
if (flag == DB_APPEND)
recno = 0;
else {
result = _GetUInt32(interp, objv[objc-2], &recno);
if (result != TCL_OK)
return (result);
}
} else {
ret = _CopyObjBytes(interp, objv[objc-2], &ktmp,
&key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBPUT(ret), "db put");
return (result);
}
key.data = ktmp;
}
ret = _CopyObjBytes(interp, objv[objc-1], &dtmp,
&data.size, &freedata);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBPUT(ret), "db put");
goto out;
}
data.data = dtmp;
_debug_check();
ret = dbp->put(dbp, txn, &key, &data, flag);
result = _ReturnSetup(interp, ret, DB_RETOK_DBPUT(ret), "db put");
if (ret == 0 &&
(type == DB_RECNO || type == DB_QUEUE) && flag == DB_APPEND) {
res = Tcl_NewLongObj((long)recno);
Tcl_SetObjResult(interp, res);
}
out:
if (freedata)
(void)__os_free(dbp->dbenv, dtmp);
if (freekey)
(void)__os_free(dbp->dbenv, ktmp);
return (result);
}
/*
* tcl_db_get --
*/
static int
tcl_DbGet(interp, objc, objv, dbp, ispget)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
int ispget; /* 1 for pget, 0 for get */
{
static char *dbgetopts[] = {
#if CONFIG_TEST
"-dirty",
"-multi",
#endif
"-consume",
"-consume_wait",
"-get_both",
"-glob",
"-partial",
"-recno",
"-rmw",
"-txn",
"--",
NULL
};
enum dbgetopts {
#if CONFIG_TEST
DBGET_DIRTY,
DBGET_MULTI,
#endif
DBGET_CONSUME,
DBGET_CONSUME_WAIT,
DBGET_BOTH,
DBGET_GLOB,
DBGET_PART,
DBGET_RECNO,
DBGET_RMW,
DBGET_TXN,
DBGET_ENDARG
};
DBC *dbc;
DBT key, pkey, data, save;
DBTYPE type;
DB_TXN *txn;
Tcl_Obj **elemv, *retlist;
void *dtmp, *ktmp;
u_int32_t flag, cflag, isdup, mflag, rmw;
int bufsize, elemc, end, endarg, freekey, freedata, i;
int optindex, result, ret, useglob, useprecno, userecno;
char *arg, *pattern, *prefix, msg[MSG_SIZE];
db_recno_t precno, recno;
result = TCL_OK;
freekey = freedata = 0;
cflag = endarg = flag = mflag = rmw = 0;
useglob = userecno = useprecno = 0;
txn = NULL;
pattern = prefix = NULL;
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?-args? key");
return (TCL_ERROR);
}
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
memset(&save, 0, sizeof(save));
/* For the primary key in a pget call. */
memset(&pkey, 0, sizeof(pkey));
/*
* Get the command name index from the object based on the options
* defined above.
*/
i = 2;
(void)dbp->get_type(dbp, &type);
end = objc;
while (i < end) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbgetopts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
arg = Tcl_GetStringFromObj(objv[i], NULL);
if (arg[0] == '-') {
result = IS_HELP(objv[i]);
goto out;
} else
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbgetopts)optindex) {
#if CONFIG_TEST
case DBGET_DIRTY:
rmw |= DB_DIRTY_READ;
break;
case DBGET_MULTI:
mflag |= DB_MULTIPLE;
result = Tcl_GetIntFromObj(interp, objv[i], &bufsize);
if (result != TCL_OK)
goto out;
i++;
break;
#endif
case DBGET_BOTH:
/*
* Change 'end' and make sure we aren't already past
* the new end.
*/
if (i > objc - 2) {
Tcl_WrongNumArgs(interp, 2, objv,
"?-get_both key data?");
result = TCL_ERROR;
break;
}
end = objc - 2;
FLAG_CHECK(flag);
flag = DB_GET_BOTH;
break;
case DBGET_TXN:
if (i >= end) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"Get: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
case DBGET_GLOB:
useglob = 1;
end = objc - 1;
break;
case DBGET_CONSUME:
FLAG_CHECK(flag);
flag = DB_CONSUME;
break;
case DBGET_CONSUME_WAIT:
FLAG_CHECK(flag);
flag = DB_CONSUME_WAIT;
break;
case DBGET_RECNO:
end = objc - 1;
userecno = 1;
if (type != DB_RECNO && type != DB_QUEUE) {
FLAG_CHECK(flag);
flag = DB_SET_RECNO;
}
break;
case DBGET_RMW:
rmw |= DB_RMW;
break;
case DBGET_PART:
end = objc - 1;
if (i == end) {
Tcl_WrongNumArgs(interp, 2, objv,
"?-partial {offset length}?");
result = TCL_ERROR;
break;
}
/*
* Get sublist as {offset length}
*/
result = Tcl_ListObjGetElements(interp, objv[i++],
&elemc, &elemv);
if (elemc != 2) {
Tcl_SetResult(interp,
"List must be {offset length}", TCL_STATIC);
result = TCL_ERROR;
break;
}
save.flags = DB_DBT_PARTIAL;
result = _GetUInt32(interp, elemv[0], &save.doff);
if (result != TCL_OK)
break;
result = _GetUInt32(interp, elemv[1], &save.dlen);
/*
* NOTE: We don't check result here because all we'd
* do is break anyway, and we are doing that. If you
* add code here, you WILL need to add the check
* for result. (See the check for save.doff, a few
* lines above and copy that.)
*/
break;
case DBGET_ENDARG:
endarg = 1;
break;
} /* switch */
if (result != TCL_OK)
break;
if (endarg)
break;
}
if (result != TCL_OK)
goto out;
if (type == DB_RECNO || type == DB_QUEUE)
userecno = 1;
/*
* Check args we have left versus the flags we were given.
* We might have 0, 1 or 2 left. If we have 0, it must
* be DB_CONSUME*, if 2, then DB_GET_BOTH, all others should
* be 1.
*/
if (((flag == DB_CONSUME || flag == DB_CONSUME_WAIT) && i != objc) ||
(flag == DB_GET_BOTH && i != objc - 2)) {
Tcl_SetResult(interp,
"Wrong number of key/data given based on flags specified\n",
TCL_STATIC);
result = TCL_ERROR;
goto out;
} else if (flag == 0 && i != objc - 1) {
Tcl_SetResult(interp,
"Wrong number of key/data given\n", TCL_STATIC);
result = TCL_ERROR;
goto out;
}
/*
* XXX
* We technically shouldn't be looking inside the dbp like this,
* but this is the only way to figure out whether the primary
* key should also be a recno.
*/
if (ispget) {
if (dbp->s_primary != NULL &&
(dbp->s_primary->type == DB_RECNO ||
dbp->s_primary->type == DB_QUEUE))
useprecno = 1;
}
/*
* Check for illegal combos of options.
*/
if (useglob && (userecno || flag == DB_SET_RECNO ||
type == DB_RECNO || type == DB_QUEUE)) {
Tcl_SetResult(interp,
"Cannot use -glob and record numbers.\n",
TCL_STATIC);
result = TCL_ERROR;
goto out;
}
if (useglob && flag == DB_GET_BOTH) {
Tcl_SetResult(interp,
"Only one of -glob or -get_both can be specified.\n",
TCL_STATIC);
result = TCL_ERROR;
goto out;
}
if (useglob)
pattern = Tcl_GetStringFromObj(objv[objc - 1], NULL);
/*
* This is the list we return
*/
retlist = Tcl_NewListObj(0, NULL);
save.flags |= DB_DBT_MALLOC;
/*
* isdup is used to know if we support duplicates. If not, we
* can just do a db->get call and avoid using cursors.
* XXX
* When there is a db->get_flags method, it should be used.
* isdup = dbp->get_flags(dbp) & DB_DUP;
* For now we illegally peek.
* XXX
*/
isdup = dbp->flags & DB_AM_DUP;
/*
* If the database doesn't support duplicates or we're performing
* ops that don't require returning multiple items, use DB->get
* instead of a cursor operation.
*/
if (pattern == NULL && (isdup == 0 || mflag != 0 ||
flag == DB_SET_RECNO || flag == DB_GET_BOTH ||
flag == DB_CONSUME || flag == DB_CONSUME_WAIT)) {
if (flag == DB_GET_BOTH) {
if (userecno) {
result = _GetUInt32(interp,
objv[(objc - 2)], &recno);
if (result == TCL_OK) {
key.data = &recno;
key.size = sizeof(db_recno_t);
} else
goto out;
} else {
/*
* Some get calls (SET_*) can change the
* key pointers. So, we need to store
* the allocated key space in a tmp.
*/
ret = _CopyObjBytes(interp, objv[objc-2],
&ktmp, &key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBGET(ret), "db get");
goto out;
}
key.data = ktmp;
}
/*
* Already checked args above. Fill in key and save.
* Save is used in the dbp->get call below to fill in
* data.
*
* If the "data" here is really a primary key--that
* is, if we're in a pget--and that primary key
* is a recno, treat it appropriately as an int.
*/
if (useprecno) {
result = _GetUInt32(interp,
objv[objc - 1], &precno);
if (result == TCL_OK) {
save.data = &precno;
save.size = sizeof(db_recno_t);
} else
goto out;
} else {
ret = _CopyObjBytes(interp, objv[objc-1],
&dtmp, &save.size, &freedata);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBGET(ret), "db get");
goto out;
}
save.data = dtmp;
}
} else if (flag != DB_CONSUME && flag != DB_CONSUME_WAIT) {
if (userecno) {
result = _GetUInt32(
interp, objv[(objc - 1)], &recno);
if (result == TCL_OK) {
key.data = &recno;
key.size = sizeof(db_recno_t);
} else
goto out;
} else {
/*
* Some get calls (SET_*) can change the
* key pointers. So, we need to store
* the allocated key space in a tmp.
*/
ret = _CopyObjBytes(interp, objv[objc-1],
&ktmp, &key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBGET(ret), "db get");
goto out;
}
key.data = ktmp;
}
if (mflag & DB_MULTIPLE) {
if ((ret = __os_malloc(dbp->dbenv,
bufsize, &save.data)) != 0) {
Tcl_SetResult(interp,
db_strerror(ret), TCL_STATIC);
goto out;
}
save.ulen = bufsize;
F_CLR(&save, DB_DBT_MALLOC);
F_SET(&save, DB_DBT_USERMEM);
}
}
data = save;
if (ispget) {
if (flag == DB_GET_BOTH) {
pkey.data = save.data;
pkey.size = save.size;
data.data = NULL;
data.size = 0;
}
F_SET(&pkey, DB_DBT_MALLOC);
_debug_check();
ret = dbp->pget(dbp,
txn, &key, &pkey, &data, flag | rmw);
} else {
_debug_check();
ret = dbp->get(dbp,
txn, &key, &data, flag | rmw | mflag);
}
result = _ReturnSetup(interp, ret, DB_RETOK_DBGET(ret),
"db get");
if (ret == 0) {
/*
* Success. Return a list of the form {name value}
* If it was a recno in key.data, we need to convert
* into a string/object representation of that recno.
*/
if (mflag & DB_MULTIPLE)
result = _SetMultiList(interp,
retlist, &key, &data, type, flag);
else if (type == DB_RECNO || type == DB_QUEUE)
if (ispget)
result = _Set3DBTList(interp,
retlist, &key, 1, &pkey,
useprecno, &data);
else
result = _SetListRecnoElem(interp,
retlist, *(db_recno_t *)key.data,
data.data, data.size);
else {
if (ispget)
result = _Set3DBTList(interp,
retlist, &key, 0, &pkey,
useprecno, &data);
else
result = _SetListElem(interp, retlist,
key.data, key.size,
data.data, data.size);
}
}
/*
* Free space from DBT.
*
* If we set DB_DBT_MALLOC, we need to free the space if
* and only if we succeeded (and thus if DB allocated
* anything). If DB_DBT_MALLOC is not set, this is a bulk
* get buffer, and needs to be freed no matter what.
*/
if (F_ISSET(&data, DB_DBT_MALLOC) && ret == 0)
__os_ufree(dbp->dbenv, data.data);
else if (!F_ISSET(&data, DB_DBT_MALLOC))
__os_free(dbp->dbenv, data.data);
if (ispget && ret == 0)
__os_ufree(dbp->dbenv, pkey.data);
if (result == TCL_OK)
Tcl_SetObjResult(interp, retlist);
goto out;
}
if (userecno) {
result = _GetUInt32(interp, objv[(objc - 1)], &recno);
if (result == TCL_OK) {
key.data = &recno;
key.size = sizeof(db_recno_t);
} else
goto out;
} else {
/*
* Some get calls (SET_*) can change the
* key pointers. So, we need to store
* the allocated key space in a tmp.
*/
ret = _CopyObjBytes(interp, objv[objc-1], &ktmp,
&key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBGET(ret), "db get");
return (result);
}
key.data = ktmp;
}
ret = dbp->cursor(dbp, txn, &dbc, 0);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db cursor");
if (result == TCL_ERROR)
goto out;
/*
* At this point, we have a cursor, if we have a pattern,
* we go to the nearest one and step forward until we don't
* have any more that match the pattern prefix. If we have
* an exact key, we go to that key position, and step through
* all the duplicates. In either case we build up a list of
* the form {{key data} {key data}...} along the way.
*/
memset(&data, 0, sizeof(data));
/*
* Restore any "partial" info we have saved.
*/
data = save;
if (pattern) {
/*
* Note, prefix is returned in new space. Must free it.
*/
ret = _GetGlobPrefix(pattern, &prefix);
if (ret) {
result = TCL_ERROR;
Tcl_SetResult(interp,
"Unable to allocate pattern space", TCL_STATIC);
goto out1;
}
key.data = prefix;
key.size = strlen(prefix);
/*
* If they give us an empty pattern string
* (i.e. -glob *), go through entire DB.
*/
if (strlen(prefix) == 0)
cflag = DB_FIRST;
else
cflag = DB_SET_RANGE;
} else
cflag = DB_SET;
if (ispget) {
_debug_check();
F_SET(&pkey, DB_DBT_MALLOC);
ret = dbc->c_pget(dbc, &key, &pkey, &data, cflag | rmw);
} else {
_debug_check();
ret = dbc->c_get(dbc, &key, &data, cflag | rmw);
}
result = _ReturnSetup(interp, ret, DB_RETOK_DBCGET(ret),
"db get (cursor)");
if (result == TCL_ERROR)
goto out1;
if (ret == 0 && pattern &&
memcmp(key.data, prefix, strlen(prefix)) != 0) {
/*
* Free space from DB_DBT_MALLOC
*/
free(data.data);
goto out1;
}
if (pattern)
cflag = DB_NEXT;
else
cflag = DB_NEXT_DUP;
while (ret == 0 && result == TCL_OK) {
/*
* Build up our {name value} sublist
*/
if (ispget)
result = _Set3DBTList(interp, retlist, &key, 0,
&pkey, useprecno, &data);
else
result = _SetListElem(interp, retlist,
key.data, key.size, data.data, data.size);
/*
* Free space from DB_DBT_MALLOC
*/
if (ispget)
free(pkey.data);
free(data.data);
if (result != TCL_OK)
break;
/*
* Append {name value} to return list
*/
memset(&key, 0, sizeof(key));
memset(&pkey, 0, sizeof(pkey));
memset(&data, 0, sizeof(data));
/*
* Restore any "partial" info we have saved.
*/
data = save;
if (ispget) {
F_SET(&pkey, DB_DBT_MALLOC);
ret = dbc->c_pget(dbc, &key, &pkey, &data, cflag | rmw);
} else
ret = dbc->c_get(dbc, &key, &data, cflag | rmw);
if (ret == 0 && pattern &&
memcmp(key.data, prefix, strlen(prefix)) != 0) {
/*
* Free space from DB_DBT_MALLOC
*/
free(data.data);
break;
}
}
out1:
dbc->c_close(dbc);
if (result == TCL_OK)
Tcl_SetObjResult(interp, retlist);
out:
/*
* _GetGlobPrefix(), the function which allocates prefix, works
* by copying and condensing another string. Thus prefix may
* have multiple nuls at the end, so we free using __os_free().
*/
if (prefix != NULL)
__os_free(dbp->dbenv, prefix);
if (freedata)
(void)__os_free(dbp->dbenv, dtmp);
if (freekey)
(void)__os_free(dbp->dbenv, ktmp);
return (result);
}
/*
* tcl_db_delete --
*/
static int
tcl_DbDelete(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
static char *dbdelopts[] = {
"-auto_commit",
"-glob",
"-txn",
NULL
};
enum dbdelopts {
DBDEL_AUTO_COMMIT,
DBDEL_GLOB,
DBDEL_TXN
};
DBC *dbc;
DBT key, data;
DBTYPE type;
DB_TXN *txn;
void *ktmp;
db_recno_t recno;
int freekey, i, optindex, result, ret;
u_int32_t flag;
char *arg, *pattern, *prefix, msg[MSG_SIZE];
result = TCL_OK;
freekey = 0;
flag = 0;
pattern = prefix = NULL;
txn = NULL;
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?-args? key");
return (TCL_ERROR);
}
memset(&key, 0, sizeof(key));
/*
* The first arg must be -auto_commit, -glob, -txn or a list of keys.
*/
i = 2;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbdelopts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
/*
* If we don't have a -auto_commit, -glob or -txn,
* then the remaining args must be exact keys.
* Reset the result so we don't get an errant error
* message if there is another error.
*/
if (IS_HELP(objv[i]) == TCL_OK)
return (TCL_OK);
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbdelopts)optindex) {
case DBDEL_TXN:
if (i == objc) {
/*
* Someone could conceivably have a key of
* the same name. So just break and use it.
*/
i--;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"Delete: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
case DBDEL_AUTO_COMMIT:
flag |= DB_AUTO_COMMIT;
break;
case DBDEL_GLOB:
/*
* Get the pattern. Get the prefix and use cursors to
* get all the data items.
*/
if (i == objc) {
/*
* Someone could conceivably have a key of
* the same name. So just break and use it.
*/
i--;
break;
}
pattern = Tcl_GetStringFromObj(objv[i++], NULL);
break;
}
if (result != TCL_OK)
break;
}
if (result != TCL_OK)
goto out;
/*
* XXX
* For consistency with get, we have decided for the moment, to
* allow -glob, or one key, not many. The code was originally
* written to take many keys and we'll leave it that way, because
* tcl_DbGet may one day accept many disjoint keys to get, rather
* than one, and at that time we'd make delete be consistent. In
* any case, the code is already here and there is no need to remove,
* just check that we only have one arg left.
*
* If we have a pattern AND more keys to process, there is an error.
* Either we have some number of exact keys, or we have a pattern.
*
* If we have a pattern and an auto commit flag, there is an error.
*/
if (pattern == NULL) {
if (i != (objc - 1)) {
Tcl_WrongNumArgs(
interp, 2, objv, "?args? -glob pattern | key");
result = TCL_ERROR;
goto out;
}
} else {
if (i != objc) {
Tcl_WrongNumArgs(
interp, 2, objv, "?args? -glob pattern | key");
result = TCL_ERROR;
goto out;
}
if (flag & DB_AUTO_COMMIT) {
Tcl_SetResult(interp,
"Cannot use -auto_commit and patterns.\n",
TCL_STATIC);
result = TCL_ERROR;
goto out;
}
}
/*
* If we have remaining args, they are all exact keys. Call
* DB->del on each of those keys.
*
* If it is a RECNO database, the key is a record number and must be
* setup up to contain a db_recno_t. Otherwise the key is a "string".
*/
(void)dbp->get_type(dbp, &type);
ret = 0;
while (i < objc && ret == 0) {
memset(&key, 0, sizeof(key));
if (type == DB_RECNO || type == DB_QUEUE) {
result = _GetUInt32(interp, objv[i++], &recno);
if (result == TCL_OK) {
key.data = &recno;
key.size = sizeof(db_recno_t);
} else
return (result);
} else {
ret = _CopyObjBytes(interp, objv[i++], &ktmp,
&key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBDEL(ret), "db del");
return (result);
}
key.data = ktmp;
}
_debug_check();
ret = dbp->del(dbp, txn, &key, flag);
/*
* If we have any error, set up return result and stop
* processing keys.
*/
if (freekey)
(void)__os_free(dbp->dbenv, ktmp);
if (ret != 0)
break;
}
result = _ReturnSetup(interp, ret, DB_RETOK_DBDEL(ret), "db del");
/*
* At this point we've either finished or, if we have a pattern,
* we go to the nearest one and step forward until we don't
* have any more that match the pattern prefix.
*/
if (pattern) {
ret = dbp->cursor(dbp, txn, &dbc, 0);
if (ret != 0) {
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
"db cursor");
goto out;
}
/*
* Note, prefix is returned in new space. Must free it.
*/
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
ret = _GetGlobPrefix(pattern, &prefix);
if (ret) {
result = TCL_ERROR;
Tcl_SetResult(interp,
"Unable to allocate pattern space", TCL_STATIC);
goto out;
}
key.data = prefix;
key.size = strlen(prefix);
if (strlen(prefix) == 0)
flag = DB_FIRST;
else
flag = DB_SET_RANGE;
ret = dbc->c_get(dbc, &key, &data, flag);
while (ret == 0 &&
memcmp(key.data, prefix, strlen(prefix)) == 0) {
/*
* Each time through here the cursor is pointing
* at the current valid item. Delete it and
* move ahead.
*/
_debug_check();
ret = dbc->c_del(dbc, 0);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_DBCDEL(ret), "db c_del");
break;
}
/*
* Deleted the current, now move to the next item
* in the list, check if it matches the prefix pattern.
*/
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
ret = dbc->c_get(dbc, &key, &data, DB_NEXT);
}
if (ret == DB_NOTFOUND)
ret = 0;
/*
* _GetGlobPrefix(), the function which allocates prefix, works
* by copying and condensing another string. Thus prefix may
* have multiple nuls at the end, so we free using __os_free().
*/
__os_free(dbp->dbenv, prefix);
dbc->c_close(dbc);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db del");
}
out:
return (result);
}
/*
* tcl_db_cursor --
*/
static int
tcl_DbCursor(interp, objc, objv, dbp, dbcp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
DBC **dbcp; /* Return cursor pointer */
{
static char *dbcuropts[] = {
#if CONFIG_TEST
"-dirty",
"-update",
#endif
"-txn",
NULL
};
enum dbcuropts {
#if CONFIG_TEST
DBCUR_DIRTY,
DBCUR_UPDATE,
#endif
DBCUR_TXN
};
DB_TXN *txn;
u_int32_t flag;
int i, optindex, result, ret;
char *arg, msg[MSG_SIZE];
result = TCL_OK;
flag = 0;
txn = NULL;
i = 2;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbcuropts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
result = IS_HELP(objv[i]);
goto out;
}
i++;
switch ((enum dbcuropts)optindex) {
#if CONFIG_TEST
case DBCUR_DIRTY:
flag |= DB_DIRTY_READ;
break;
case DBCUR_UPDATE:
flag |= DB_WRITECURSOR;
break;
#endif
case DBCUR_TXN:
if (i == objc) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"Cursor: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
}
if (result != TCL_OK)
break;
}
if (result != TCL_OK)
goto out;
_debug_check();
ret = dbp->cursor(dbp, txn, dbcp, flag);
if (ret != 0)
result = _ErrorSetup(interp, ret, "db cursor");
out:
return (result);
}
/*
* tcl_DbAssociate --
* Call DB->associate().
*/
static int
tcl_DbAssociate(interp, objc, objv, dbp)
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
DB *dbp;
{
static char *dbaopts[] = {
"-auto_commit",
"-create",
"-txn",
NULL
};
enum dbaopts {
DBA_AUTO_COMMIT,
DBA_CREATE,
DBA_TXN
};
DB *sdbp;
DB_TXN *txn;
DBTCL_INFO *sdbip;
int i, optindex, result, ret;
char *arg, msg[MSG_SIZE];
u_int32_t flag;
txn = NULL;
result = TCL_OK;
flag = 0;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 2, objv, "[callback] secondary");
return (TCL_ERROR);
}
i = 2;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbaopts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
result = IS_HELP(objv[i]);
if (result == TCL_OK)
return (result);
result = TCL_OK;
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbaopts)optindex) {
case DBA_AUTO_COMMIT:
flag |= DB_AUTO_COMMIT;
break;
case DBA_CREATE:
flag |= DB_CREATE;
break;
case DBA_TXN:
if (i > (objc - 1)) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"Associate: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
}
}
if (result != TCL_OK)
return (result);
/*
* Better be 1 or 2 args left. The last arg must be the sdb
* handle. If 2 args then objc-2 is the callback proc, else
* we have a NULL callback.
*/
/* Get the secondary DB handle. */
arg = Tcl_GetStringFromObj(objv[objc - 1], NULL);
sdbp = NAME_TO_DB(arg);
if (sdbp == NULL) {
snprintf(msg, MSG_SIZE,
"Associate: Invalid database handle: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
return (TCL_ERROR);
}
/*
* The callback is simply a Tcl object containing the name
* of the callback proc, which is the second-to-last argument.
*
* Note that the callback needs to go in the *secondary* DB handle's
* info struct; we may have multiple secondaries with different
* callbacks.
*/
sdbip = (DBTCL_INFO *)sdbp->api_internal;
if (i != objc - 1) {
/*
* We have 2 args, get the callback.
*/
sdbip->i_second_call = objv[objc - 2];
Tcl_IncrRefCount(sdbip->i_second_call);
/* Now call associate. */
_debug_check();
ret = dbp->associate(dbp, txn, sdbp, tcl_second_call, flag);
} else {
/*
* We have a NULL callback.
*/
sdbip->i_second_call = NULL;
ret = dbp->associate(dbp, txn, sdbp, NULL, flag);
}
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "associate");
return (result);
}
/*
* tcl_second_call --
* Callback function for secondary indices. Get the callback
* out of ip->i_second_call and call it.
*/
static int
tcl_second_call(dbp, pkey, data, skey)
DB *dbp;
const DBT *pkey, *data;
DBT *skey;
{
DBTCL_INFO *ip;
Tcl_Interp *interp;
Tcl_Obj *pobj, *dobj, *objv[3];
int len, result, ret;
void *retbuf, *databuf;
ip = (DBTCL_INFO *)dbp->api_internal;
interp = ip->i_interp;
objv[0] = ip->i_second_call;
/*
* Create two ByteArray objects, with the contents of the pkey
* and data DBTs that are our inputs.
*/
pobj = Tcl_NewByteArrayObj(pkey->data, pkey->size);
Tcl_IncrRefCount(pobj);
dobj = Tcl_NewByteArrayObj(data->data, data->size);
Tcl_IncrRefCount(dobj);
objv[1] = pobj;
objv[2] = dobj;
result = Tcl_EvalObjv(interp, 3, objv, 0);
Tcl_DecrRefCount(pobj);
Tcl_DecrRefCount(dobj);
if (result != TCL_OK) {
__db_err(dbp->dbenv,
"Tcl callback function failed with code %d", result);
return (EINVAL);
}
retbuf =
Tcl_GetByteArrayFromObj(Tcl_GetObjResult(interp), &len);
/*
* retbuf is owned by Tcl; copy it into malloc'ed memory.
* We need to use __os_umalloc rather than ufree because this will
* be freed by DB using __os_ufree--the DB_DBT_APPMALLOC flag
* tells DB to free application-allocated memory.
*/
if ((ret = __os_umalloc(dbp->dbenv, len, &databuf)) != 0)
return (ret);
memcpy(databuf, retbuf, len);
skey->data = databuf;
skey->size = len;
F_SET(skey, DB_DBT_APPMALLOC);
return (0);
}
/*
* tcl_db_join --
*/
static int
tcl_DbJoin(interp, objc, objv, dbp, dbcp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
DBC **dbcp; /* Cursor pointer */
{
static char *dbjopts[] = {
"-nosort",
NULL
};
enum dbjopts {
DBJ_NOSORT
};
DBC **listp;
u_int32_t flag;
int adj, i, j, optindex, size, result, ret;
char *arg, msg[MSG_SIZE];
result = TCL_OK;
flag = 0;
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "curs1 curs2 ...");
return (TCL_ERROR);
}
i = 2;
adj = i;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbjopts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
result = IS_HELP(objv[i]);
if (result == TCL_OK)
return (result);
result = TCL_OK;
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbjopts)optindex) {
case DBJ_NOSORT:
flag |= DB_JOIN_NOSORT;
adj++;
break;
}
}
if (result != TCL_OK)
return (result);
/*
* Allocate one more for NULL ptr at end of list.
*/
size = sizeof(DBC *) * ((objc - adj) + 1);
ret = __os_malloc(dbp->dbenv, size, &listp);
if (ret != 0) {
Tcl_SetResult(interp, db_strerror(ret), TCL_STATIC);
return (TCL_ERROR);
}
memset(listp, 0, size);
for (j = 0, i = adj; i < objc; i++, j++) {
arg = Tcl_GetStringFromObj(objv[i], NULL);
listp[j] = NAME_TO_DBC(arg);
if (listp[j] == NULL) {
snprintf(msg, MSG_SIZE,
"Join: Invalid cursor: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
goto out;
}
}
listp[j] = NULL;
_debug_check();
ret = dbp->join(dbp, listp, dbcp, flag);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db join");
out:
__os_free(dbp->dbenv, listp);
return (result);
}
/*
* tcl_db_getjoin --
*/
static int
tcl_DbGetjoin(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
static char *dbgetjopts[] = {
#if CONFIG_TEST
"-nosort",
#endif
"-txn",
NULL
};
enum dbgetjopts {
#if CONFIG_TEST
DBGETJ_NOSORT,
#endif
DBGETJ_TXN
};
DB_TXN *txn;
DB *elemdbp;
DBC **listp;
DBC *dbc;
DBT key, data;
Tcl_Obj **elemv, *retlist;
void *ktmp;
u_int32_t flag;
int adj, elemc, freekey, i, j, optindex, result, ret, size;
char *arg, msg[MSG_SIZE];
result = TCL_OK;
flag = 0;
freekey = 0;
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "{db1 key1} {db2 key2} ...");
return (TCL_ERROR);
}
txn = NULL;
i = 2;
adj = i;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbgetjopts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
result = IS_HELP(objv[i]);
if (result == TCL_OK)
return (result);
result = TCL_OK;
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbgetjopts)optindex) {
#if CONFIG_TEST
case DBGETJ_NOSORT:
flag |= DB_JOIN_NOSORT;
adj++;
break;
#endif
case DBGETJ_TXN:
if (i == objc) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
adj += 2;
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"GetJoin: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
}
}
if (result != TCL_OK)
return (result);
size = sizeof(DBC *) * ((objc - adj) + 1);
ret = __os_malloc(NULL, size, &listp);
if (ret != 0) {
Tcl_SetResult(interp, db_strerror(ret), TCL_STATIC);
return (TCL_ERROR);
}
memset(listp, 0, size);
for (j = 0, i = adj; i < objc; i++, j++) {
/*
* Get each sublist as {db key}
*/
result = Tcl_ListObjGetElements(interp, objv[i],
&elemc, &elemv);
if (elemc != 2) {
Tcl_SetResult(interp, "Lists must be {db key}",
TCL_STATIC);
result = TCL_ERROR;
goto out;
}
/*
* Get a pointer to that open db. Then, open a cursor in
* that db, and go to the "key" place.
*/
elemdbp = NAME_TO_DB(Tcl_GetStringFromObj(elemv[0], NULL));
if (elemdbp == NULL) {
snprintf(msg, MSG_SIZE, "Get_join: Invalid db: %s\n",
Tcl_GetStringFromObj(elemv[0], NULL));
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
goto out;
}
ret = elemdbp->cursor(elemdbp, txn, &listp[j], 0);
if ((result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
"db cursor")) == TCL_ERROR)
goto out;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
ret = _CopyObjBytes(interp, elemv[elemc-1], &ktmp,
&key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_STD(ret), "db join");
goto out;
}
key.data = ktmp;
ret = (listp[j])->c_get(listp[j], &key, &data, DB_SET);
if ((result = _ReturnSetup(interp, ret, DB_RETOK_DBCGET(ret),
"db cget")) == TCL_ERROR)
goto out;
}
listp[j] = NULL;
_debug_check();
ret = dbp->join(dbp, listp, &dbc, flag);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db join");
if (result == TCL_ERROR)
goto out;
retlist = Tcl_NewListObj(0, NULL);
while (ret == 0 && result == TCL_OK) {
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.flags |= DB_DBT_MALLOC;
data.flags |= DB_DBT_MALLOC;
ret = dbc->c_get(dbc, &key, &data, 0);
/*
* Build up our {name value} sublist
*/
if (ret == 0) {
result = _SetListElem(interp, retlist,
key.data, key.size,
data.data, data.size);
free(key.data);
free(data.data);
}
}
dbc->c_close(dbc);
if (result == TCL_OK)
Tcl_SetObjResult(interp, retlist);
out:
if (freekey)
(void)__os_free(dbp->dbenv, ktmp);
while (j) {
if (listp[j])
(listp[j])->c_close(listp[j]);
j--;
}
__os_free(dbp->dbenv, listp);
return (result);
}
/*
* tcl_DbCount --
*/
static int
tcl_DbCount(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
Tcl_Obj *res;
DBC *dbc;
DBT key, data;
void *ktmp;
db_recno_t count, recno;
int freekey, result, ret;
result = TCL_OK;
count = 0;
freekey = 0;
res = NULL;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "key");
return (TCL_ERROR);
}
memset(&key, 0, sizeof(key));
/*
* Get the count for our key.
* We do this by getting a cursor for this DB. Moving the cursor
* to the set location, and getting a count on that cursor.
*/
ret = 0;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
/*
* If it's a queue or recno database, we must make sure to
* treat the key as a recno rather than as a byte string.
*/
if (dbp->type == DB_RECNO || dbp->type == DB_QUEUE) {
result = _GetUInt32(interp, objv[2], &recno);
if (result == TCL_OK) {
key.data = &recno;
key.size = sizeof(db_recno_t);
} else
return (result);
} else {
ret = _CopyObjBytes(interp, objv[2], &ktmp,
&key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_STD(ret), "db count");
return (result);
}
key.data = ktmp;
}
_debug_check();
ret = dbp->cursor(dbp, NULL, &dbc, 0);
if (ret != 0) {
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
"db cursor");
goto out;
}
/*
* Move our cursor to the key.
*/
ret = dbc->c_get(dbc, &key, &data, DB_SET);
if (ret == DB_NOTFOUND)
count = 0;
else {
ret = dbc->c_count(dbc, &count, 0);
if (ret != 0) {
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
"db c count");
goto out;
}
}
res = Tcl_NewLongObj((long)count);
Tcl_SetObjResult(interp, res);
out:
if (freekey)
(void)__os_free(dbp->dbenv, ktmp);
(void)dbc->c_close(dbc);
return (result);
}
#if CONFIG_TEST
/*
* tcl_DbKeyRange --
*/
static int
tcl_DbKeyRange(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
static char *dbkeyropts[] = {
"-txn",
NULL
};
enum dbkeyropts {
DBKEYR_TXN
};
DB_TXN *txn;
DB_KEY_RANGE range;
DBT key;
DBTYPE type;
Tcl_Obj *myobjv[3], *retlist;
void *ktmp;
db_recno_t recno;
u_int32_t flag;
int freekey, i, myobjc, optindex, result, ret;
char *arg, msg[MSG_SIZE];
result = TCL_OK;
flag = 0;
freekey = 0;
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id? key");
return (TCL_ERROR);
}
txn = NULL;
i = 2;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbkeyropts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
result = IS_HELP(objv[i]);
if (result == TCL_OK)
return (result);
result = TCL_OK;
Tcl_ResetResult(interp);
break;
}
i++;
switch ((enum dbkeyropts)optindex) {
case DBKEYR_TXN:
if (i == objc) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"KeyRange: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
}
}
if (result != TCL_OK)
return (result);
(void)dbp->get_type(dbp, &type);
ret = 0;
/*
* Make sure we have a key.
*/
if (i != (objc - 1)) {
Tcl_WrongNumArgs(interp, 2, objv, "?args? key");
result = TCL_ERROR;
goto out;
}
memset(&key, 0, sizeof(key));
if (type == DB_RECNO || type == DB_QUEUE) {
result = _GetUInt32(interp, objv[i], &recno);
if (result == TCL_OK) {
key.data = &recno;
key.size = sizeof(db_recno_t);
} else
return (result);
} else {
ret = _CopyObjBytes(interp, objv[i++], &ktmp,
&key.size, &freekey);
if (ret != 0) {
result = _ReturnSetup(interp, ret,
DB_RETOK_STD(ret), "db keyrange");
return (result);
}
key.data = ktmp;
}
_debug_check();
ret = dbp->key_range(dbp, txn, &key, &range, flag);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db keyrange");
if (result == TCL_ERROR)
goto out;
/*
* If we succeeded, set up return list.
*/
myobjc = 3;
myobjv[0] = Tcl_NewDoubleObj(range.less);
myobjv[1] = Tcl_NewDoubleObj(range.equal);
myobjv[2] = Tcl_NewDoubleObj(range.greater);
retlist = Tcl_NewListObj(myobjc, myobjv);
if (result == TCL_OK)
Tcl_SetObjResult(interp, retlist);
out:
if (freekey)
(void)__os_free(dbp->dbenv, ktmp);
return (result);
}
#endif
/*
* tcl_DbTruncate --
*/
static int
tcl_DbTruncate(interp, objc, objv, dbp)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB *dbp; /* Database pointer */
{
static char *dbcuropts[] = {
"-auto_commit",
"-txn",
NULL
};
enum dbcuropts {
DBTRUNC_AUTO_COMMIT,
DBTRUNC_TXN
};
DB_TXN *txn;
Tcl_Obj *res;
u_int32_t count, flag;
int i, optindex, result, ret;
char *arg, msg[MSG_SIZE];
txn = NULL;
flag = 0;
result = TCL_OK;
i = 2;
while (i < objc) {
if (Tcl_GetIndexFromObj(interp, objv[i], dbcuropts, "option",
TCL_EXACT, &optindex) != TCL_OK) {
result = IS_HELP(objv[i]);
goto out;
}
i++;
switch ((enum dbcuropts)optindex) {
case DBTRUNC_AUTO_COMMIT:
flag |= DB_AUTO_COMMIT;
break;
case DBTRUNC_TXN:
if (i == objc) {
Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
result = TCL_ERROR;
break;
}
arg = Tcl_GetStringFromObj(objv[i++], NULL);
txn = NAME_TO_TXN(arg);
if (txn == NULL) {
snprintf(msg, MSG_SIZE,
"Truncate: Invalid txn: %s\n", arg);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
}
break;
}
if (result != TCL_OK)
break;
}
if (result != TCL_OK)
goto out;
_debug_check();
ret = dbp->truncate(dbp, txn, &count, flag);
if (ret != 0)
result = _ErrorSetup(interp, ret, "db truncate");
else {
res = Tcl_NewLongObj((long)count);
Tcl_SetObjResult(interp, res);
}
out:
return (result);
}
|