Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Refactoring of the vdbe Mem functions and the APIs that deal with them. The code will not compile in its current state. (CVS 1465) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
bba6684d502ba1ecd9614d2470ec9429 |
User & Date: | drh 2004-05-26 23:25:31.000 |
Context
2004-05-26
| ||
23:43 | Break Mem.flags into Mem.type and Mem.enc. (CVS 1466) (check-in: 68ef170286 user: drh tags: trunk) | |
23:25 | Refactoring of the vdbe Mem functions and the APIs that deal with them. The code will not compile in its current state. (CVS 1465) (check-in: bba6684d50 user: drh tags: trunk) | |
16:54 | Remove dataType and includeTypes flags from function definitions. Added new P3_FUNCDEF type for P3 arguments on opcodes. Fixes to several user functions. 28 tests fail now. (CVS 1464) (check-in: 36e0316259 user: drh tags: trunk) | |
Changes
Changes to src/date.c.
︙ | ︙ | |||
12 13 14 15 16 17 18 | ** This file contains the C functions that implement date and time ** functions for SQLite. ** ** There is only one exported symbol in this file - the function ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. ** All other code has file scope. ** | | | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ** This file contains the C functions that implement date and time ** functions for SQLite. ** ** There is only one exported symbol in this file - the function ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. ** All other code has file scope. ** ** $Id: date.c,v 1.26 2004/05/26 23:25:31 drh Exp $ ** ** NOTES: ** ** SQLite processes all times and dates as Julian Day numbers. The ** dates and times are stored as the number of days since noon ** in Greenwich on November 24, 4714 B.C. according to the Gregorian ** calendar system. |
︙ | ︙ | |||
641 642 643 644 645 646 647 | ** the resulting time into the DateTime structure p. Return 0 ** on success and 1 if there are any errors. */ static int isDate(int argc, sqlite3_value **argv, DateTime *p){ int i; if( argc==0 ) return 1; if( SQLITE3_NULL==sqlite3_value_type(argv[0]) || | | | | 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | ** the resulting time into the DateTime structure p. Return 0 ** on success and 1 if there are any errors. */ static int isDate(int argc, sqlite3_value **argv, DateTime *p){ int i; if( argc==0 ) return 1; if( SQLITE3_NULL==sqlite3_value_type(argv[0]) || parseDateOrTime(sqlite3_value_text(argv[0]), p) ) return 1; for(i=1; i<argc; i++){ if( SQLITE3_NULL==sqlite3_value_type(argv[i]) || parseModifier(sqlite3_value_text(argv[i]), p) ) return 1; } return 0; } /* ** The following routines implement the various date and time functions |
︙ | ︙ | |||
757 758 759 760 761 762 763 | sqlite3_context *context, int argc, sqlite3_value **argv ){ DateTime x; int n, i, j; char *z; | | | 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 | sqlite3_context *context, int argc, sqlite3_value **argv ){ DateTime x; int n, i, j; char *z; const char *zFmt = sqlite3_value_text(argv[0]); char zBuf[100]; if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return; for(i=0, n=1; zFmt[i]; i++, n++){ if( zFmt[i]=='%' ){ switch( zFmt[i+1] ){ case 'd': case 'H': |
︙ | ︙ |
Changes to src/func.c.
︙ | ︙ | |||
12 13 14 15 16 17 18 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** | | | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** ** $Id: func.c,v 1.58 2004/05/26 23:25:31 drh Exp $ */ #include <ctype.h> #include <math.h> #include <stdlib.h> #include <assert.h> #include "sqliteInt.h" #include "vdbeInt.h" |
︙ | ︙ | |||
84 85 86 87 88 89 90 | case SQLITE3_BLOB: case SQLITE3_INTEGER: case SQLITE3_FLOAT: { sqlite3_result_int32(context, sqlite3_value_bytes(argv[0])); break; } case SQLITE3_TEXT: { | | | | | | 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 | case SQLITE3_BLOB: case SQLITE3_INTEGER: case SQLITE3_FLOAT: { sqlite3_result_int32(context, sqlite3_value_bytes(argv[0])); break; } case SQLITE3_TEXT: { const char *z = sqlite3_value_text(argv[0]); for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } sqlite3_result_int32(context, len); break; } default: { sqlite3_result_null(context); break; } } } /* ** Implementation of the abs() function */ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ const char *z; assert( argc==1 ); switch( sqlite3_value_type(argv[0]) ){ case SQLITE3_INTEGER: { sqlite3_result_int64(context, -sqlite3_value_int64(argv[0])); break; } case SQLITE3_NULL: { sqlite3_result_null(context); break; } default: { sqlite3_result_double(context, -sqlite3_value_double(argv[0])); break; } } } /* ** Implementation of the substr() function */ static void substrFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ const char *z; const char *z2; int i; int p1, p2, len; assert( argc==3 ); z = sqlite3_value_text(argv[0]); if( z==0 ) return; p1 = sqlite3_value_int(argv[1]); p2 = sqlite3_value_int(argv[2]); for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } if( p1<0 ){ p1 += len; if( p1<0 ){ |
︙ | ︙ | |||
176 177 178 179 180 181 182 | if( argc==2 ){ if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return; n = sqlite3_value_int(argv[1]); if( n>30 ) n = 30; if( n<0 ) n = 0; } if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; | | | | | 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 | if( argc==2 ){ if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return; n = sqlite3_value_int(argv[1]); if( n>30 ) n = 30; if( n<0 ) n = 0; } if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; r = sqlite3_value_double(argv[0]); sprintf(zBuf,"%.*f",n,r); sqlite3_result_text(context, zBuf, -1, 1); } /* ** Implementation of the upper() and lower() SQL functions. */ static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ char *z; int i; if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; z = sqliteMalloc(sqlite3_value_bytes(argv[0])); if( z==0 ) return; strcpy(z, sqlite3_value_text(argv[0])); for(i=0; z[i]; i++){ if( islower(z[i]) ) z[i] = toupper(z[i]); } sqlite3_result_text(context, z, -1, 1); sqliteFree(z); } static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ char *z; int i; if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; z = sqliteMalloc(sqlite3_value_bytes(argv[0])); if( z==0 ) return; strcpy(z, sqlite3_value_text(argv[0])); for(i=0; z[i]; i++){ if( isupper(z[i]) ) z[i] = tolower(z[i]); } sqlite3_result_text(context, z, -1, 1); sqliteFree(z); } |
︙ | ︙ | |||
297 298 299 300 301 302 303 | ** is implemented as like(A,B). */ static void likeFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ | | | | | | 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 | ** is implemented as like(A,B). */ static void likeFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *zA = sqlite3_value_text(argv[0]); const unsigned char *zB = sqlite3_value_text(argv[1]); if( zA && zB ){ sqlite3_result_int32(context, sqlite3LikeCompare(zA, zB)); } } /* ** Implementation of the glob() SQL function. This function implements ** the build-in GLOB operator. The first argument to the function is the ** string and the second argument is the pattern. So, the SQL statements: ** ** A GLOB B ** ** is implemented as glob(A,B). */ static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){ const unsigned char *zA = sqlite3_value_text(argv[0]); const unsigned char *zB = sqlite3_value_text(argv[1]); if( zA && zB ){ sqlite3_result_int32(context, sqlite3GlobCompare(zA, zB)); } } /* ** Implementation of the NULLIF(x,y) function. The result is the first |
︙ | ︙ | |||
360 361 362 363 364 365 366 | ** Implementation of the QUOTE() function. This function takes a single ** argument. If the argument is numeric, the return value is the same as ** the argument. If the argument is NULL, the return value is the string ** "NULL". Otherwise, the argument is enclosed in single quotes with ** single-quote escapes. */ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ | < | | 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 | ** Implementation of the QUOTE() function. This function takes a single ** argument. If the argument is numeric, the return value is the same as ** the argument. If the argument is NULL, the return value is the string ** "NULL". Otherwise, the argument is enclosed in single quotes with ** single-quote escapes. */ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ if( argc<1 ) return; switch( sqlite3_value_type(argv[0]) ){ case SQLITE3_NULL: { sqlite3_result_text(context, "NULL", 4, 0); break; } case SQLITE3_INTEGER: case SQLITE3_FLOAT: { sqlite3_result(context, argv[0]); break; } case SQLITE3_BLOB: /*** FIX ME. Use a BLOB encoding ***/ case SQLITE3_TEXT: { int i,j,n; const char *zArg = sqlite3_value_text(argv[0]); char *z; for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } z = sqliteMalloc( i+n+3 ); if( z==0 ) return; z[0] = '\''; for(i=0, j=1; zArg[i]; i++){ |
︙ | ︙ | |||
415 416 417 418 419 420 421 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, }; assert( argc==1 ); | | | 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, }; assert( argc==1 ); zIn = sqlite3_value_text(argv[0]); for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} if( zIn[i] ){ zResult[0] = toupper(zIn[i]); for(j=1; j<4 && zIn[i]; i++){ int code = iCode[zIn[i]&0x7f]; if( code>0 ){ zResult[j++] = code + '0'; |
︙ | ︙ | |||
495 496 497 498 499 500 501 | /* ** Routines used to compute the sum or average. */ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ SumCtx *p; if( argc<1 ) return; | | | | | | 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 | /* ** Routines used to compute the sum or average. */ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ SumCtx *p; if( argc<1 ) return; p = sqlite3_aggregate_context(context, sizeof(*p)); if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){ p->sum += sqlite3_value_double(argv[0]); p->cnt++; } } static void sumFinalize(sqlite3_context *context){ SumCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); sqlite3_result_double(context, p ? p->sum : 0.0); } static void avgFinalize(sqlite3_context *context){ SumCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); if( p && p->cnt>0 ){ sqlite3_result_double(context, p->sum/(double)p->cnt); } } /* ** An instance of the following structure holds the context of a |
︙ | ︙ | |||
566 567 568 569 570 571 572 | }; /* ** Routines to implement the count() aggregate function. */ static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ CountCtx *p; | | | | | 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 | }; /* ** Routines to implement the count() aggregate function. */ static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ CountCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){ p->n++; } } static void countFinalize(sqlite3_context *context){ CountCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); sqlite3_result_int32(context, p ? p->n : 0); } /* ** This function tracks state information for the min() and max() ** aggregate functions. */ typedef struct MinMaxCtx MinMaxCtx; struct MinMaxCtx { char *z; /* The best so far */ char zBuf[28]; /* Space that can be used for storage */ }; /* ** Routines to implement min() and max() aggregate functions. */ static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ int max = 0; int cmp = 0; Mem *pArg = (Mem *)argv[0]; Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; if( pBest->flags ){ /* This step function is used for both the min() and max() aggregates, ** the only difference between the two being that the sense of the ** comparison is inverted. For the max() aggregate, the |
︙ | ︙ | |||
618 619 620 621 622 623 624 | } }else{ sqlite3VdbeMemCopy(pBest, pArg); } } static void minMaxFinalize(sqlite3_context *context){ sqlite3_value *pRes; | | | 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | } }else{ sqlite3VdbeMemCopy(pBest, pArg); } } static void minMaxFinalize(sqlite3_context *context){ sqlite3_value *pRes; pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); if( pRes->flags ){ sqlite3_result(context, pRes); } } /* ** This function registered all of the above C functions as SQL |
︙ | ︙ |
Changes to src/legacy.c.
︙ | ︙ | |||
10 11 12 13 14 15 16 | ** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** ** $Id: legacy.c,v 1.3 2004/05/26 23:25:31 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> /* |
︙ | ︙ | |||
86 87 88 89 90 91 92 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); } nCallback++; } if( rc==SQLITE_ROW ){ azVals = &azCols[nCol]; for(i=0; i<nCol; i++){ | | | 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); } nCallback++; } if( rc==SQLITE_ROW ){ azVals = &azCols[nCol]; for(i=0; i<nCol; i++){ azVals[i] = (char *)sqlite3_column_text(pStmt, i); } } if( xCallback(pArg, nCol, azVals, azCols) ){ rc = SQLITE_ABORT; goto exec_out; } } |
︙ | ︙ | |||
129 130 131 132 133 134 135 | } }else if( pzErrMsg ){ *pzErrMsg = 0; } return rc; } | < < | 129 130 131 132 133 134 135 | } }else if( pzErrMsg ){ *pzErrMsg = 0; } return rc; } |
Changes to src/md5.c.
︙ | ︙ | |||
356 357 358 359 360 361 362 | ** During testing, the special md5sum() aggregate function is available. ** inside SQLite. The following routines implement that function. */ static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ MD5Context *p; int i; if( argc<1 ) return; | | | | < < < | 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 | ** During testing, the special md5sum() aggregate function is available. ** inside SQLite. The following routines implement that function. */ static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ MD5Context *p; int i; if( argc<1 ) return; p = sqlite3_aggregate_context(context, sizeof(*p)); if( p==0 ) return; if( sqlite3_aggregate_count(context)==1 ){ MD5Init(p); } for(i=0; i<argc; i++){ const char *zData = sqlite3_value_text(argv[i]); if( zData ){ MD5Update(p, zData, strlen(zData)); } } } static void md5finalize(sqlite3_context *context){ MD5Context *p; unsigned char digest[16]; char zBuf[33]; p = sqlite3_aggregate_context(context, sizeof(*p)); MD5Final(digest,p); DigestToBase16(digest, zBuf); sqlite3_result_text(context, zBuf, -1, 1); } void Md5_Register(sqlite *db){ sqlite3_create_function(db, "md5sum", -1, 0, 0, 0, 0, md5step, md5finalize); } |
Changes to src/sqlite.h.in.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This header file defines the interface that the SQLite library ** presents to client programs. ** | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This header file defines the interface that the SQLite library ** presents to client programs. ** ** @(#) $Id: sqlite.h.in,v 1.82 2004/05/26 23:25:31 drh Exp $ */ #ifndef _SQLITE_H_ #define _SQLITE_H_ #include <stdarg.h> /* Needed for the definition of va_list */ /* ** Make sure we can call this stuff from C++. |
︙ | ︙ | |||
326 327 328 329 330 331 332 | /* ** Call this routine to free the memory that sqlite3_get_table() allocated. */ void sqlite3_free_table(char **result); /* | | | | | | > > | | 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | /* ** Call this routine to free the memory that sqlite3_get_table() allocated. */ void sqlite3_free_table(char **result); /* ** The following routines are variants of the "sprintf()" from the ** standard C library. The resulting string is written into memory ** obtained from malloc() so that there is never a possiblity of buffer ** overflow. These routines also implement some additional formatting ** options that are useful for constructing SQL statements. ** ** The strings returned by these routines should be freed by calling ** sqlite3_free(). ** ** All of the usual printf formatting options apply. In addition, there ** is a "%q" option. %q works like %s in that it substitutes a null-terminated ** string from the argument list. But %q also doubles every '\'' character. ** %q is designed for use inside a string literal. By doubling each '\'' ** character it escapes that character and allows it to be inserted into ** the string. |
︙ | ︙ | |||
363 364 365 366 367 368 369 | ** ** INSERT INTO table1 VALUES('It's a happy day!'); ** ** This second example is an SQL syntax error. As a general rule you ** should always use %q instead of %s when inserting text into a string ** literal. */ | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | ** ** INSERT INTO table1 VALUES('It's a happy day!'); ** ** This second example is an SQL syntax error. As a general rule you ** should always use %q instead of %s when inserting text into a string ** literal. */ char *sqlite3_mprintf(const char*,...); char *sqlite3_vmprintf(const char*, va_list); void sqlite3_free(char *z); /* ** Windows systems need functions to call to return the sqlite3_version ** and sqlite3_encoding strings. */ const char *sqlite3_libversion(void); const char *sqlite3_libencoding(void); |
︙ | ︙ | |||
529 530 531 532 533 534 535 | ** Registering a NULL function disables the callback. ** ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** */ void *sqlite3_commit_hook(sqlite*, int(*)(void*), void*); /* | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | > | | | | | | > | < < < < < < < < < < < < < < < < | 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 | ** Registering a NULL function disables the callback. ** ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** */ void *sqlite3_commit_hook(sqlite*, int(*)(void*), void*); /* ** Open the sqlite database file "filename". The "filename" is UTF-8 ** encoded for sqlite3_open() and UTF-16 encoded in the native byte order ** for sqlite3_open16(). An sqlite3* handle is returned in *ppDb, even ** if an error occurs. If the database is opened (or created) successfully, ** then SQLITE_OK is returned. Otherwise an error code is returned. The ** sqlite3_errmsg() or sqlite3_errmsg16() routines can be used to obtain ** an English language description of the error. ** ** If the database file does not exist, then a new database is created. ** The encoding for the database is UTF-8 if sqlite3_open() is called and ** UTF-16 if sqlite3_open16 is used. ** ** Whether or not an error occurs when it is opened, resources associated ** with the sqlite3* handle should be released by passing it to ** sqlite3_close() when it is no longer required. */ int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb, /* OUT: SQLite db handle */ const char **args /* Null terminated array of option strings */ ); int sqlite3_open16( const void *filename, /* Database filename (UTF-16) */ sqlite3 **ppDb, /* OUT: SQLite db handle */ const char **args /* Null terminated array of option strings */ ); /* |
︙ | ︙ | |||
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nBytes, /* Length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); /* ** Return the number of columns in the result set returned by the compiled ** SQL statement. This routine returns 0 if pStmt is an SQL statement ** that does not return data (for example an UPDATE). */ int sqlite3_column_count(sqlite3_stmt *pStmt); /* ** The first parameter is a compiled SQL statement. This function returns ** the column heading for the Nth column of that statement, where N is the | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > < < < < < < | 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 | sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nBytes, /* Length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); /* ** In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(), ** one or more literals can be replace by a wildcard "?" or ":N:" where ** N is an integer. These value of these wildcard literals can be set ** using the routines listed below. ** ** In every case, the first parameter is a pointer to the sqlite3_stmt ** structure returned from sqlite3_prepare(). The second parameter is the ** index of the wildcard. The first "?" has an index of 1. ":N:" wildcards ** use the index N. ** ** When the eCopy parameter is true, a copy of the value is made into ** memory obtained and managed by SQLite. When eCopy is false, SQLite ** assumes that the value is a constant and just stores a pointer to the ** value without making a copy. ** ** The sqlite3_bind_* routine must be called before sqlite3_step() after ** an sqlite3_prepare() or sqlite3_reset(). Unbound wildcards are interpreted ** as NULL. */ void sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, int eCopy); void sqlite3_bind_double(sqlite3_stmt*, int, double); void sqlite3_bind_int(sqlite3_stmt*, int, int); void sqlite3_bind_int64(sqlite3_stmt*, int, long long int); void sqlite3_bind_null(sqlite3_stmt*, int); void sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, int eCopy); void sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int n, int eCopy); void sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); /* ** Return the number of columns in the result set returned by the compiled ** SQL statement. This routine returns 0 if pStmt is an SQL statement ** that does not return data (for example an UPDATE). */ int sqlite3_column_count(sqlite3_stmt *pStmt); /* ** The first parameter is a compiled SQL statement. This function returns ** the column heading for the Nth column of that statement, where N is the ** second function parameter. The string returned is UTF-8 for ** sqlite3_column_name() and UTF-16 for sqlite3_column_name16(). */ const char *sqlite3_column_name(sqlite3_stmt*,int); const void *sqlite3_column_name16(sqlite3_stmt*,int); /* ** The first parameter is a compiled SQL statement. If this statement ** is a SELECT statement, the Nth column of the returned result set ** of the SELECT is a table column then the declared type of the table ** column is returned. If the Nth column of the result set is not at table |
︙ | ︙ | |||
764 765 766 767 768 769 770 | ** The first parameter is a compiled SQL statement. If this statement ** is a SELECT statement, the Nth column of the returned result set ** of the SELECT is a table column then the declared type of the table ** column is returned. If the Nth column of the result set is not at table ** column, then a NULL pointer is returned. The returned string is always ** UTF-16 encoded. For example, in the database schema: ** | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 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 | ** The first parameter is a compiled SQL statement. If this statement ** is a SELECT statement, the Nth column of the returned result set ** of the SELECT is a table column then the declared type of the table ** column is returned. If the Nth column of the result set is not at table ** column, then a NULL pointer is returned. The returned string is always ** UTF-16 encoded. For example, in the database schema: ** ** CREATE TABLE t1(c1 INTEGER); ** ** And the following statement compiled: ** ** SELECT c1 + 1, 0 FROM t1; ** ** Then this routine would return the string "INTEGER" for the second ** result column (i==1), and a NULL pointer for the first result column ** (i==0). */ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); /* ** After an SQL query has been compiled with a call to either ** sqlite3_prepare() or sqlite3_prepare16(), then this function must be ** called one or more times to execute the statement. ** ** The return value will be either SQLITE_BUSY, SQLITE_DONE, ** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. |
︙ | ︙ | |||
934 935 936 937 938 939 940 | ** will return the same value as the sqlite3_column_count() function. ** After sqlite3_step() has returned an SQLITE_DONE, SQLITE_BUSY or ** error code, or before sqlite3_step() has been called on a ** compiled SQL statement, this routine returns zero. */ int sqlite3_data_count(sqlite3_stmt *pStmt); | > > > > | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < > > | < | | > > < < | < < | | > | | | | | | | | | > > > > > > > > > > | > > | | < < < > > | | > > > > > > | > | | > > > > > > > > | | < < | < < < < < < < < < | | 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 | ** will return the same value as the sqlite3_column_count() function. ** After sqlite3_step() has returned an SQLITE_DONE, SQLITE_BUSY or ** error code, or before sqlite3_step() has been called on a ** compiled SQL statement, this routine returns zero. */ int sqlite3_data_count(sqlite3_stmt *pStmt); /* ** Values are stored in the database in one of the following fundamental ** types. */ #define SQLITE_INTEGER 1 #define SQLITE_FLOAT 2 #define SQLITE_TEXT 3 #define SQLITE_BLOB 4 #define SQLITE_NULL 5 /* ** The next group of routines returns information about the information ** in a single column of the current result row of a query. In every ** case the first parameter is a pointer to the SQL statement that is being ** executed (the sqlite_stmt* that was returned from sqlite3_prepare()) and ** the second argument is the index of the column for which information ** should be returned. iCol is zero-indexed. The left-most column as an ** index of 0. ** ** If the SQL statement is not currently point to a valid row, or if the ** the colulmn index is out of range, the result is undefined. ** ** These routines attempt to convert the value where appropriate. For ** example, if the internal representation is FLOAT and a text result ** is requested, sprintf() is used internally to do the conversion ** automatically. The following table details the conversions that ** are applied: ** ** Internal Type Requested Type Conversion ** ------------- -------------- -------------------------- ** NULL INTEGER Result is 0 ** NULL FLOAT Result is 0.0 ** NULL TEXT Result is an empty string ** NULL BLOB Result is a zero-length BLOB ** INTEGER FLOAT Convert from integer to float ** INTEGER TEXT ASCII rendering of the integer ** INTEGER BLOB Same as for INTEGER->TEXT ** FLOAT INTEGER Convert from float to integer ** FLOAT TEXT ASCII rendering of the float ** FLOAT BLOB Same as FLOAT->TEXT ** TEXT INTEGER Use atoi() ** TEXT FLOAT Use atof() ** TEXT BLOB No change ** BLOB INTEGER Convert to TEXT then use atoi() ** BLOB FLOAT Convert to TEXT then use atof() ** BLOB TEXT Add a \000 terminator if needed ** ** The following access routines are provided: ** ** _type() Return the datatype of the result. This is one of ** SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, ** or SQLITE_NULL. ** _blob() Return the value of a BLOB. ** _bytes() Return the number of bytes in a BLOB value or the number ** of bytes in a TEXT value represented as UTF-8. The \000 ** terminator is included in the byte count for TEXT values. ** _bytes16() Return the number of bytes in a BLOB value or the number ** of bytes in a TEXT value represented as UTF-16. The \u0000 ** terminator is included in the byte count for TEXT values. ** _double() Return a FLOAT value. ** _int() Return an INTEGER value in the host computer's native ** integer representation. This might be either a 32- or 64-bit ** integer depending on the host. ** _int64() Return an INTEGER value as a 64-bit signed integer. ** _text() Return the value as UTF-8 text. ** _text16() Return the value as UTF-16 text. */ void *sqlite3_column_blob(sqlite3_stmt*, int iCol) int sqlite3_column_bytes(sqlite3_stmt*, int iCol) int sqlite3_column_bytes16(sqlite3_stmt*, int iCol) double sqlite3_column_double(sqlite3_stmt*, int iCol) int sqlite3_column_int(sqlite3_stmt*, int iCol) long long int sqlite3_column_int64(sqlite3_stmt*, int iCol) const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol) const void *sqlite3_column_text16(sqlite3_stmt*, int iCol) int sqlite3_column_type(sqlite3_stmt*, int iCol); /* ** The sqlite3_finalize() function is called to delete a compiled ** SQL statement obtained by a previous call to sqlite3_prepare() ** or sqlite3_prepare16(). If the statement was executed successfully, or ** not executed at all, then SQLITE_OK is returned. If execution of the ** statement failed then an error code is returned. |
︙ | ︙ | |||
1127 1128 1129 1130 1131 1132 1133 | /* ** The next routine returns the number of calls to xStep for a particular ** aggregate function instance. The current call to xStep counts so this ** routine always returns at least 1. */ int sqlite3_aggregate_count(sqlite3_context*); | < < < < < < | < < < < < < < < < < < < | < < < < < | < | < | | < < < < < | | | < < < < | | < < < | < < < < < < < | | < | < | < < < < < < < < | < < | | < < < < < | < | < < < < | < < < < < | | < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | 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 | /* ** The next routine returns the number of calls to xStep for a particular ** aggregate function instance. The current call to xStep counts so this ** routine always returns at least 1. */ int sqlite3_aggregate_count(sqlite3_context*); /* ** The next group of routines returns information about parameters to ** a user-defined function. Function implementations use these routines ** to access their parameters. These routines are the same as the ** sqlite3_column_* routines except that these routines take a single ** sqlite3_value* pointer instead of an sqlite3_stmt* and an integer ** column number. */ void *sqlite3_value_blob(sqlite3_value*) int sqlite3_value_bytes(sqlite3_value*) int sqlite3_value_bytes16(sqlite3_value*) double sqlite3_value_double(sqlite3_value*) int sqlite3_value_int(sqlite3_value*) long long int sqlite3_value_int64(sqlite3_value*) const unsigned char *sqlite3_value_text(sqlite3_value*) const void *sqlite3_value_text16(sqlite3_value*) int sqlite3_value_type(sqlite3_value*); /* ** Aggregate functions use the following routine to allocate ** a structure for storing their state. The first time this routine ** is called for a particular aggregate, a new structure of size nBytes ** is allocated, zeroed, and returned. On subsequent calls (for the ** same aggregate instance) the same buffer is returned. The implementation ** of the aggregate can use the returned buffer to accumulate data. ** ** The buffer allocated is freed automatically by SQLite. */ void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); /* ** The pUserData parameter to the sqlite3_create_function() and ** sqlite3_create_aggregate() routines used to register user functions ** is available to the implementation of the function using this ** call. */ void *sqlite3_user_data(sqlite3_context*); /* ** User-defined functions invoke the following routines in order to ** set their return value. */ void sqlite3_result_blob(sqlite3_context*, const void*, int n, int eCopy); void sqlite3_result_double(sqlite3_context*, double); void sqlite3_result_error(sqlite3_context*, const char*, int); void sqlite3_result_error16(sqlite3_context*, const void*, int); void sqlite3_result_int(sqlite3_context*, int); void sqlite3_result_int64(sqlite3_context*, long long int); void sqlite3_result_null(sqlite3_context*); void sqlite3_result_text(sqlite3_context*, const char*, int n, int eCopy); void sqlite3_result_text16(sqlite3_context*, const void*, int n, int eCopy); void sqlite3_result_value(sqlite3_context*, sqlite3_value*); #ifdef __cplusplus } /* End of the 'extern "C"' block */ #endif #endif |
Changes to src/tclsqlite.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** A TCL Interface to SQLite ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** A TCL Interface to SQLite ** ** $Id: tclsqlite.c,v 1.73 2004/05/26 23:25:31 drh Exp $ */ #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ #include "sqliteInt.h" #include "tcl.h" #include <stdlib.h> #include <string.h> |
︙ | ︙ | |||
387 388 389 390 391 392 393 | Tcl_DStringInit(&cmd); Tcl_DStringAppend(&cmd, p->zScript, -1); for(i=0; i<argc; i++){ if( SQLITE3_NULL==sqlite3_value_type(argv[i]) ){ Tcl_DStringAppendElement(&cmd, ""); }else{ | | | 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | Tcl_DStringInit(&cmd); Tcl_DStringAppend(&cmd, p->zScript, -1); for(i=0; i<argc; i++){ if( SQLITE3_NULL==sqlite3_value_type(argv[i]) ){ Tcl_DStringAppendElement(&cmd, ""); }else{ Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i])); } } rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); if( rc ){ sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); }else{ sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, 1); |
︙ | ︙ |
Changes to src/test1.c.
︙ | ︙ | |||
9 10 11 12 13 14 15 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing the printf() interface to SQLite. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** | | | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing the printf() interface to SQLite. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** ** $Id: test1.c,v 1.59 2004/05/26 23:25:31 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include "os.h" #include <stdlib.h> #include <string.h> |
︙ | ︙ | |||
296 297 298 299 300 301 302 | ** Implementation of the x_coalesce() function. ** Return the first argument non-NULL argument. */ static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ int i; for(i=0; i<argc; i++){ if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){ | | | 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | ** Implementation of the x_coalesce() function. ** Return the first argument non-NULL argument. */ static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ int i; for(i=0; i<argc; i++){ if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){ sqlite3_result_text(context, sqlite3_value_text(argv[i]), -1, 1); break; } } } /* ** A structure into which to accumulate text. |
︙ | ︙ | |||
369 370 371 372 373 374 375 | sqlite3_context *context, int argc, sqlite3_value **argv ){ struct dstr x; memset(&x, 0, sizeof(x)); sqlite3_exec((sqlite*)sqlite3_user_data(context), | | | 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | sqlite3_context *context, int argc, sqlite3_value **argv ){ struct dstr x; memset(&x, 0, sizeof(x)); sqlite3_exec((sqlite*)sqlite3_user_data(context), sqlite3_value_text(argv[0]), execFuncCallback, &x, 0); sqlite3_result_text(context, x.z, x.nUsed, 1); sqliteFree(x.z); } /* ** Usage: sqlite_test_create_function DB |
︙ | ︙ | |||
419 420 421 422 423 424 425 | */ typedef struct CountCtx CountCtx; struct CountCtx { int n; }; static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ CountCtx *p; | | | | 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | */ typedef struct CountCtx CountCtx; struct CountCtx { int n; }; static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ CountCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0]) ) && p ){ p->n++; } } static void countFinalize(sqlite3_context *context){ CountCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); sqlite3_result_int32(context, p ? p->n : 0); } /* ** Usage: sqlite_test_create_aggregate DB ** ** Call the sqlite3_create_function API on the given database in order |
︙ | ︙ | |||
641 642 643 644 645 646 647 | /* ** The following routine is a user-defined SQL function whose purpose ** is to test the sqlite_set_result() API. */ static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ while( argc>=2 ){ | | | | 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | /* ** The following routine is a user-defined SQL function whose purpose ** is to test the sqlite_set_result() API. */ static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ while( argc>=2 ){ const char *zArg0 = sqlite3_value_text(argv[0]); const char *zArg1 = sqlite3_value_text(argv[1]); if( zArg0==0 ){ sqlite3_result_error(context, "first argument to test function " "may not be NULL", -1); }else if( sqlite3StrICmp(zArg0,"string")==0 ){ sqlite3_result_text(context, zArg1, -1, 1); }else if( zArg1==0 ){ sqlite3_result_error(context, "2nd argument may not be NULL if the " |
︙ | ︙ | |||
1312 1313 1314 1315 1316 1317 1318 | if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; Tcl_SetResult(interp, (char *)errorName(rc), 0); return TCL_OK; } /* | | | | | | | | | 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 | if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; Tcl_SetResult(interp, (char *)errorName(rc), 0); return TCL_OK; } /* ** Usage: sqlite3_column_text STMT column ** ** Advance the statement to the next row. */ static int test_column_text( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ sqlite3_stmt *pStmt; int col; Tcl_Obj *pRet; if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetString(objv[0]), " STMT column", 0); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; if( SQLITE3_BLOB==sqlite3_column_type(pStmt, col) ){ int len = sqlite3_column_bytes(pStmt, col); pRet = Tcl_NewByteArrayObj(sqlite3_column_text(pStmt, col), len); }else{ pRet = Tcl_NewStringObj(sqlite3_column_text(pStmt, col), -1); } Tcl_SetObjResult(interp, pRet); return TCL_OK; } /* ** Usage: sqlite3_column_text16 STMT column ** ** Advance the statement to the next row. */ static int test_column_text16( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ sqlite3_stmt *pStmt; int col; Tcl_Obj *pRet; int len; if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetString(objv[0]), " STMT column", 0); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; len = sqlite3_column_bytes16(pStmt, col); pRet = Tcl_NewByteArrayObj(sqlite3_column_text16(pStmt, col), len); Tcl_SetObjResult(interp, pRet); return TCL_OK; } /* ** Usage: sqlite3_column_name STMT column |
︙ | ︙ | |||
1659 1660 1661 1662 1663 1664 1665 | { "sqlite3_prepare", (Tcl_ObjCmdProc*)test_prepare }, { "sqlite3_prepare16", (Tcl_ObjCmdProc*)test_prepare16 }, { "sqlite3_open", (Tcl_ObjCmdProc*)test_open }, { "sqlite3_open16", (Tcl_ObjCmdProc*)test_open16 }, { "sqlite3_finalize", (Tcl_ObjCmdProc*)test_finalize }, { "sqlite3_reset", (Tcl_ObjCmdProc*)test_reset }, { "sqlite3_step", (Tcl_ObjCmdProc*)test_step}, | | | | 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 | { "sqlite3_prepare", (Tcl_ObjCmdProc*)test_prepare }, { "sqlite3_prepare16", (Tcl_ObjCmdProc*)test_prepare16 }, { "sqlite3_open", (Tcl_ObjCmdProc*)test_open }, { "sqlite3_open16", (Tcl_ObjCmdProc*)test_open16 }, { "sqlite3_finalize", (Tcl_ObjCmdProc*)test_finalize }, { "sqlite3_reset", (Tcl_ObjCmdProc*)test_reset }, { "sqlite3_step", (Tcl_ObjCmdProc*)test_step}, { "sqlite3_column_text", (Tcl_ObjCmdProc*)test_column_text }, { "sqlite3_column_text16", (Tcl_ObjCmdProc*)test_column_text16 }, { "sqlite3_column_count", (Tcl_ObjCmdProc*)test_column_count }, { "sqlite3_column_name", (Tcl_ObjCmdProc*)test_column_name }, { "sqlite3_column_name16", (Tcl_ObjCmdProc*)test_column_name16 }, { "sqlite3_column_type", (Tcl_ObjCmdProc*)test_column_type }, { "sqlite3_column_int", (Tcl_ObjCmdProc*)test_column_int }, { "sqlite3_data_count", (Tcl_ObjCmdProc*)test_data_count }, { "add_reverse_collating_func", (Tcl_ObjCmdProc*)reverse_collfunc }, |
︙ | ︙ |
Changes to src/test4.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2003 December 18 ** ** 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. ** ************************************************************************* ** Code for testing the the SQLite library in a multithreaded environment. ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2003 December 18 ** ** 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. ** ************************************************************************* ** Code for testing the the SQLite library in a multithreaded environment. ** ** $Id: test4.c,v 1.8 2004/05/26 23:25:31 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include "os.h" #if defined(OS_UNIX) && OS_UNIX==1 && defined(THREADSAFE) && THREADSAFE==1 #include <stdlib.h> #include <string.h> |
︙ | ︙ | |||
494 495 496 497 498 499 500 | p->rc = SQLITE_ERROR; return; } p->rc = sqlite3_step(p->pStmt); if( p->rc==SQLITE_ROW ){ p->argc = sqlite3_column_count(p->pStmt); for(i=0; i<sqlite3_data_count(p->pStmt); i++){ | | | 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | p->rc = SQLITE_ERROR; return; } p->rc = sqlite3_step(p->pStmt); if( p->rc==SQLITE_ROW ){ p->argc = sqlite3_column_count(p->pStmt); for(i=0; i<sqlite3_data_count(p->pStmt); i++){ p->argv[i] = sqlite3_column_text(p->pStmt, i); } for(i=0; i<p->argc; i++){ p->colv[i] = sqlite3_column_name(p->pStmt, i); } } } |
︙ | ︙ | |||
644 645 646 647 648 649 650 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); } return TCL_OK; } #else int Sqlitetest4_Init(Tcl_Interp *interp){ return TCL_OK; } #endif /* OS_UNIX */ | < < < | 644 645 646 647 648 649 650 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); } return TCL_OK; } #else int Sqlitetest4_Init(Tcl_Interp *interp){ return TCL_OK; } #endif /* OS_UNIX */ |
Changes to src/vdbe.c.
︙ | ︙ | |||
39 40 41 42 43 44 45 | ** ** 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. ** | | | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ** ** 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.336 2004/05/26 23:25:31 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include "vdbeInt.h" /* |
︙ | ︙ | |||
71 72 73 74 75 76 77 | /* ** This macro takes a single parameter, a pointer to a Mem structure. ** It returns the string encoding for the Mem structure, one of TEXT_Utf8 ** TEXT_Utf16le or TEXT_Utf16be. */ #define MemEnc(p) ( \ | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | /* ** This macro takes a single parameter, a pointer to a Mem structure. ** It returns the string encoding for the Mem structure, one of TEXT_Utf8 ** TEXT_Utf16le or TEXT_Utf16be. */ #define MemEnc(p) ( \ ((p)->flags&MEM_Utf16le)?TEXT_Utf16le: \ ((p)->flags&MEM_Utf16be)?TEXT_Utf16be:TEXT_Utf8) ) /* ** Release the memory associated with the given stack level. This ** leaves the Mem.flags field in an inconsistent state. */ #define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); } |
︙ | ︙ | |||
219 220 221 222 223 224 225 | /* ** Parameter "enc" is one of TEXT_Utf8, TEXT_Utf16le or TEXT_Utf16be. ** Return the corresponding MEM_Utf* value. */ static int encToFlags(u8 enc){ switch( enc ){ | | < < < < < < < < < < < < | | 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 | /* ** Parameter "enc" is one of TEXT_Utf8, TEXT_Utf16le or TEXT_Utf16be. ** Return the corresponding MEM_Utf* value. */ static int encToFlags(u8 enc){ switch( enc ){ case TEXT_Utf8: return MEM_Utf8; case TEXT_Utf16be: return MEM_Utf16be; case TEXT_Utf16le: return MEM_Utf16le; } assert(0); } /* ** Set the encoding flags of memory cell "pMem" to the correct values ** for the database encoding "enc" (one of TEXT_Utf8, TEXT_Utf16le or ** TEXT_Utf16be). */ #define SetEncodingFlags(pMem, enc) ((pMem)->flags = \ ((pMem->flags & ~(MEM_Utf8|MEM_Utf16le|MEM_Utf16be))) | encToFlags(enc)) static int SetEncoding(Mem*, int); /* ** Convert the given stack entity into a string if it isn't one ** already. Return non-zero if a malloc() fails. */ #define Stringify(P, enc) \ (!((P)->flags&(MEM_Str|MEM_Blob)) && hardStringify(P, enc)) static int hardStringify(Mem *pStack, u8 enc){ int rc = SQLITE_OK; int fg = pStack->flags; assert( !(fg&(MEM_Str|MEM_Blob)) ); assert( fg&(MEM_Int|MEM_Real|MEM_Null) ); |
︙ | ︙ | |||
340 341 342 343 344 345 346 | memcpy(z, pStack->z, pStack->n); pStack->z = z; pStack->flags &= ~MEM_Ephem; pStack->flags |= MEM_Dyn; return 0; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | memcpy(z, pStack->z, pStack->n); pStack->z = z; pStack->flags &= ~MEM_Ephem; pStack->flags |= MEM_Dyn; return 0; } /* ** Convert the given stack entity into a integer if it isn't one ** already. ** ** Any prior string or real representation is invalidated. ** NULLs are converted into 0. */ |
︙ | ︙ | |||
502 503 504 505 506 507 508 | }else{ pStack->r = 0.0; } /* pStack->flags |= MEM_Real; */ pStack->flags = MEM_Real; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | }else{ pStack->r = 0.0; } /* pStack->flags |= MEM_Real; */ pStack->flags = MEM_Real; } /* ** Insert a new aggregate element and make it the element that ** has focus. ** ** Return 0 on success and 1 if memory is exhausted. |
︙ | ︙ | |||
1789 1790 1791 1792 1793 1794 1795 | case OP_String: { char *z = pOp->p3; u8 op = pOp->opcode; pTos++; pTos->flags = 0; | < < < < < < < < < < | > | > > > > > > > > > > > > > | > > > > > > > | 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 | case OP_String: { char *z = pOp->p3; u8 op = pOp->opcode; pTos++; pTos->flags = 0; /* If this is an OP_Real or OP_Integer opcode, set the pTos->r or pTos->i ** values respectively. */ if( op==OP_Real ){ assert( z ); assert( sqlite3IsNumber(z, 0, TEXT_Utf8) ); pTos->r = sqlite3AtoF(z, 0); pTos->flags = MEM_Real; }else if( op==OP_Integer ){ pTos->flags = MEM_Int; pTos->i = pOp->p1; if( pTos->i==0 && pOp->p3 ){ sqlite3GetInt64(pOp->p3, &pTos->i); } } if( z ){ /* FIX ME: For now the code in expr.c always puts UTF-8 in P3. It ** should transform text to the native encoding before doing so. */ if( db->enc!=TEXT_Utf8 ){ rc = sqlite3utfTranslate(z, -1, TEXT_Utf8, (void **)&pTos->z, &pTos->n, db->enc); if( rc!=SQLITE_OK ){ assert( !pTos->z ); goto abort_due_to_error; } pTos->flags |= MEM_Str | MEM_Dyn | MEM_Term; }else{ pTos->z = z; pTos->n = strlen(z) + 1; pTos->flags |= MEM_Str | MEM_Static | MEM_Term; } }else if( op==OP_String ){ pTos->flags = MEM_Null; } break; } /* Opcode: Variable P1 * * ** |
︙ | ︙ | |||
2032 2033 2034 2035 2036 2037 2038 | case OP_Callback: { int i; assert( p->nResColumn==pOp->p1 ); for(i=0; i<pOp->p1; i++){ Mem *pVal = &pTos[0-i]; SetEncodingFlags(pVal, db->enc); | | < | 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 | case OP_Callback: { int i; assert( p->nResColumn==pOp->p1 ); for(i=0; i<pOp->p1; i++){ Mem *pVal = &pTos[0-i]; SetEncodingFlags(pVal, db->enc); sqlite3VdbeMemNulTerminate(pVal); } p->resOnStack = 1; p->nCallback++; p->popStack = pOp->p1; p->pc = pc + 1; p->pTos = pTos; |
︙ | ︙ | |||
2284 2285 2286 2287 2288 2289 2290 | n = pOp->p1; apVal = p->apArg; assert( apVal || n==0 ); pArg = &pTos[1-n]; for(i=0; i<n; i++, pArg++){ SetEncodingFlags(pArg, db->enc); | < | 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 | n = pOp->p1; apVal = p->apArg; assert( apVal || n==0 ); pArg = &pTos[1-n]; for(i=0; i<n; i++, pArg++){ SetEncodingFlags(pArg, db->enc); apVal[i] = pArg; } ctx.pFunc = (FuncDef*)pOp->p3; ctx.s.flags = MEM_Null; ctx.s.z = 0; ctx.isError = 0; |
︙ | ︙ | |||
5632 5633 5634 5635 5636 5637 5638 | pRec = &pTos[-n]; assert( pRec>=p->aStack ); apVal = p->apArg; assert( apVal || n==0 ); for(i=0; i<n; i++, pRec++){ | | | < | 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 | pRec = &pTos[-n]; assert( pRec>=p->aStack ); apVal = p->apArg; assert( apVal || n==0 ); for(i=0; i<n; i++, pRec++){ apVal[i] = pRec; SetEncodingFlags(pRec, db->enc); } i = pTos->i; assert( i>=0 && i<p->agg.nMem ); ctx.pFunc = (FuncDef*)pOp->p3; pMem = &p->agg.pCurrent->aMem[i]; ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */ ctx.pAgg = pMem->z; |
︙ | ︙ |
Changes to src/vdbeInt.h.
︙ | ︙ | |||
133 134 135 136 137 138 139 | int flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ double r; /* Real value */ char *z; /* String or BLOB value */ char zShort[NBFS]; /* Space for short strings */ }; typedef struct Mem Mem; | < < < < < < < < < < < < < < < < < < < < < < < < < | 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | int flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ double r; /* Real value */ char *z; /* String or BLOB value */ char zShort[NBFS]; /* Space for short strings */ }; typedef struct Mem Mem; /* One or more of the following flags are set to indicate the valid ** representations of the value stored in the Mem struct. ** ** If the MEM_Null flag is set, then the value is an SQL NULL value. ** No other flags may be set in this case. ** ** If the MEM_Str flag is set then Mem.z points at a string representation. |
︙ | ︙ | |||
211 212 213 214 215 216 217 | ** flags, which describe the text encoding of the string if the MEM_Str ** flag is true, are not generally valid for Mem* objects handled by the ** VDBE. ** ** When a user-defined function is called (see OP_Function), the Mem* ** objects that store the argument values for the function call are ** passed to the user-defined function routine cast to sqlite3_value*. | | | | | 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 | ** flags, which describe the text encoding of the string if the MEM_Str ** flag is true, are not generally valid for Mem* objects handled by the ** VDBE. ** ** When a user-defined function is called (see OP_Function), the Mem* ** objects that store the argument values for the function call are ** passed to the user-defined function routine cast to sqlite3_value*. ** The user routine may then call sqlite3_value_text() or ** sqlite3_value_text16() to request a UTF-8 or UTF-16 string. If the ** string representation currently stored in Mem.z is not the requested ** encoding, then a translation occurs. To keep track of things, the ** MEM_Utf* flags are set correctly for the database encoding before a ** user-routine is called, and kept up to date if any translations occur ** thereafter. ** ** When sqlite3_step() returns SQLITE3_ROW, indicating that a row of data ** is ready for processing by the caller, the data values are stored ** internally as Mem* objects. Before sqlite3_step() returns, the MEM_Utf* ** flags are set correctly for the database encoding. A translation may ** take place if the user requests a non-native encoding via ** sqlite3_column_text() or sqlite3_column_text16(). If this occurs, then ** the MEM_Utf* flags are updated accordingly. */ #define MEM_Utf8 0x2000 /* String uses UTF-8 encoding */ #define MEM_Utf16be 0x4000 /* String uses UTF-16 big-endian */ #define MEM_Utf16le 0x8000 /* String uses UTF-16 little-endian */ /* The following MEM_ value appears only in AggElem.aMem.s.flag fields. |
︙ | ︙ |
Added src/vdbeapi.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 | /* ** 2004 May 26 ** ** 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 use to implement APIs that are part of the ** VDBE. */ #include "sqliteInt.h" #include "vdbeInt.h" /**************************** sqlite3_value_ ******************************* ** The following routines extract information from a Mem or sqlite3_value ** structure. */ const void *sqlite3_value_blob(sqlite3_value *pVal){ Mem *p = (Mem*)pVal; if( p->flags & (MEM_Blob|MEM_Str) ){ return p->z; }else{ return sqlite3_value_text(pVal); } } int sqlite3_value_bytes(sqlite3_value *pVal){ Mem *p = (Mem*)pVal; if( (p->flags & MEM_Blob)!=0 || sqlite3_value_text(pVal) ){ return p->n; } return 0; } int sqlite3_value_bytes16(sqlite3_value *pVal){ Mem *p = (Mem*)pVal; if( (p->flags & MEM_Blob)!=0 || sqlite3_value_text16(pVal) ){ return ((Mem *)pVal)->n; } return 0; } double sqlite3_value_double(sqlite3_value *pVal){ Mem *pMem = (Mem *)pVal; Realify(pMem, flagsToEnc(pMem->flags)); return pMem->r; } int sqlite3_value_int(sqlite3_value *pVal){ Mem *pMem = (Mem *)pVal; Integerify(pMem, flagsToEnc(pMem->flags)); return (int)pVal->i; } long long int sqlite3_value_int64(sqlite3_value *pVal){ Mem *pMem = (Mem *)pVal; Integerify(pMem, flagsToEnc(pMem->flags)); return pVal->i; } const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ if( pVal->flags&MEM_Null ){ /* For a NULL return a NULL Pointer */ return 0; } if( pVal->flags&MEM_Str ){ /* If there is already a string representation, make sure it is in ** encoded in UTF-8. */ SetEncoding(pVal, MEM_Utf8|MEM_Term); }else if( !(pVal->flags&MEM_Blob) ){ /* Otherwise, unless this is a blob, convert it to a UTF-8 string */ Stringify(pVal, TEXT_Utf8); } return pVal->z; } const void *sqlite3_value_text16(sqlite3_value* pVal){ if( pVal->flags&MEM_Null ){ /* For a NULL return a NULL Pointer */ return 0; } if( pVal->flags&MEM_Str ){ /* If there is already a string representation, make sure it is in ** encoded in UTF-16 machine byte order. */ SetEncoding(pVal, encToFlags(TEXT_Utf16)|MEM_Term); }else if( !(pVal->flags&MEM_Blob) ){ /* Otherwise, unless this is a blob, convert it to a UTF-16 string */ Stringify(pVal, TEXT_Utf16); } return (const void *)(pVal->z); } int sqlite3_value_type(sqlite3_value* pVal){ int f = ((Mem *)pVal)->flags; if( f&MEM_Null ){ return SQLITE3_NULL; } if( f&MEM_Int ){ return SQLITE3_INTEGER; } if( f&MEM_Real ){ return SQLITE3_FLOAT; } if( f&MEM_Str ){ return SQLITE3_TEXT; } if( f&MEM_Blob ){ return SQLITE3_BLOB; } assert(0); } /**************************** sqlite3_result_ ******************************* ** The following routines are used by user-defined functions to specify ** the function result. */ void sqlite3_result_blob( sqlite3_context *pCtx, const void *z, int n, int eCopy ){ assert( n>0 ); MemSetStr(&pCtx->s, z, n, 0, eCopy); } void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ sqlite3VdbeMemSetDouble(&pCtx->s, rVal); } void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ pCtx->isError = 1; sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1); } void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ pCtx->isError = 1; sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1); } void sqlite3_result_int32(sqlite3_context *pCtx, int iVal){ sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); } void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ sqlite3VdbeMemSetInt64(&pCtx->s, iVal); } void sqlite3_result_null(sqlite3_context *pCtx){ sqilte3VdbeMemSetNull(&pCtx->s); } void sqlite3_result_text( sqlite3_context *pCtx, const char *z, int n, int eCopy ){ MemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy); } void sqlite3_result_text16( sqlite3_context *pCtx, const void *z, int n, int eCopy ){ MemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy); } void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ sqlite3VdbeMemCopy(&pCtx->s, pValue); } /* ** Execute the statement pStmt, either until a row of data is ready, the ** statement is completely executed or an error occurs. */ int sqlite3_step(sqlite3_stmt *pStmt){ Vdbe *p = (Vdbe*)pStmt; sqlite *db; int rc; if( p->magic!=VDBE_MAGIC_RUN ){ return SQLITE_MISUSE; } db = p->db; if( sqlite3SafetyOn(db) ){ p->rc = SQLITE_MISUSE; return SQLITE_MISUSE; } if( p->explain ){ rc = sqlite3VdbeList(p); }else{ rc = sqlite3VdbeExec(p); } if( sqlite3SafetyOff(db) ){ rc = SQLITE_MISUSE; } sqlite3Error(p->db, rc, p->zErrMsg); return rc; } /* ** Return the number of columns in the result set for the statement pStmt. */ int sqlite3_column_count(sqlite3_stmt *pStmt){ Vdbe *pVm = (Vdbe *)pStmt; return pVm->nResColumn; } /* ** Return the number of values available from the current row of the ** currently executing statement pStmt. */ int sqlite3_data_count(sqlite3_stmt *pStmt){ Vdbe *pVm = (Vdbe *)pStmt; if( !pVm->resOnStack ) return 0; return pVm->nResColumn; } /* ** Check to see if column iCol of the given statement is valid. If ** it is, return a pointer to the Mem for the value of that column. ** If iCol is not valid, return a pointer to a Mem which has a value ** of NULL. */ static Mem *columnMem(sqlite3_stmt *pStmt, int i){ Vdbe *pVm = (Vdbe *)pStmt; int vals = sqlite3_data_count(pStmt); if( i>=vals || i<0 ){ static Mem nullMem; if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } sqlite3Error(pVm->db, SQLITE_RANGE, 0); return &nullMem; } return &pVm->pTos[(1-vals)+i]; } /**************************** sqlite3_column_ ******************************* ** The following routines are used to access elements of the current row ** in the result set. */ int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ return sqlite3_value_bytes( columnMem(pStmt,i) ); } int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ return sqlite3_value_bytes16( columnMem(pStmt,i) ); } double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ return sqlite3_value_double( columnMem(pStmt,i) ); } int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ return sqlite3_value_int( columnMem(pStmt,i) ); } long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ return sqlite3_value_int64( columnMem(pStmt,i) ); } const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ return sqlite3_value_text( columnMem(pStmt,i) ); } const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ return sqlite3_value_text16( columnMem(pStmt,i) ); } int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ return sqlite3_value_type( columnMem(pStmt,i) ); } /* ** Return the name of the Nth column of the result set returned by SQL ** statement pStmt. */ const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ Vdbe *p = (Vdbe *)pStmt; Mem *pColName; if( N>=sqlite3_column_count(pStmt) || N<0 ){ sqlite3Error(p->db, SQLITE_RANGE, 0); return 0; } pColName = &(p->aColName[N]); return sqlite3_value_text(pColName); } /* ** Return the name of the 'i'th column of the result set of SQL statement ** pStmt, encoded as UTF-16. */ const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ Vdbe *p = (Vdbe *)pStmt; Mem *pColName; if( N>=sqlite3_column_count(pStmt) || N<0 ){ sqlite3Error(p->db, SQLITE_RANGE, 0); return 0; } pColName = &(p->aColName[N]); return sqlite3_value_text16(pColName); } /* ** This routine returns either the column name, or declaration type (see ** sqlite3_column_decltype16() ) of the 'i'th column of the result set of ** SQL statement pStmt. The returned string is UTF-16 encoded. ** ** The declaration type is returned if 'decltype' is true, otherwise ** the column name. */ static const void *columnName16(sqlite3_stmt *pStmt, int i, int decltype){ Vdbe *p = (Vdbe *)pStmt; if( i>=sqlite3_column_count(pStmt) || i<0 ){ sqlite3Error(p->db, SQLITE_RANGE, 0); return 0; } if( decltype ){ i += p->nResColumn; } if( !p->azColName16 ){ p->azColName16 = (void **)sqliteMalloc(sizeof(void *)*p->nResColumn*2); if( !p->azColName16 ){ sqlite3Error(p->db, SQLITE_NOMEM, 0); return 0; } } if( !p->azColName16[i] ){ if( SQLITE3_BIGENDIAN ){ p->azColName16[i] = sqlite3utf8to16be(p->azColName[i], -1); } if( !p->azColName16[i] ){ sqlite3Error(p->db, SQLITE_NOMEM, 0); return 0; } } return p->azColName16[i]; } /* ** Return the column declaration type (if applicable) of the 'i'th column ** of the result set of SQL statement pStmt, encoded as UTF-8. */ const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int i){ Vdbe *p = (Vdbe *)pStmt; if( i>=sqlite3_column_count(pStmt) || i<0 ){ sqlite3Error(p->db, SQLITE_RANGE, 0); return 0; } return p->azColName[i+p->nResColumn]; } /* ** Return the column declaration type (if applicable) of the 'i'th column ** of the result set of SQL statement pStmt, encoded as UTF-16. */ const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int i){ return columnName16(pStmt, i, 1); } /******************************* sqlite3_bind_ *************************** ** ** Routines used to attach values to wildcards in a compiled SQL statement. */ /* ** Unbind the value bound to variable i in virtual machine p. This is the ** the same as binding a NULL value to the column. If the "i" parameter is ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. ** ** The error code stored in database p->db is overwritten with the return ** value in any case. */ static int vdbeUnbind(Vdbe *p, int i){ Mem *pVar; if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){ sqlite3Error(p->db, SQLITE_MISUSE, 0); return SQLITE_MISUSE; } if( i<1 || i>p->nVar ){ sqlite3Error(p->db, SQLITE_RANGE, 0); return SQLITE_RANGE; } i--; pVar = &p->apVar[i]; if( pVar->flags&MEM_Dyn ){ sqliteFree(pVar->z); } pVar->flags = MEM_Null; sqlite3Error(p->db, SQLITE_OK, 0); return SQLITE_OK; } /* ** Bind a blob value to an SQL statement variable. */ int sqlite3_bind_blob( sqlite3_stmt *p, int i, const void *zData, int nData, int eCopy ){ Vdbe *p = (Vdbe *)pStmt; Mem *pVar; int rc; rc = vdbeUnbind(p, i); if( rc ){ return rc; } pVar = &p->apVar[i-1]; rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy); return rc; } int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ int rc; Vdbe *p = (Vdbe *)pStmt; Mem *pVar; rc = vdbeUnbind(p, i); if( rc==SQLITE_OK ){ sqlite3VdbeMemSetReal(&p->apVar[i-1], rValue); } return SQLITE_OK; } int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ return sqlite3_bind_int64(p, i, (long long int)iValue); } int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){ int rc; Vdbe *p = (Vdbe *)pStmt; rc = vdbeUnbind(p, i); if( rc==SQLITE_OK ){ sqlite3VdbeMemSetInt(&p->apVar[i-1], iValue); } return rc; } int sqlite3_bind_null(sqlite3_stmt* p, int i){ return vdbeUnbind((Vdbe *)p, i); } int sqlite3_bind_text( sqlite3_stmt *pStmt, int i, const char *zData, int nData, int eCopy ){ Vdbe *p = (Vdbe *)pStmt; Mem *pVar; int rc; rc = vdbeUnbind(p, i); if( rc ){ return rc; } pVar = &p->apVar[i-1]; rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy); if( rc ){ return rc; } rc = sqlite3VdbeSetEncoding(pVar, p->db->enc); return rc; } int sqlite3_bind_text16( sqlite3_stmt *pStmt, int i, const void *zData, int nData, int eCopy ){ Vdbe *p = (Vdbe *)pStmt; Mem *pVar; int rc; rc = vdbeUnbind(p, i); if( rc ){ return rc; } Mem *pVar = &p->apVar[i-1]; /* There may or may not be a byte order mark at the start of the UTF-16. ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the ** actual byte order used by this string. If the string does happen ** to contain a BOM, then move zData so that it points to the first ** byte after the BOM. */ txt_enc = sqlite3UtfReadBom(zData, nData); if( txt_enc ){ zData = (void *)(((u8 *)zData) + 2); nData -= 2; }else{ txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le; } rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy); if( rc ){ return rc; } rc = sqlite3VdbeSetEncoding(pVar, p->db->enc); return rc; } |
Changes to src/vdbeaux.c.
︙ | ︙ | |||
438 439 440 441 442 443 444 | ** 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 sqlite3_context structure which is only defined in ** this source file. */ | | | 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | ** 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 sqlite3_context structure which is only defined in ** this source file. */ void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ assert( p && p->pFunc && p->pFunc->xStep ); if( p->pAgg==0 ){ if( nByte<=NBFS ){ p->pAgg = (void*)p->s.z; memset(p->pAgg, 0, nByte); }else{ p->pAgg = sqliteMalloc( nByte ); |
︙ | ︙ | |||
580 581 582 583 584 585 586 | 0 }; assert( p->explain ); /* Even though this opcode does not put dynamic strings onto the ** the stack, they may become dynamic if the user calls | | | 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | 0 }; assert( p->explain ); /* Even though this opcode does not put dynamic strings onto the ** the stack, they may become dynamic if the user calls ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. */ if( p->pTos==&p->aStack[4] ){ for(i=0; i<5; i++){ if( p->aStack[i].flags & MEM_Dyn ){ sqliteFree(p->aStack[i].z); } p->aStack[i].flags = 0; |
︙ | ︙ |
Added src/vdbemem.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 | /* ** 2004 May 26 ** ** 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 use to manipulate "Mem" structure. A "Mem" ** stores a single value in the VDBE. Mem is an opaque structure visible ** only within the VDBE. Interface routines refer to a Mem using the ** name sqlite_value */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include "vdbeInt.h" /* ** Given a Mem.flags value, return TEXT_Utf8, TEXT_Utf16le, or TEXT_Utf16be ** as appropriate. */ #define flagsToEnc(F) \ (((F)&MEM_Utf8)?TEXT_Utf8: \ ((F)&MEM_Utf16be)?TEXT_Utf16be:TEXT_Utf16le) /* ** If pMem is a string object, this routine sets the encoding of the string ** (to one of UTF-8 or UTF16) and whether or not the string is ** nul-terminated. If pMem is not a string object, then this routine is ** a no-op. ** ** The second argument, "flags" consists of one of MEM_Utf8, MEM_Utf16le ** or MEM_Utf16be, possible ORed with MEM_Term. If necessary this function ** manipulates the value stored by pMem so that it matches the flags passed ** in "flags". ** ** SQLITE_OK is returned if the conversion is successful (or not required). ** SQLITE_NOMEM may be returned if a malloc() fails during conversion ** between formats. */ int sqlite3VdbeSetEncoding(Mem *pMem, int flags){ u8 enc1; /* Current string encoding (TEXT_Utf* value) */ u8 enc2; /* Required string encoding (TEXT_Utf* value) */ /* If this is not a string, do nothing. */ if( !(pMem->flags&MEM_Str) ){ return SQLITE_OK; } enc1 = flagsToEnc(pMem->flags); enc2 = flagsToEnc(flags); if( enc1!=enc2 ){ if( enc1==TEXT_Utf8 || enc2==TEXT_Utf8 ){ /* If the current encoding does not match the desired encoding, then ** we will need to do some translation between encodings. */ char *z; int n; int rc = sqlite3utfTranslate(pMem->z,pMem->n,enc1,(void **)&z,&n,enc2); if( rc!=SQLITE_OK ){ return rc; } /* Result of sqlite3utfTranslate is currently always dynamically ** allocated and nul terminated. This might be altered as a performance ** enhancement later. */ pMem->z = z; pMem->n = n; pMem->flags = (MEM_Str | MEM_Dyn | MEM_Term | flags); }else{ /* Must be translating between UTF-16le and UTF-16be. */ int i; if( pMem->flags&MEM_Static ){ Dynamicify(pMem, enc1); } for(i=0; i<pMem->n; i+=2){ char c = pMem->z[i]; pMem->z[i] = pMem->z[i+1]; pMem->z[i+1] = c; } SetEncodingFlags(pMem, enc2); } } if( (flags&MEM_Term) && !(pMem->flags&MEM_Term) ){ /* If we did not do any translation, but currently the string is ** not nul terminated (and is required to be), then we add the ** nul terminator now. We never have to do this if we translated ** the encoding of the string, as the translation functions return ** nul terminated values. */ int f = pMem->flags; int nulTermLen = 2; /* The number of 0x00 bytes to append */ if( enc2==MEM_Utf8 ){ nulTermLen = 1; } if( pMem->n+nulTermLen<=NBFS ){ /* If the string plus the nul terminator will fit in the Mem.zShort ** buffer, and it is not already stored there, copy it there. */ if( !(f&MEM_Short) ){ memcpy(pMem->z, pMem->zShort, pMem->n); if( f&MEM_Dyn ){ sqliteFree(pMem->z); } pMem->z = pMem->zShort; pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Dyn); pMem->flags |= MEM_Short; } }else{ /* Otherwise we have to malloc for memory. If the string is already ** dynamic, use sqliteRealloc(). Otherwise sqliteMalloc() enough ** space for the string and the nul terminator, and copy the string ** data there. */ if( f&MEM_Dyn ){ pMem->z = (char *)sqliteRealloc(pMem->z, pMem->n+nulTermLen); if( !pMem->z ){ return SQLITE_NOMEM; } }else{ char *z = (char *)sqliteMallocRaw(pMem->n+nulTermLen); memcpy(z, pMem->z, pMem->n); pMem->z = z; pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short); pMem->flags |= MEM_Dyn; } } /* pMem->z now points at the string data, with enough space at the end ** to insert the nul nul terminator. pMem->n has not yet been updated. */ memcpy(&pMem->z[pMem->n], "\0\0", nulTermLen); pMem->n += nulTermLen; pMem->flags |= MEM_Term; } return SQLITE_OK; } static void releaseMem(Mem *p){ if( p->flags & MEM_Dyn ){ sqliteFree(p); } } /* ** Delete any previous value and set the value stored in *pMem to NULL. */ void sqlite3VdbeMemSetNull(Mem *pMem){ releaseMem(pMem); pMem->flags = MEM_Null; } /* ** Delete any previous value and set the value stored in *pMem to val, ** manifest type INTEGER. */ void sqlite3VdbeMemSetInt(Mem *pMem, i64 val){ releaseMem(pMem); pMem->i = val; pMem->flags = MEM_Int; } /* ** Delete any previous value and set the value stored in *pMem to val, ** manifest type REAL. */ void sqlite3VdbeMemSetReal(Mem *pMem, double val){ releaseMem(pMem); pMem->r = val; pMem->flags = MEM_Real; } /* ** Copy the contents of memory cell pFrom into pTo. */ int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ releaseMem(pTo); memcpy(pTo, pFrom, sizeof(*pFrom)); if( pTo->flags&MEM_Short ){ pTo->z = pTo->zShort; }else if( pTo->flags&(MEM_Ephem|MEM_Dyn) ){ pTo->flags = pTo->flags&(~(MEM_Static|MEM_Ephem|MEM_Short|MEM_Dyn)); if( pTo->n>NBFS ){ pTo->z = sqliteMalloc(pTo->n); if( !pTo->z ) return SQLITE_NOMEM; pTo->flags |= MEM_Dyn; }else{ pTo->z = pTo->zShort; pTo->flags |= MEM_Short; } memcpy(pTo->z, pFrom->z, pTo->n); } return SQLITE_OK; } int sqlite3VdbeMemSetStr( Mem *pMem, /* Memory cell to set to string value */ const char *z, /* String pointer */ int n, /* Bytes in string, or negative */ u8 enc, /* Encoding of z */ int eCopy /* True if this function should make a copy of z */ ){ Mem tmp; releaseMem(pMem); if( !z ){ /* If z is NULL, just set *pMem to contain NULL. */ return SQLITE_OK; } pMem->z = (char *)z; if( eCopy ){ pMem->flags = MEM_Ephem|MEM_Str; }else{ pMem->flags = MEM_Static|MEM_Str; } pMem->flags |= encToFlags(enc); pMem->n = n; switch( enc ){ case 0: pMem->flags |= MEM_Blob; break; case TEXT_Utf8: pMem->flags |= MEM_Utf8; if( n<0 ){ pMem->n = strlen(z)+1; pMem->flags |= MEM_Term; }else if( z[pMem->n-1]==0 ){ pMem->flags |= MEM_Term; } break; case TEXT_Utf16le: case TEXT_Utf16be: pMem->flags |= (enc==TEXT_Utf16le?MEM_Utf16le:MEM_Utf16be); if( n<0 ){ pMem->n = sqlite3utf16ByteLen(z,-1)+1; pMem->flags |= MEM_Term; }else if( z[pMem->n-1]==0 && z[pMem->n-2]==0 ){ pMem->flags |= MEM_Term; } break; default: assert(0); } Deephemeralize(pMem); } int sqlite3VdbeMemNulTerminate(Mem *pMem){ int nulTermLen; int f = pMem->flags; assert( pMem->flags&MEM_Str && !pMem->flags&MEM_Term ); assert( flagsToEnc(pMem->flags) ); nulTermLen = (flagsToEnc(f)==TEXT_Utf8?1:2); if( pMem->n+nulTermLen<=NBFS ){ /* If the string plus the nul terminator will fit in the Mem.zShort ** buffer, and it is not already stored there, copy it there. */ if( !(f&MEM_Short) ){ memcpy(pMem->z, pMem->zShort, pMem->n); if( f&MEM_Dyn ){ sqliteFree(pMem->z); } pMem->z = pMem->zShort; pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Dyn); pMem->flags |= MEM_Short; } }else{ /* Otherwise we have to malloc for memory. If the string is already ** dynamic, use sqliteRealloc(). Otherwise sqliteMalloc() enough ** space for the string and the nul terminator, and copy the string ** data there. */ if( f&MEM_Dyn ){ pMem->z = (char *)sqliteRealloc(pMem->z, pMem->n+nulTermLen); if( !pMem->z ){ return SQLITE_NOMEM; } }else{ char *z = (char *)sqliteMalloc(pMem->n+nulTermLen); memcpy(z, pMem->z, pMem->n); pMem->z = z; pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short); pMem->flags |= MEM_Dyn; } } /* pMem->z now points at the string data, with enough space at the end ** to insert the nul nul terminator. pMem->n has not yet been updated. */ memcpy(&pMem->z[pMem->n], "\0\0", nulTermLen); pMem->n += nulTermLen; pMem->flags |= MEM_Term; } /* ** Compare the values contained by the two memory cells, returning ** negative, zero or positive if pMem1 is less than, equal to, or greater ** than pMem2. Sorting order is NULL's first, followed by numbers (integers ** and reals) sorted numerically, followed by text ordered by the collating ** sequence pColl and finally blob's ordered by memcmp(). ** ** Two NULL values are considered equal by this function. */ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ int rc; int f1, f2; int combined_flags; /* Interchange pMem1 and pMem2 if the collating sequence specifies ** DESC order. */ f1 = pMem1->flags; f2 = pMem2->flags; combined_flags = f1|f2; /* If one value is NULL, it is less than the other. If both values ** are NULL, return 0. */ if( combined_flags&MEM_Null ){ return (f2&MEM_Null) - (f1&MEM_Null); } /* If one value is a number and the other is not, the number is less. ** If both are numbers, compare as reals if one is a real, or as integers ** if both values are integers. */ if( combined_flags&(MEM_Int|MEM_Real) ){ if( !(f1&(MEM_Int|MEM_Real)) ){ return 1; } if( !(f2&(MEM_Int|MEM_Real)) ){ return -1; } if( (f1 & f2 & MEM_Int)==0 ){ double r1, r2; if( (f1&MEM_Real)==0 ){ r1 = pMem1->i; }else{ r1 = pMem1->r; } if( (f2&MEM_Real)==0 ){ r2 = pMem2->i; }else{ r2 = pMem2->r; } if( r1<r2 ) return -1; if( r1>r2 ) return 1; return 0; }else{ assert( f1&MEM_Int ); assert( f2&MEM_Int ); if( pMem1->i < pMem2->i ) return -1; if( pMem1->i > pMem2->i ) return 1; return 0; } } /* If one value is a string and the other is a blob, the string is less. ** If both are strings, compare using the collating functions. */ if( combined_flags&MEM_Str ){ if( (f1 & MEM_Str)==0 ){ return 1; } if( (f2 & MEM_Str)==0 ){ return -1; } if( pColl && pColl->xCmp ){ return pColl->xCmp(pColl->pUser, pMem1->n, pMem1->z, pMem2->n, pMem2->z); }else{ /* If no collating sequence is defined, fall through into the ** blob case below and use memcmp() for the comparison. */ } } /* Both values must be blobs. Compare using memcmp(). */ rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); if( rc==0 ){ rc = pMem1->n - pMem2->n; } return rc; } |