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