Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/vdbemem.c

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2004 May 26 2 ** 2004 May 26
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 assert( (pMem->flags&MEM_RowSet)==0 ); 193 assert( (pMem->flags&MEM_RowSet)==0 );
194 ExpandBlob(pMem); 194 ExpandBlob(pMem);
195 f = pMem->flags; 195 f = pMem->flags;
196 if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){ 196 if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
197 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ 197 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
198 return SQLITE_NOMEM; 198 return SQLITE_NOMEM;
199 } 199 }
200 pMem->z[pMem->n] = 0; 200 pMem->z[pMem->n] = 0;
201 pMem->z[pMem->n+1] = 0; 201 pMem->z[pMem->n+1] = 0;
202 pMem->flags |= MEM_Term; 202 pMem->flags |= MEM_Term;
203 }
204 pMem->flags &= ~MEM_Ephem;
203 #ifdef SQLITE_DEBUG 205 #ifdef SQLITE_DEBUG
204 pMem->pScopyFrom = 0; 206 pMem->pScopyFrom = 0;
205 #endif 207 #endif
206 }
207 208
208 return SQLITE_OK; 209 return SQLITE_OK;
209 } 210 }
210 211
211 /* 212 /*
212 ** If the given Mem* has a zero-filled tail, turn it into an ordinary 213 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
213 ** blob stored in dynamically allocated space. 214 ** blob stored in dynamically allocated space.
214 */ 215 */
215 #ifndef SQLITE_OMIT_INCRBLOB 216 #ifndef SQLITE_OMIT_INCRBLOB
216 int sqlite3VdbeMemExpandBlob(Mem *pMem){ 217 int sqlite3VdbeMemExpandBlob(Mem *pMem){
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 /* 581 /*
581 ** Cast the datatype of the value in pMem according to the affinity 582 ** Cast the datatype of the value in pMem according to the affinity
582 ** "aff". Casting is different from applying affinity in that a cast 583 ** "aff". Casting is different from applying affinity in that a cast
583 ** is forced. In other words, the value is converted into the desired 584 ** is forced. In other words, the value is converted into the desired
584 ** affinity even if that results in loss of data. This routine is 585 ** affinity even if that results in loss of data. This routine is
585 ** used (for example) to implement the SQL "cast()" operator. 586 ** used (for example) to implement the SQL "cast()" operator.
586 */ 587 */
587 void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ 588 void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
588 if( pMem->flags & MEM_Null ) return; 589 if( pMem->flags & MEM_Null ) return;
589 switch( aff ){ 590 switch( aff ){
590 case SQLITE_AFF_NONE: { /* Really a cast to BLOB */ 591 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
591 if( (pMem->flags & MEM_Blob)==0 ){ 592 if( (pMem->flags & MEM_Blob)==0 ){
592 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); 593 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
593 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); 594 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
594 MemSetTypeFlag(pMem, MEM_Blob); 595 MemSetTypeFlag(pMem, MEM_Blob);
595 }else{ 596 }else{
596 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob); 597 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
597 } 598 }
598 break; 599 break;
599 } 600 }
600 case SQLITE_AFF_NUMERIC: { 601 case SQLITE_AFF_NUMERIC: {
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){ 763 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
763 if( pX->pScopyFrom==pMem ){ 764 if( pX->pScopyFrom==pMem ){
764 pX->flags |= MEM_Undefined; 765 pX->flags |= MEM_Undefined;
765 pX->pScopyFrom = 0; 766 pX->pScopyFrom = 0;
766 } 767 }
767 } 768 }
768 pMem->pScopyFrom = 0; 769 pMem->pScopyFrom = 0;
769 } 770 }
770 #endif /* SQLITE_DEBUG */ 771 #endif /* SQLITE_DEBUG */
771 772
772 /*
773 ** Size of struct Mem not including the Mem.zMalloc member.
774 */
775 #define MEMCELLSIZE offsetof(Mem,zMalloc)
776 773
777 /* 774 /*
778 ** Make an shallow copy of pFrom into pTo. Prior contents of 775 ** Make an shallow copy of pFrom into pTo. Prior contents of
779 ** pTo are freed. The pFrom->z field is not duplicated. If 776 ** pTo are freed. The pFrom->z field is not duplicated. If
780 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z 777 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
781 ** and flags gets srcType (either MEM_Ephem or MEM_Static). 778 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
782 */ 779 */
780 static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
781 vdbeMemClearExternAndSetNull(pTo);
782 assert( !VdbeMemDynamic(pTo) );
783 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
784 }
783 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ 785 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
784 assert( (pFrom->flags & MEM_RowSet)==0 ); 786 assert( (pFrom->flags & MEM_RowSet)==0 );
785 assert( pTo->db==pFrom->db ); 787 assert( pTo->db==pFrom->db );
786 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); 788 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
787 memcpy(pTo, pFrom, MEMCELLSIZE); 789 memcpy(pTo, pFrom, MEMCELLSIZE);
788 if( (pFrom->flags&MEM_Static)==0 ){ 790 if( (pFrom->flags&MEM_Static)==0 ){
789 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); 791 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
790 assert( srcType==MEM_Ephem || srcType==MEM_Static ); 792 assert( srcType==MEM_Ephem || srcType==MEM_Static );
791 pTo->flags |= srcType; 793 pTo->flags |= srcType;
792 } 794 }
793 } 795 }
794 796
795 /* 797 /*
796 ** Make a full copy of pFrom into pTo. Prior contents of pTo are 798 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
797 ** freed before the copy is made. 799 ** freed before the copy is made.
798 */ 800 */
799 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ 801 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
800 int rc = SQLITE_OK; 802 int rc = SQLITE_OK;
801 803
802 assert( pTo->db==pFrom->db ); 804 /* The pFrom==0 case in the following assert() is when an sqlite3_value
805 ** from sqlite3_value_dup() is used as the argument
806 ** to sqlite3_result_value(). */
807 assert( pTo->db==pFrom->db || pFrom->db==0 );
803 assert( (pFrom->flags & MEM_RowSet)==0 ); 808 assert( (pFrom->flags & MEM_RowSet)==0 );
804 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); 809 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
805 memcpy(pTo, pFrom, MEMCELLSIZE); 810 memcpy(pTo, pFrom, MEMCELLSIZE);
806 pTo->flags &= ~MEM_Dyn; 811 pTo->flags &= ~MEM_Dyn;
807 if( pTo->flags&(MEM_Str|MEM_Blob) ){ 812 if( pTo->flags&(MEM_Str|MEM_Blob) ){
808 if( 0==(pFrom->flags&MEM_Static) ){ 813 if( 0==(pFrom->flags&MEM_Static) ){
809 pTo->flags |= MEM_Ephem; 814 pTo->flags |= MEM_Ephem;
810 rc = sqlite3VdbeMemMakeWriteable(pTo); 815 rc = sqlite3VdbeMemMakeWriteable(pTo);
811 } 816 }
812 } 817 }
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
939 ** 944 **
940 ** The pMem object must have been initialized. This routine will use 945 ** The pMem object must have been initialized. This routine will use
941 ** pMem->zMalloc to hold the content from the btree, if possible. New 946 ** pMem->zMalloc to hold the content from the btree, if possible. New
942 ** pMem->zMalloc space will be allocated if necessary. The calling routine 947 ** pMem->zMalloc space will be allocated if necessary. The calling routine
943 ** is responsible for making sure that the pMem object is eventually 948 ** is responsible for making sure that the pMem object is eventually
944 ** destroyed. 949 ** destroyed.
945 ** 950 **
946 ** If this routine fails for any reason (malloc returns NULL or unable 951 ** If this routine fails for any reason (malloc returns NULL or unable
947 ** to read from the disk) then the pMem is left in an inconsistent state. 952 ** to read from the disk) then the pMem is left in an inconsistent state.
948 */ 953 */
954 static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
955 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
956 u32 offset, /* Offset from the start of data to return bytes from. */
957 u32 amt, /* Number of bytes to return. */
958 int key, /* If true, retrieve from the btree key, not data. */
959 Mem *pMem /* OUT: Return data in this Mem structure. */
960 ){
961 int rc;
962 pMem->flags = MEM_Null;
963 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
964 if( key ){
965 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
966 }else{
967 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
968 }
969 if( rc==SQLITE_OK ){
970 pMem->z[amt] = 0;
971 pMem->z[amt+1] = 0;
972 pMem->flags = MEM_Blob|MEM_Term;
973 pMem->n = (int)amt;
974 }else{
975 sqlite3VdbeMemRelease(pMem);
976 }
977 }
978 return rc;
979 }
949 int sqlite3VdbeMemFromBtree( 980 int sqlite3VdbeMemFromBtree(
950 BtCursor *pCur, /* Cursor pointing at record to retrieve. */ 981 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
951 u32 offset, /* Offset from the start of data to return bytes from. */ 982 u32 offset, /* Offset from the start of data to return bytes from. */
952 u32 amt, /* Number of bytes to return. */ 983 u32 amt, /* Number of bytes to return. */
953 int key, /* If true, retrieve from the btree key, not data. */ 984 int key, /* If true, retrieve from the btree key, not data. */
954 Mem *pMem /* OUT: Return data in this Mem structure. */ 985 Mem *pMem /* OUT: Return data in this Mem structure. */
955 ){ 986 ){
956 char *zData; /* Data from the btree layer */ 987 char *zData; /* Data from the btree layer */
957 u32 available = 0; /* Number of bytes available on the local btree page */ 988 u32 available = 0; /* Number of bytes available on the local btree page */
958 int rc = SQLITE_OK; /* Return code */ 989 int rc = SQLITE_OK; /* Return code */
959 990
960 assert( sqlite3BtreeCursorIsValid(pCur) ); 991 assert( sqlite3BtreeCursorIsValid(pCur) );
961 assert( !VdbeMemDynamic(pMem) ); 992 assert( !VdbeMemDynamic(pMem) );
962 993
963 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 994 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
964 ** that both the BtShared and database handle mutexes are held. */ 995 ** that both the BtShared and database handle mutexes are held. */
965 assert( (pMem->flags & MEM_RowSet)==0 ); 996 assert( (pMem->flags & MEM_RowSet)==0 );
966 if( key ){ 997 if( key ){
967 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); 998 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
968 }else{ 999 }else{
969 zData = (char *)sqlite3BtreeDataFetch(pCur, &available); 1000 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
970 } 1001 }
971 assert( zData!=0 ); 1002 assert( zData!=0 );
972 1003
973 if( offset+amt<=available ){ 1004 if( offset+amt<=available ){
974 pMem->z = &zData[offset]; 1005 pMem->z = &zData[offset];
975 pMem->flags = MEM_Blob|MEM_Ephem; 1006 pMem->flags = MEM_Blob|MEM_Ephem;
976 pMem->n = (int)amt; 1007 pMem->n = (int)amt;
977 }else{ 1008 }else{
978 pMem->flags = MEM_Null; 1009 rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
979 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
980 if( key ){
981 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
982 }else{
983 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
984 }
985 if( rc==SQLITE_OK ){
986 pMem->z[amt] = 0;
987 pMem->z[amt+1] = 0;
988 pMem->flags = MEM_Blob|MEM_Term;
989 pMem->n = (int)amt;
990 }else{
991 sqlite3VdbeMemRelease(pMem);
992 }
993 }
994 } 1010 }
995 1011
996 return rc; 1012 return rc;
997 } 1013 }
998 1014
999 /* 1015 /*
1000 ** The pVal argument is known to be a value other than NULL. 1016 ** The pVal argument is known to be a value other than NULL.
1001 ** Convert it into a string with encoding enc and return a pointer 1017 ** Convert it into a string with encoding enc and return a pointer
1002 ** to a zero-terminated version of that string. 1018 ** to a zero-terminated version of that string.
1003 */ 1019 */
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 }; 1099 };
1084 1100
1085 /* 1101 /*
1086 ** Allocate and return a pointer to a new sqlite3_value object. If 1102 ** Allocate and return a pointer to a new sqlite3_value object. If
1087 ** the second argument to this function is NULL, the object is allocated 1103 ** the second argument to this function is NULL, the object is allocated
1088 ** by calling sqlite3ValueNew(). 1104 ** by calling sqlite3ValueNew().
1089 ** 1105 **
1090 ** Otherwise, if the second argument is non-zero, then this function is 1106 ** Otherwise, if the second argument is non-zero, then this function is
1091 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not 1107 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
1092 ** already been allocated, allocate the UnpackedRecord structure that 1108 ** already been allocated, allocate the UnpackedRecord structure that
1093 ** that function will return to its caller here. Then return a pointer 1109 ** that function will return to its caller here. Then return a pointer to
1094 ** an sqlite3_value within the UnpackedRecord.a[] array. 1110 ** an sqlite3_value within the UnpackedRecord.a[] array.
1095 */ 1111 */
1096 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ 1112 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
1097 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1113 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1098 if( p ){ 1114 if( p ){
1099 UnpackedRecord *pRec = p->ppRec[0]; 1115 UnpackedRecord *pRec = p->ppRec[0];
1100 1116
1101 if( pRec==0 ){ 1117 if( pRec==0 ){
1102 Index *pIdx = p->pIdx; /* Index being probed */ 1118 Index *pIdx = p->pIdx; /* Index being probed */
1103 int nByte; /* Bytes of space to allocate */ 1119 int nByte; /* Bytes of space to allocate */
(...skipping 24 matching lines...) Expand all
1128 pRec->nField = p->iVal+1; 1144 pRec->nField = p->iVal+1;
1129 return &pRec->aMem[p->iVal]; 1145 return &pRec->aMem[p->iVal];
1130 } 1146 }
1131 #else 1147 #else
1132 UNUSED_PARAMETER(p); 1148 UNUSED_PARAMETER(p);
1133 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ 1149 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
1134 return sqlite3ValueNew(db); 1150 return sqlite3ValueNew(db);
1135 } 1151 }
1136 1152
1137 /* 1153 /*
1154 ** The expression object indicated by the second argument is guaranteed
1155 ** to be a scalar SQL function. If
1156 **
1157 ** * all function arguments are SQL literals,
1158 ** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
1159 ** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
1160 **
1161 ** then this routine attempts to invoke the SQL function. Assuming no
1162 ** error occurs, output parameter (*ppVal) is set to point to a value
1163 ** object containing the result before returning SQLITE_OK.
1164 **
1165 ** Affinity aff is applied to the result of the function before returning.
1166 ** If the result is a text value, the sqlite3_value object uses encoding
1167 ** enc.
1168 **
1169 ** If the conditions above are not met, this function returns SQLITE_OK
1170 ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
1171 ** NULL and an SQLite error code returned.
1172 */
1173 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1174 static int valueFromFunction(
1175 sqlite3 *db, /* The database connection */
1176 Expr *p, /* The expression to evaluate */
1177 u8 enc, /* Encoding to use */
1178 u8 aff, /* Affinity to use */
1179 sqlite3_value **ppVal, /* Write the new value here */
1180 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
1181 ){
1182 sqlite3_context ctx; /* Context object for function invocation */
1183 sqlite3_value **apVal = 0; /* Function arguments */
1184 int nVal = 0; /* Size of apVal[] array */
1185 FuncDef *pFunc = 0; /* Function definition */
1186 sqlite3_value *pVal = 0; /* New value */
1187 int rc = SQLITE_OK; /* Return code */
1188 int nName; /* Size of function name in bytes */
1189 ExprList *pList = 0; /* Function arguments */
1190 int i; /* Iterator variable */
1191
1192 assert( pCtx!=0 );
1193 assert( (p->flags & EP_TokenOnly)==0 );
1194 pList = p->x.pList;
1195 if( pList ) nVal = pList->nExpr;
1196 nName = sqlite3Strlen30(p->u.zToken);
1197 pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0);
1198 assert( pFunc );
1199 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
1200 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
1201 ){
1202 return SQLITE_OK;
1203 }
1204
1205 if( pList ){
1206 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
1207 if( apVal==0 ){
1208 rc = SQLITE_NOMEM;
1209 goto value_from_function_out;
1210 }
1211 for(i=0; i<nVal; i++){
1212 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
1213 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
1214 }
1215 }
1216
1217 pVal = valueNew(db, pCtx);
1218 if( pVal==0 ){
1219 rc = SQLITE_NOMEM;
1220 goto value_from_function_out;
1221 }
1222
1223 assert( pCtx->pParse->rc==SQLITE_OK );
1224 memset(&ctx, 0, sizeof(ctx));
1225 ctx.pOut = pVal;
1226 ctx.pFunc = pFunc;
1227 pFunc->xFunc(&ctx, nVal, apVal);
1228 if( ctx.isError ){
1229 rc = ctx.isError;
1230 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
1231 }else{
1232 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
1233 assert( rc==SQLITE_OK );
1234 rc = sqlite3VdbeChangeEncoding(pVal, enc);
1235 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
1236 rc = SQLITE_TOOBIG;
1237 pCtx->pParse->nErr++;
1238 }
1239 }
1240 pCtx->pParse->rc = rc;
1241
1242 value_from_function_out:
1243 if( rc!=SQLITE_OK ){
1244 pVal = 0;
1245 }
1246 if( apVal ){
1247 for(i=0; i<nVal; i++){
1248 sqlite3ValueFree(apVal[i]);
1249 }
1250 sqlite3DbFree(db, apVal);
1251 }
1252
1253 *ppVal = pVal;
1254 return rc;
1255 }
1256 #else
1257 # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
1258 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
1259
1260 /*
1138 ** Extract a value from the supplied expression in the manner described 1261 ** Extract a value from the supplied expression in the manner described
1139 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object 1262 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
1140 ** using valueNew(). 1263 ** using valueNew().
1141 ** 1264 **
1142 ** If pCtx is NULL and an error occurs after the sqlite3_value object 1265 ** If pCtx is NULL and an error occurs after the sqlite3_value object
1143 ** has been allocated, it is freed before returning. Or, if pCtx is not 1266 ** has been allocated, it is freed before returning. Or, if pCtx is not
1144 ** NULL, it is assumed that the caller will free any allocated object 1267 ** NULL, it is assumed that the caller will free any allocated object
1145 ** in all cases. 1268 ** in all cases.
1146 */ 1269 */
1147 static int valueFromExpr( 1270 static int valueFromExpr(
(...skipping 11 matching lines...) Expand all
1159 const char *zNeg = ""; 1282 const char *zNeg = "";
1160 int rc = SQLITE_OK; 1283 int rc = SQLITE_OK;
1161 1284
1162 if( !pExpr ){ 1285 if( !pExpr ){
1163 *ppVal = 0; 1286 *ppVal = 0;
1164 return SQLITE_OK; 1287 return SQLITE_OK;
1165 } 1288 }
1166 while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft; 1289 while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
1167 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; 1290 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
1168 1291
1292 /* Compressed expressions only appear when parsing the DEFAULT clause
1293 ** on a table column definition, and hence only when pCtx==0. This
1294 ** check ensures that an EP_TokenOnly expression is never passed down
1295 ** into valueFromFunction(). */
1296 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
1297
1169 if( op==TK_CAST ){ 1298 if( op==TK_CAST ){
1170 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); 1299 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
1171 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); 1300 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
1172 testcase( rc!=SQLITE_OK ); 1301 testcase( rc!=SQLITE_OK );
1173 if( *ppVal ){ 1302 if( *ppVal ){
1174 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8); 1303 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
1175 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8); 1304 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
1176 } 1305 }
1177 return rc; 1306 return rc;
1178 } 1307 }
(...skipping 12 matching lines...) Expand all
1191 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ 1320 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
1192 pVal = valueNew(db, pCtx); 1321 pVal = valueNew(db, pCtx);
1193 if( pVal==0 ) goto no_mem; 1322 if( pVal==0 ) goto no_mem;
1194 if( ExprHasProperty(pExpr, EP_IntValue) ){ 1323 if( ExprHasProperty(pExpr, EP_IntValue) ){
1195 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); 1324 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
1196 }else{ 1325 }else{
1197 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); 1326 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
1198 if( zVal==0 ) goto no_mem; 1327 if( zVal==0 ) goto no_mem;
1199 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); 1328 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
1200 } 1329 }
1201 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ 1330 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
1202 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); 1331 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
1203 }else{ 1332 }else{
1204 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); 1333 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
1205 } 1334 }
1206 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; 1335 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
1207 if( enc!=SQLITE_UTF8 ){ 1336 if( enc!=SQLITE_UTF8 ){
1208 rc = sqlite3VdbeChangeEncoding(pVal, enc); 1337 rc = sqlite3VdbeChangeEncoding(pVal, enc);
1209 } 1338 }
1210 }else if( op==TK_UMINUS ) { 1339 }else if( op==TK_UMINUS ) {
1211 /* This branch happens for multiple negative signs. Ex: -(-5) */ 1340 /* This branch happens for multiple negative signs. Ex: -(-5) */
(...skipping 23 matching lines...) Expand all
1235 pVal = valueNew(db, pCtx); 1364 pVal = valueNew(db, pCtx);
1236 if( !pVal ) goto no_mem; 1365 if( !pVal ) goto no_mem;
1237 zVal = &pExpr->u.zToken[2]; 1366 zVal = &pExpr->u.zToken[2];
1238 nVal = sqlite3Strlen30(zVal)-1; 1367 nVal = sqlite3Strlen30(zVal)-1;
1239 assert( zVal[nVal]=='\'' ); 1368 assert( zVal[nVal]=='\'' );
1240 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, 1369 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
1241 0, SQLITE_DYNAMIC); 1370 0, SQLITE_DYNAMIC);
1242 } 1371 }
1243 #endif 1372 #endif
1244 1373
1374 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1375 else if( op==TK_FUNCTION && pCtx!=0 ){
1376 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
1377 }
1378 #endif
1379
1245 *ppVal = pVal; 1380 *ppVal = pVal;
1246 return rc; 1381 return rc;
1247 1382
1248 no_mem: 1383 no_mem:
1249 db->mallocFailed = 1; 1384 db->mallocFailed = 1;
1250 sqlite3DbFree(db, zVal); 1385 sqlite3DbFree(db, zVal);
1251 assert( *ppVal==0 ); 1386 assert( *ppVal==0 );
1252 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1387 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1253 if( pCtx==0 ) sqlite3ValueFree(pVal); 1388 if( pCtx==0 ) sqlite3ValueFree(pVal);
1254 #else 1389 #else
(...skipping 30 matching lines...) Expand all
1285 ** 1420 **
1286 ** This is used to convert the value stored in the 'sample' column of the 1421 ** This is used to convert the value stored in the 'sample' column of the
1287 ** sqlite_stat3 table to the record format SQLite uses internally. 1422 ** sqlite_stat3 table to the record format SQLite uses internally.
1288 */ 1423 */
1289 static void recordFunc( 1424 static void recordFunc(
1290 sqlite3_context *context, 1425 sqlite3_context *context,
1291 int argc, 1426 int argc,
1292 sqlite3_value **argv 1427 sqlite3_value **argv
1293 ){ 1428 ){
1294 const int file_format = 1; 1429 const int file_format = 1;
1295 int iSerial; /* Serial type */ 1430 u32 iSerial; /* Serial type */
1296 int nSerial; /* Bytes of space for iSerial as varint */ 1431 int nSerial; /* Bytes of space for iSerial as varint */
1297 int nVal; /* Bytes of space required for argv[0] */ 1432 u32 nVal; /* Bytes of space required for argv[0] */
1298 int nRet; 1433 int nRet;
1299 sqlite3 *db; 1434 sqlite3 *db;
1300 u8 *aRet; 1435 u8 *aRet;
1301 1436
1302 UNUSED_PARAMETER( argc ); 1437 UNUSED_PARAMETER( argc );
1303 iSerial = sqlite3VdbeSerialType(argv[0], file_format); 1438 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
1304 nSerial = sqlite3VarintLen(iSerial); 1439 nSerial = sqlite3VarintLen(iSerial);
1305 nVal = sqlite3VdbeSerialTypeLen(iSerial);
1306 db = sqlite3_context_db_handle(context); 1440 db = sqlite3_context_db_handle(context);
1307 1441
1308 nRet = 1 + nSerial + nVal; 1442 nRet = 1 + nSerial + nVal;
1309 aRet = sqlite3DbMallocRaw(db, nRet); 1443 aRet = sqlite3DbMallocRaw(db, nRet);
1310 if( aRet==0 ){ 1444 if( aRet==0 ){
1311 sqlite3_result_error_nomem(context); 1445 sqlite3_result_error_nomem(context);
1312 }else{ 1446 }else{
1313 aRet[0] = nSerial+1; 1447 aRet[0] = nSerial+1;
1314 putVarint32(&aRet[1], iSerial); 1448 putVarint32(&aRet[1], iSerial);
1315 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial); 1449 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
1521 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes 1655 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
1522 ** the object. 1656 ** the object.
1523 */ 1657 */
1524 void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){ 1658 void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
1525 if( pRec ){ 1659 if( pRec ){
1526 int i; 1660 int i;
1527 int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField; 1661 int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
1528 Mem *aMem = pRec->aMem; 1662 Mem *aMem = pRec->aMem;
1529 sqlite3 *db = aMem[0].db; 1663 sqlite3 *db = aMem[0].db;
1530 for(i=0; i<nCol; i++){ 1664 for(i=0; i<nCol; i++){
1531 if( aMem[i].szMalloc ) sqlite3DbFree(db, aMem[i].zMalloc); 1665 sqlite3VdbeMemRelease(&aMem[i]);
1532 } 1666 }
1533 sqlite3KeyInfoUnref(pRec->pKeyInfo); 1667 sqlite3KeyInfoUnref(pRec->pKeyInfo);
1534 sqlite3DbFree(db, pRec); 1668 sqlite3DbFree(db, pRec);
1535 } 1669 }
1536 } 1670 }
1537 #endif /* ifdef SQLITE_ENABLE_STAT4 */ 1671 #endif /* ifdef SQLITE_ENABLE_STAT4 */
1538 1672
1539 /* 1673 /*
1540 ** Change the string value of an sqlite3_value object 1674 ** Change the string value of an sqlite3_value object
1541 */ 1675 */
(...skipping 10 matching lines...) Expand all
1552 /* 1686 /*
1553 ** Free an sqlite3_value object 1687 ** Free an sqlite3_value object
1554 */ 1688 */
1555 void sqlite3ValueFree(sqlite3_value *v){ 1689 void sqlite3ValueFree(sqlite3_value *v){
1556 if( !v ) return; 1690 if( !v ) return;
1557 sqlite3VdbeMemRelease((Mem *)v); 1691 sqlite3VdbeMemRelease((Mem *)v);
1558 sqlite3DbFree(((Mem*)v)->db, v); 1692 sqlite3DbFree(((Mem*)v)->db, v);
1559 } 1693 }
1560 1694
1561 /* 1695 /*
1562 ** Return the number of bytes in the sqlite3_value object assuming 1696 ** The sqlite3ValueBytes() routine returns the number of bytes in the
1563 ** that it uses the encoding "enc" 1697 ** sqlite3_value object assuming that it uses the encoding "enc".
1698 ** The valueBytes() routine is a helper function.
1564 */ 1699 */
1700 static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
1701 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
1702 }
1565 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ 1703 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1566 Mem *p = (Mem*)pVal; 1704 Mem *p = (Mem*)pVal;
1567 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){ 1705 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
1706 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
1707 return p->n;
1708 }
1709 if( (p->flags & MEM_Blob)!=0 ){
1568 if( p->flags & MEM_Zero ){ 1710 if( p->flags & MEM_Zero ){
1569 return p->n + p->u.nZero; 1711 return p->n + p->u.nZero;
1570 }else{ 1712 }else{
1571 return p->n; 1713 return p->n;
1572 } 1714 }
1573 } 1715 }
1574 return 0; 1716 if( p->flags & MEM_Null ) return 0;
1717 return valueBytes(pVal, enc);
1575 } 1718 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/vdbeblob.c ('k') | third_party/sqlite/sqlite-src-3100200/src/vdbesort.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698