Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Split almost 1300 lines of code out of vdbe.c into separate files vdbeInt.h and vdbeaux.c. (CVS 1094) |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
bfd69391d3d63675f206ffd8ff0401ea |
User & Date: | drh 2003-09-06 20:12:01 |
Context
2003-09-06
| ||
22:18 | Update Makefile.in for the new vdbeaux.c file. Remove the experimental "sqlite_instantiate()" routine and replace it with "sqlite_bind()" which is more like ODBC and JDBC. (CVS 1095) check-in: 990bb118 user: drh tags: trunk | |
20:12 | Split almost 1300 lines of code out of vdbe.c into separate files vdbeInt.h and vdbeaux.c. (CVS 1094) check-in: bfd69391 user: drh tags: trunk | |
01:10 | The beginnings of changes to support pre-compiled SQL. Mostly untested, though all regression tests to pass. (CVS 1093) check-in: 912f47c7 user: drh tags: trunk | |
Changes
Changes to main.mk.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 .. 93 94 95 96 97 98 99 100 101 102 103 104 105 106 ... 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 ... 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 ... 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# Object files for the SQLite library. # LIBOBJ = attach.o auth.o btree.o btree_rb.o build.o copy.o delete.o \ expr.o func.o hash.o insert.o \ main.o opcodes.o os.o pager.o parse.o pragma.o printf.o random.o \ select.o table.o tokenize.o trigger.o update.o util.o \ vacuum.o vdbe.o where.o tclsqlite.o # All of the source code files. # SRC = \ $(TOP)/src/attach.c \ $(TOP)/src/auth.c \ $(TOP)/src/btree.c \ ................................................................................ $(TOP)/src/tokenize.c \ $(TOP)/src/trigger.c \ $(TOP)/src/update.c \ $(TOP)/src/util.c \ $(TOP)/src/vacuum.c \ $(TOP)/src/vdbe.c \ $(TOP)/src/vdbe.h \ $(TOP)/src/where.c # Source code to the test files. # TESTSRC = \ $(TOP)/src/btree.c \ $(TOP)/src/func.c \ ................................................................................ sqlite.h \ $(TOP)/src/btree.h \ config.h \ $(TOP)/src/hash.h \ opcodes.h \ $(TOP)/src/os.h \ $(TOP)/src/sqliteInt.h \ $(TOP)/src/vdbe.h \ parse.h # This is the default Makefile target. The objects listed here # are what get build when you type just "make" with no arguments. # all: sqlite.h config.h libsqlite.a sqlite$(EXE) # Generate the file "last_change" which contains the date of change ................................................................................ # This target creates a directory named "tsrc" and fills it with # copies of all of the C source code and header files needed to # build on the target system. Some of the C source code and header # files are automatically generated. This target takes care of # all that automatic generation. # target_source: $(SRC) $(HDR) opcodes.c rm -rf tsrc mkdir tsrc cp $(SRC) $(HDR) tsrc rm tsrc/sqlite.h.in tsrc/parse.y cp parse.c opcodes.c tsrc # Rules to build the LEMON compiler generator # lemon: $(TOP)/tool/lemon.c $(TOP)/tool/lempar.c $(BCC) -o lemon $(TOP)/tool/lemon.c ................................................................................ util.o: $(TOP)/src/util.c $(HDR) $(TCCX) -c $(TOP)/src/util.c vacuum.o: $(TOP)/src/vacuum.c $(HDR) $(TCCX) -c $(TOP)/src/vacuum.c vdbe.o: $(TOP)/src/vdbe.c $(HDR) $(TCCX) -c $(TOP)/src/vdbe.c where.o: $(TOP)/src/where.c $(HDR) $(TCCX) -c $(TOP)/src/where.c copy.o: $(TOP)/src/copy.c $(HDR) $(TCCX) -c $(TOP)/src/copy.c |
| > > | > > > > > > | | | > > > |
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 .. 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 ... 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 ... 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 ... 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# Object files for the SQLite library. # LIBOBJ = attach.o auth.o btree.o btree_rb.o build.o copy.o delete.o \ expr.o func.o hash.o insert.o \ main.o opcodes.o os.o pager.o parse.o pragma.o printf.o random.o \ select.o table.o tokenize.o trigger.o update.o util.o \ vacuum.o vdbe.o vdbeaux.o where.o tclsqlite.o # All of the source code files. # SRC = \ $(TOP)/src/attach.c \ $(TOP)/src/auth.c \ $(TOP)/src/btree.c \ ................................................................................ $(TOP)/src/tokenize.c \ $(TOP)/src/trigger.c \ $(TOP)/src/update.c \ $(TOP)/src/util.c \ $(TOP)/src/vacuum.c \ $(TOP)/src/vdbe.c \ $(TOP)/src/vdbe.h \ $(TOP)/src/vdbeaux.c \ $(TOP)/src/vdbeInt.h \ $(TOP)/src/where.c # Source code to the test files. # TESTSRC = \ $(TOP)/src/btree.c \ $(TOP)/src/func.c \ ................................................................................ sqlite.h \ $(TOP)/src/btree.h \ config.h \ $(TOP)/src/hash.h \ opcodes.h \ $(TOP)/src/os.h \ $(TOP)/src/sqliteInt.h \ $(TOP)/src/vdbe.h \ parse.h # Header files used by the VDBE submodule # VDBEHDR = \ $(HDR) \ $(TOP)/src/vdbeInt.h # This is the default Makefile target. The objects listed here # are what get build when you type just "make" with no arguments. # all: sqlite.h config.h libsqlite.a sqlite$(EXE) # Generate the file "last_change" which contains the date of change ................................................................................ # This target creates a directory named "tsrc" and fills it with # copies of all of the C source code and header files needed to # build on the target system. Some of the C source code and header # files are automatically generated. This target takes care of # all that automatic generation. # target_source: $(SRC) $(VDBEHDR) opcodes.c rm -rf tsrc mkdir tsrc cp $(SRC) $(VDBEHDR) tsrc rm tsrc/sqlite.h.in tsrc/parse.y cp parse.c opcodes.c tsrc # Rules to build the LEMON compiler generator # lemon: $(TOP)/tool/lemon.c $(TOP)/tool/lempar.c $(BCC) -o lemon $(TOP)/tool/lemon.c ................................................................................ util.o: $(TOP)/src/util.c $(HDR) $(TCCX) -c $(TOP)/src/util.c vacuum.o: $(TOP)/src/vacuum.c $(HDR) $(TCCX) -c $(TOP)/src/vacuum.c vdbe.o: $(TOP)/src/vdbe.c $(VDBEHDR) $(TCCX) -c $(TOP)/src/vdbe.c vdbeaux.o: $(TOP)/src/vdbeaux.c $(VDBEHDR) $(TCCX) -c $(TOP)/src/vdbeaux.c where.o: $(TOP)/src/where.c $(HDR) $(TCCX) -c $(TOP)/src/where.c copy.o: $(TOP)/src/copy.c $(HDR) $(TCCX) -c $(TOP)/src/copy.c |
Changes to src/vdbe.c.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 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 ... 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 .... 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 .... 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 .... 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 .... 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 .... 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 .... 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 .... 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 .... 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 .... 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 .... 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 .... 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 .... 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 .... 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 .... 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 .... 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 .... 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 .... 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 .... 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 .... 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 .... 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 .... 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 .... 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 .... 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 .... 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 .... 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 .... 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 |
** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** The code in this file implements the Virtual Database Engine (VDBE) ** ** The SQL parser generates a program which is then executed by ** the VDBE to do the work of the SQL statement. VDBE programs are ** similar in form to assembly language. The program consists of ** a linear sequence of operations. Each operation has an opcode ** and 3 operands. Operands P1 and P2 are integers. Operand P3 ** is a null-terminated string. The P2 operand must be non-negative. ................................................................................ ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** ** $Id: vdbe.c,v 1.238 2003/09/06 01:10:48 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> /* ** The makefile scans this source file and creates the following ** array of string constants which are the names of all VDBE opcodes. ** This array is defined in a separate source code file named opcode.c ** which is automatically generated by the makefile. */ extern char *sqliteOpcodeNames[]; /* ** The following global variable is incremented every time a cursor ** moves, either by the OP_MoveTo or the OP_Next opcode. The test ** procedures use this information to make sure that indices are ** working correctly. This variable has no function other than to ** help verify the correct operation of the library. */ int sqlite_search_count = 0; /* ** SQL is translated into a sequence of instructions to be ** executed by a virtual machine. Each instruction is an instance ** of the following structure. */ typedef struct VdbeOp Op; /* ** Boolean values */ typedef unsigned char Bool; /* ** A cursor is a pointer into a single BTree within a database file. ** The cursor can seek to a BTree entry with a particular key, or ** loop over all entries of the Btree. You can also insert new BTree ** entries or retrieve the key or data from the entry that the cursor ** is currently pointing to. ** ** Every cursor that the virtual machine has open is represented by an ** instance of the following structure. ** ** If the Cursor.isTriggerRow flag is set it means that this cursor is ** really a single row that represents the NEW or OLD pseudo-table of ** a row trigger. The data for the row is stored in Cursor.pData and ** the rowid is in Cursor.iKey. */ struct Cursor { BtCursor *pCursor; /* The cursor structure of the backend */ int lastRecno; /* Last recno from a Next or NextIdx operation */ int nextRowid; /* Next rowid returned by OP_NewRowid */ Bool recnoIsValid; /* True if lastRecno is valid */ Bool keyAsData; /* The OP_Column command works on key instead of data */ Bool atFirst; /* True if pointing to first entry */ Bool useRandomRowid; /* Generate new record numbers semi-randomly */ Bool nullRow; /* True if pointing to a row with no data */ Bool nextRowidValid; /* True if the nextRowid field is valid */ Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */ Btree *pBt; /* Separate file holding temporary table */ int nData; /* Number of bytes in pData */ char *pData; /* Data for a NEW or OLD pseudo-table */ int iKey; /* Key for the NEW or OLD pseudo-table row */ }; typedef struct Cursor Cursor; /* ** A sorter builds a list of elements to be sorted. Each element of ** the list is an instance of the following structure. */ typedef struct Sorter Sorter; struct Sorter { int nKey; /* Number of bytes in the key */ char *zKey; /* The key by which we will sort */ int nData; /* Number of bytes in the data */ char *pData; /* The data associated with this key */ Sorter *pNext; /* Next in the list */ }; /* ** Number of buckets used for merge-sort. */ #define NSORT 30 /* ** Number of bytes of string storage space available to each stack ** layer without having to malloc. NBFS is short for Number of Bytes ** For Strings. */ #define NBFS 32 /* ** A single level of the stack is an instance of the following ** structure. Except, string values are stored on a separate ** list of of pointers to character. The reason for storing ** strings separately is so that they can be easily passed ** to the callback function. */ struct Stack { int i; /* Integer value */ int n; /* Number of characters in string value, including '\0' */ int flags; /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */ double r; /* Real value */ char z[NBFS]; /* Space for short strings */ }; typedef struct Stack Stack; /* ** Memory cells use the same structure as the stack except that space ** for an arbitrary string is added. */ struct Mem { Stack s; /* All values of the memory cell besides string */ char *z; /* String value for this memory cell */ }; typedef struct Mem Mem; /* ** Allowed values for Stack.flags */ #define STK_Null 0x0001 /* Value is NULL */ #define STK_Str 0x0002 /* Value is a string */ #define STK_Int 0x0004 /* Value is an integer */ #define STK_Real 0x0008 /* Value is a real number */ #define STK_Dyn 0x0010 /* Need to call sqliteFree() on zStack[] */ #define STK_Static 0x0020 /* zStack[] points to a static string */ #define STK_Ephem 0x0040 /* zStack[] points to an ephemeral string */ /* The following STK_ value appears only in AggElem.aMem.s.flag fields. ** It indicates that the corresponding AggElem.aMem.z points to a ** aggregate function context that needs to be finalized. */ #define STK_AggCtx 0x0040 /* zStack[] points to an agg function context */ /* ** The "context" argument for a installable function. A pointer to an ** instance of this structure is the first argument to the routines used ** implement the SQL functions. ** ** There is a typedef for this structure in sqlite.h. So all routines, ** even the public interface to SQLite, can use a pointer to this structure. ** But this file is the only place where the internal details of this ** structure are known. ** ** This structure is defined inside of vdbe.c because it uses substructures ** (Stack) which are only defined there. */ struct sqlite_func { FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ Stack s; /* Small strings, ints, and double values go here */ char *z; /* Space for holding dynamic string results */ void *pAgg; /* Aggregate context */ u8 isError; /* Set to true for an error */ u8 isStep; /* Current in the step function */ int cnt; /* Number of times that the step function has been called */ }; /* ** An Agg structure describes an Aggregator. Each Agg consists of ** zero or more Aggregator elements (AggElem). Each AggElem contains ** a key and one or more values. The values are used in processing ** aggregate functions in a SELECT. The key is used to implement ** the GROUP BY clause of a select. */ typedef struct Agg Agg; typedef struct AggElem AggElem; struct Agg { int nMem; /* Number of values stored in each AggElem */ AggElem *pCurrent; /* The AggElem currently in focus */ HashElem *pSearch; /* The hash element for pCurrent */ Hash hash; /* Hash table of all aggregate elements */ FuncDef **apFunc; /* Information about aggregate functions */ }; struct AggElem { char *zKey; /* The key to this AggElem */ int nKey; /* Number of bytes in the key, including '\0' at end */ Mem aMem[1]; /* The values for this AggElem */ }; /* ** A Set structure is used for quick testing to see if a value ** is part of a small set. Sets are used to implement code like ** this: ** x.y IN ('hi','hoo','hum') */ typedef struct Set Set; struct Set { Hash hash; /* A set is just a hash table */ HashElem *prev; /* Previously accessed hash elemen */ }; /* ** A Keylist is a bunch of keys into a table. The keylist can ** grow without bound. The keylist stores the ROWIDs of database ** records that need to be deleted or updated. */ typedef struct Keylist Keylist; struct Keylist { int nKey; /* Number of slots in aKey[] */ int nUsed; /* Next unwritten slot in aKey[] */ int nRead; /* Next unread slot in aKey[] */ Keylist *pNext; /* Next block of keys */ int aKey[1]; /* One or more keys. Extra space allocated as needed */ }; /* ** An instance of the virtual machine. This structure contains the complete ** state of the virtual machine. ** ** The "sqlite_vm" structure pointer that is returned by sqlite_compile() ** is really a pointer to an instance of this structure. */ struct Vdbe { sqlite *db; /* The whole database */ Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ FILE *trace; /* Write an execution trace here, if not NULL */ int nOp; /* Number of instructions in the program */ int nOpAlloc; /* Number of slots allocated for aOp[] */ Op *aOp; /* Space to hold the virtual machine's program */ int nLabel; /* Number of labels used */ int nLabelAlloc; /* Number of slots allocated in aLabel[] */ int *aLabel; /* Space to hold the labels */ int tos; /* Index of top of stack */ Stack *aStack; /* The operand stack, except string values */ char **zStack; /* Text or binary values of the stack */ char **azColName; /* Becomes the 4th parameter to callbacks */ int nCursor; /* Number of slots in aCsr[] */ Cursor *aCsr; /* One element of this array for each open cursor */ Sorter *pSort; /* A linked list of objects to be sorted */ FILE *pFile; /* At most one open file handler */ int nField; /* Number of file fields */ char **azField; /* Data for each file field */ int nVariable; /* Number of entries in azVariable[] */ char **azVariable; /* Values for the OP_Variable opcode */ char *zLine; /* A single line from the input file */ int nLineAlloc; /* Number of spaces allocated for zLine */ int magic; /* Magic number for sanity checking */ int nMem; /* Number of memory locations currently allocated */ Mem *aMem; /* The memory locations */ Agg agg; /* Aggregate information */ int nSet; /* Number of sets allocated */ Set *aSet; /* An array of sets */ int nCallback; /* Number of callbacks invoked so far */ Keylist *pList; /* A list of ROWIDs */ int keylistStackDepth; /* The size of the "keylist" stack */ Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */ int pc; /* The program counter */ int rc; /* Value to return */ unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */ int errorAction; /* Recovery action to do in case of an error */ int undoTransOnError; /* If error, either ROLLBACK or COMMIT */ int inTempTrans; /* True if temp database is transactioned */ int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */ int returnDepth; /* Next unused element in returnStack[] */ int nResColumn; /* Number of columns in one row of the result set */ char **azResColumn; /* Values for one row of result */ int (*xCallback)(void*,int,char**,char**); /* Callback for SELECT results */ void *pCbArg; /* First argument to xCallback() */ int popStack; /* Pop the stack this much on entry to VdbeExec() */ char *zErrMsg; /* Error message written here */ u8 explain; /* True if EXPLAIN present on SQL command */ }; /* ** The following are allowed values for Vdbe.magic */ #define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ #define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ #define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ /* ** When debugging the code generator in a symbolic debugger, one can ** set the sqlite_vdbe_addop_trace to 1 and all opcodes will be printed ** as they are added to the instruction stream. */ #ifndef NDEBUG int sqlite_vdbe_addop_trace = 0; static void vdbePrintOp(FILE*, int, Op*); #endif /* ** Create a new virtual database engine. */ Vdbe *sqliteVdbeCreate(sqlite *db){ Vdbe *p; p = sqliteMalloc( sizeof(Vdbe) ); if( p==0 ) return 0; p->db = db; if( db->pVdbe ){ db->pVdbe->pPrev = p; } p->pNext = db->pVdbe; p->pPrev = 0; db->pVdbe = p; p->magic = VDBE_MAGIC_INIT; return p; } /* ** Turn tracing on or off */ void sqliteVdbeTrace(Vdbe *p, FILE *trace){ p->trace = trace; } /* ** Add a new instruction to the list of instructions current in the ** VDBE. Return the address of the new instruction. ** ** Parameters: ** ** p Pointer to the VDBE ** ** op The opcode for this instruction ** ** p1, p2 First two of the three possible operands. ** ** Use the sqliteVdbeResolveLabel() function to fix an address and ** the sqliteVdbeChangeP3() function to change the value of the P3 ** operand. */ int sqliteVdbeAddOp(Vdbe *p, int op, int p1, int p2){ int i; i = p->nOp; p->nOp++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nOpAlloc ){ int oldSize = p->nOpAlloc; Op *aNew; p->nOpAlloc = p->nOpAlloc*2 + 100; aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); if( aNew==0 ){ p->nOpAlloc = oldSize; return 0; } p->aOp = aNew; memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); } p->aOp[i].opcode = op; p->aOp[i].p1 = p1; if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){ p2 = p->aLabel[-1-p2]; } p->aOp[i].p2 = p2; p->aOp[i].p3 = 0; p->aOp[i].p3type = P3_NOTUSED; #ifndef NDEBUG if( sqlite_vdbe_addop_trace ) vdbePrintOp(0, i, &p->aOp[i]); #endif return i; } /* ** Create a new symbolic label for an instruction that has yet to be ** coded. The symbolic label is really just a negative number. The ** label can be used as the P2 value of an operation. Later, when ** the label is resolved to a specific address, the VDBE will scan ** through its operation list and change all values of P2 which match ** the label into the resolved address. ** ** The VDBE knows that a P2 value is a label because labels are ** always negative and P2 values are suppose to be non-negative. ** Hence, a negative P2 value is a label that has yet to be resolved. */ int sqliteVdbeMakeLabel(Vdbe *p){ int i; i = p->nLabel++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nLabelAlloc ){ int *aNew; p->nLabelAlloc = p->nLabelAlloc*2 + 10; aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); if( aNew==0 ){ sqliteFree(p->aLabel); } p->aLabel = aNew; } if( p->aLabel==0 ){ p->nLabel = 0; p->nLabelAlloc = 0; return 0; } p->aLabel[i] = -1; return -1-i; } /* ** Resolve label "x" to be the address of the next instruction to ** be inserted. The parameter "x" must have been obtained from ** a prior call to sqliteVdbeMakeLabel(). */ void sqliteVdbeResolveLabel(Vdbe *p, int x){ int j; assert( p->magic==VDBE_MAGIC_INIT ); if( x<0 && (-x)<=p->nLabel && p->aOp ){ if( p->aLabel[-1-x]==p->nOp ) return; assert( p->aLabel[-1-x]<0 ); p->aLabel[-1-x] = p->nOp; for(j=0; j<p->nOp; j++){ if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp; } } } /* ** Return the address of the next instruction to be inserted. */ int sqliteVdbeCurrentAddr(Vdbe *p){ assert( p->magic==VDBE_MAGIC_INIT ); return p->nOp; } /* ** Add a whole list of operations to the operation stack. Return the ** address of the first operation added. */ int sqliteVdbeAddOpList(Vdbe *p, int nOp, VdbeOp const *aOp){ int addr; assert( p->magic==VDBE_MAGIC_INIT ); if( p->nOp + nOp >= p->nOpAlloc ){ int oldSize = p->nOpAlloc; Op *aNew; p->nOpAlloc = p->nOpAlloc*2 + nOp + 10; aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); if( aNew==0 ){ p->nOpAlloc = oldSize; return 0; } p->aOp = aNew; memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); } addr = p->nOp; if( nOp>0 ){ int i; for(i=0; i<nOp; i++){ int p2 = aOp[i].p2; p->aOp[i+addr] = aOp[i]; if( p2<0 ) p->aOp[i+addr].p2 = addr + ADDR(p2); p->aOp[i+addr].p3type = aOp[i].p3 ? P3_STATIC : P3_NOTUSED; #ifndef NDEBUG if( sqlite_vdbe_addop_trace ) vdbePrintOp(0, i+addr, &p->aOp[i+addr]); #endif } p->nOp += nOp; } return addr; } /* ** Change the value of the P1 operand for a specific instruction. ** This routine is useful when a large program is loaded from a ** static array using sqliteVdbeAddOpList but we want to make a ** few minor changes to the program. */ void sqliteVdbeChangeP1(Vdbe *p, int addr, int val){ assert( p->magic==VDBE_MAGIC_INIT ); if( p && addr>=0 && p->nOp>addr && p->aOp ){ p->aOp[addr].p1 = val; } } /* ** Change the value of the P2 operand for a specific instruction. ** This routine is useful for setting a jump destination. */ void sqliteVdbeChangeP2(Vdbe *p, int addr, int val){ assert( val>=0 ); assert( p->magic==VDBE_MAGIC_INIT ); if( p && addr>=0 && p->nOp>addr && p->aOp ){ p->aOp[addr].p2 = val; } } /* ** Change the value of the P3 operand for a specific instruction. ** This routine is useful when a large program is loaded from a ** static array using sqliteVdbeAddOpList but we want to make a ** few minor changes to the program. ** ** If n>=0 then the P3 operand is dynamic, meaning that a copy of ** the string is made into memory obtained from sqliteMalloc(). ** A value of n==0 means copy bytes of zP3 up to and including the ** first null byte. If n>0 then copy n+1 bytes of zP3. ** ** If n==P3_STATIC it means that zP3 is a pointer to a constant static ** string and we can just copy the pointer. n==P3_POINTER means zP3 is ** a pointer to some object other than a string. ** ** If addr<0 then change P3 on the most recently inserted instruction. */ void sqliteVdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ Op *pOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p==0 || p->aOp==0 ) return; if( addr<0 || addr>=p->nOp ){ addr = p->nOp - 1; if( addr<0 ) return; } pOp = &p->aOp[addr]; if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); pOp->p3 = 0; } if( zP3==0 ){ pOp->p3 = 0; pOp->p3type = P3_NOTUSED; }else if( n<0 ){ pOp->p3 = (char*)zP3; pOp->p3type = n; }else{ sqliteSetNString(&pOp->p3, zP3, n, 0); pOp->p3type = P3_DYNAMIC; } } /* ** If the P3 operand to the specified instruction appears ** to be a quoted string token, then this procedure removes ** the quotes. ** ** The quoting operator can be either a grave ascent (ASCII 0x27) ** or a double quote character (ASCII 0x22). Two quotes in a row ** resolve to be a single actual quote character within the string. */ void sqliteVdbeDequoteP3(Vdbe *p, int addr){ Op *pOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p->aOp==0 || addr<0 || addr>=p->nOp ) return; pOp = &p->aOp[addr]; if( pOp->p3==0 || pOp->p3[0]==0 ) return; if( pOp->p3type==P3_POINTER ) return; if( pOp->p3type!=P3_DYNAMIC ){ pOp->p3 = sqliteStrDup(pOp->p3); pOp->p3type = P3_DYNAMIC; } sqliteDequote(pOp->p3); } /* ** On the P3 argument of the given instruction, change all ** strings of whitespace characters into a single space and ** delete leading and trailing whitespace. */ void sqliteVdbeCompressSpace(Vdbe *p, int addr){ unsigned char *z; int i, j; Op *pOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p->aOp==0 || addr<0 || addr>=p->nOp ) return; pOp = &p->aOp[addr]; if( pOp->p3type==P3_POINTER ){ return; } if( pOp->p3type!=P3_DYNAMIC ){ pOp->p3 = sqliteStrDup(pOp->p3); pOp->p3type = P3_DYNAMIC; } z = (unsigned char*)pOp->p3; if( z==0 ) return; i = j = 0; while( isspace(z[i]) ){ i++; } while( z[i] ){ if( isspace(z[i]) ){ z[j++] = ' '; while( isspace(z[++i]) ){} }else{ z[j++] = z[i++]; } } while( j>0 && isspace(z[j-1]) ){ j--; } z[j] = 0; } /* ** Search for the current program for the given opcode and P2 ** value. Return the address plus 1 if found and 0 if not found. */ int sqliteVdbeFindOp(Vdbe *p, int op, int p2){ int i; assert( p->magic==VDBE_MAGIC_INIT ); for(i=0; i<p->nOp; i++){ if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1; } return 0; } /* ** Return the opcode for a given address. */ VdbeOp *sqliteVdbeGetOp(Vdbe *p, int addr){ assert( p->magic==VDBE_MAGIC_INIT ); assert( addr>=0 && addr<p->nOp ); return &p->aOp[addr]; } /* ** The following group or routines are employed by installable functions ** to return their results. ** ** The sqlite_set_result_string() routine can be used to return a string ** value or to return a NULL. To return a NULL, pass in NULL for zResult. ** A copy is made of the string before this routine returns so it is safe ** to pass in an ephemeral string. ** ** sqlite_set_result_error() works like sqlite_set_result_string() except ** that it signals a fatal error. The string argument, if any, is the ** error message. If the argument is NULL a generic substitute error message ** is used. ** ** The sqlite_set_result_int() and sqlite_set_result_double() set the return ** value of the user function to an integer or a double. ** ** These routines are defined here in vdbe.c because they depend on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ char *sqlite_set_result_string(sqlite_func *p, const char *zResult, int n){ assert( !p->isStep ); if( p->s.flags & STK_Dyn ){ sqliteFree(p->z); } if( zResult==0 ){ p->s.flags = STK_Null; n = 0; p->z = 0; p->s.n = 0; }else{ if( n<0 ) n = strlen(zResult); if( n<NBFS-1 ){ memcpy(p->s.z, zResult, n); p->s.z[n] = 0; p->s.flags = STK_Str; p->z = p->s.z; }else{ p->z = sqliteMallocRaw( n+1 ); if( p->z ){ memcpy(p->z, zResult, n); p->z[n] = 0; } p->s.flags = STK_Str | STK_Dyn; } p->s.n = n+1; } return p->z; } void sqlite_set_result_int(sqlite_func *p, int iResult){ assert( !p->isStep ); if( p->s.flags & STK_Dyn ){ sqliteFree(p->z); } p->s.i = iResult; p->s.flags = STK_Int; } void sqlite_set_result_double(sqlite_func *p, double rResult){ assert( !p->isStep ); if( p->s.flags & STK_Dyn ){ sqliteFree(p->z); } p->s.r = rResult; p->s.flags = STK_Real; } void sqlite_set_result_error(sqlite_func *p, const char *zMsg, int n){ assert( !p->isStep ); sqlite_set_result_string(p, zMsg, n); p->isError = 1; } /* ** Extract the user data from a sqlite_func structure and return a ** pointer to it. ** ** This routine is defined here in vdbe.c because it depends on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ void *sqlite_user_data(sqlite_func *p){ assert( p && p->pFunc ); return p->pFunc->pUserData; } /* ** Allocate or return the aggregate context for a user function. A new ** context is allocated on the first call. Subsequent calls return the ** same context that was returned on prior calls. ** ** This routine is defined here in vdbe.c because it depends on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ void *sqlite_aggregate_context(sqlite_func *p, int nByte){ assert( p && p->pFunc && p->pFunc->xStep ); if( p->pAgg==0 ){ if( nByte<=NBFS ){ p->pAgg = (void*)p->z; }else{ p->pAgg = sqliteMalloc( nByte ); } } return p->pAgg; } /* ** Return the number of times the Step function of a aggregate has been ** called. ** ** This routine is defined here in vdbe.c because it depends on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ int sqlite_aggregate_count(sqlite_func *p){ assert( p && p->pFunc && p->pFunc->xStep ); return p->cnt; } /* ** Advance the virtual machine to the next output row. ** ** The return vale will be either SQLITE_BUSY, SQLITE_DONE, ** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. ** ................................................................................ } if( sqliteSafetyOff(db) ){ return SQLITE_MISUSE; } return rc; } /* ** Reset an Agg structure. Delete all its contents. ** ** For installable aggregate functions, if the step function has been ** called, make sure the finalizer function has also been called. The ** finalizer might need to free memory that was allocated as part of its ** private context. If the finalizer has not been called yet, call it ** now. */ static void AggReset(Agg *pAgg){ int i; HashElem *p; for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ AggElem *pElem = sqliteHashData(p); assert( pAgg->apFunc!=0 ); for(i=0; i<pAgg->nMem; i++){ Mem *pMem = &pElem->aMem[i]; if( pAgg->apFunc[i] && (pMem->s.flags & STK_AggCtx)!=0 ){ sqlite_func ctx; ctx.pFunc = pAgg->apFunc[i]; ctx.s.flags = STK_Null; ctx.z = 0; ctx.pAgg = pMem->z; ctx.cnt = pMem->s.i; ctx.isStep = 0; ctx.isError = 0; (*pAgg->apFunc[i]->xFinalize)(&ctx); if( pMem->z!=0 && pMem->z!=pMem->s.z ){ sqliteFree(pMem->z); } }else if( pMem->s.flags & STK_Dyn ){ sqliteFree(pMem->z); } } sqliteFree(pElem); } sqliteHashClear(&pAgg->hash); sqliteFree(pAgg->apFunc); pAgg->apFunc = 0; pAgg->pCurrent = 0; pAgg->pSearch = 0; pAgg->nMem = 0; } /* ** Insert a new aggregate element and make it the element that ** has focus. ** ** Return 0 on success and 1 if memory is exhausted. */ static int AggInsert(Agg *p, char *zKey, int nKey){ ................................................................................ p->aStack[i].r = p->aStack[i].i; }else{ p->aStack[i].r = 0.0; } p->aStack[i].flags |= STK_Real; } /* ** Pop the stack N times. Free any memory associated with the ** popped stack elements. */ static void PopStack(Vdbe *p, int N){ assert( N>=0 ); if( p->zStack==0 ) return; assert( p->aStack || sqlite_malloc_failed ); if( p->aStack==0 ) return; while( N-- > 0 ){ if( p->aStack[p->tos].flags & STK_Dyn ){ sqliteFree(p->zStack[p->tos]); } p->aStack[p->tos].flags = 0; p->zStack[p->tos] = 0; p->tos--; } } /* ** Here is a macro to handle the common case of popping the stack ** once. This macro only works from within the sqliteVdbeExec() ** function. */ #define POPSTACK \ assert(p->tos>=0); \ if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \ p->tos--; /* ** Delete a keylist */ static void KeylistFree(Keylist *p){ while( p ){ Keylist *pNext = p->pNext; sqliteFree(p); p = pNext; } } /* ** Close a cursor and release all the resources that cursor happens ** to hold. */ static void cleanupCursor(Cursor *pCx){ if( pCx->pCursor ){ sqliteBtreeCloseCursor(pCx->pCursor); } if( pCx->pBt ){ sqliteBtreeClose(pCx->pBt); } sqliteFree(pCx->pData); memset(pCx, 0, sizeof(Cursor)); } /* ** Close all cursors */ static void closeAllCursors(Vdbe *p){ int i; for(i=0; i<p->nCursor; i++){ cleanupCursor(&p->aCsr[i]); } sqliteFree(p->aCsr); p->aCsr = 0; p->nCursor = 0; } /* ** Remove any elements that remain on the sorter for the VDBE given. */ static void SorterReset(Vdbe *p){ while( p->pSort ){ Sorter *pSorter = p->pSort; p->pSort = pSorter->pNext; sqliteFree(pSorter->zKey); sqliteFree(pSorter->pData); sqliteFree(pSorter); } } /* ** Delete the variables in p->azVariable[] */ static void ClearVariableArray(Vdbe *p){ sqliteFree(p->azVariable); p->nVariable = 0; p->azVariable = 0; } /* ** Clean up the VM after execution. ** ** This routine will automatically close any cursors, lists, and/or ** sorters that were left open. It also deletes the values of ** variables in the azVariable[] array. */ static void Cleanup(Vdbe *p){ int i; PopStack(p, p->tos+1); closeAllCursors(p); if( p->aMem ){ for(i=0; i<p->nMem; i++){ if( p->aMem[i].s.flags & STK_Dyn ){ sqliteFree(p->aMem[i].z); } } } sqliteFree(p->aMem); p->aMem = 0; p->nMem = 0; if( p->pList ){ KeylistFree(p->pList); p->pList = 0; } SorterReset(p); if( p->pFile ){ if( p->pFile!=stdin ) fclose(p->pFile); p->pFile = 0; } if( p->azField ){ sqliteFree(p->azField); p->azField = 0; } p->nField = 0; if( p->zLine ){ sqliteFree(p->zLine); p->zLine = 0; } p->nLineAlloc = 0; AggReset(&p->agg); if( p->aSet ){ for(i=0; i<p->nSet; i++){ sqliteHashClear(&p->aSet[i].hash); } } sqliteFree(p->aSet); p->aSet = 0; p->nSet = 0; if( p->keylistStack ){ int ii; for(ii = 0; ii < p->keylistStackDepth; ii++){ KeylistFree(p->keylistStack[ii]); } sqliteFree(p->keylistStack); p->keylistStackDepth = 0; p->keylistStack = 0; } sqliteFree(p->zErrMsg); p->zErrMsg = 0; ClearVariableArray(p); } /* ** Delete an entire VDBE. */ void sqliteVdbeDelete(Vdbe *p){ int i; if( p==0 ) return; Cleanup(p); if( p->pPrev ){ p->pPrev->pNext = p->pNext; }else{ assert( p->db->pVdbe==p ); p->db->pVdbe = p->pNext; } if( p->pNext ){ p->pNext->pPrev = p->pPrev; } p->pPrev = p->pNext = 0; if( p->nOpAlloc==0 ){ p->aOp = 0; p->nOp = 0; } for(i=0; i<p->nOp; i++){ if( p->aOp[i].p3type==P3_DYNAMIC ){ sqliteFree(p->aOp[i].p3); } } sqliteFree(p->aOp); sqliteFree(p->aLabel); sqliteFree(p->aStack); p->magic = VDBE_MAGIC_DEAD; sqliteFree(p); } /* ** Give a listing of the program in the virtual machine. ** ** The interface is the same as sqliteVdbeExec(). But instead of ** running the code, it invokes the callback once for each instruction. ** This feature is used to implement "EXPLAIN". */ int sqliteVdbeList( Vdbe *p /* The VDBE */ ){ sqlite *db = p->db; int i; static char *azColumnNames[] = { "addr", "opcode", "p1", "p2", "p3", "int", "text", "int", "int", "text", 0 }; assert( p->popStack==0 ); assert( p->explain ); p->azColName = azColumnNames; p->azResColumn = p->zStack; for(i=0; i<5; i++) p->zStack[i] = p->aStack[i].z; p->rc = SQLITE_OK; for(i=p->pc; p->rc==SQLITE_OK && i<p->nOp; i++){ if( db->flags & SQLITE_Interrupt ){ db->flags &= ~SQLITE_Interrupt; if( db->magic!=SQLITE_MAGIC_BUSY ){ p->rc = SQLITE_MISUSE; }else{ p->rc = SQLITE_INTERRUPT; } sqliteSetString(&p->zErrMsg, sqlite_error_string(p->rc), 0); break; } sprintf(p->zStack[0],"%d",i); sprintf(p->zStack[2],"%d", p->aOp[i].p1); sprintf(p->zStack[3],"%d", p->aOp[i].p2); if( p->aOp[i].p3type==P3_POINTER ){ sprintf(p->aStack[4].z, "ptr(%#x)", (int)p->aOp[i].p3); p->zStack[4] = p->aStack[4].z; }else{ p->zStack[4] = p->aOp[i].p3; } p->zStack[1] = sqliteOpcodeNames[p->aOp[i].opcode]; if( p->xCallback==0 ){ p->pc = i+1; p->azResColumn = p->zStack; p->nResColumn = 5; return SQLITE_ROW; } if( sqliteSafetyOff(db) ){ p->rc = SQLITE_MISUSE; break; } if( p->xCallback(p->pCbArg, 5, p->zStack, p->azColName) ){ p->rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ){ p->rc = SQLITE_MISUSE; } } return p->rc==SQLITE_OK ? SQLITE_DONE : SQLITE_ERROR; } /* ** The parameters are pointers to the head of two sorted lists ** of Sorter structures. Merge these two lists together and return ** a single sorted list. This routine forms the core of the merge-sort ** algorithm. ** ** In the case of a tie, left sorts in front of right. ................................................................................ break; } } zBuf[i] = 0; return i>0 ? zBuf : 0; } #if !defined(NDEBUG) || defined(VDBE_PROFILE) /* ** Print a single opcode. This routine is used for debugging only. */ static void vdbePrintOp(FILE *pOut, int pc, Op *pOp){ char *zP3; char zPtr[40]; if( pOp->p3type==P3_POINTER ){ sprintf(zPtr, "ptr(%#x)", (int)pOp->p3); zP3 = zPtr; }else{ zP3 = pOp->p3; } if( pOut==0 ) pOut = stdout; fprintf(pOut,"%4d %-12s %4d %4d %s\n", pc, sqliteOpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3 ? zP3 : ""); fflush(pOut); } #endif /* ** Make sure there is space in the Vdbe structure to hold at least ** mxCursor cursors. If there is not currently enough space, then ** allocate more. ** ** If a memory allocation error occurs, return 1. Return 0 if ** everything works. ................................................................................ ** but that meant we more testing that we needed. By only testing the ** flag on jump instructions, we get a (small) speed improvement. */ #define CHECK_FOR_INTERRUPT \ if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt; /* ** Prepare a virtual machine for execution. This involves things such ** as allocating stack space and initializing the program counter. ** After the VDBE has be prepped, it can be executed by one or more ** calls to sqliteVdbeExec(). ** ** The behavior of sqliteVdbeExec() is influenced by the parameters to ** this routine. If xCallback is NULL, then sqliteVdbeExec() will return ** with SQLITE_ROW whenever there is a row of the result set ready ** to be delivered. p->azResColumn will point to the row and ** p->nResColumn gives the number of columns in the row. If xCallback ** is not NULL, then the xCallback() routine is invoked to process each ** row in the result set. */ void sqliteVdbeMakeReady( Vdbe *p, /* The VDBE */ sqlite_callback xCallback, /* Result callback */ void *pCallbackArg, /* 1st argument to xCallback() */ int isExplain /* True if the EXPLAIN keywords is present */ ){ int n; assert( p!=0 ); assert( p->aStack==0 ); assert( p->magic==VDBE_MAGIC_INIT ); /* Add a HALT instruction to the very end of the program. */ if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){ sqliteVdbeAddOp(p, OP_Halt, 0, 0); } /* No instruction ever pushes more than a single element onto the ** stack. And the stack never grows on successive executions of the ** same loop. So the total number of instructions is an upper bound ** on the maximum stack depth required. ** ** Allocation all the stack space we will ever need. */ n = isExplain ? 10 : p->nOp; p->aStack = sqliteMalloc( n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) ); p->zStack = (char**)&p->aStack[n]; p->azColName = (char**)&p->zStack[n]; sqliteHashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0); p->agg.pSearch = 0; #ifdef MEMORY_DEBUG if( sqliteOsFileExists("vdbe_trace") ){ p->trace = stdout; } #endif p->tos = -1; p->pc = 0; p->rc = SQLITE_OK; p->uniqueCnt = 0; p->returnDepth = 0; p->errorAction = OE_Abort; p->undoTransOnError = 0; p->xCallback = xCallback; p->pCbArg = pCallbackArg; p->popStack = 0; p->explain |= isExplain; p->magic = VDBE_MAGIC_RUN; #ifdef VDBE_PROFILE for(i=0; i<p->nOp; i++){ p->aOp[i].cnt = 0; p->aOp[i].cycles = 0; } #endif } /* ** Execute as much of a VDBE program as we can then return. ** ** sqliteVdbeMakeReady() must be called before this routine in order to ** close the program with a final OP_Halt and to set up the callbacks ** and the error message pointer. ** ................................................................................ if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; assert( db->magic==SQLITE_MAGIC_BUSY ); assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); p->rc = SQLITE_OK; assert( p->explain==0 ); if( sqlite_malloc_failed ) goto no_mem; if( p->popStack ){ PopStack(p, p->popStack); p->popStack = 0; } for(pc=p->pc; rc==SQLITE_OK; pc++){ assert( pc>=0 && pc<p->nOp ); #ifdef VDBE_PROFILE origPc = pc; start = hwtime(); ................................................................................ #endif pOp = &p->aOp[pc]; /* Only allow tracing if NDEBUG is not defined. */ #ifndef NDEBUG if( p->trace ){ vdbePrintOp(p->trace, pc, pOp); } #endif switch( pOp->opcode ){ /***************************************************************************** ** What follows is a massive switch statement where each case implements a ................................................................................ /* Opcode: Pop P1 * * ** ** P1 elements are popped off of the top of stack and discarded. */ case OP_Pop: { assert( p->tos+1>=pOp->p1 ); PopStack(p, pOp->p1); break; } /* Opcode: Dup P1 P2 * ** ** A copy of the P1-th element of the stack ** is made and pushed onto the top of the stack. ................................................................................ } if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; if( p->xCallback(p->pCbArg, pOp->p1, &zStack[i], p->azColName)!=0 ){ rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; p->nCallback++; PopStack(p, pOp->p1); if( sqlite_malloc_failed ) goto no_mem; break; } /* Opcode: NullCallback P1 * * ** ** Invoke the callback function once with the 2nd argument (the ................................................................................ break; }else{ Stringify(p, i); nByte += aStack[i].n - 1 + nSep; } } if( nByte<0 ){ if( pOp->p2==0 ) PopStack(p, nField); p->tos++; aStack[p->tos].flags = STK_Null; zStack[p->tos] = 0; break; } zNew = sqliteMallocRaw( nByte ); if( zNew==0 ) goto no_mem; ................................................................................ } if( nSep>0 && i<p->tos ){ memcpy(&zNew[j], zSep, nSep); j += nSep; } } zNew[j] = 0; if( pOp->p2==0 ) PopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; aStack[p->tos].flags = STK_Str|STK_Dyn; zStack[p->tos] = zNew; break; } ................................................................................ Release(p, nos); aStack[nos].r = b; aStack[nos].flags = STK_Real; } break; divide_by_zero: PopStack(p, 2); p->tos = nos; aStack[nos].flags = STK_Null; break; } /* Opcode: Function P1 * P3 ** ................................................................................ } ctx.pFunc = (FuncDef*)pOp->p3; ctx.s.flags = STK_Null; ctx.z = 0; ctx.isError = 0; ctx.isStep = 0; (*ctx.pFunc->xFunc)(&ctx, n, (const char**)&zStack[p->tos-n+1]); PopStack(p, n); p->tos++; aStack[p->tos] = ctx.s; if( ctx.s.flags & STK_Dyn ){ zStack[p->tos] = ctx.z; }else if( ctx.s.flags & STK_Str ){ zStack[p->tos] = aStack[p->tos].z; }else{ ................................................................................ VERIFY( if( p->tos+1-cnt<0 ) goto not_enough_stack; ) for(i=0; i<cnt; i++){ if( aStack[p->tos-i].flags & STK_Null ){ pc = pOp->p2-1; break; } } if( pOp->p1>0 ) PopStack(p, cnt); break; } /* Opcode: NotNull P1 P2 * ** ** Jump to P2 if the top value on the stack is not NULL. Pop the ** stack if P1 is greater than zero. If P1 is less than or equal to ................................................................................ } for(i=p->tos-nField+1; i<=p->tos; i++){ if( (aStack[i].flags & STK_Null)==0 ){ memcpy(&zNewRecord[j], zStack[i], aStack[i].n); j += aStack[i].n; } } PopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; if( nByte<=NBFS ){ assert( zNewRecord==zTemp ); memcpy(aStack[p->tos].z, zTemp, nByte); zStack[p->tos] = aStack[p->tos].z; aStack[p->tos].flags = STK_Str; ................................................................................ } } if( addRowid ){ u32 iKey; Integerify(p, p->tos-nField); iKey = intToKey(aStack[p->tos-nField].i); memcpy(&zNewKey[j], &iKey, sizeof(u32)); PopStack(p, nField+1); if( pOp->p2 && containsNull ) pc = pOp->p2 - 1; }else{ if( pOp->p2==0 ) PopStack(p, nField+addRowid); } p->tos++; aStack[p->tos].n = nByte; if( nByte<=NBFS ){ assert( zNewKey==zTemp ); zStack[p->tos] = aStack[p->tos].z; memcpy(zStack[p->tos], zTemp, nByte); ................................................................................ sqliteSetString(&p->zErrMsg, "root page number less than 2", 0); rc = SQLITE_INTERNAL; break; } } VERIFY( if( i<0 ) goto bad_instruction; ) if( expandCursorArraySize(p, i) ) goto no_mem; cleanupCursor(&p->aCsr[i]); memset(&p->aCsr[i], 0, sizeof(Cursor)); p->aCsr[i].nullRow = 1; if( pX==0 ) break; do{ rc = sqliteBtreeCursor(pX, p2, wrFlag, &p->aCsr[i].pCursor); switch( rc ){ case SQLITE_BUSY: { ................................................................................ */ case OP_OpenTemp: { int i = pOp->p1; Cursor *pCx; VERIFY( if( i<0 ) goto bad_instruction; ) if( expandCursorArraySize(p, i) ) goto no_mem; pCx = &p->aCsr[i]; cleanupCursor(pCx); memset(pCx, 0, sizeof(*pCx)); pCx->nullRow = 1; rc = sqliteBtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt); if( rc==SQLITE_OK ){ rc = sqliteBtreeBeginTrans(pCx->pBt); } ................................................................................ */ case OP_OpenPseudo: { int i = pOp->p1; Cursor *pCx; VERIFY( if( i<0 ) goto bad_instruction; ) if( expandCursorArraySize(p, i) ) goto no_mem; pCx = &p->aCsr[i]; cleanupCursor(pCx); memset(pCx, 0, sizeof(*pCx)); pCx->nullRow = 1; pCx->pseudoTable = 1; break; } /* Opcode: Close P1 * * ................................................................................ ** ** Close a cursor previously opened as P1. If P1 is not ** currently open, this instruction is a no-op. */ case OP_Close: { int i = pOp->p1; if( i>=0 && i<p->nCursor ){ cleanupCursor(&p->aCsr[i]); } break; } /* Opcode: MoveTo P1 P2 * ** ** Pop the top of the stack and use its value as a key. Reposition ................................................................................ /* Opcode: ListReset * * * ** ** Reset the temporary storage buffer so that it holds nothing. */ case OP_ListReset: { if( p->pList ){ KeylistFree(p->pList); p->pList = 0; } break; } /* Opcode: ListPush * * * ** ................................................................................ ** ** Restore the Vdbe list to the state it was in when ListPush was last ** executed. */ case OP_ListPop: { assert(p->keylistStackDepth > 0); p->keylistStackDepth--; KeylistFree(p->pList); p->pList = p->keylistStack[p->keylistStackDepth]; p->keylistStack[p->keylistStackDepth] = 0; if( p->keylistStackDepth == 0 ){ sqliteFree(p->keylistStack); p->keylistStack = 0; } break; ................................................................................ azArg[j] = 0; }else{ azArg[j] = z; strcpy(z, zStack[i]); z += aStack[i].n; } } PopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; zStack[p->tos] = (char*)azArg; aStack[p->tos].flags = STK_Str|STK_Dyn; break; } ................................................................................ memcpy(&zNewKey[j], zStack[i], aStack[i].n-1); j += aStack[i].n-1; zNewKey[j++] = 0; } } zNewKey[j] = 0; assert( j<nByte ); PopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; aStack[p->tos].flags = STK_Str|STK_Dyn; zStack[p->tos] = zNewKey; break; } ................................................................................ } /* Opcode: SortReset * * * ** ** Remove any elements that remain on the sorter. */ case OP_SortReset: { SorterReset(p); break; } /* Opcode: FileOpen * * P3 ** ** Open the file named by P3 for reading using the FileRead opcode. ** If P3 is "stdin" then open standard input for reading. ................................................................................ /* Opcode: AggReset * P2 * ** ** Reset the aggregator so that it no longer contains any data. ** Future aggregator elements will contain P2 values each. */ case OP_AggReset: { AggReset(&p->agg); p->agg.nMem = pOp->p2; p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) ); if( p->agg.apFunc==0 ) goto no_mem; break; } /* Opcode: AggInit * P2 P3 ................................................................................ ctx.pAgg = pMem->z; ctx.cnt = ++pMem->s.i; ctx.isError = 0; ctx.isStep = 1; (ctx.pFunc->xStep)(&ctx, n, (const char**)&zStack[p->tos-n]); pMem->z = ctx.pAgg; pMem->s.flags = STK_AggCtx; PopStack(p, n+1); if( ctx.isError ){ rc = SQLITE_ERROR; } break; } /* Opcode: AggFocus * P2 * ................................................................................ #ifdef VDBE_PROFILE { long long elapse = hwtime() - start; pOp->cycles += elapse; pOp->cnt++; #if 0 fprintf(stdout, "%10lld ", elapse); vdbePrintOp(stdout, origPc, &p->aOp[origPc]); #endif } #endif /* The following code adds nothing to the actual functionality ** of the program. It is only here for testing and debugging. ** On the other hand, it does burn CPU cycles every time through ................................................................................ bad_instruction: sprintf(zBuf,"%d",pc); sqliteSetString(&p->zErrMsg, "illegal operation at ", zBuf, 0); rc = SQLITE_INTERNAL; goto vdbe_halt; ) } /* ** Clean up a VDBE after execution but do not delete the VDBE just yet. ** Write any error messages into *pzErrMsg. Return the result code. ** ** After this routine is run, the VDBE should be ready to be executed ** again. */ int sqliteVdbeReset(Vdbe *p, char **pzErrMsg){ sqlite *db = p->db; int i; if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); return SQLITE_MISUSE; } if( p->zErrMsg ){ if( pzErrMsg && *pzErrMsg==0 ){ *pzErrMsg = p->zErrMsg; }else{ sqliteFree(p->zErrMsg); } p->zErrMsg = 0; } Cleanup(p); if( p->rc!=SQLITE_OK ){ switch( p->errorAction ){ case OE_Abort: { if( !p->undoTransOnError ){ for(i=0; i<db->nDb; i++){ if( db->aDb[i].pBt ){ sqliteBtreeRollbackCkpt(db->aDb[i].pBt); } } break; } /* Fall through to ROLLBACK */ } case OE_Rollback: { sqliteRollbackAll(db); db->flags &= ~SQLITE_InTrans; db->onError = OE_Default; break; } default: { if( p->undoTransOnError ){ sqliteRollbackAll(db); db->flags &= ~SQLITE_InTrans; db->onError = OE_Default; } break; } } sqliteRollbackInternalChanges(db); } for(i=0; i<db->nDb; i++){ if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){ sqliteBtreeCommitCkpt(db->aDb[i].pBt); db->aDb[i].inTrans = 1; } } assert( p->tos<p->pc || sqlite_malloc_failed==1 ); #ifdef VDBE_PROFILE { FILE *out = fopen("vdbe_profile.out", "a"); if( out ){ int i; fprintf(out, "---- "); for(i=0; i<p->nOp; i++){ fprintf(out, "%02x", p->aOp[i].opcode); } fprintf(out, "\n"); for(i=0; i<p->nOp; i++){ fprintf(out, "%6d %10lld %8lld ", p->aOp[i].cnt, p->aOp[i].cycles, p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 ); vdbePrintOp(out, i, &p->aOp[i]); } fclose(out); } } #endif p->magic = VDBE_MAGIC_INIT; return p->rc; } /* ** Clean up and delete a VDBE after execution. Return an integer which is ** the result code. Write any error message text into *pzErrMsg. */ int sqliteVdbeFinalize(Vdbe *p, char **pzErrMsg){ int rc; sqlite *db; if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); return SQLITE_MISUSE; } db = p->db; rc = sqliteVdbeReset(p, pzErrMsg); sqliteVdbeDelete(p); if( db->want_to_close && db->pVdbe==0 ){ sqlite_close(db); } return rc; } /* ** Set the values of all variables. Variable $1 in the original SQL will ** be the string azValue[0]. $2 will have the value azValue[1]. And ** so forth. If a value is out of range (for example $3 when nValue==2) ** then its value will be NULL. ** ** This routine overrides any prior call. */ int sqliteVdbeSetVariables(Vdbe *p, int nValue, const char **azValue){ int i, n; char *z; if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 || p->nVariable!=0 ){ return SQLITE_MISUSE; } ClearVariableArray(p); if( nValue==0 ){ p->nVariable = 0; p->azVariable = 0; } for(i=n=0; i<nValue; i++){ if( azValue[i] ) n += strlen(azValue[i]) + 1; } p->azVariable = sqliteMalloc( sizeof(p->azVariable[0])*nValue + n ); if( p->azVariable==0 ){ p->nVariable = 0; return SQLITE_NOMEM; } z = (char*)&p->azVariable[nValue]; for(i=0; i<nValue; i++){ if( azValue[i]==0 ){ p->azVariable[i] = 0; }else{ p->azVariable[i] = z; n = strlen(azValue[i]); memcpy(z, azValue[i], n+1); z += n+1; } } p->nVariable = nValue; return SQLITE_OK; } #if 0 /* ** Create a new Vdbe in *pOut and populate it with the program from p. Then ** pass p to sqliteVdbeFinalize(). */ int sqliteVdbeReset(Vdbe *p, char ** pErrMsg, Vdbe** pOut){ if( pOut && p->rc != SQLITE_SCHEMA ){ /* Create a new VDBE and populate it with the program used by the old ** VDBE. Don't copy the last instruction of the program, as this is an ** OP_Halt coded by sqliteVdbeMakeReady(). */ *pOut = sqliteVdbeCreate( p->db ); (*pOut)->aOp = p->aOp; (*pOut)->nOp = p->nOp-1; (*pOut)->nOpAlloc = p->nOpAlloc; sqliteVdbeMakeReady( *pOut, p->xCallback, p->pCbArg, (int)p->explain ); p->aOp = 0; p->nOp = 0; p->nOpAlloc = 0; }else if( pOut ){ *pOut = NULL; } return sqliteVdbeFinalize(p, pErrMsg); } #endif |
| > > > > > > > | > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | | | | | | | | | | | | | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 .. 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 ... 133 134 135 136 137 138 139 140 141 142 143 144 145 146 ... 326 327 328 329 330 331 332 333 334 335 336 337 338 339 ... 431 432 433 434 435 436 437 438 439 440 441 442 443 444 ... 480 481 482 483 484 485 486 487 488 489 490 491 492 493 ... 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 ... 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 ... 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 ... 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 ... 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 .... 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 .... 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 .... 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 .... 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 .... 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 .... 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 .... 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 .... 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 .... 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 .... 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 .... 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 .... 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 .... 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 .... 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 .... 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 .... 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 .... 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 .... 4773 4774 4775 4776 4777 4778 4779 |
** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** The code in this file implements execution method of the ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c") ** handles housekeeping details such as creating and deleting ** VDBE instances. This file is solely interested in executing ** the VDBE program. ** ** In the external interface, an "sqlite_vm*" is an opaque pointer ** to a VDBE. ** ** The SQL parser generates a program which is then executed by ** the VDBE to do the work of the SQL statement. VDBE programs are ** similar in form to assembly language. The program consists of ** a linear sequence of operations. Each operation has an opcode ** and 3 operands. Operands P1 and P2 are integers. Operand P3 ** is a null-terminated string. The P2 operand must be non-negative. ................................................................................ ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** ** $Id: vdbe.c,v 1.239 2003/09/06 20:12:01 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include "vdbeInt.h" /* ** The following global variable is incremented every time a cursor ** moves, either by the OP_MoveTo or the OP_Next opcode. The test ** procedures use this information to make sure that indices are ** working correctly. This variable has no function other than to ** help verify the correct operation of the library. */ int sqlite_search_count = 0; /* ** Advance the virtual machine to the next output row. ** ** The return vale will be either SQLITE_BUSY, SQLITE_DONE, ** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. ** ................................................................................ } if( sqliteSafetyOff(db) ){ return SQLITE_MISUSE; } return rc; } /* ** Insert a new aggregate element and make it the element that ** has focus. ** ** Return 0 on success and 1 if memory is exhausted. */ static int AggInsert(Agg *p, char *zKey, int nKey){ ................................................................................ p->aStack[i].r = p->aStack[i].i; }else{ p->aStack[i].r = 0.0; } p->aStack[i].flags |= STK_Real; } /* ** The parameters are pointers to the head of two sorted lists ** of Sorter structures. Merge these two lists together and return ** a single sorted list. This routine forms the core of the merge-sort ** algorithm. ** ** In the case of a tie, left sorts in front of right. ................................................................................ break; } } zBuf[i] = 0; return i>0 ? zBuf : 0; } /* ** Make sure there is space in the Vdbe structure to hold at least ** mxCursor cursors. If there is not currently enough space, then ** allocate more. ** ** If a memory allocation error occurs, return 1. Return 0 if ** everything works. ................................................................................ ** but that meant we more testing that we needed. By only testing the ** flag on jump instructions, we get a (small) speed improvement. */ #define CHECK_FOR_INTERRUPT \ if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt; /* ** Execute as much of a VDBE program as we can then return. ** ** sqliteVdbeMakeReady() must be called before this routine in order to ** close the program with a final OP_Halt and to set up the callbacks ** and the error message pointer. ** ................................................................................ if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; assert( db->magic==SQLITE_MAGIC_BUSY ); assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); p->rc = SQLITE_OK; assert( p->explain==0 ); if( sqlite_malloc_failed ) goto no_mem; if( p->popStack ){ sqliteVdbePopStack(p, p->popStack); p->popStack = 0; } for(pc=p->pc; rc==SQLITE_OK; pc++){ assert( pc>=0 && pc<p->nOp ); #ifdef VDBE_PROFILE origPc = pc; start = hwtime(); ................................................................................ #endif pOp = &p->aOp[pc]; /* Only allow tracing if NDEBUG is not defined. */ #ifndef NDEBUG if( p->trace ){ sqliteVdbePrintOp(p->trace, pc, pOp); } #endif switch( pOp->opcode ){ /***************************************************************************** ** What follows is a massive switch statement where each case implements a ................................................................................ /* Opcode: Pop P1 * * ** ** P1 elements are popped off of the top of stack and discarded. */ case OP_Pop: { assert( p->tos+1>=pOp->p1 ); sqliteVdbePopStack(p, pOp->p1); break; } /* Opcode: Dup P1 P2 * ** ** A copy of the P1-th element of the stack ** is made and pushed onto the top of the stack. ................................................................................ } if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; if( p->xCallback(p->pCbArg, pOp->p1, &zStack[i], p->azColName)!=0 ){ rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; p->nCallback++; sqliteVdbePopStack(p, pOp->p1); if( sqlite_malloc_failed ) goto no_mem; break; } /* Opcode: NullCallback P1 * * ** ** Invoke the callback function once with the 2nd argument (the ................................................................................ break; }else{ Stringify(p, i); nByte += aStack[i].n - 1 + nSep; } } if( nByte<0 ){ if( pOp->p2==0 ) sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].flags = STK_Null; zStack[p->tos] = 0; break; } zNew = sqliteMallocRaw( nByte ); if( zNew==0 ) goto no_mem; ................................................................................ } if( nSep>0 && i<p->tos ){ memcpy(&zNew[j], zSep, nSep); j += nSep; } } zNew[j] = 0; if( pOp->p2==0 ) sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; aStack[p->tos].flags = STK_Str|STK_Dyn; zStack[p->tos] = zNew; break; } ................................................................................ Release(p, nos); aStack[nos].r = b; aStack[nos].flags = STK_Real; } break; divide_by_zero: sqliteVdbePopStack(p, 2); p->tos = nos; aStack[nos].flags = STK_Null; break; } /* Opcode: Function P1 * P3 ** ................................................................................ } ctx.pFunc = (FuncDef*)pOp->p3; ctx.s.flags = STK_Null; ctx.z = 0; ctx.isError = 0; ctx.isStep = 0; (*ctx.pFunc->xFunc)(&ctx, n, (const char**)&zStack[p->tos-n+1]); sqliteVdbePopStack(p, n); p->tos++; aStack[p->tos] = ctx.s; if( ctx.s.flags & STK_Dyn ){ zStack[p->tos] = ctx.z; }else if( ctx.s.flags & STK_Str ){ zStack[p->tos] = aStack[p->tos].z; }else{ ................................................................................ VERIFY( if( p->tos+1-cnt<0 ) goto not_enough_stack; ) for(i=0; i<cnt; i++){ if( aStack[p->tos-i].flags & STK_Null ){ pc = pOp->p2-1; break; } } if( pOp->p1>0 ) sqliteVdbePopStack(p, cnt); break; } /* Opcode: NotNull P1 P2 * ** ** Jump to P2 if the top value on the stack is not NULL. Pop the ** stack if P1 is greater than zero. If P1 is less than or equal to ................................................................................ } for(i=p->tos-nField+1; i<=p->tos; i++){ if( (aStack[i].flags & STK_Null)==0 ){ memcpy(&zNewRecord[j], zStack[i], aStack[i].n); j += aStack[i].n; } } sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; if( nByte<=NBFS ){ assert( zNewRecord==zTemp ); memcpy(aStack[p->tos].z, zTemp, nByte); zStack[p->tos] = aStack[p->tos].z; aStack[p->tos].flags = STK_Str; ................................................................................ } } if( addRowid ){ u32 iKey; Integerify(p, p->tos-nField); iKey = intToKey(aStack[p->tos-nField].i); memcpy(&zNewKey[j], &iKey, sizeof(u32)); sqliteVdbePopStack(p, nField+1); if( pOp->p2 && containsNull ) pc = pOp->p2 - 1; }else{ if( pOp->p2==0 ) sqliteVdbePopStack(p, nField+addRowid); } p->tos++; aStack[p->tos].n = nByte; if( nByte<=NBFS ){ assert( zNewKey==zTemp ); zStack[p->tos] = aStack[p->tos].z; memcpy(zStack[p->tos], zTemp, nByte); ................................................................................ sqliteSetString(&p->zErrMsg, "root page number less than 2", 0); rc = SQLITE_INTERNAL; break; } } VERIFY( if( i<0 ) goto bad_instruction; ) if( expandCursorArraySize(p, i) ) goto no_mem; sqliteVdbeCleanupCursor(&p->aCsr[i]); memset(&p->aCsr[i], 0, sizeof(Cursor)); p->aCsr[i].nullRow = 1; if( pX==0 ) break; do{ rc = sqliteBtreeCursor(pX, p2, wrFlag, &p->aCsr[i].pCursor); switch( rc ){ case SQLITE_BUSY: { ................................................................................ */ case OP_OpenTemp: { int i = pOp->p1; Cursor *pCx; VERIFY( if( i<0 ) goto bad_instruction; ) if( expandCursorArraySize(p, i) ) goto no_mem; pCx = &p->aCsr[i]; sqliteVdbeCleanupCursor(pCx); memset(pCx, 0, sizeof(*pCx)); pCx->nullRow = 1; rc = sqliteBtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt); if( rc==SQLITE_OK ){ rc = sqliteBtreeBeginTrans(pCx->pBt); } ................................................................................ */ case OP_OpenPseudo: { int i = pOp->p1; Cursor *pCx; VERIFY( if( i<0 ) goto bad_instruction; ) if( expandCursorArraySize(p, i) ) goto no_mem; pCx = &p->aCsr[i]; sqliteVdbeCleanupCursor(pCx); memset(pCx, 0, sizeof(*pCx)); pCx->nullRow = 1; pCx->pseudoTable = 1; break; } /* Opcode: Close P1 * * ................................................................................ ** ** Close a cursor previously opened as P1. If P1 is not ** currently open, this instruction is a no-op. */ case OP_Close: { int i = pOp->p1; if( i>=0 && i<p->nCursor ){ sqliteVdbeCleanupCursor(&p->aCsr[i]); } break; } /* Opcode: MoveTo P1 P2 * ** ** Pop the top of the stack and use its value as a key. Reposition ................................................................................ /* Opcode: ListReset * * * ** ** Reset the temporary storage buffer so that it holds nothing. */ case OP_ListReset: { if( p->pList ){ sqliteVdbeKeylistFree(p->pList); p->pList = 0; } break; } /* Opcode: ListPush * * * ** ................................................................................ ** ** Restore the Vdbe list to the state it was in when ListPush was last ** executed. */ case OP_ListPop: { assert(p->keylistStackDepth > 0); p->keylistStackDepth--; sqliteVdbeKeylistFree(p->pList); p->pList = p->keylistStack[p->keylistStackDepth]; p->keylistStack[p->keylistStackDepth] = 0; if( p->keylistStackDepth == 0 ){ sqliteFree(p->keylistStack); p->keylistStack = 0; } break; ................................................................................ azArg[j] = 0; }else{ azArg[j] = z; strcpy(z, zStack[i]); z += aStack[i].n; } } sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; zStack[p->tos] = (char*)azArg; aStack[p->tos].flags = STK_Str|STK_Dyn; break; } ................................................................................ memcpy(&zNewKey[j], zStack[i], aStack[i].n-1); j += aStack[i].n-1; zNewKey[j++] = 0; } } zNewKey[j] = 0; assert( j<nByte ); sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; aStack[p->tos].flags = STK_Str|STK_Dyn; zStack[p->tos] = zNewKey; break; } ................................................................................ } /* Opcode: SortReset * * * ** ** Remove any elements that remain on the sorter. */ case OP_SortReset: { sqliteVdbeSorterReset(p); break; } /* Opcode: FileOpen * * P3 ** ** Open the file named by P3 for reading using the FileRead opcode. ** If P3 is "stdin" then open standard input for reading. ................................................................................ /* Opcode: AggReset * P2 * ** ** Reset the aggregator so that it no longer contains any data. ** Future aggregator elements will contain P2 values each. */ case OP_AggReset: { sqliteVdbeAggReset(&p->agg); p->agg.nMem = pOp->p2; p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) ); if( p->agg.apFunc==0 ) goto no_mem; break; } /* Opcode: AggInit * P2 P3 ................................................................................ ctx.pAgg = pMem->z; ctx.cnt = ++pMem->s.i; ctx.isError = 0; ctx.isStep = 1; (ctx.pFunc->xStep)(&ctx, n, (const char**)&zStack[p->tos-n]); pMem->z = ctx.pAgg; pMem->s.flags = STK_AggCtx; sqliteVdbePopStack(p, n+1); if( ctx.isError ){ rc = SQLITE_ERROR; } break; } /* Opcode: AggFocus * P2 * ................................................................................ #ifdef VDBE_PROFILE { long long elapse = hwtime() - start; pOp->cycles += elapse; pOp->cnt++; #if 0 fprintf(stdout, "%10lld ", elapse); sqliteVdbePrintOp(stdout, origPc, &p->aOp[origPc]); #endif } #endif /* The following code adds nothing to the actual functionality ** of the program. It is only here for testing and debugging. ** On the other hand, it does burn CPU cycles every time through ................................................................................ bad_instruction: sprintf(zBuf,"%d",pc); sqliteSetString(&p->zErrMsg, "illegal operation at ", zBuf, 0); rc = SQLITE_INTERNAL; goto vdbe_halt; ) } |
Added src/vdbeInt.h.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
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 |
/* ** 2003 September 6 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This is the header file for information that is private to the ** VDBE. This information used to all be at the top of the single ** source code file "vdbe.c". When that file became too big (over ** 6000 lines long) it was split up into several smaller files and ** this header information was factored out. */ /* ** The makefile scans this source file and creates the following ** array of string constants which are the names of all VDBE opcodes. ** This array is defined in a separate source code file named opcode.c ** which is automatically generated by the makefile. */ extern char *sqliteOpcodeNames[]; /* ** SQL is translated into a sequence of instructions to be ** executed by a virtual machine. Each instruction is an instance ** of the following structure. */ typedef struct VdbeOp Op; /* ** Boolean values */ typedef unsigned char Bool; /* ** A cursor is a pointer into a single BTree within a database file. ** The cursor can seek to a BTree entry with a particular key, or ** loop over all entries of the Btree. You can also insert new BTree ** entries or retrieve the key or data from the entry that the cursor ** is currently pointing to. ** ** Every cursor that the virtual machine has open is represented by an ** instance of the following structure. ** ** If the Cursor.isTriggerRow flag is set it means that this cursor is ** really a single row that represents the NEW or OLD pseudo-table of ** a row trigger. The data for the row is stored in Cursor.pData and ** the rowid is in Cursor.iKey. */ struct Cursor { BtCursor *pCursor; /* The cursor structure of the backend */ int lastRecno; /* Last recno from a Next or NextIdx operation */ int nextRowid; /* Next rowid returned by OP_NewRowid */ Bool recnoIsValid; /* True if lastRecno is valid */ Bool keyAsData; /* The OP_Column command works on key instead of data */ Bool atFirst; /* True if pointing to first entry */ Bool useRandomRowid; /* Generate new record numbers semi-randomly */ Bool nullRow; /* True if pointing to a row with no data */ Bool nextRowidValid; /* True if the nextRowid field is valid */ Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */ Btree *pBt; /* Separate file holding temporary table */ int nData; /* Number of bytes in pData */ char *pData; /* Data for a NEW or OLD pseudo-table */ int iKey; /* Key for the NEW or OLD pseudo-table row */ }; typedef struct Cursor Cursor; /* ** A sorter builds a list of elements to be sorted. Each element of ** the list is an instance of the following structure. */ typedef struct Sorter Sorter; struct Sorter { int nKey; /* Number of bytes in the key */ char *zKey; /* The key by which we will sort */ int nData; /* Number of bytes in the data */ char *pData; /* The data associated with this key */ Sorter *pNext; /* Next in the list */ }; /* ** Number of buckets used for merge-sort. */ #define NSORT 30 /* ** Number of bytes of string storage space available to each stack ** layer without having to malloc. NBFS is short for Number of Bytes ** For Strings. */ #define NBFS 32 /* ** A single level of the stack is an instance of the following ** structure. Except, string values are stored on a separate ** list of of pointers to character. The reason for storing ** strings separately is so that they can be easily passed ** to the callback function. */ struct Stack { int i; /* Integer value */ int n; /* Number of characters in string value, including '\0' */ int flags; /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */ double r; /* Real value */ char z[NBFS]; /* Space for short strings */ }; typedef struct Stack Stack; /* ** Memory cells use the same structure as the stack except that space ** for an arbitrary string is added. */ struct Mem { Stack s; /* All values of the memory cell besides string */ char *z; /* String value for this memory cell */ }; typedef struct Mem Mem; /* ** Allowed values for Stack.flags */ #define STK_Null 0x0001 /* Value is NULL */ #define STK_Str 0x0002 /* Value is a string */ #define STK_Int 0x0004 /* Value is an integer */ #define STK_Real 0x0008 /* Value is a real number */ #define STK_Dyn 0x0010 /* Need to call sqliteFree() on zStack[] */ #define STK_Static 0x0020 /* zStack[] points to a static string */ #define STK_Ephem 0x0040 /* zStack[] points to an ephemeral string */ /* The following STK_ value appears only in AggElem.aMem.s.flag fields. ** It indicates that the corresponding AggElem.aMem.z points to a ** aggregate function context that needs to be finalized. */ #define STK_AggCtx 0x0040 /* zStack[] points to an agg function context */ /* ** The "context" argument for a installable function. A pointer to an ** instance of this structure is the first argument to the routines used ** implement the SQL functions. ** ** There is a typedef for this structure in sqlite.h. So all routines, ** even the public interface to SQLite, can use a pointer to this structure. ** But this file is the only place where the internal details of this ** structure are known. ** ** This structure is defined inside of vdbe.c because it uses substructures ** (Stack) which are only defined there. */ struct sqlite_func { FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ Stack s; /* Small strings, ints, and double values go here */ char *z; /* Space for holding dynamic string results */ void *pAgg; /* Aggregate context */ u8 isError; /* Set to true for an error */ u8 isStep; /* Current in the step function */ int cnt; /* Number of times that the step function has been called */ }; /* ** An Agg structure describes an Aggregator. Each Agg consists of ** zero or more Aggregator elements (AggElem). Each AggElem contains ** a key and one or more values. The values are used in processing ** aggregate functions in a SELECT. The key is used to implement ** the GROUP BY clause of a select. */ typedef struct Agg Agg; typedef struct AggElem AggElem; struct Agg { int nMem; /* Number of values stored in each AggElem */ AggElem *pCurrent; /* The AggElem currently in focus */ HashElem *pSearch; /* The hash element for pCurrent */ Hash hash; /* Hash table of all aggregate elements */ FuncDef **apFunc; /* Information about aggregate functions */ }; struct AggElem { char *zKey; /* The key to this AggElem */ int nKey; /* Number of bytes in the key, including '\0' at end */ Mem aMem[1]; /* The values for this AggElem */ }; /* ** A Set structure is used for quick testing to see if a value ** is part of a small set. Sets are used to implement code like ** this: ** x.y IN ('hi','hoo','hum') */ typedef struct Set Set; struct Set { Hash hash; /* A set is just a hash table */ HashElem *prev; /* Previously accessed hash elemen */ }; /* ** A Keylist is a bunch of keys into a table. The keylist can ** grow without bound. The keylist stores the ROWIDs of database ** records that need to be deleted or updated. */ typedef struct Keylist Keylist; struct Keylist { int nKey; /* Number of slots in aKey[] */ int nUsed; /* Next unwritten slot in aKey[] */ int nRead; /* Next unread slot in aKey[] */ Keylist *pNext; /* Next block of keys */ int aKey[1]; /* One or more keys. Extra space allocated as needed */ }; /* ** An instance of the virtual machine. This structure contains the complete ** state of the virtual machine. ** ** The "sqlite_vm" structure pointer that is returned by sqlite_compile() ** is really a pointer to an instance of this structure. */ struct Vdbe { sqlite *db; /* The whole database */ Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ FILE *trace; /* Write an execution trace here, if not NULL */ int nOp; /* Number of instructions in the program */ int nOpAlloc; /* Number of slots allocated for aOp[] */ Op *aOp; /* Space to hold the virtual machine's program */ int nLabel; /* Number of labels used */ int nLabelAlloc; /* Number of slots allocated in aLabel[] */ int *aLabel; /* Space to hold the labels */ int tos; /* Index of top of stack */ Stack *aStack; /* The operand stack, except string values */ char **zStack; /* Text or binary values of the stack */ char **azColName; /* Becomes the 4th parameter to callbacks */ int nCursor; /* Number of slots in aCsr[] */ Cursor *aCsr; /* One element of this array for each open cursor */ Sorter *pSort; /* A linked list of objects to be sorted */ FILE *pFile; /* At most one open file handler */ int nField; /* Number of file fields */ char **azField; /* Data for each file field */ int nVariable; /* Number of entries in azVariable[] */ char **azVariable; /* Values for the OP_Variable opcode */ char *zLine; /* A single line from the input file */ int nLineAlloc; /* Number of spaces allocated for zLine */ int magic; /* Magic number for sanity checking */ int nMem; /* Number of memory locations currently allocated */ Mem *aMem; /* The memory locations */ Agg agg; /* Aggregate information */ int nSet; /* Number of sets allocated */ Set *aSet; /* An array of sets */ int nCallback; /* Number of callbacks invoked so far */ Keylist *pList; /* A list of ROWIDs */ int keylistStackDepth; /* The size of the "keylist" stack */ Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */ int pc; /* The program counter */ int rc; /* Value to return */ unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */ int errorAction; /* Recovery action to do in case of an error */ int undoTransOnError; /* If error, either ROLLBACK or COMMIT */ int inTempTrans; /* True if temp database is transactioned */ int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */ int returnDepth; /* Next unused element in returnStack[] */ int nResColumn; /* Number of columns in one row of the result set */ char **azResColumn; /* Values for one row of result */ int (*xCallback)(void*,int,char**,char**); /* Callback for SELECT results */ void *pCbArg; /* First argument to xCallback() */ int popStack; /* Pop the stack this much on entry to VdbeExec() */ char *zErrMsg; /* Error message written here */ u8 explain; /* True if EXPLAIN present on SQL command */ }; /* ** The following are allowed values for Vdbe.magic */ #define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ #define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ #define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ /* ** Here is a macro to handle the common case of popping the stack ** once. This macro only works from within the sqliteVdbeExec() ** function. */ #define POPSTACK \ assert(p->tos>=0); \ if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \ p->tos--; /* ** Function prototypes */ void sqliteVdbeCleanupCursor(Cursor*); void sqliteVdbeSorterReset(Vdbe*); void sqliteVdbeAggReset(Agg*); void sqliteVdbeKeylistFree(Keylist*); void sqliteVdbePopStack(Vdbe*,int); #if !defined(NDEBUG) || defined(VDBE_PROFILE) void sqliteVdbePrintOp(FILE*, int, Op*); #endif |
Added src/vdbeaux.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
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 |
/* ** 2003 September 6 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains code used for creating, destroying, and populating ** a VDBE (or an "sqlite_vm" as it is known to the outside world.) Prior ** to version 2.8.7, all this code was combined into the vdbe.c source file. ** But that file was getting too big so this subroutines were split out. */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include "vdbeInt.h" /* ** When debugging the code generator in a symbolic debugger, one can ** set the sqlite_vdbe_addop_trace to 1 and all opcodes will be printed ** as they are added to the instruction stream. */ #ifndef NDEBUG int sqlite_vdbe_addop_trace = 0; #endif /* ** Create a new virtual database engine. */ Vdbe *sqliteVdbeCreate(sqlite *db){ Vdbe *p; p = sqliteMalloc( sizeof(Vdbe) ); if( p==0 ) return 0; p->db = db; if( db->pVdbe ){ db->pVdbe->pPrev = p; } p->pNext = db->pVdbe; p->pPrev = 0; db->pVdbe = p; p->magic = VDBE_MAGIC_INIT; return p; } /* ** Turn tracing on or off */ void sqliteVdbeTrace(Vdbe *p, FILE *trace){ p->trace = trace; } /* ** Add a new instruction to the list of instructions current in the ** VDBE. Return the address of the new instruction. ** ** Parameters: ** ** p Pointer to the VDBE ** ** op The opcode for this instruction ** ** p1, p2 First two of the three possible operands. ** ** Use the sqliteVdbeResolveLabel() function to fix an address and ** the sqliteVdbeChangeP3() function to change the value of the P3 ** operand. */ int sqliteVdbeAddOp(Vdbe *p, int op, int p1, int p2){ int i; i = p->nOp; p->nOp++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nOpAlloc ){ int oldSize = p->nOpAlloc; Op *aNew; p->nOpAlloc = p->nOpAlloc*2 + 100; aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); if( aNew==0 ){ p->nOpAlloc = oldSize; return 0; } p->aOp = aNew; memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); } p->aOp[i].opcode = op; p->aOp[i].p1 = p1; if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){ p2 = p->aLabel[-1-p2]; } p->aOp[i].p2 = p2; p->aOp[i].p3 = 0; p->aOp[i].p3type = P3_NOTUSED; #ifndef NDEBUG if( sqlite_vdbe_addop_trace ) sqliteVdbePrintOp(0, i, &p->aOp[i]); #endif return i; } /* ** Create a new symbolic label for an instruction that has yet to be ** coded. The symbolic label is really just a negative number. The ** label can be used as the P2 value of an operation. Later, when ** the label is resolved to a specific address, the VDBE will scan ** through its operation list and change all values of P2 which match ** the label into the resolved address. ** ** The VDBE knows that a P2 value is a label because labels are ** always negative and P2 values are suppose to be non-negative. ** Hence, a negative P2 value is a label that has yet to be resolved. */ int sqliteVdbeMakeLabel(Vdbe *p){ int i; i = p->nLabel++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nLabelAlloc ){ int *aNew; p->nLabelAlloc = p->nLabelAlloc*2 + 10; aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); if( aNew==0 ){ sqliteFree(p->aLabel); } p->aLabel = aNew; } if( p->aLabel==0 ){ p->nLabel = 0; p->nLabelAlloc = 0; return 0; } p->aLabel[i] = -1; return -1-i; } /* ** Resolve label "x" to be the address of the next instruction to ** be inserted. The parameter "x" must have been obtained from ** a prior call to sqliteVdbeMakeLabel(). */ void sqliteVdbeResolveLabel(Vdbe *p, int x){ int j; assert( p->magic==VDBE_MAGIC_INIT ); if( x<0 && (-x)<=p->nLabel && p->aOp ){ if( p->aLabel[-1-x]==p->nOp ) return; assert( p->aLabel[-1-x]<0 ); p->aLabel[-1-x] = p->nOp; for(j=0; j<p->nOp; j++){ if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp; } } } /* ** Return the address of the next instruction to be inserted. */ int sqliteVdbeCurrentAddr(Vdbe *p){ assert( p->magic==VDBE_MAGIC_INIT ); return p->nOp; } /* ** Add a whole list of operations to the operation stack. Return the ** address of the first operation added. */ int sqliteVdbeAddOpList(Vdbe *p, int nOp, VdbeOp const *aOp){ int addr; assert( p->magic==VDBE_MAGIC_INIT ); if( p->nOp + nOp >= p->nOpAlloc ){ int oldSize = p->nOpAlloc; Op *aNew; p->nOpAlloc = p->nOpAlloc*2 + nOp + 10; aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); if( aNew==0 ){ p->nOpAlloc = oldSize; return 0; } p->aOp = aNew; memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); } addr = p->nOp; if( nOp>0 ){ int i; for(i=0; i<nOp; i++){ int p2 = aOp[i].p2; p->aOp[i+addr] = aOp[i]; if( p2<0 ) p->aOp[i+addr].p2 = addr + ADDR(p2); p->aOp[i+addr].p3type = aOp[i].p3 ? P3_STATIC : P3_NOTUSED; #ifndef NDEBUG if( sqlite_vdbe_addop_trace ){ sqliteVdbePrintOp(0, i+addr, &p->aOp[i+addr]); } #endif } p->nOp += nOp; } return addr; } /* ** Change the value of the P1 operand for a specific instruction. ** This routine is useful when a large program is loaded from a ** static array using sqliteVdbeAddOpList but we want to make a ** few minor changes to the program. */ void sqliteVdbeChangeP1(Vdbe *p, int addr, int val){ assert( p->magic==VDBE_MAGIC_INIT ); if( p && addr>=0 && p->nOp>addr && p->aOp ){ p->aOp[addr].p1 = val; } } /* ** Change the value of the P2 operand for a specific instruction. ** This routine is useful for setting a jump destination. */ void sqliteVdbeChangeP2(Vdbe *p, int addr, int val){ assert( val>=0 ); assert( p->magic==VDBE_MAGIC_INIT ); if( p && addr>=0 && p->nOp>addr && p->aOp ){ p->aOp[addr].p2 = val; } } /* ** Change the value of the P3 operand for a specific instruction. ** This routine is useful when a large program is loaded from a ** static array using sqliteVdbeAddOpList but we want to make a ** few minor changes to the program. ** ** If n>=0 then the P3 operand is dynamic, meaning that a copy of ** the string is made into memory obtained from sqliteMalloc(). ** A value of n==0 means copy bytes of zP3 up to and including the ** first null byte. If n>0 then copy n+1 bytes of zP3. ** ** If n==P3_STATIC it means that zP3 is a pointer to a constant static ** string and we can just copy the pointer. n==P3_POINTER means zP3 is ** a pointer to some object other than a string. ** ** If addr<0 then change P3 on the most recently inserted instruction. */ void sqliteVdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ Op *pOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p==0 || p->aOp==0 ) return; if( addr<0 || addr>=p->nOp ){ addr = p->nOp - 1; if( addr<0 ) return; } pOp = &p->aOp[addr]; if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); pOp->p3 = 0; } if( zP3==0 ){ pOp->p3 = 0; pOp->p3type = P3_NOTUSED; }else if( n<0 ){ pOp->p3 = (char*)zP3; pOp->p3type = n; }else{ sqliteSetNString(&pOp->p3, zP3, n, 0); pOp->p3type = P3_DYNAMIC; } } /* ** If the P3 operand to the specified instruction appears ** to be a quoted string token, then this procedure removes ** the quotes. ** ** The quoting operator can be either a grave ascent (ASCII 0x27) ** or a double quote character (ASCII 0x22). Two quotes in a row ** resolve to be a single actual quote character within the string. */ void sqliteVdbeDequoteP3(Vdbe *p, int addr){ Op *pOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p->aOp==0 || addr<0 || addr>=p->nOp ) return; pOp = &p->aOp[addr]; if( pOp->p3==0 || pOp->p3[0]==0 ) return; if( pOp->p3type==P3_POINTER ) return; if( pOp->p3type!=P3_DYNAMIC ){ pOp->p3 = sqliteStrDup(pOp->p3); pOp->p3type = P3_DYNAMIC; } sqliteDequote(pOp->p3); } /* ** On the P3 argument of the given instruction, change all ** strings of whitespace characters into a single space and ** delete leading and trailing whitespace. */ void sqliteVdbeCompressSpace(Vdbe *p, int addr){ unsigned char *z; int i, j; Op *pOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p->aOp==0 || addr<0 || addr>=p->nOp ) return; pOp = &p->aOp[addr]; if( pOp->p3type==P3_POINTER ){ return; } if( pOp->p3type!=P3_DYNAMIC ){ pOp->p3 = sqliteStrDup(pOp->p3); pOp->p3type = P3_DYNAMIC; } z = (unsigned char*)pOp->p3; if( z==0 ) return; i = j = 0; while( isspace(z[i]) ){ i++; } while( z[i] ){ if( isspace(z[i]) ){ z[j++] = ' '; while( isspace(z[++i]) ){} }else{ z[j++] = z[i++]; } } while( j>0 && isspace(z[j-1]) ){ j--; } z[j] = 0; } /* ** Search for the current program for the given opcode and P2 ** value. Return the address plus 1 if found and 0 if not found. */ int sqliteVdbeFindOp(Vdbe *p, int op, int p2){ int i; assert( p->magic==VDBE_MAGIC_INIT ); for(i=0; i<p->nOp; i++){ if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1; } return 0; } /* ** Return the opcode for a given address. */ VdbeOp *sqliteVdbeGetOp(Vdbe *p, int addr){ assert( p->magic==VDBE_MAGIC_INIT ); assert( addr>=0 && addr<p->nOp ); return &p->aOp[addr]; } /* ** The following group or routines are employed by installable functions ** to return their results. ** ** The sqlite_set_result_string() routine can be used to return a string ** value or to return a NULL. To return a NULL, pass in NULL for zResult. ** A copy is made of the string before this routine returns so it is safe ** to pass in an ephemeral string. ** ** sqlite_set_result_error() works like sqlite_set_result_string() except ** that it signals a fatal error. The string argument, if any, is the ** error message. If the argument is NULL a generic substitute error message ** is used. ** ** The sqlite_set_result_int() and sqlite_set_result_double() set the return ** value of the user function to an integer or a double. ** ** These routines are defined here in vdbe.c because they depend on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ char *sqlite_set_result_string(sqlite_func *p, const char *zResult, int n){ assert( !p->isStep ); if( p->s.flags & STK_Dyn ){ sqliteFree(p->z); } if( zResult==0 ){ p->s.flags = STK_Null; n = 0; p->z = 0; p->s.n = 0; }else{ if( n<0 ) n = strlen(zResult); if( n<NBFS-1 ){ memcpy(p->s.z, zResult, n); p->s.z[n] = 0; p->s.flags = STK_Str; p->z = p->s.z; }else{ p->z = sqliteMallocRaw( n+1 ); if( p->z ){ memcpy(p->z, zResult, n); p->z[n] = 0; } p->s.flags = STK_Str | STK_Dyn; } p->s.n = n+1; } return p->z; } void sqlite_set_result_int(sqlite_func *p, int iResult){ assert( !p->isStep ); if( p->s.flags & STK_Dyn ){ sqliteFree(p->z); } p->s.i = iResult; p->s.flags = STK_Int; } void sqlite_set_result_double(sqlite_func *p, double rResult){ assert( !p->isStep ); if( p->s.flags & STK_Dyn ){ sqliteFree(p->z); } p->s.r = rResult; p->s.flags = STK_Real; } void sqlite_set_result_error(sqlite_func *p, const char *zMsg, int n){ assert( !p->isStep ); sqlite_set_result_string(p, zMsg, n); p->isError = 1; } /* ** Extract the user data from a sqlite_func structure and return a ** pointer to it. ** ** This routine is defined here in vdbe.c because it depends on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ void *sqlite_user_data(sqlite_func *p){ assert( p && p->pFunc ); return p->pFunc->pUserData; } /* ** Allocate or return the aggregate context for a user function. A new ** context is allocated on the first call. Subsequent calls return the ** same context that was returned on prior calls. ** ** This routine is defined here in vdbe.c because it depends on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ void *sqlite_aggregate_context(sqlite_func *p, int nByte){ assert( p && p->pFunc && p->pFunc->xStep ); if( p->pAgg==0 ){ if( nByte<=NBFS ){ p->pAgg = (void*)p->z; }else{ p->pAgg = sqliteMalloc( nByte ); } } return p->pAgg; } /* ** Return the number of times the Step function of a aggregate has been ** called. ** ** This routine is defined here in vdbe.c because it depends on knowing ** the internals of the sqlite_func structure which is only defined in ** this source file. */ int sqlite_aggregate_count(sqlite_func *p){ assert( p && p->pFunc && p->pFunc->xStep ); return p->cnt; } #if !defined(NDEBUG) || defined(VDBE_PROFILE) /* ** Print a single opcode. This routine is used for debugging only. */ void sqliteVdbePrintOp(FILE *pOut, int pc, Op *pOp){ char *zP3; char zPtr[40]; if( pOp->p3type==P3_POINTER ){ sprintf(zPtr, "ptr(%#x)", (int)pOp->p3); zP3 = zPtr; }else{ zP3 = pOp->p3; } if( pOut==0 ) pOut = stdout; fprintf(pOut,"%4d %-12s %4d %4d %s\n", pc, sqliteOpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3 ? zP3 : ""); fflush(pOut); } #endif /* ** Give a listing of the program in the virtual machine. ** ** The interface is the same as sqliteVdbeExec(). But instead of ** running the code, it invokes the callback once for each instruction. ** This feature is used to implement "EXPLAIN". */ int sqliteVdbeList( Vdbe *p /* The VDBE */ ){ sqlite *db = p->db; int i; static char *azColumnNames[] = { "addr", "opcode", "p1", "p2", "p3", "int", "text", "int", "int", "text", 0 }; assert( p->popStack==0 ); assert( p->explain ); p->azColName = azColumnNames; p->azResColumn = p->zStack; for(i=0; i<5; i++) p->zStack[i] = p->aStack[i].z; p->rc = SQLITE_OK; for(i=p->pc; p->rc==SQLITE_OK && i<p->nOp; i++){ if( db->flags & SQLITE_Interrupt ){ db->flags &= ~SQLITE_Interrupt; if( db->magic!=SQLITE_MAGIC_BUSY ){ p->rc = SQLITE_MISUSE; }else{ p->rc = SQLITE_INTERRUPT; } sqliteSetString(&p->zErrMsg, sqlite_error_string(p->rc), 0); break; } sprintf(p->zStack[0],"%d",i); sprintf(p->zStack[2],"%d", p->aOp[i].p1); sprintf(p->zStack[3],"%d", p->aOp[i].p2); if( p->aOp[i].p3type==P3_POINTER ){ sprintf(p->aStack[4].z, "ptr(%#x)", (int)p->aOp[i].p3); p->zStack[4] = p->aStack[4].z; }else{ p->zStack[4] = p->aOp[i].p3; } p->zStack[1] = sqliteOpcodeNames[p->aOp[i].opcode]; if( p->xCallback==0 ){ p->pc = i+1; p->azResColumn = p->zStack; p->nResColumn = 5; return SQLITE_ROW; } if( sqliteSafetyOff(db) ){ p->rc = SQLITE_MISUSE; break; } if( p->xCallback(p->pCbArg, 5, p->zStack, p->azColName) ){ p->rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ){ p->rc = SQLITE_MISUSE; } } return p->rc==SQLITE_OK ? SQLITE_DONE : SQLITE_ERROR; } /* ** Prepare a virtual machine for execution. This involves things such ** as allocating stack space and initializing the program counter. ** After the VDBE has be prepped, it can be executed by one or more ** calls to sqliteVdbeExec(). ** ** The behavior of sqliteVdbeExec() is influenced by the parameters to ** this routine. If xCallback is NULL, then sqliteVdbeExec() will return ** with SQLITE_ROW whenever there is a row of the result set ready ** to be delivered. p->azResColumn will point to the row and ** p->nResColumn gives the number of columns in the row. If xCallback ** is not NULL, then the xCallback() routine is invoked to process each ** row in the result set. */ void sqliteVdbeMakeReady( Vdbe *p, /* The VDBE */ sqlite_callback xCallback, /* Result callback */ void *pCallbackArg, /* 1st argument to xCallback() */ int isExplain /* True if the EXPLAIN keywords is present */ ){ int n; assert( p!=0 ); assert( p->aStack==0 ); assert( p->magic==VDBE_MAGIC_INIT ); /* Add a HALT instruction to the very end of the program. */ if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){ sqliteVdbeAddOp(p, OP_Halt, 0, 0); } /* No instruction ever pushes more than a single element onto the ** stack. And the stack never grows on successive executions of the ** same loop. So the total number of instructions is an upper bound ** on the maximum stack depth required. ** ** Allocation all the stack space we will ever need. */ n = isExplain ? 10 : p->nOp; p->aStack = sqliteMalloc( n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) ); p->zStack = (char**)&p->aStack[n]; p->azColName = (char**)&p->zStack[n]; sqliteHashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0); p->agg.pSearch = 0; #ifdef MEMORY_DEBUG if( sqliteOsFileExists("vdbe_trace") ){ p->trace = stdout; } #endif p->tos = -1; p->pc = 0; p->rc = SQLITE_OK; p->uniqueCnt = 0; p->returnDepth = 0; p->errorAction = OE_Abort; p->undoTransOnError = 0; p->xCallback = xCallback; p->pCbArg = pCallbackArg; p->popStack = 0; p->explain |= isExplain; p->magic = VDBE_MAGIC_RUN; #ifdef VDBE_PROFILE for(i=0; i<p->nOp; i++){ p->aOp[i].cnt = 0; p->aOp[i].cycles = 0; } #endif } /* ** Remove any elements that remain on the sorter for the VDBE given. */ void sqliteVdbeSorterReset(Vdbe *p){ while( p->pSort ){ Sorter *pSorter = p->pSort; p->pSort = pSorter->pNext; sqliteFree(pSorter->zKey); sqliteFree(pSorter->pData); sqliteFree(pSorter); } } /* ** Pop the stack N times. Free any memory associated with the ** popped stack elements. */ void sqliteVdbePopStack(Vdbe *p, int N){ assert( N>=0 ); if( p->zStack==0 ) return; assert( p->aStack || sqlite_malloc_failed ); if( p->aStack==0 ) return; while( N-- > 0 ){ if( p->aStack[p->tos].flags & STK_Dyn ){ sqliteFree(p->zStack[p->tos]); } p->aStack[p->tos].flags = 0; p->zStack[p->tos] = 0; p->tos--; } } /* ** Reset an Agg structure. Delete all its contents. ** ** For installable aggregate functions, if the step function has been ** called, make sure the finalizer function has also been called. The ** finalizer might need to free memory that was allocated as part of its ** private context. If the finalizer has not been called yet, call it ** now. */ void sqliteVdbeAggReset(Agg *pAgg){ int i; HashElem *p; for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ AggElem *pElem = sqliteHashData(p); assert( pAgg->apFunc!=0 ); for(i=0; i<pAgg->nMem; i++){ Mem *pMem = &pElem->aMem[i]; if( pAgg->apFunc[i] && (pMem->s.flags & STK_AggCtx)!=0 ){ sqlite_func ctx; ctx.pFunc = pAgg->apFunc[i]; ctx.s.flags = STK_Null; ctx.z = 0; ctx.pAgg = pMem->z; ctx.cnt = pMem->s.i; ctx.isStep = 0; ctx.isError = 0; (*pAgg->apFunc[i]->xFinalize)(&ctx); if( pMem->z!=0 && pMem->z!=pMem->s.z ){ sqliteFree(pMem->z); } }else if( pMem->s.flags & STK_Dyn ){ sqliteFree(pMem->z); } } sqliteFree(pElem); } sqliteHashClear(&pAgg->hash); sqliteFree(pAgg->apFunc); pAgg->apFunc = 0; pAgg->pCurrent = 0; pAgg->pSearch = 0; pAgg->nMem = 0; } /* ** Delete a keylist */ void sqliteVdbeKeylistFree(Keylist *p){ while( p ){ Keylist *pNext = p->pNext; sqliteFree(p); p = pNext; } } /* ** Close a cursor and release all the resources that cursor happens ** to hold. */ void sqliteVdbeCleanupCursor(Cursor *pCx){ if( pCx->pCursor ){ sqliteBtreeCloseCursor(pCx->pCursor); } if( pCx->pBt ){ sqliteBtreeClose(pCx->pBt); } sqliteFree(pCx->pData); memset(pCx, 0, sizeof(Cursor)); } /* ** Close all cursors */ static void closeAllCursors(Vdbe *p){ int i; for(i=0; i<p->nCursor; i++){ sqliteVdbeCleanupCursor(&p->aCsr[i]); } sqliteFree(p->aCsr); p->aCsr = 0; p->nCursor = 0; } /* ** Delete the variables in p->azVariable[] */ static void ClearVariableArray(Vdbe *p){ sqliteFree(p->azVariable); p->nVariable = 0; p->azVariable = 0; } /* ** Clean up the VM after execution. ** ** This routine will automatically close any cursors, lists, and/or ** sorters that were left open. It also deletes the values of ** variables in the azVariable[] array. */ static void Cleanup(Vdbe *p){ int i; sqliteVdbePopStack(p, p->tos+1); closeAllCursors(p); if( p->aMem ){ for(i=0; i<p->nMem; i++){ if( p->aMem[i].s.flags & STK_Dyn ){ sqliteFree(p->aMem[i].z); } } } sqliteFree(p->aMem); p->aMem = 0; p->nMem = 0; if( p->pList ){ sqliteVdbeKeylistFree(p->pList); p->pList = 0; } sqliteVdbeSorterReset(p); if( p->pFile ){ if( p->pFile!=stdin ) fclose(p->pFile); p->pFile = 0; } if( p->azField ){ sqliteFree(p->azField); p->azField = 0; } p->nField = 0; if( p->zLine ){ sqliteFree(p->zLine); p->zLine = 0; } p->nLineAlloc = 0; sqliteVdbeAggReset(&p->agg); if( p->aSet ){ for(i=0; i<p->nSet; i++){ sqliteHashClear(&p->aSet[i].hash); } } sqliteFree(p->aSet); p->aSet = 0; p->nSet = 0; if( p->keylistStack ){ int ii; for(ii = 0; ii < p->keylistStackDepth; ii++){ sqliteVdbeKeylistFree(p->keylistStack[ii]); } sqliteFree(p->keylistStack); p->keylistStackDepth = 0; p->keylistStack = 0; } sqliteFree(p->zErrMsg); p->zErrMsg = 0; ClearVariableArray(p); } /* ** Clean up a VDBE after execution but do not delete the VDBE just yet. ** Write any error messages into *pzErrMsg. Return the result code. ** ** After this routine is run, the VDBE should be ready to be executed ** again. */ int sqliteVdbeReset(Vdbe *p, char **pzErrMsg){ sqlite *db = p->db; int i; if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); return SQLITE_MISUSE; } if( p->zErrMsg ){ if( pzErrMsg && *pzErrMsg==0 ){ *pzErrMsg = p->zErrMsg; }else{ sqliteFree(p->zErrMsg); } p->zErrMsg = 0; } Cleanup(p); if( p->rc!=SQLITE_OK ){ switch( p->errorAction ){ case OE_Abort: { if( !p->undoTransOnError ){ for(i=0; i<db->nDb; i++){ if( db->aDb[i].pBt ){ sqliteBtreeRollbackCkpt(db->aDb[i].pBt); } } break; } /* Fall through to ROLLBACK */ } case OE_Rollback: { sqliteRollbackAll(db); db->flags &= ~SQLITE_InTrans; db->onError = OE_Default; break; } default: { if( p->undoTransOnError ){ sqliteRollbackAll(db); db->flags &= ~SQLITE_InTrans; db->onError = OE_Default; } break; } } sqliteRollbackInternalChanges(db); } for(i=0; i<db->nDb; i++){ if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){ sqliteBtreeCommitCkpt(db->aDb[i].pBt); db->aDb[i].inTrans = 1; } } assert( p->tos<p->pc || sqlite_malloc_failed==1 ); #ifdef VDBE_PROFILE { FILE *out = fopen("vdbe_profile.out", "a"); if( out ){ int i; fprintf(out, "---- "); for(i=0; i<p->nOp; i++){ fprintf(out, "%02x", p->aOp[i].opcode); } fprintf(out, "\n"); for(i=0; i<p->nOp; i++){ fprintf(out, "%6d %10lld %8lld ", p->aOp[i].cnt, p->aOp[i].cycles, p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 ); sqliteVdbePrintOp(out, i, &p->aOp[i]); } fclose(out); } } #endif p->magic = VDBE_MAGIC_INIT; return p->rc; } /* ** Clean up and delete a VDBE after execution. Return an integer which is ** the result code. Write any error message text into *pzErrMsg. */ int sqliteVdbeFinalize(Vdbe *p, char **pzErrMsg){ int rc; sqlite *db; if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); return SQLITE_MISUSE; } db = p->db; rc = sqliteVdbeReset(p, pzErrMsg); sqliteVdbeDelete(p); if( db->want_to_close && db->pVdbe==0 ){ sqlite_close(db); } return rc; } /* ** Set the values of all variables. Variable $1 in the original SQL will ** be the string azValue[0]. $2 will have the value azValue[1]. And ** so forth. If a value is out of range (for example $3 when nValue==2) ** then its value will be NULL. ** ** This routine overrides any prior call. */ int sqliteVdbeSetVariables(Vdbe *p, int nValue, const char **azValue){ int i, n; char *z; if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 || p->nVariable!=0 ){ return SQLITE_MISUSE; } ClearVariableArray(p); if( nValue==0 ){ p->nVariable = 0; p->azVariable = 0; } for(i=n=0; i<nValue; i++){ if( azValue[i] ) n += strlen(azValue[i]) + 1; } p->azVariable = sqliteMalloc( sizeof(p->azVariable[0])*nValue + n ); if( p->azVariable==0 ){ p->nVariable = 0; return SQLITE_NOMEM; } z = (char*)&p->azVariable[nValue]; for(i=0; i<nValue; i++){ if( azValue[i]==0 ){ p->azVariable[i] = 0; }else{ p->azVariable[i] = z; n = strlen(azValue[i]); memcpy(z, azValue[i], n+1); z += n+1; } } p->nVariable = nValue; return SQLITE_OK; } /* ** Delete an entire VDBE. */ void sqliteVdbeDelete(Vdbe *p){ int i; if( p==0 ) return; Cleanup(p); if( p->pPrev ){ p->pPrev->pNext = p->pNext; }else{ assert( p->db->pVdbe==p ); p->db->pVdbe = p->pNext; } if( p->pNext ){ p->pNext->pPrev = p->pPrev; } p->pPrev = p->pNext = 0; if( p->nOpAlloc==0 ){ p->aOp = 0; p->nOp = 0; } for(i=0; i<p->nOp; i++){ if( p->aOp[i].p3type==P3_DYNAMIC ){ sqliteFree(p->aOp[i].p3); } } sqliteFree(p->aOp); sqliteFree(p->aLabel); sqliteFree(p->aStack); p->magic = VDBE_MAGIC_DEAD; sqliteFree(p); } |