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