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