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 |
| 17 | */ |
| 18 | #include "sqliteInt.h" |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 19 | #include "vdbeInt.h" |
| 20 | |
| 21 | /* |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 22 | ** If pMem is an object with a valid string representation, this routine |
| 23 | ** ensures the internal encoding for the string representation is |
| 24 | ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 25 | ** |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 26 | ** If pMem is not a string object, or the encoding of the string |
| 27 | ** representation is already stored using the requested encoding, then this |
| 28 | ** routine is a no-op. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 29 | ** |
| 30 | ** SQLITE_OK is returned if the conversion is successful (or not required). |
| 31 | ** SQLITE_NOMEM may be returned if a malloc() fails during conversion |
| 32 | ** between formats. |
| 33 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 34 | int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 35 | int rc; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 36 | assert( (pMem->flags&MEM_RowSet)==0 ); |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 37 | assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE |
| 38 | || desiredEnc==SQLITE_UTF16BE ); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 39 | if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 40 | return SQLITE_OK; |
| 41 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 42 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 43 | #ifdef SQLITE_OMIT_UTF16 |
| 44 | return SQLITE_ERROR; |
| 45 | #else |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 46 | |
| 47 | /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, |
| 48 | ** then the encoding of the value may not have changed. |
| 49 | */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 50 | rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc); |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 51 | assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); |
| 52 | assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); |
| 53 | assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 54 | return rc; |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 55 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 58 | /* |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 59 | ** Make sure pMem->z points to a writable allocation of at least |
| 60 | ** n bytes. |
| 61 | ** |
| 62 | ** If the memory cell currently contains string or blob data |
| 63 | ** and the third argument passed to this function is true, the |
| 64 | ** current content of the cell is preserved. Otherwise, it may |
| 65 | ** be discarded. |
| 66 | ** |
| 67 | ** This function sets the MEM_Dyn flag and clears any xDel callback. |
| 68 | ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is |
| 69 | ** not set, Mem.n is zeroed. |
| 70 | */ |
| 71 | int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 72 | assert( 1 >= |
| 73 | ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) + |
| 74 | (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + |
| 75 | ((pMem->flags&MEM_Ephem) ? 1 : 0) + |
| 76 | ((pMem->flags&MEM_Static) ? 1 : 0) |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 77 | ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 78 | assert( (pMem->flags&MEM_RowSet)==0 ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 79 | |
drh | af005fb | 2008-07-09 16:51:51 +0000 | [diff] [blame] | 80 | if( n<32 ) n = 32; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 81 | if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 82 | if( preserve && pMem->z==pMem->zMalloc ){ |
| 83 | pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); |
| 84 | preserve = 0; |
| 85 | }else{ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 86 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 87 | pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 88 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 89 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 90 | |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 91 | if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 92 | memcpy(pMem->zMalloc, pMem->z, pMem->n); |
| 93 | } |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 94 | if( pMem->flags&MEM_Dyn && pMem->xDel ){ |
drh | aa538a5 | 2012-01-19 16:57:16 +0000 | [diff] [blame] | 95 | assert( pMem->xDel!=SQLITE_DYNAMIC ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 96 | pMem->xDel((void *)(pMem->z)); |
| 97 | } |
| 98 | |
| 99 | pMem->z = pMem->zMalloc; |
drh | 753cc10 | 2008-11-11 00:21:30 +0000 | [diff] [blame] | 100 | if( pMem->z==0 ){ |
| 101 | pMem->flags = MEM_Null; |
| 102 | }else{ |
| 103 | pMem->flags &= ~(MEM_Ephem|MEM_Static); |
| 104 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 105 | pMem->xDel = 0; |
| 106 | return (pMem->z ? SQLITE_OK : SQLITE_NOMEM); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
drh | dab898f | 2008-07-30 13:14:55 +0000 | [diff] [blame] | 110 | ** Make the given Mem object MEM_Dyn. In other words, make it so |
| 111 | ** that any TEXT or BLOB content is stored in memory obtained from |
| 112 | ** malloc(). In this way, we know that the memory is safe to be |
| 113 | ** overwritten or altered. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 114 | ** |
| 115 | ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. |
| 116 | */ |
drh | dab898f | 2008-07-30 13:14:55 +0000 | [diff] [blame] | 117 | int sqlite3VdbeMemMakeWriteable(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) ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 120 | assert( (pMem->flags&MEM_RowSet)==0 ); |
drh | 45d2930 | 2012-01-08 22:18:33 +0000 | [diff] [blame] | 121 | ExpandBlob(pMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 122 | f = pMem->flags; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 123 | if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 124 | if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ |
| 125 | return SQLITE_NOMEM; |
| 126 | } |
| 127 | pMem->z[pMem->n] = 0; |
| 128 | pMem->z[pMem->n+1] = 0; |
| 129 | pMem->flags |= MEM_Term; |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 130 | #ifdef SQLITE_DEBUG |
| 131 | pMem->pScopyFrom = 0; |
| 132 | #endif |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 133 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 134 | |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 135 | return SQLITE_OK; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 139 | ** 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] | 140 | ** blob stored in dynamically allocated space. |
| 141 | */ |
danielk1977 | 246ad31 | 2007-05-16 14:23:00 +0000 | [diff] [blame] | 142 | #ifndef SQLITE_OMIT_INCRBLOB |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 143 | int sqlite3VdbeMemExpandBlob(Mem *pMem){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 144 | if( pMem->flags & MEM_Zero ){ |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 145 | int nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 146 | assert( pMem->flags&MEM_Blob ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 147 | assert( (pMem->flags&MEM_RowSet)==0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 148 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 149 | |
| 150 | /* Set nByte to the number of bytes required to store the expanded blob. */ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 151 | nByte = pMem->n + pMem->u.nZero; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 152 | if( nByte<=0 ){ |
| 153 | nByte = 1; |
| 154 | } |
| 155 | if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 156 | return SQLITE_NOMEM; |
| 157 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 158 | |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 159 | memset(&pMem->z[pMem->n], 0, pMem->u.nZero); |
| 160 | pMem->n += pMem->u.nZero; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 161 | pMem->flags &= ~(MEM_Zero|MEM_Term); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 162 | } |
| 163 | return SQLITE_OK; |
| 164 | } |
danielk1977 | 246ad31 | 2007-05-16 14:23:00 +0000 | [diff] [blame] | 165 | #endif |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 169 | ** Make sure the given Mem is \u0000 terminated. |
| 170 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 171 | int sqlite3VdbeMemNulTerminate(Mem *pMem){ |
| 172 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 173 | if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 174 | return SQLITE_OK; /* Nothing to do */ |
| 175 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 176 | if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ |
| 177 | return SQLITE_NOMEM; |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 178 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 179 | pMem->z[pMem->n] = 0; |
| 180 | pMem->z[pMem->n+1] = 0; |
| 181 | pMem->flags |= MEM_Term; |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 182 | return SQLITE_OK; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 186 | ** Add MEM_Str to the set of representations for the given Mem. Numbers |
| 187 | ** are converted using sqlite3_snprintf(). Converting a BLOB to a string |
| 188 | ** is a no-op. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 189 | ** |
| 190 | ** Existing representations MEM_Int and MEM_Real are *not* invalidated. |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 191 | ** |
| 192 | ** A MEM_Null value will never be passed to this function. This function is |
| 193 | ** used for converting values to text for returning to the user (i.e. via |
| 194 | ** sqlite3_value_text()), or for ensuring that values to be used as btree |
| 195 | ** keys are strings. In the former case a NULL pointer is returned the |
| 196 | ** user and the later is an internal programming error. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 197 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 198 | int sqlite3VdbeMemStringify(Mem *pMem, int enc){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 199 | int rc = SQLITE_OK; |
| 200 | int fg = pMem->flags; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 201 | const int nByte = 32; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 202 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 203 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | def0fec | 2007-05-10 15:37:52 +0000 | [diff] [blame] | 204 | assert( !(fg&MEM_Zero) ); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 205 | assert( !(fg&(MEM_Str|MEM_Blob)) ); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 206 | assert( fg&(MEM_Int|MEM_Real) ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 207 | assert( (pMem->flags&MEM_RowSet)==0 ); |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 208 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 209 | |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 210 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 211 | if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
| 212 | return SQLITE_NOMEM; |
| 213 | } |
| 214 | |
| 215 | /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8 |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 216 | ** string representation of the value. Then, if the required encoding |
| 217 | ** is UTF-16le or UTF-16be do a translation. |
| 218 | ** |
| 219 | ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. |
| 220 | */ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 221 | if( fg & MEM_Int ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 222 | sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 223 | }else{ |
| 224 | assert( fg & MEM_Real ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 225 | sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 226 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 227 | pMem->n = sqlite3Strlen30(pMem->z); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 228 | pMem->enc = SQLITE_UTF8; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 229 | pMem->flags |= MEM_Str|MEM_Term; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 230 | sqlite3VdbeChangeEncoding(pMem, enc); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 231 | return rc; |
| 232 | } |
| 233 | |
| 234 | /* |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 235 | ** Memory cell pMem contains the context of an aggregate function. |
| 236 | ** This routine calls the finalize method for that function. The |
| 237 | ** result of the aggregate is stored back into pMem. |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 238 | ** |
| 239 | ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK |
| 240 | ** otherwise. |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 241 | */ |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 242 | int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ |
| 243 | int rc = SQLITE_OK; |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 244 | if( ALWAYS(pFunc && pFunc->xFinalize) ){ |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 245 | sqlite3_context ctx; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 246 | assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 247 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 709b8cb | 2008-08-22 14:41:00 +0000 | [diff] [blame] | 248 | memset(&ctx, 0, sizeof(ctx)); |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 249 | ctx.s.flags = MEM_Null; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 250 | ctx.s.db = pMem->db; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 251 | ctx.pMem = pMem; |
| 252 | ctx.pFunc = pFunc; |
drh | ee9ff67 | 2010-09-03 18:50:48 +0000 | [diff] [blame] | 253 | pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 254 | assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 255 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
drh | 092d5ef | 2008-12-10 11:49:06 +0000 | [diff] [blame] | 256 | memcpy(pMem, &ctx.s, sizeof(ctx.s)); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 257 | rc = ctx.isError; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 258 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 259 | return rc; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 263 | ** If the memory cell contains a string value that must be freed by |
| 264 | ** invoking an external callback, free it now. Calling this function |
| 265 | ** does not free any Mem.zMalloc buffer. |
| 266 | */ |
| 267 | void sqlite3VdbeMemReleaseExternal(Mem *p){ |
| 268 | assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); |
drh | 2d36eb4 | 2011-08-29 02:49:41 +0000 | [diff] [blame] | 269 | if( p->flags&MEM_Agg ){ |
| 270 | sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 271 | assert( (p->flags & MEM_Agg)==0 ); |
| 272 | sqlite3VdbeMemRelease(p); |
| 273 | }else if( p->flags&MEM_Dyn && p->xDel ){ |
| 274 | assert( (p->flags&MEM_RowSet)==0 ); |
drh | aa538a5 | 2012-01-19 16:57:16 +0000 | [diff] [blame] | 275 | assert( p->xDel!=SQLITE_DYNAMIC ); |
drh | 2d36eb4 | 2011-08-29 02:49:41 +0000 | [diff] [blame] | 276 | p->xDel((void *)p->z); |
| 277 | p->xDel = 0; |
| 278 | }else if( p->flags&MEM_RowSet ){ |
| 279 | sqlite3RowSetClear(p->u.pRowSet); |
| 280 | }else if( p->flags&MEM_Frame ){ |
| 281 | sqlite3VdbeMemSetNull(p); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 286 | ** Release any memory held by the Mem. This may leave the Mem in an |
| 287 | ** inconsistent state, for example with (Mem.z==0) and |
| 288 | ** (Mem.type==SQLITE_TEXT). |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 289 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 290 | void sqlite3VdbeMemRelease(Mem *p){ |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 291 | VdbeMemRelease(p); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 292 | sqlite3DbFree(p->db, p->zMalloc); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 293 | p->z = 0; |
| 294 | p->zMalloc = 0; |
| 295 | p->xDel = 0; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 299 | ** Convert a 64-bit IEEE double into a 64-bit signed integer. |
| 300 | ** If the double is too large, return 0x8000000000000000. |
| 301 | ** |
| 302 | ** Most systems appear to do this simply by assigning |
| 303 | ** variables and without the extra range tests. But |
| 304 | ** there are reports that windows throws an expection |
| 305 | ** if the floating point value is out of range. (See ticket #2880.) |
| 306 | ** Because we do not completely understand the problem, we will |
| 307 | ** take the conservative approach and always do range tests |
| 308 | ** before attempting the conversion. |
| 309 | */ |
| 310 | static i64 doubleToInt64(double r){ |
drh | 52d1452 | 2010-01-13 15:15:40 +0000 | [diff] [blame] | 311 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 312 | /* When floating-point is omitted, double and int64 are the same thing */ |
| 313 | return r; |
| 314 | #else |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 315 | /* |
| 316 | ** Many compilers we encounter do not define constants for the |
| 317 | ** minimum and maximum 64-bit integers, or they define them |
| 318 | ** inconsistently. And many do not understand the "LL" notation. |
| 319 | ** So we define our own static constants here using nothing |
| 320 | ** larger than a 32-bit integer constant. |
| 321 | */ |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 322 | static const i64 maxInt = LARGEST_INT64; |
| 323 | static const i64 minInt = SMALLEST_INT64; |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 324 | |
| 325 | if( r<(double)minInt ){ |
| 326 | return minInt; |
| 327 | }else if( r>(double)maxInt ){ |
drh | f9e749c | 2009-03-29 15:12:09 +0000 | [diff] [blame] | 328 | /* minInt is correct here - not maxInt. It turns out that assigning |
| 329 | ** a very large positive number to an integer results in a very large |
| 330 | ** negative integer. This makes no sense, but it is what x86 hardware |
| 331 | ** does so for compatibility we will do the same in software. */ |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 332 | return minInt; |
| 333 | }else{ |
| 334 | return (i64)r; |
| 335 | } |
drh | 52d1452 | 2010-01-13 15:15:40 +0000 | [diff] [blame] | 336 | #endif |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 340 | ** Return some kind of integer value which is the best we can do |
| 341 | ** at representing the value that *pMem describes as an integer. |
| 342 | ** If pMem is an integer, then the value is exact. If pMem is |
| 343 | ** a floating-point then the value returned is the integer part. |
| 344 | ** If pMem is a string or blob, then we make an attempt to convert |
drh | 347a7cb | 2009-03-23 21:37:04 +0000 | [diff] [blame] | 345 | ** it into a integer and return that. If pMem represents an |
| 346 | ** an SQL-NULL value, return 0. |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 347 | ** |
drh | 347a7cb | 2009-03-23 21:37:04 +0000 | [diff] [blame] | 348 | ** If pMem represents a string value, its encoding might be changed. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 349 | */ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 350 | i64 sqlite3VdbeIntValue(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 351 | int flags; |
| 352 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 353 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 354 | flags = pMem->flags; |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 355 | if( flags & MEM_Int ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 356 | return pMem->u.i; |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 357 | }else if( flags & MEM_Real ){ |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 358 | return doubleToInt64(pMem->r); |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 359 | }else if( flags & (MEM_Str|MEM_Blob) ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 360 | i64 value = 0; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 361 | assert( pMem->z || pMem->n==0 ); |
| 362 | testcase( pMem->z==0 ); |
| 363 | sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc); |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 364 | return value; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 365 | }else{ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 366 | return 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 367 | } |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 371 | ** Return the best representation of pMem that we can get into a |
| 372 | ** double. If pMem is already a double or an integer, return its |
| 373 | ** value. If it is a string or blob, try to convert it to a double. |
| 374 | ** If it is a NULL, return 0.0. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 375 | */ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 376 | double sqlite3VdbeRealValue(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 377 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 378 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 379 | if( pMem->flags & MEM_Real ){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 380 | return pMem->r; |
| 381 | }else if( pMem->flags & MEM_Int ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 382 | return (double)pMem->u.i; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 383 | }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 384 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 385 | double val = (double)0; |
drh | e062d7b | 2010-10-05 12:05:32 +0000 | [diff] [blame] | 386 | sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 387 | return val; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 388 | }else{ |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 389 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 390 | return (double)0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 391 | } |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 395 | ** The MEM structure is already a MEM_Real. Try to also make it a |
| 396 | ** MEM_Int if we can. |
| 397 | */ |
| 398 | void sqlite3VdbeIntegerAffinity(Mem *pMem){ |
| 399 | assert( pMem->flags & MEM_Real ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 400 | assert( (pMem->flags & MEM_RowSet)==0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 401 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 402 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
drh | efe3d65 | 2008-01-11 00:06:10 +0000 | [diff] [blame] | 403 | |
drh | d8c303f | 2008-01-11 15:27:03 +0000 | [diff] [blame] | 404 | pMem->u.i = doubleToInt64(pMem->r); |
drh | 94c3a2b | 2009-06-17 16:20:04 +0000 | [diff] [blame] | 405 | |
| 406 | /* Only mark the value as an integer if |
| 407 | ** |
| 408 | ** (1) the round-trip conversion real->int->real is a no-op, and |
| 409 | ** (2) The integer is neither the largest nor the smallest |
| 410 | ** possible integer (ticket #3922) |
| 411 | ** |
drh | e74871a | 2009-08-14 17:53:39 +0000 | [diff] [blame] | 412 | ** The second and third terms in the following conditional enforces |
| 413 | ** the second condition under the assumption that addition overflow causes |
| 414 | ** values to wrap around. On x86 hardware, the third term is always |
| 415 | ** true and could be omitted. But we leave it in because other |
| 416 | ** architectures might behave differently. |
drh | 94c3a2b | 2009-06-17 16:20:04 +0000 | [diff] [blame] | 417 | */ |
drh | bb8c1b5 | 2012-02-10 01:25:13 +0000 | [diff] [blame] | 418 | if( pMem->r==(double)pMem->u.i |
| 419 | && pMem->u.i>SMALLEST_INT64 |
| 420 | #if defined(__i486__) || defined(__x86_64__) |
| 421 | && ALWAYS(pMem->u.i<LARGEST_INT64) |
| 422 | #else |
| 423 | && pMem->u.i<LARGEST_INT64 |
| 424 | #endif |
| 425 | ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 426 | pMem->flags |= MEM_Int; |
| 427 | } |
| 428 | } |
| 429 | |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 430 | /* |
| 431 | ** Convert pMem to type integer. Invalidate any prior representations. |
| 432 | */ |
| 433 | int sqlite3VdbeMemIntegerify(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 434 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 435 | assert( (pMem->flags & MEM_RowSet)==0 ); |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 436 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 437 | |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 438 | pMem->u.i = sqlite3VdbeIntValue(pMem); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 439 | MemSetTypeFlag(pMem, MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 440 | return SQLITE_OK; |
| 441 | } |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 442 | |
| 443 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 444 | ** Convert pMem so that it is of type MEM_Real. |
| 445 | ** Invalidate any prior representations. |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 446 | */ |
| 447 | int sqlite3VdbeMemRealify(Mem *pMem){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 448 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 449 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 450 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 451 | pMem->r = sqlite3VdbeRealValue(pMem); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 452 | MemSetTypeFlag(pMem, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 453 | return SQLITE_OK; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | ** Convert pMem so that it has types MEM_Real or MEM_Int or both. |
| 458 | ** Invalidate any prior representations. |
drh | 4b5db5a | 2010-01-21 01:53:07 +0000 | [diff] [blame] | 459 | ** |
| 460 | ** Every effort is made to force the conversion, even if the input |
| 461 | ** is a string that does not look completely like a number. Convert |
| 462 | ** as much of the string as we can and ignore the rest. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 463 | */ |
| 464 | int sqlite3VdbeMemNumerify(Mem *pMem){ |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 465 | if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){ |
| 466 | assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); |
| 467 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 468 | if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){ |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 469 | MemSetTypeFlag(pMem, MEM_Int); |
| 470 | }else{ |
| 471 | pMem->r = sqlite3VdbeRealValue(pMem); |
| 472 | MemSetTypeFlag(pMem, MEM_Real); |
| 473 | sqlite3VdbeIntegerAffinity(pMem); |
| 474 | } |
drh | cd7b46d | 2007-05-16 11:55:56 +0000 | [diff] [blame] | 475 | } |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 476 | assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 ); |
| 477 | pMem->flags &= ~(MEM_Str|MEM_Blob); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 478 | return SQLITE_OK; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
| 482 | ** Delete any previous value and set the value stored in *pMem to NULL. |
| 483 | */ |
| 484 | void sqlite3VdbeMemSetNull(Mem *pMem){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 485 | if( pMem->flags & MEM_Frame ){ |
dan | 2710657 | 2010-12-01 08:04:47 +0000 | [diff] [blame] | 486 | VdbeFrame *pFrame = pMem->u.pFrame; |
| 487 | pFrame->pParent = pFrame->v->pDelFrame; |
| 488 | pFrame->v->pDelFrame = pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 489 | } |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 490 | if( pMem->flags & MEM_RowSet ){ |
| 491 | sqlite3RowSetClear(pMem->u.pRowSet); |
| 492 | } |
| 493 | MemSetTypeFlag(pMem, MEM_Null); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 494 | pMem->type = SQLITE_NULL; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 498 | ** Delete any previous value and set the value to be a BLOB of length |
| 499 | ** n containing all zeros. |
| 500 | */ |
| 501 | void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ |
| 502 | sqlite3VdbeMemRelease(pMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 503 | pMem->flags = MEM_Blob|MEM_Zero; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 504 | pMem->type = SQLITE_BLOB; |
| 505 | pMem->n = 0; |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 506 | if( n<0 ) n = 0; |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 507 | pMem->u.nZero = n; |
danielk1977 | def0fec | 2007-05-10 15:37:52 +0000 | [diff] [blame] | 508 | pMem->enc = SQLITE_UTF8; |
danielk1977 | f16c624 | 2009-07-18 14:36:23 +0000 | [diff] [blame] | 509 | |
| 510 | #ifdef SQLITE_OMIT_INCRBLOB |
| 511 | sqlite3VdbeMemGrow(pMem, n, 0); |
| 512 | if( pMem->z ){ |
| 513 | pMem->n = n; |
| 514 | memset(pMem->z, 0, n); |
| 515 | } |
| 516 | #endif |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 520 | ** Delete any previous value and set the value stored in *pMem to val, |
| 521 | ** manifest type INTEGER. |
| 522 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 523 | void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 524 | sqlite3VdbeMemRelease(pMem); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 525 | pMem->u.i = val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 526 | pMem->flags = MEM_Int; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 527 | pMem->type = SQLITE_INTEGER; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 528 | } |
| 529 | |
drh | 7ec5ea9 | 2010-01-13 00:04:13 +0000 | [diff] [blame] | 530 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 531 | /* |
| 532 | ** Delete any previous value and set the value stored in *pMem to val, |
| 533 | ** manifest type REAL. |
| 534 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 535 | void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ |
drh | 0de3ae9 | 2008-04-28 16:55:26 +0000 | [diff] [blame] | 536 | if( sqlite3IsNaN(val) ){ |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 537 | sqlite3VdbeMemSetNull(pMem); |
| 538 | }else{ |
| 539 | sqlite3VdbeMemRelease(pMem); |
| 540 | pMem->r = val; |
| 541 | pMem->flags = MEM_Real; |
| 542 | pMem->type = SQLITE_FLOAT; |
| 543 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 544 | } |
drh | 7ec5ea9 | 2010-01-13 00:04:13 +0000 | [diff] [blame] | 545 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 546 | |
| 547 | /* |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 548 | ** Delete any previous value and set the value of pMem to be an |
| 549 | ** empty boolean index. |
| 550 | */ |
| 551 | void sqlite3VdbeMemSetRowSet(Mem *pMem){ |
| 552 | sqlite3 *db = pMem->db; |
| 553 | assert( db!=0 ); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 554 | assert( (pMem->flags & MEM_RowSet)==0 ); |
| 555 | sqlite3VdbeMemRelease(pMem); |
| 556 | pMem->zMalloc = sqlite3DbMallocRaw(db, 64); |
drh | 8d99363 | 2008-12-04 22:17:55 +0000 | [diff] [blame] | 557 | if( db->mallocFailed ){ |
| 558 | pMem->flags = MEM_Null; |
| 559 | }else{ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 560 | assert( pMem->zMalloc ); |
| 561 | pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, |
| 562 | sqlite3DbMallocSize(db, pMem->zMalloc)); |
| 563 | assert( pMem->u.pRowSet!=0 ); |
drh | 8d99363 | 2008-12-04 22:17:55 +0000 | [diff] [blame] | 564 | pMem->flags = MEM_RowSet; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | /* |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 569 | ** Return true if the Mem object contains a TEXT or BLOB that is |
| 570 | ** too large - whose size exceeds SQLITE_MAX_LENGTH. |
| 571 | */ |
| 572 | int sqlite3VdbeMemTooBig(Mem *p){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 573 | assert( p->db!=0 ); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 574 | if( p->flags & (MEM_Str|MEM_Blob) ){ |
| 575 | int n = p->n; |
| 576 | if( p->flags & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 577 | n += p->u.nZero; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 578 | } |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 579 | return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 580 | } |
| 581 | return 0; |
| 582 | } |
| 583 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 584 | #ifdef SQLITE_DEBUG |
| 585 | /* |
| 586 | ** This routine prepares a memory cell for modication by breaking |
| 587 | ** its link to a shallow copy and by marking any current shallow |
| 588 | ** copies of this cell as invalid. |
| 589 | ** |
| 590 | ** This is used for testing and debugging only - to make sure shallow |
| 591 | ** copies are not misused. |
| 592 | */ |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 593 | void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 594 | int i; |
| 595 | Mem *pX; |
| 596 | for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){ |
| 597 | if( pX->pScopyFrom==pMem ){ |
| 598 | pX->flags |= MEM_Invalid; |
| 599 | pX->pScopyFrom = 0; |
| 600 | } |
| 601 | } |
| 602 | pMem->pScopyFrom = 0; |
| 603 | } |
| 604 | #endif /* SQLITE_DEBUG */ |
| 605 | |
danielk1977 | e5f5b8f | 2008-03-28 18:11:16 +0000 | [diff] [blame] | 606 | /* |
| 607 | ** Size of struct Mem not including the Mem.zMalloc member. |
| 608 | */ |
mlcreech | fe3f4e8 | 2008-03-29 23:25:27 +0000 | [diff] [blame] | 609 | #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 610 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 611 | /* |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 612 | ** Make an shallow copy of pFrom into pTo. Prior contents of |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 613 | ** pTo are freed. The pFrom->z field is not duplicated. If |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 614 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z |
| 615 | ** and flags gets srcType (either MEM_Ephem or MEM_Static). |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 616 | */ |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 617 | void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 618 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 619 | VdbeMemRelease(pTo); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 620 | memcpy(pTo, pFrom, MEMCELLSIZE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 621 | pTo->xDel = 0; |
dan | 5fea907 | 2010-03-05 18:46:12 +0000 | [diff] [blame] | 622 | if( (pFrom->flags&MEM_Static)==0 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 623 | pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 624 | assert( srcType==MEM_Ephem || srcType==MEM_Static ); |
| 625 | pTo->flags |= srcType; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | ** Make a full copy of pFrom into pTo. Prior contents of pTo are |
| 631 | ** freed before the copy is made. |
| 632 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 633 | int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 634 | int rc = SQLITE_OK; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 635 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 636 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 637 | VdbeMemRelease(pTo); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 638 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 639 | pTo->flags &= ~MEM_Dyn; |
| 640 | |
| 641 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| 642 | if( 0==(pFrom->flags&MEM_Static) ){ |
| 643 | pTo->flags |= MEM_Ephem; |
| 644 | rc = sqlite3VdbeMemMakeWriteable(pTo); |
danielk1977 | 9172fd8 | 2008-02-14 15:31:52 +0000 | [diff] [blame] | 645 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 646 | } |
| 647 | |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 648 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 649 | } |
| 650 | |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 651 | /* |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 652 | ** Transfer the contents of pFrom to pTo. Any existing value in pTo is |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 653 | ** freed. If pFrom contains ephemeral data, a copy is made. |
| 654 | ** |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 655 | ** pFrom contains an SQL NULL when this routine returns. |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 656 | */ |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 657 | void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 658 | assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); |
| 659 | assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); |
| 660 | assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 661 | |
| 662 | sqlite3VdbeMemRelease(pTo); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 663 | memcpy(pTo, pFrom, sizeof(Mem)); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 664 | pFrom->flags = MEM_Null; |
| 665 | pFrom->xDel = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 666 | pFrom->zMalloc = 0; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 670 | ** Change the value of a Mem to be a string or a BLOB. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 671 | ** |
| 672 | ** The memory management strategy depends on the value of the xDel |
| 673 | ** parameter. If the value passed is SQLITE_TRANSIENT, then the |
| 674 | ** string is copied into a (possibly existing) buffer managed by the |
| 675 | ** Mem structure. Otherwise, any existing buffer is freed and the |
| 676 | ** pointer copied. |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 677 | ** |
| 678 | ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH |
| 679 | ** size limit) then no memory allocation occurs. If the string can be |
| 680 | ** stored without allocating memory, then it is. If a memory allocation |
| 681 | ** is required to store the string, then value of pMem is unchanged. In |
| 682 | ** either case, SQLITE_TOOBIG is returned. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 683 | */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 684 | int sqlite3VdbeMemSetStr( |
| 685 | Mem *pMem, /* Memory cell to set to string value */ |
| 686 | const char *z, /* String pointer */ |
| 687 | int n, /* Bytes in string, or negative */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 688 | u8 enc, /* Encoding of z. 0 for BLOBs */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 689 | void (*xDel)(void*) /* Destructor function */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 690 | ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 691 | int nByte = n; /* New value for pMem->n */ |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame] | 692 | int iLimit; /* Maximum allowed string or blob size */ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 693 | u16 flags = 0; /* New value for pMem->flags */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 694 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 695 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 696 | assert( (pMem->flags & MEM_RowSet)==0 ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 697 | |
| 698 | /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 699 | if( !z ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 700 | sqlite3VdbeMemSetNull(pMem); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 701 | return SQLITE_OK; |
| 702 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 703 | |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame] | 704 | if( pMem->db ){ |
| 705 | iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 706 | }else{ |
| 707 | iLimit = SQLITE_MAX_LENGTH; |
| 708 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 709 | flags = (enc==0?MEM_Blob:MEM_Str); |
| 710 | if( nByte<0 ){ |
| 711 | assert( enc!=0 ); |
drh | 8fd3897 | 2008-02-19 15:44:09 +0000 | [diff] [blame] | 712 | if( enc==SQLITE_UTF8 ){ |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame] | 713 | for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){} |
drh | 8fd3897 | 2008-02-19 15:44:09 +0000 | [diff] [blame] | 714 | }else{ |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame] | 715 | for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} |
drh | 8fd3897 | 2008-02-19 15:44:09 +0000 | [diff] [blame] | 716 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 717 | flags |= MEM_Term; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 718 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 719 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 720 | /* The following block sets the new values of Mem.z and Mem.xDel. It |
| 721 | ** also sets a flag in local variable "flags" to indicate the memory |
| 722 | ** management (one of MEM_Dyn or MEM_Static). |
| 723 | */ |
| 724 | if( xDel==SQLITE_TRANSIENT ){ |
| 725 | int nAlloc = nByte; |
| 726 | if( flags&MEM_Term ){ |
| 727 | nAlloc += (enc==SQLITE_UTF8?1:2); |
| 728 | } |
drh | 0793f1b | 2008-11-05 17:41:19 +0000 | [diff] [blame] | 729 | if( nByte>iLimit ){ |
| 730 | return SQLITE_TOOBIG; |
| 731 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 732 | if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){ |
| 733 | return SQLITE_NOMEM; |
| 734 | } |
| 735 | memcpy(pMem->z, z, nAlloc); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 736 | }else if( xDel==SQLITE_DYNAMIC ){ |
| 737 | sqlite3VdbeMemRelease(pMem); |
| 738 | pMem->zMalloc = pMem->z = (char *)z; |
| 739 | pMem->xDel = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 740 | }else{ |
| 741 | sqlite3VdbeMemRelease(pMem); |
| 742 | pMem->z = (char *)z; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 743 | pMem->xDel = xDel; |
| 744 | flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 745 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 746 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 747 | pMem->n = nByte; |
| 748 | pMem->flags = flags; |
| 749 | pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); |
| 750 | pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 751 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 752 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 753 | if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ |
| 754 | return SQLITE_NOMEM; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 755 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 756 | #endif |
| 757 | |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 758 | if( nByte>iLimit ){ |
| 759 | return SQLITE_TOOBIG; |
| 760 | } |
| 761 | |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 762 | return SQLITE_OK; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* |
| 766 | ** Compare the values contained by the two memory cells, returning |
| 767 | ** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 768 | ** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 769 | ** and reals) sorted numerically, followed by text ordered by the collating |
| 770 | ** sequence pColl and finally blob's ordered by memcmp(). |
| 771 | ** |
| 772 | ** Two NULL values are considered equal by this function. |
| 773 | */ |
| 774 | int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 775 | int rc; |
| 776 | int f1, f2; |
| 777 | int combined_flags; |
| 778 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 779 | f1 = pMem1->flags; |
| 780 | f2 = pMem2->flags; |
| 781 | combined_flags = f1|f2; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 782 | assert( (combined_flags & MEM_RowSet)==0 ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 783 | |
| 784 | /* If one value is NULL, it is less than the other. If both values |
| 785 | ** are NULL, return 0. |
| 786 | */ |
| 787 | if( combined_flags&MEM_Null ){ |
| 788 | return (f2&MEM_Null) - (f1&MEM_Null); |
| 789 | } |
| 790 | |
| 791 | /* If one value is a number and the other is not, the number is less. |
| 792 | ** If both are numbers, compare as reals if one is a real, or as integers |
| 793 | ** if both values are integers. |
| 794 | */ |
| 795 | if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 796 | if( !(f1&(MEM_Int|MEM_Real)) ){ |
| 797 | return 1; |
| 798 | } |
| 799 | if( !(f2&(MEM_Int|MEM_Real)) ){ |
| 800 | return -1; |
| 801 | } |
| 802 | if( (f1 & f2 & MEM_Int)==0 ){ |
| 803 | double r1, r2; |
| 804 | if( (f1&MEM_Real)==0 ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 805 | r1 = (double)pMem1->u.i; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 806 | }else{ |
| 807 | r1 = pMem1->r; |
| 808 | } |
| 809 | if( (f2&MEM_Real)==0 ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 810 | r2 = (double)pMem2->u.i; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 811 | }else{ |
| 812 | r2 = pMem2->r; |
| 813 | } |
| 814 | if( r1<r2 ) return -1; |
| 815 | if( r1>r2 ) return 1; |
| 816 | return 0; |
| 817 | }else{ |
| 818 | assert( f1&MEM_Int ); |
| 819 | assert( f2&MEM_Int ); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 820 | if( pMem1->u.i < pMem2->u.i ) return -1; |
| 821 | if( pMem1->u.i > pMem2->u.i ) return 1; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 822 | return 0; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* If one value is a string and the other is a blob, the string is less. |
| 827 | ** If both are strings, compare using the collating functions. |
| 828 | */ |
| 829 | if( combined_flags&MEM_Str ){ |
| 830 | if( (f1 & MEM_Str)==0 ){ |
| 831 | return 1; |
| 832 | } |
| 833 | if( (f2 & MEM_Str)==0 ){ |
| 834 | return -1; |
| 835 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 836 | |
| 837 | assert( pMem1->enc==pMem2->enc ); |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 838 | assert( pMem1->enc==SQLITE_UTF8 || |
| 839 | pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 840 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 841 | /* The collation sequence must be defined at this point, even if |
| 842 | ** the user deletes the collation sequence after the vdbe program is |
| 843 | ** compiled (this was not always the case). |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 844 | */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 845 | assert( !pColl || pColl->xCmp ); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 846 | |
| 847 | if( pColl ){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 848 | if( pMem1->enc==pColl->enc ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 849 | /* The strings are already in the correct encoding. Call the |
| 850 | ** comparison function directly */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 851 | return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 852 | }else{ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 853 | const void *v1, *v2; |
| 854 | int n1, n2; |
danielk1977 | 7eae4f5 | 2008-09-16 12:06:08 +0000 | [diff] [blame] | 855 | Mem c1; |
| 856 | Mem c2; |
| 857 | memset(&c1, 0, sizeof(c1)); |
| 858 | memset(&c2, 0, sizeof(c2)); |
| 859 | sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 860 | sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 861 | v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 862 | n1 = v1==0 ? 0 : c1.n; |
| 863 | v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 864 | n2 = v2==0 ? 0 : c2.n; |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 865 | rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
danielk1977 | 7eae4f5 | 2008-09-16 12:06:08 +0000 | [diff] [blame] | 866 | sqlite3VdbeMemRelease(&c1); |
| 867 | sqlite3VdbeMemRelease(&c2); |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 868 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 869 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 870 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 871 | /* If a NULL pointer was passed as the collate function, fall through |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 872 | ** to the blob case and use memcmp(). */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 873 | } |
| 874 | |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 875 | /* Both values must be blobs. Compare using memcmp(). */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 876 | rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 877 | if( rc==0 ){ |
| 878 | rc = pMem1->n - pMem2->n; |
| 879 | } |
| 880 | return rc; |
| 881 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 882 | |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 883 | /* |
| 884 | ** Move data out of a btree key or data field and into a Mem structure. |
| 885 | ** The data or key is taken from the entry that pCur is currently pointing |
| 886 | ** to. offset and amt determine what portion of the data or key to retrieve. |
| 887 | ** key is true to get the key or false to get data. The result is written |
| 888 | ** into the pMem element. |
| 889 | ** |
| 890 | ** The pMem structure is assumed to be uninitialized. Any prior content |
| 891 | ** is overwritten without being freed. |
| 892 | ** |
| 893 | ** If this routine fails for any reason (malloc returns NULL or unable |
| 894 | ** to read from the disk) then the pMem is left in an inconsistent state. |
| 895 | */ |
| 896 | int sqlite3VdbeMemFromBtree( |
| 897 | BtCursor *pCur, /* Cursor pointing at record to retrieve. */ |
| 898 | int offset, /* Offset from the start of data to return bytes from. */ |
| 899 | int amt, /* Number of bytes to return. */ |
| 900 | int key, /* If true, retrieve from the btree key, not data. */ |
| 901 | Mem *pMem /* OUT: Return data in this Mem structure. */ |
| 902 | ){ |
danielk1977 | 4b0aa4c | 2009-05-28 11:05:57 +0000 | [diff] [blame] | 903 | char *zData; /* Data from the btree layer */ |
| 904 | int available = 0; /* Number of bytes available on the local btree page */ |
| 905 | int rc = SQLITE_OK; /* Return code */ |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 906 | |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 907 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 908 | |
danielk1977 | 4b0aa4c | 2009-05-28 11:05:57 +0000 | [diff] [blame] | 909 | /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() |
| 910 | ** that both the BtShared and database handle mutexes are held. */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 911 | assert( (pMem->flags & MEM_RowSet)==0 ); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 912 | if( key ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 913 | zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 914 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 915 | zData = (char *)sqlite3BtreeDataFetch(pCur, &available); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 916 | } |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 917 | assert( zData!=0 ); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 918 | |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 919 | if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 920 | sqlite3VdbeMemRelease(pMem); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 921 | pMem->z = &zData[offset]; |
| 922 | pMem->flags = MEM_Blob|MEM_Ephem; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 923 | }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ |
| 924 | pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 925 | pMem->enc = 0; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 926 | pMem->type = SQLITE_BLOB; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 927 | if( key ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 928 | rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 929 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 930 | rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 931 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 932 | pMem->z[amt] = 0; |
| 933 | pMem->z[amt+1] = 0; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 934 | if( rc!=SQLITE_OK ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 935 | sqlite3VdbeMemRelease(pMem); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 938 | pMem->n = amt; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 939 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 940 | return rc; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 941 | } |
| 942 | |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 943 | /* This function is only available internally, it is not part of the |
| 944 | ** external API. It works in a similar way to sqlite3_value_text(), |
| 945 | ** except the data returned is in the encoding specified by the second |
| 946 | ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or |
| 947 | ** SQLITE_UTF8. |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 948 | ** |
| 949 | ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. |
| 950 | ** If that is the case, then the result must be aligned on an even byte |
| 951 | ** boundary. |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 952 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 953 | const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 954 | if( !pVal ) return 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 955 | |
| 956 | assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 957 | assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 958 | assert( (pVal->flags & MEM_RowSet)==0 ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 959 | |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 960 | if( pVal->flags&MEM_Null ){ |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 961 | return 0; |
| 962 | } |
drh | f1f6c58 | 2006-01-12 19:42:41 +0000 | [diff] [blame] | 963 | assert( (MEM_Blob>>3) == MEM_Str ); |
| 964 | pVal->flags |= (pVal->flags & MEM_Blob)>>3; |
drh | 45d2930 | 2012-01-08 22:18:33 +0000 | [diff] [blame] | 965 | ExpandBlob(pVal); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 966 | if( pVal->flags&MEM_Str ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 967 | sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); |
shane | 1fc4129 | 2008-07-08 22:28:48 +0000 | [diff] [blame] | 968 | if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 969 | assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 970 | if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 971 | return 0; |
| 972 | } |
| 973 | } |
drh | 2faf5f5 | 2011-12-30 15:17:47 +0000 | [diff] [blame] | 974 | sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */ |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 975 | }else{ |
| 976 | assert( (pVal->flags&MEM_Blob)==0 ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 977 | sqlite3VdbeMemStringify(pVal, enc); |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 978 | assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 979 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 980 | assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 |
| 981 | || pVal->db->mallocFailed ); |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 982 | if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ |
| 983 | return pVal->z; |
| 984 | }else{ |
| 985 | return 0; |
| 986 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 987 | } |
| 988 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 989 | /* |
| 990 | ** Create a new sqlite3_value object. |
| 991 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 992 | sqlite3_value *sqlite3ValueNew(sqlite3 *db){ |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 993 | Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 994 | if( p ){ |
| 995 | p->flags = MEM_Null; |
| 996 | p->type = SQLITE_NULL; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 997 | p->db = db; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 998 | } |
| 999 | return p; |
| 1000 | } |
| 1001 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1002 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1003 | ** Create a new sqlite3_value object, containing the value of pExpr. |
| 1004 | ** |
| 1005 | ** This only works for very simple expressions that consist of one constant |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 1006 | ** token (i.e. "5", "5.1", "'a string'"). If the expression can |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1007 | ** be converted directly into a value, then the value is allocated and |
| 1008 | ** a pointer written to *ppVal. The caller is responsible for deallocating |
| 1009 | ** the value by passing it to sqlite3ValueFree() later on. If the expression |
| 1010 | ** cannot be converted to a value, then *ppVal is set to NULL. |
| 1011 | */ |
| 1012 | int sqlite3ValueFromExpr( |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1013 | sqlite3 *db, /* The database connection */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1014 | Expr *pExpr, /* The expression to evaluate */ |
| 1015 | u8 enc, /* Encoding to use */ |
| 1016 | u8 affinity, /* Affinity to use */ |
| 1017 | sqlite3_value **ppVal /* Write the new value here */ |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1018 | ){ |
| 1019 | int op; |
| 1020 | char *zVal = 0; |
| 1021 | sqlite3_value *pVal = 0; |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1022 | int negInt = 1; |
| 1023 | const char *zNeg = ""; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1024 | |
| 1025 | if( !pExpr ){ |
| 1026 | *ppVal = 0; |
| 1027 | return SQLITE_OK; |
| 1028 | } |
| 1029 | op = pExpr->op; |
drh | 4a466d3 | 2010-06-25 14:17:58 +0000 | [diff] [blame] | 1030 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1031 | /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3. |
drh | 4a466d3 | 2010-06-25 14:17:58 +0000 | [diff] [blame] | 1032 | ** The ifdef here is to enable us to achieve 100% branch test coverage even |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1033 | ** when SQLITE_ENABLE_STAT3 is omitted. |
drh | 4a466d3 | 2010-06-25 14:17:58 +0000 | [diff] [blame] | 1034 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1035 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 4a466d3 | 2010-06-25 14:17:58 +0000 | [diff] [blame] | 1036 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 1037 | #else |
| 1038 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 1039 | #endif |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1040 | |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1041 | /* Handle negative integers in a single step. This is needed in the |
| 1042 | ** case when the value is -9223372036854775808. |
| 1043 | */ |
| 1044 | if( op==TK_UMINUS |
| 1045 | && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){ |
| 1046 | pExpr = pExpr->pLeft; |
| 1047 | op = pExpr->op; |
| 1048 | negInt = -1; |
| 1049 | zNeg = "-"; |
| 1050 | } |
| 1051 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1052 | if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1053 | pVal = sqlite3ValueNew(db); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1054 | if( pVal==0 ) goto no_mem; |
| 1055 | if( ExprHasProperty(pExpr, EP_IntValue) ){ |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1056 | sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1057 | }else{ |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1058 | zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1059 | if( zVal==0 ) goto no_mem; |
| 1060 | sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); |
drh | 3995c26 | 2009-08-19 22:14:17 +0000 | [diff] [blame] | 1061 | if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1062 | } |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1063 | if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ |
drh | e3b9bfe | 2009-05-05 12:54:50 +0000 | [diff] [blame] | 1064 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1065 | }else{ |
drh | e3b9bfe | 2009-05-05 12:54:50 +0000 | [diff] [blame] | 1066 | sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); |
| 1067 | } |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1068 | if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; |
drh | e3b9bfe | 2009-05-05 12:54:50 +0000 | [diff] [blame] | 1069 | if( enc!=SQLITE_UTF8 ){ |
| 1070 | sqlite3VdbeChangeEncoding(pVal, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1071 | } |
| 1072 | }else if( op==TK_UMINUS ) { |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1073 | /* This branch happens for multiple negative signs. Ex: -(-5) */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1074 | if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){ |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1075 | sqlite3VdbeMemNumerify(pVal); |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 1076 | if( pVal->u.i==SMALLEST_INT64 ){ |
| 1077 | pVal->flags &= MEM_Int; |
| 1078 | pVal->flags |= MEM_Real; |
| 1079 | pVal->r = (double)LARGEST_INT64; |
| 1080 | }else{ |
| 1081 | pVal->u.i = -pVal->u.i; |
| 1082 | } |
| 1083 | pVal->r = -pVal->r; |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1084 | sqlite3ValueApplyAffinity(pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1085 | } |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 1086 | }else if( op==TK_NULL ){ |
| 1087 | pVal = sqlite3ValueNew(db); |
drh | b1aa0ab | 2011-02-18 17:23:23 +0000 | [diff] [blame] | 1088 | if( pVal==0 ) goto no_mem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1089 | } |
| 1090 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 1091 | else if( op==TK_BLOB ){ |
| 1092 | int nVal; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1093 | assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); |
| 1094 | assert( pExpr->u.zToken[1]=='\'' ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1095 | pVal = sqlite3ValueNew(db); |
danielk1977 | f150c9d | 2008-10-30 17:21:12 +0000 | [diff] [blame] | 1096 | if( !pVal ) goto no_mem; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1097 | zVal = &pExpr->u.zToken[2]; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1098 | nVal = sqlite3Strlen30(zVal)-1; |
| 1099 | assert( zVal[nVal]=='\'' ); |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 1100 | sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1101 | 0, SQLITE_DYNAMIC); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1102 | } |
| 1103 | #endif |
| 1104 | |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1105 | if( pVal ){ |
| 1106 | sqlite3VdbeMemStoreType(pVal); |
| 1107 | } |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1108 | *ppVal = pVal; |
| 1109 | return SQLITE_OK; |
| 1110 | |
| 1111 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1112 | db->mallocFailed = 1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1113 | sqlite3DbFree(db, zVal); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1114 | sqlite3ValueFree(pVal); |
| 1115 | *ppVal = 0; |
| 1116 | return SQLITE_NOMEM; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1120 | ** Change the string value of an sqlite3_value object |
| 1121 | */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1122 | void sqlite3ValueSetStr( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1123 | sqlite3_value *v, /* Value to be set */ |
| 1124 | int n, /* Length of string z */ |
| 1125 | const void *z, /* Text of the new string */ |
| 1126 | u8 enc, /* Encoding to use */ |
| 1127 | void (*xDel)(void*) /* Destructor for the string */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1128 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1129 | if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1132 | /* |
| 1133 | ** Free an sqlite3_value object |
| 1134 | */ |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1135 | void sqlite3ValueFree(sqlite3_value *v){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1136 | if( !v ) return; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1137 | sqlite3VdbeMemRelease((Mem *)v); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1138 | sqlite3DbFree(((Mem*)v)->db, v); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1141 | /* |
| 1142 | ** Return the number of bytes in the sqlite3_value object assuming |
| 1143 | ** that it uses the encoding "enc" |
| 1144 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1145 | int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1146 | Mem *p = (Mem*)pVal; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1147 | if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1148 | if( p->flags & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 1149 | return p->n + p->u.nZero; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1150 | }else{ |
| 1151 | return p->n; |
| 1152 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 1153 | } |
| 1154 | return 0; |
| 1155 | } |