drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 26 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains code use to manipulate "Mem" structure. A "Mem" |
| 14 | ** stores a single value in the VDBE. Mem is an opaque structure visible |
| 15 | ** only within the VDBE. Interface routines refer to a Mem using the |
| 16 | ** name sqlite_value |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 17 | ** |
| 18 | ** $Id: vdbemem.c,v 1.115 2008/05/16 04:51:55 danielk1977 Exp $ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include "sqliteInt.h" |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #include "vdbeInt.h" |
| 23 | |
| 24 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 25 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 26 | ** P if required. |
| 27 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 28 | #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 31 | ** If pMem is an object with a valid string representation, this routine |
| 32 | ** ensures the internal encoding for the string representation is |
| 33 | ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 34 | ** |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 35 | ** If pMem is not a string object, or the encoding of the string |
| 36 | ** representation is already stored using the requested encoding, then this |
| 37 | ** routine is a no-op. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 38 | ** |
| 39 | ** SQLITE_OK is returned if the conversion is successful (or not required). |
| 40 | ** SQLITE_NOMEM may be returned if a malloc() fails during conversion |
| 41 | ** between formats. |
| 42 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 43 | int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 44 | int rc; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 45 | if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 46 | return SQLITE_OK; |
| 47 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 48 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 49 | #ifdef SQLITE_OMIT_UTF16 |
| 50 | return SQLITE_ERROR; |
| 51 | #else |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 52 | |
| 53 | /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, |
| 54 | ** then the encoding of the value may not have changed. |
| 55 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 56 | rc = sqlite3VdbeMemTranslate(pMem, desiredEnc); |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 57 | assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); |
| 58 | assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); |
| 59 | assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 60 | return rc; |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 61 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 62 | } |
| 63 | |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 64 | /* |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 65 | ** Make sure pMem->z points to a writable allocation of at least |
| 66 | ** n bytes. |
| 67 | ** |
| 68 | ** If the memory cell currently contains string or blob data |
| 69 | ** and the third argument passed to this function is true, the |
| 70 | ** current content of the cell is preserved. Otherwise, it may |
| 71 | ** be discarded. |
| 72 | ** |
| 73 | ** This function sets the MEM_Dyn flag and clears any xDel callback. |
| 74 | ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is |
| 75 | ** not set, Mem.n is zeroed. |
| 76 | */ |
| 77 | int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 78 | assert( 1 >= |
| 79 | ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) + |
| 80 | (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + |
| 81 | ((pMem->flags&MEM_Ephem) ? 1 : 0) + |
| 82 | ((pMem->flags&MEM_Static) ? 1 : 0) |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 83 | ); |
| 84 | |
danielk1977 | f934c5a | 2008-03-28 19:15:34 +0000 | [diff] [blame] | 85 | if( !pMem->zMalloc || sqlite3MallocSize(pMem->zMalloc)<n ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 86 | n = (n>32?n:32); |
| 87 | if( preserve && pMem->z==pMem->zMalloc ){ |
| 88 | pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 89 | if( !pMem->z ){ |
mlcreech | 6cc9c28 | 2008-04-02 04:23:32 +0000 | [diff] [blame] | 90 | pMem->flags = MEM_Null; |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 91 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 92 | preserve = 0; |
| 93 | }else{ |
| 94 | sqlite3_free(pMem->zMalloc); |
| 95 | pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 96 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 97 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 98 | |
| 99 | if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){ |
| 100 | memcpy(pMem->zMalloc, pMem->z, pMem->n); |
| 101 | } |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 102 | if( pMem->flags&MEM_Dyn && pMem->xDel ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 103 | pMem->xDel((void *)(pMem->z)); |
| 104 | } |
| 105 | |
| 106 | pMem->z = pMem->zMalloc; |
| 107 | pMem->flags &= ~(MEM_Ephem|MEM_Static); |
| 108 | pMem->xDel = 0; |
| 109 | return (pMem->z ? SQLITE_OK : SQLITE_NOMEM); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 113 | ** Make the given Mem object MEM_Dyn. |
| 114 | ** |
| 115 | ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. |
| 116 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 117 | int sqlite3VdbeMemDynamicify(Mem *pMem){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 118 | int f; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 119 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 120 | expandBlob(pMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 121 | f = pMem->flags; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 122 | if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 123 | if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ |
| 124 | return SQLITE_NOMEM; |
| 125 | } |
| 126 | pMem->z[pMem->n] = 0; |
| 127 | pMem->z[pMem->n+1] = 0; |
| 128 | pMem->flags |= MEM_Term; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 129 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 130 | |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 131 | return SQLITE_OK; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 135 | ** If the given Mem* has a zero-filled tail, turn it into an ordinary |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 136 | ** blob stored in dynamically allocated space. |
| 137 | */ |
danielk1977 | 246ad31 | 2007-05-16 14:23:00 +0000 | [diff] [blame] | 138 | #ifndef SQLITE_OMIT_INCRBLOB |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 139 | int sqlite3VdbeMemExpandBlob(Mem *pMem){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 140 | if( pMem->flags & MEM_Zero ){ |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 141 | int nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 142 | assert( pMem->flags&MEM_Blob ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 143 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 144 | |
| 145 | /* Set nByte to the number of bytes required to store the expanded blob. */ |
| 146 | nByte = pMem->n + pMem->u.i; |
| 147 | if( nByte<=0 ){ |
| 148 | nByte = 1; |
| 149 | } |
| 150 | if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 151 | return SQLITE_NOMEM; |
| 152 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 153 | |
| 154 | memset(&pMem->z[pMem->n], 0, pMem->u.i); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 155 | pMem->n += pMem->u.i; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 156 | pMem->flags &= ~(MEM_Zero|MEM_Term); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 157 | } |
| 158 | return SQLITE_OK; |
| 159 | } |
danielk1977 | 246ad31 | 2007-05-16 14:23:00 +0000 | [diff] [blame] | 160 | #endif |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 161 | |
| 162 | |
| 163 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 164 | ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes |
| 165 | ** of the Mem.z[] array can be modified. |
| 166 | ** |
| 167 | ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. |
| 168 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 169 | int sqlite3VdbeMemMakeWriteable(Mem *pMem){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 170 | return sqlite3VdbeMemDynamicify(pMem); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Make sure the given Mem is \u0000 terminated. |
| 175 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 176 | int sqlite3VdbeMemNulTerminate(Mem *pMem){ |
| 177 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 178 | if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 179 | return SQLITE_OK; /* Nothing to do */ |
| 180 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 181 | if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ |
| 182 | return SQLITE_NOMEM; |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 183 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 184 | pMem->z[pMem->n] = 0; |
| 185 | pMem->z[pMem->n+1] = 0; |
| 186 | pMem->flags |= MEM_Term; |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 187 | return SQLITE_OK; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 191 | ** Add MEM_Str to the set of representations for the given Mem. Numbers |
| 192 | ** are converted using sqlite3_snprintf(). Converting a BLOB to a string |
| 193 | ** is a no-op. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 194 | ** |
| 195 | ** Existing representations MEM_Int and MEM_Real are *not* invalidated. |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 196 | ** |
| 197 | ** A MEM_Null value will never be passed to this function. This function is |
| 198 | ** used for converting values to text for returning to the user (i.e. via |
| 199 | ** sqlite3_value_text()), or for ensuring that values to be used as btree |
| 200 | ** keys are strings. In the former case a NULL pointer is returned the |
| 201 | ** user and the later is an internal programming error. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 202 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 203 | int sqlite3VdbeMemStringify(Mem *pMem, int enc){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 204 | int rc = SQLITE_OK; |
| 205 | int fg = pMem->flags; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 206 | const int nByte = 32; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 207 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 208 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | def0fec | 2007-05-10 15:37:52 +0000 | [diff] [blame] | 209 | assert( !(fg&MEM_Zero) ); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 210 | assert( !(fg&(MEM_Str|MEM_Blob)) ); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 211 | assert( fg&(MEM_Int|MEM_Real) ); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 212 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 213 | if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
| 214 | return SQLITE_NOMEM; |
| 215 | } |
| 216 | |
| 217 | /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8 |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 218 | ** string representation of the value. Then, if the required encoding |
| 219 | ** is UTF-16le or UTF-16be do a translation. |
| 220 | ** |
| 221 | ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. |
| 222 | */ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 223 | if( fg & MEM_Int ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 224 | sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 225 | }else{ |
| 226 | assert( fg & MEM_Real ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 227 | sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 228 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 229 | pMem->n = strlen(pMem->z); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 230 | pMem->enc = SQLITE_UTF8; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 231 | pMem->flags |= MEM_Str|MEM_Term; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 232 | sqlite3VdbeChangeEncoding(pMem, enc); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | /* |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 237 | ** Memory cell pMem contains the context of an aggregate function. |
| 238 | ** This routine calls the finalize method for that function. The |
| 239 | ** result of the aggregate is stored back into pMem. |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 240 | ** |
| 241 | ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK |
| 242 | ** otherwise. |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 243 | */ |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 244 | int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ |
| 245 | int rc = SQLITE_OK; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 246 | if( pFunc && pFunc->xFinalize ){ |
| 247 | sqlite3_context ctx; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 248 | assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 249 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 250 | ctx.s.flags = MEM_Null; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 251 | ctx.s.db = pMem->db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 252 | ctx.s.zMalloc = 0; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 253 | ctx.pMem = pMem; |
| 254 | ctx.pFunc = pFunc; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 255 | ctx.isError = 0; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 256 | pFunc->xFinalize(&ctx); |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 257 | assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 258 | sqlite3_free(pMem->zMalloc); |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 259 | *pMem = ctx.s; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 260 | rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK); |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 261 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 262 | return rc; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 266 | ** If the memory cell contains a string value that must be freed by |
| 267 | ** invoking an external callback, free it now. Calling this function |
| 268 | ** does not free any Mem.zMalloc buffer. |
| 269 | */ |
| 270 | void sqlite3VdbeMemReleaseExternal(Mem *p){ |
| 271 | assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); |
| 272 | if( p->flags&MEM_Agg ){ |
| 273 | sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 274 | assert( (p->flags & MEM_Agg)==0 ); |
| 275 | sqlite3VdbeMemRelease(p); |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 276 | }else if( p->flags&MEM_Dyn && p->xDel ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 277 | p->xDel((void *)p->z); |
| 278 | p->xDel = 0; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 283 | ** Release any memory held by the Mem. This may leave the Mem in an |
| 284 | ** inconsistent state, for example with (Mem.z==0) and |
| 285 | ** (Mem.type==SQLITE_TEXT). |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 286 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 287 | void sqlite3VdbeMemRelease(Mem *p){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 288 | sqlite3VdbeMemReleaseExternal(p); |
| 289 | sqlite3_free(p->zMalloc); |
| 290 | p->z = 0; |
| 291 | p->zMalloc = 0; |
| 292 | p->xDel = 0; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 296 | ** Convert a 64-bit IEEE double into a 64-bit signed integer. |
| 297 | ** If the double is too large, return 0x8000000000000000. |
| 298 | ** |
| 299 | ** Most systems appear to do this simply by assigning |
| 300 | ** variables and without the extra range tests. But |
| 301 | ** there are reports that windows throws an expection |
| 302 | ** if the floating point value is out of range. (See ticket #2880.) |
| 303 | ** Because we do not completely understand the problem, we will |
| 304 | ** take the conservative approach and always do range tests |
| 305 | ** before attempting the conversion. |
| 306 | */ |
| 307 | static i64 doubleToInt64(double r){ |
| 308 | /* |
| 309 | ** Many compilers we encounter do not define constants for the |
| 310 | ** minimum and maximum 64-bit integers, or they define them |
| 311 | ** inconsistently. And many do not understand the "LL" notation. |
| 312 | ** So we define our own static constants here using nothing |
| 313 | ** larger than a 32-bit integer constant. |
| 314 | */ |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 315 | static const i64 maxInt = LARGEST_INT64; |
| 316 | static const i64 minInt = SMALLEST_INT64; |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 317 | |
| 318 | if( r<(double)minInt ){ |
| 319 | return minInt; |
| 320 | }else if( r>(double)maxInt ){ |
| 321 | return minInt; |
| 322 | }else{ |
| 323 | return (i64)r; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 328 | ** Return some kind of integer value which is the best we can do |
| 329 | ** at representing the value that *pMem describes as an integer. |
| 330 | ** If pMem is an integer, then the value is exact. If pMem is |
| 331 | ** a floating-point then the value returned is the integer part. |
| 332 | ** If pMem is a string or blob, then we make an attempt to convert |
| 333 | ** it into a integer and return that. If pMem is NULL, return 0. |
| 334 | ** |
| 335 | ** If pMem is a string, its encoding might be changed. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 336 | */ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 337 | i64 sqlite3VdbeIntValue(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 338 | int flags; |
| 339 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 340 | flags = pMem->flags; |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 341 | if( flags & MEM_Int ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 342 | return pMem->u.i; |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 343 | }else if( flags & MEM_Real ){ |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 344 | return doubleToInt64(pMem->r); |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 345 | }else if( flags & (MEM_Str|MEM_Blob) ){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 346 | i64 value; |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 347 | pMem->flags |= MEM_Str; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 348 | if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) |
| 349 | || sqlite3VdbeMemNulTerminate(pMem) ){ |
drh | c01be74 | 2005-11-03 14:29:55 +0000 | [diff] [blame] | 350 | return 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 351 | } |
| 352 | assert( pMem->z ); |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 353 | sqlite3Atoi64(pMem->z, &value); |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 354 | return value; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 355 | }else{ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 356 | return 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 357 | } |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 361 | ** Return the best representation of pMem that we can get into a |
| 362 | ** double. If pMem is already a double or an integer, return its |
| 363 | ** value. If it is a string or blob, try to convert it to a double. |
| 364 | ** If it is a NULL, return 0.0. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 365 | */ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 366 | double sqlite3VdbeRealValue(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 367 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 368 | if( pMem->flags & MEM_Real ){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 369 | return pMem->r; |
| 370 | }else if( pMem->flags & MEM_Int ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 371 | return (double)pMem->u.i; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 372 | }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 373 | double val = 0.0; |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 374 | pMem->flags |= MEM_Str; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 375 | if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) |
| 376 | || sqlite3VdbeMemNulTerminate(pMem) ){ |
drh | c01be74 | 2005-11-03 14:29:55 +0000 | [diff] [blame] | 377 | return 0.0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 378 | } |
| 379 | assert( pMem->z ); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 380 | sqlite3AtoF(pMem->z, &val); |
| 381 | return val; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 382 | }else{ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 383 | return 0.0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 384 | } |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 388 | ** The MEM structure is already a MEM_Real. Try to also make it a |
| 389 | ** MEM_Int if we can. |
| 390 | */ |
| 391 | void sqlite3VdbeIntegerAffinity(Mem *pMem){ |
| 392 | assert( pMem->flags & MEM_Real ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 393 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | efe3d65 | 2008-01-11 00:06:10 +0000 | [diff] [blame] | 394 | |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 395 | pMem->u.i = doubleToInt64(pMem->r); |
| 396 | if( pMem->r==(double)pMem->u.i ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 397 | pMem->flags |= MEM_Int; |
| 398 | } |
| 399 | } |
| 400 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 401 | static void setTypeFlag(Mem *pMem, int f){ |
| 402 | MemSetTypeFlag(pMem, f); |
| 403 | } |
| 404 | |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 405 | /* |
| 406 | ** Convert pMem to type integer. Invalidate any prior representations. |
| 407 | */ |
| 408 | int sqlite3VdbeMemIntegerify(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 409 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 410 | pMem->u.i = sqlite3VdbeIntValue(pMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 411 | setTypeFlag(pMem, MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 412 | return SQLITE_OK; |
| 413 | } |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 414 | |
| 415 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 416 | ** Convert pMem so that it is of type MEM_Real. |
| 417 | ** Invalidate any prior representations. |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 418 | */ |
| 419 | int sqlite3VdbeMemRealify(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 420 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 421 | pMem->r = sqlite3VdbeRealValue(pMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 422 | setTypeFlag(pMem, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 423 | return SQLITE_OK; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | ** Convert pMem so that it has types MEM_Real or MEM_Int or both. |
| 428 | ** Invalidate any prior representations. |
| 429 | */ |
| 430 | int sqlite3VdbeMemNumerify(Mem *pMem){ |
drh | cd7b46d | 2007-05-16 11:55:56 +0000 | [diff] [blame] | 431 | double r1, r2; |
| 432 | i64 i; |
| 433 | assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ); |
| 434 | assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 435 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | cd7b46d | 2007-05-16 11:55:56 +0000 | [diff] [blame] | 436 | r1 = sqlite3VdbeRealValue(pMem); |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 437 | i = doubleToInt64(r1); |
drh | cd7b46d | 2007-05-16 11:55:56 +0000 | [diff] [blame] | 438 | r2 = (double)i; |
| 439 | if( r1==r2 ){ |
| 440 | sqlite3VdbeMemIntegerify(pMem); |
| 441 | }else{ |
| 442 | pMem->r = r1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 443 | setTypeFlag(pMem, MEM_Real); |
drh | cd7b46d | 2007-05-16 11:55:56 +0000 | [diff] [blame] | 444 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 445 | return SQLITE_OK; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
| 449 | ** Delete any previous value and set the value stored in *pMem to NULL. |
| 450 | */ |
| 451 | void sqlite3VdbeMemSetNull(Mem *pMem){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 452 | setTypeFlag(pMem, MEM_Null); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 453 | pMem->type = SQLITE_NULL; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 457 | ** Delete any previous value and set the value to be a BLOB of length |
| 458 | ** n containing all zeros. |
| 459 | */ |
| 460 | void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ |
| 461 | sqlite3VdbeMemRelease(pMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 462 | setTypeFlag(pMem, MEM_Blob); |
| 463 | pMem->flags = MEM_Blob|MEM_Zero; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 464 | pMem->type = SQLITE_BLOB; |
| 465 | pMem->n = 0; |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 466 | if( n<0 ) n = 0; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 467 | pMem->u.i = n; |
danielk1977 | def0fec | 2007-05-10 15:37:52 +0000 | [diff] [blame] | 468 | pMem->enc = SQLITE_UTF8; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 472 | ** Delete any previous value and set the value stored in *pMem to val, |
| 473 | ** manifest type INTEGER. |
| 474 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 475 | void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 476 | sqlite3VdbeMemRelease(pMem); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 477 | pMem->u.i = val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 478 | pMem->flags = MEM_Int; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 479 | pMem->type = SQLITE_INTEGER; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
| 483 | ** Delete any previous value and set the value stored in *pMem to val, |
| 484 | ** manifest type REAL. |
| 485 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 486 | void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ |
drh | 0de3ae9 | 2008-04-28 16:55:26 +0000 | [diff] [blame] | 487 | if( sqlite3IsNaN(val) ){ |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 488 | sqlite3VdbeMemSetNull(pMem); |
| 489 | }else{ |
| 490 | sqlite3VdbeMemRelease(pMem); |
| 491 | pMem->r = val; |
| 492 | pMem->flags = MEM_Real; |
| 493 | pMem->type = SQLITE_FLOAT; |
| 494 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 498 | ** Return true if the Mem object contains a TEXT or BLOB that is |
| 499 | ** too large - whose size exceeds SQLITE_MAX_LENGTH. |
| 500 | */ |
| 501 | int sqlite3VdbeMemTooBig(Mem *p){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 502 | assert( p->db!=0 ); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 503 | if( p->flags & (MEM_Str|MEM_Blob) ){ |
| 504 | int n = p->n; |
| 505 | if( p->flags & MEM_Zero ){ |
| 506 | n += p->u.i; |
| 507 | } |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 508 | return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
danielk1977 | e5f5b8f | 2008-03-28 18:11:16 +0000 | [diff] [blame] | 513 | /* |
| 514 | ** Size of struct Mem not including the Mem.zMalloc member. |
| 515 | */ |
mlcreech | fe3f4e8 | 2008-03-29 23:25:27 +0000 | [diff] [blame] | 516 | #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 517 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 518 | /* |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 519 | ** Make an shallow copy of pFrom into pTo. Prior contents of |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 520 | ** pTo are freed. The pFrom->z field is not duplicated. If |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 521 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z |
| 522 | ** and flags gets srcType (either MEM_Ephem or MEM_Static). |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 523 | */ |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 524 | void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 525 | sqlite3VdbeMemReleaseExternal(pTo); |
| 526 | memcpy(pTo, pFrom, MEMCELLSIZE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 527 | pTo->xDel = 0; |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 528 | if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 529 | pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 530 | assert( srcType==MEM_Ephem || srcType==MEM_Static ); |
| 531 | pTo->flags |= srcType; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | ** Make a full copy of pFrom into pTo. Prior contents of pTo are |
| 537 | ** freed before the copy is made. |
| 538 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 539 | int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 540 | int rc = SQLITE_OK; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 541 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 542 | sqlite3VdbeMemReleaseExternal(pTo); |
| 543 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 544 | pTo->flags &= ~MEM_Dyn; |
| 545 | |
| 546 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| 547 | if( 0==(pFrom->flags&MEM_Static) ){ |
| 548 | pTo->flags |= MEM_Ephem; |
| 549 | rc = sqlite3VdbeMemMakeWriteable(pTo); |
danielk1977 | 9172fd8 | 2008-02-14 15:31:52 +0000 | [diff] [blame] | 550 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 551 | } |
| 552 | |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 553 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 554 | } |
| 555 | |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 556 | /* |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 557 | ** Transfer the contents of pFrom to pTo. Any existing value in pTo is |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 558 | ** freed. If pFrom contains ephemeral data, a copy is made. |
| 559 | ** |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 560 | ** pFrom contains an SQL NULL when this routine returns. |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 561 | */ |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 562 | void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 563 | assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); |
| 564 | assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); |
| 565 | assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 566 | |
| 567 | sqlite3VdbeMemRelease(pTo); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 568 | memcpy(pTo, pFrom, sizeof(Mem)); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 569 | pFrom->flags = MEM_Null; |
| 570 | pFrom->xDel = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 571 | pFrom->zMalloc = 0; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 575 | ** Change the value of a Mem to be a string or a BLOB. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 576 | ** |
| 577 | ** The memory management strategy depends on the value of the xDel |
| 578 | ** parameter. If the value passed is SQLITE_TRANSIENT, then the |
| 579 | ** string is copied into a (possibly existing) buffer managed by the |
| 580 | ** Mem structure. Otherwise, any existing buffer is freed and the |
| 581 | ** pointer copied. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 582 | */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 583 | int sqlite3VdbeMemSetStr( |
| 584 | Mem *pMem, /* Memory cell to set to string value */ |
| 585 | const char *z, /* String pointer */ |
| 586 | int n, /* Bytes in string, or negative */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 587 | u8 enc, /* Encoding of z. 0 for BLOBs */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 588 | void (*xDel)(void*) /* Destructor function */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 589 | ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 590 | int nByte = n; /* New value for pMem->n */ |
| 591 | int flags = 0; /* New value for pMem->flags */ |
| 592 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 593 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 594 | |
| 595 | /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 596 | if( !z ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 597 | sqlite3VdbeMemSetNull(pMem); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 598 | return SQLITE_OK; |
| 599 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 600 | |
| 601 | flags = (enc==0?MEM_Blob:MEM_Str); |
| 602 | if( nByte<0 ){ |
| 603 | assert( enc!=0 ); |
drh | 8fd3897 | 2008-02-19 15:44:09 +0000 | [diff] [blame] | 604 | if( enc==SQLITE_UTF8 ){ |
| 605 | for(nByte=0; z[nByte]; nByte++){} |
| 606 | }else{ |
| 607 | for(nByte=0; z[nByte] | z[nByte+1]; nByte+=2){} |
| 608 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 609 | flags |= MEM_Term; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 610 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 611 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 612 | /* The following block sets the new values of Mem.z and Mem.xDel. It |
| 613 | ** also sets a flag in local variable "flags" to indicate the memory |
| 614 | ** management (one of MEM_Dyn or MEM_Static). |
| 615 | */ |
| 616 | if( xDel==SQLITE_TRANSIENT ){ |
| 617 | int nAlloc = nByte; |
| 618 | if( flags&MEM_Term ){ |
| 619 | nAlloc += (enc==SQLITE_UTF8?1:2); |
| 620 | } |
| 621 | if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){ |
| 622 | return SQLITE_NOMEM; |
| 623 | } |
| 624 | memcpy(pMem->z, z, nAlloc); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 625 | }else{ |
| 626 | sqlite3VdbeMemRelease(pMem); |
| 627 | pMem->z = (char *)z; |
| 628 | pMem->xDel = xDel; |
| 629 | flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); |
| 630 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 631 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 632 | pMem->n = nByte; |
| 633 | pMem->flags = flags; |
| 634 | pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); |
| 635 | pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 636 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 637 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 638 | if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ |
| 639 | return SQLITE_NOMEM; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 640 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 641 | #endif |
| 642 | |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 643 | return SQLITE_OK; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
| 647 | ** Compare the values contained by the two memory cells, returning |
| 648 | ** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 649 | ** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 650 | ** and reals) sorted numerically, followed by text ordered by the collating |
| 651 | ** sequence pColl and finally blob's ordered by memcmp(). |
| 652 | ** |
| 653 | ** Two NULL values are considered equal by this function. |
| 654 | */ |
| 655 | int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 656 | int rc; |
| 657 | int f1, f2; |
| 658 | int combined_flags; |
| 659 | |
| 660 | /* Interchange pMem1 and pMem2 if the collating sequence specifies |
| 661 | ** DESC order. |
| 662 | */ |
| 663 | f1 = pMem1->flags; |
| 664 | f2 = pMem2->flags; |
| 665 | combined_flags = f1|f2; |
| 666 | |
| 667 | /* If one value is NULL, it is less than the other. If both values |
| 668 | ** are NULL, return 0. |
| 669 | */ |
| 670 | if( combined_flags&MEM_Null ){ |
| 671 | return (f2&MEM_Null) - (f1&MEM_Null); |
| 672 | } |
| 673 | |
| 674 | /* If one value is a number and the other is not, the number is less. |
| 675 | ** If both are numbers, compare as reals if one is a real, or as integers |
| 676 | ** if both values are integers. |
| 677 | */ |
| 678 | if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 679 | if( !(f1&(MEM_Int|MEM_Real)) ){ |
| 680 | return 1; |
| 681 | } |
| 682 | if( !(f2&(MEM_Int|MEM_Real)) ){ |
| 683 | return -1; |
| 684 | } |
| 685 | if( (f1 & f2 & MEM_Int)==0 ){ |
| 686 | double r1, r2; |
| 687 | if( (f1&MEM_Real)==0 ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 688 | r1 = pMem1->u.i; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 689 | }else{ |
| 690 | r1 = pMem1->r; |
| 691 | } |
| 692 | if( (f2&MEM_Real)==0 ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 693 | r2 = pMem2->u.i; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 694 | }else{ |
| 695 | r2 = pMem2->r; |
| 696 | } |
| 697 | if( r1<r2 ) return -1; |
| 698 | if( r1>r2 ) return 1; |
| 699 | return 0; |
| 700 | }else{ |
| 701 | assert( f1&MEM_Int ); |
| 702 | assert( f2&MEM_Int ); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 703 | if( pMem1->u.i < pMem2->u.i ) return -1; |
| 704 | if( pMem1->u.i > pMem2->u.i ) return 1; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* If one value is a string and the other is a blob, the string is less. |
| 710 | ** If both are strings, compare using the collating functions. |
| 711 | */ |
| 712 | if( combined_flags&MEM_Str ){ |
| 713 | if( (f1 & MEM_Str)==0 ){ |
| 714 | return 1; |
| 715 | } |
| 716 | if( (f2 & MEM_Str)==0 ){ |
| 717 | return -1; |
| 718 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 719 | |
| 720 | assert( pMem1->enc==pMem2->enc ); |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 721 | assert( pMem1->enc==SQLITE_UTF8 || |
| 722 | pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 723 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 724 | /* The collation sequence must be defined at this point, even if |
| 725 | ** the user deletes the collation sequence after the vdbe program is |
| 726 | ** compiled (this was not always the case). |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 727 | */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 728 | assert( !pColl || pColl->xCmp ); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 729 | |
| 730 | if( pColl ){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 731 | if( pMem1->enc==pColl->enc ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 732 | /* The strings are already in the correct encoding. Call the |
| 733 | ** comparison function directly */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 734 | return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 735 | }else{ |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 736 | u8 origEnc = pMem1->enc; |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 737 | const void *v1, *v2; |
| 738 | int n1, n2; |
| 739 | /* Convert the strings into the encoding that the comparison |
| 740 | ** function expects */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 741 | v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 742 | n1 = v1==0 ? 0 : pMem1->n; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 743 | assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) ); |
| 744 | v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 745 | n2 = v2==0 ? 0 : pMem2->n; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 746 | assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) ); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 747 | /* Do the comparison */ |
| 748 | rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 749 | /* Convert the strings back into the database encoding */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 750 | sqlite3ValueText((sqlite3_value*)pMem1, origEnc); |
| 751 | sqlite3ValueText((sqlite3_value*)pMem2, origEnc); |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 752 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 753 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 754 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 755 | /* If a NULL pointer was passed as the collate function, fall through |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 756 | ** to the blob case and use memcmp(). */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 757 | } |
| 758 | |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 759 | /* Both values must be blobs. Compare using memcmp(). */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 760 | rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 761 | if( rc==0 ){ |
| 762 | rc = pMem1->n - pMem2->n; |
| 763 | } |
| 764 | return rc; |
| 765 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 766 | |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 767 | /* |
| 768 | ** Move data out of a btree key or data field and into a Mem structure. |
| 769 | ** The data or key is taken from the entry that pCur is currently pointing |
| 770 | ** to. offset and amt determine what portion of the data or key to retrieve. |
| 771 | ** key is true to get the key or false to get data. The result is written |
| 772 | ** into the pMem element. |
| 773 | ** |
| 774 | ** The pMem structure is assumed to be uninitialized. Any prior content |
| 775 | ** is overwritten without being freed. |
| 776 | ** |
| 777 | ** If this routine fails for any reason (malloc returns NULL or unable |
| 778 | ** to read from the disk) then the pMem is left in an inconsistent state. |
| 779 | */ |
| 780 | int sqlite3VdbeMemFromBtree( |
| 781 | BtCursor *pCur, /* Cursor pointing at record to retrieve. */ |
| 782 | int offset, /* Offset from the start of data to return bytes from. */ |
| 783 | int amt, /* Number of bytes to return. */ |
| 784 | int key, /* If true, retrieve from the btree key, not data. */ |
| 785 | Mem *pMem /* OUT: Return data in this Mem structure. */ |
| 786 | ){ |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 787 | char *zData; /* Data from the btree layer */ |
| 788 | int available = 0; /* Number of bytes available on the local btree page */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 789 | sqlite3 *db; /* Database connection */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 790 | int rc = SQLITE_OK; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 791 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 792 | db = sqlite3BtreeCursorDb(pCur); |
| 793 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 794 | if( key ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 795 | zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 796 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 797 | zData = (char *)sqlite3BtreeDataFetch(pCur, &available); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 798 | } |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 799 | assert( zData!=0 ); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 800 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 801 | if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){ |
| 802 | sqlite3VdbeMemRelease(pMem); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 803 | pMem->z = &zData[offset]; |
| 804 | pMem->flags = MEM_Blob|MEM_Ephem; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 805 | }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ |
| 806 | pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 807 | pMem->enc = 0; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 808 | pMem->type = SQLITE_BLOB; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 809 | if( key ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 810 | rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 811 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 812 | rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 813 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 814 | pMem->z[amt] = 0; |
| 815 | pMem->z[amt+1] = 0; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 816 | if( rc!=SQLITE_OK ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 817 | sqlite3VdbeMemRelease(pMem); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 820 | pMem->n = amt; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 821 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 822 | return rc; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 823 | } |
| 824 | |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 825 | #if 0 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 826 | /* |
| 827 | ** Perform various checks on the memory cell pMem. An assert() will |
| 828 | ** fail if pMem is internally inconsistent. |
| 829 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 830 | void sqlite3VdbeMemSanity(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 831 | int flags = pMem->flags; |
| 832 | assert( flags!=0 ); /* Must define some type */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 833 | if( flags & (MEM_Str|MEM_Blob) ){ |
| 834 | int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 835 | assert( x!=0 ); /* Strings must define a string subtype */ |
| 836 | assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 837 | assert( pMem->z!=0 ); /* Strings must have a value */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 838 | /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 839 | assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort ); |
| 840 | assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort ); |
drh | 22276bd | 2004-06-22 22:54:22 +0000 | [diff] [blame] | 841 | /* No destructor unless there is MEM_Dyn */ |
| 842 | assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 843 | |
| 844 | if( (flags & MEM_Str) ){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 845 | assert( pMem->enc==SQLITE_UTF8 || |
| 846 | pMem->enc==SQLITE_UTF16BE || |
| 847 | pMem->enc==SQLITE_UTF16LE |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 848 | ); |
| 849 | /* If the string is UTF-8 encoded and nul terminated, then pMem->n |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 850 | ** must be the length of the string. (Later:) If the database file |
| 851 | ** has been corrupted, '\000' characters might have been inserted |
| 852 | ** into the middle of the string. In that case, the strlen() might |
| 853 | ** be less. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 854 | */ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 855 | if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 856 | assert( strlen(pMem->z)<=pMem->n ); |
| 857 | assert( pMem->z[pMem->n]==0 ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | }else{ |
| 861 | /* Cannot define a string subtype for non-string objects */ |
| 862 | assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 ); |
drh | 22276bd | 2004-06-22 22:54:22 +0000 | [diff] [blame] | 863 | assert( pMem->xDel==0 ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 864 | } |
| 865 | /* MEM_Null excludes all other types */ |
| 866 | assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0 |
| 867 | || (pMem->flags&MEM_Null)==0 ); |
drh | f0bce09 | 2005-08-20 13:47:41 +0000 | [diff] [blame] | 868 | /* If the MEM is both real and integer, the values are equal */ |
| 869 | assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 870 | || pMem->r==pMem->u.i ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 871 | } |
| 872 | #endif |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 873 | |
| 874 | /* This function is only available internally, it is not part of the |
| 875 | ** external API. It works in a similar way to sqlite3_value_text(), |
| 876 | ** except the data returned is in the encoding specified by the second |
| 877 | ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or |
| 878 | ** SQLITE_UTF8. |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 879 | ** |
| 880 | ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. |
| 881 | ** If that is the case, then the result must be aligned on an even byte |
| 882 | ** boundary. |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 883 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 884 | const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 885 | if( !pVal ) return 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 886 | |
| 887 | assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 888 | assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 889 | |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 890 | if( pVal->flags&MEM_Null ){ |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 891 | return 0; |
| 892 | } |
drh | f1f6c58 | 2006-01-12 19:42:41 +0000 | [diff] [blame] | 893 | assert( (MEM_Blob>>3) == MEM_Str ); |
| 894 | pVal->flags |= (pVal->flags & MEM_Blob)>>3; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 895 | expandBlob(pVal); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 896 | if( pVal->flags&MEM_Str ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 897 | sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 898 | if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 899 | assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 900 | if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 901 | return 0; |
| 902 | } |
| 903 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 904 | sqlite3VdbeMemNulTerminate(pVal); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 905 | }else{ |
| 906 | assert( (pVal->flags&MEM_Blob)==0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 907 | sqlite3VdbeMemStringify(pVal, enc); |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 908 | assert( 0==(1&(int)pVal->z) ); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 909 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 910 | assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 |
| 911 | || pVal->db->mallocFailed ); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 912 | if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ |
| 913 | return pVal->z; |
| 914 | }else{ |
| 915 | return 0; |
| 916 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 917 | } |
| 918 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 919 | /* |
| 920 | ** Create a new sqlite3_value object. |
| 921 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 922 | sqlite3_value *sqlite3ValueNew(sqlite3 *db){ |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 923 | Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 924 | if( p ){ |
| 925 | p->flags = MEM_Null; |
| 926 | p->type = SQLITE_NULL; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 927 | p->db = db; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 928 | } |
| 929 | return p; |
| 930 | } |
| 931 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 932 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 933 | ** Create a new sqlite3_value object, containing the value of pExpr. |
| 934 | ** |
| 935 | ** This only works for very simple expressions that consist of one constant |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 936 | ** token (i.e. "5", "5.1", "'a string'"). If the expression can |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 937 | ** be converted directly into a value, then the value is allocated and |
| 938 | ** a pointer written to *ppVal. The caller is responsible for deallocating |
| 939 | ** the value by passing it to sqlite3ValueFree() later on. If the expression |
| 940 | ** cannot be converted to a value, then *ppVal is set to NULL. |
| 941 | */ |
| 942 | int sqlite3ValueFromExpr( |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 943 | sqlite3 *db, /* The database connection */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 944 | Expr *pExpr, /* The expression to evaluate */ |
| 945 | u8 enc, /* Encoding to use */ |
| 946 | u8 affinity, /* Affinity to use */ |
| 947 | sqlite3_value **ppVal /* Write the new value here */ |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 948 | ){ |
| 949 | int op; |
| 950 | char *zVal = 0; |
| 951 | sqlite3_value *pVal = 0; |
| 952 | |
| 953 | if( !pExpr ){ |
| 954 | *ppVal = 0; |
| 955 | return SQLITE_OK; |
| 956 | } |
| 957 | op = pExpr->op; |
| 958 | |
| 959 | if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 960 | zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n); |
| 961 | pVal = sqlite3ValueNew(db); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 962 | if( !zVal || !pVal ) goto no_mem; |
| 963 | sqlite3Dequote(zVal); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 964 | sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 965 | if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 966 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 967 | }else{ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 968 | sqlite3ValueApplyAffinity(pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 969 | } |
| 970 | }else if( op==TK_UMINUS ) { |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 971 | if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 972 | pVal->u.i = -1 * pVal->u.i; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 973 | pVal->r = -1.0 * pVal->r; |
| 974 | } |
| 975 | } |
| 976 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 977 | else if( op==TK_BLOB ){ |
| 978 | int nVal; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 979 | assert( pExpr->token.n>=3 ); |
| 980 | assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); |
| 981 | assert( pExpr->token.z[1]=='\'' ); |
| 982 | assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 983 | pVal = sqlite3ValueNew(db); |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 984 | nVal = pExpr->token.n - 3; |
| 985 | zVal = (char*)pExpr->token.z + 2; |
| 986 | sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, |
| 987 | 0, sqlite3_free); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 988 | } |
| 989 | #endif |
| 990 | |
| 991 | *ppVal = pVal; |
| 992 | return SQLITE_OK; |
| 993 | |
| 994 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 995 | db->mallocFailed = 1; |
| 996 | sqlite3_free(zVal); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 997 | sqlite3ValueFree(pVal); |
| 998 | *ppVal = 0; |
| 999 | return SQLITE_NOMEM; |
| 1000 | } |
| 1001 | |
| 1002 | /* |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1003 | ** Change the string value of an sqlite3_value object |
| 1004 | */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1005 | void sqlite3ValueSetStr( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1006 | sqlite3_value *v, /* Value to be set */ |
| 1007 | int n, /* Length of string z */ |
| 1008 | const void *z, /* Text of the new string */ |
| 1009 | u8 enc, /* Encoding to use */ |
| 1010 | void (*xDel)(void*) /* Destructor for the string */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1011 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1012 | if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1015 | /* |
| 1016 | ** Free an sqlite3_value object |
| 1017 | */ |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1018 | void sqlite3ValueFree(sqlite3_value *v){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1019 | if( !v ) return; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1020 | sqlite3VdbeMemRelease((Mem *)v); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1021 | sqlite3_free(v); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1024 | /* |
| 1025 | ** Return the number of bytes in the sqlite3_value object assuming |
| 1026 | ** that it uses the encoding "enc" |
| 1027 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1028 | int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1029 | Mem *p = (Mem*)pVal; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1030 | if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1031 | if( p->flags & MEM_Zero ){ |
| 1032 | return p->n+p->u.i; |
| 1033 | }else{ |
| 1034 | return p->n; |
| 1035 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1036 | } |
| 1037 | return 0; |
| 1038 | } |