SQLite

Check-in [8a66a502ba]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add versions of OP_MakeRecord and OP_Column that use manifest typing (not activated yet). (CVS 1334)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8a66a502ba09e3d858d2f45df9b3b665ebb85d5b
User & Date: danielk1977 2004-05-10 07:17:31.000
Context
2004-05-10
07:17
Add versions of OP_MakeRecord and OP_Column that use manifest typing (not activated yet). (CVS 1335) (check-in: 9ea8e8ab23 user: danielk1977 tags: trunk)
07:17
Add versions of OP_MakeRecord and OP_Column that use manifest typing (not activated yet). (CVS 1334) (check-in: 8a66a502ba user: danielk1977 tags: trunk)
01:17
Change some code that assumes the root-page of sqlite_master is 2 (it is now 1) (CVS 1333) (check-in: 37ae528fb8 user: danielk1977 tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/sqliteInt.h.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.225 2004/05/10 01:17:37 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.226 2004/05/10 07:17:31 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
1280
1281
1282
1283
1284
1285
1286
1287
1288

1289
int sqlite3FitsIn32Bits(const char *);

unsigned char *sqlite3utf16to8(const void *pData, int N);
void *sqlite3utf8to16be(const unsigned char *pIn, int N);
void *sqlite3utf8to16le(const unsigned char *pIn, int N);
void sqlite3utf16to16le(void *pData, int N);
void sqlite3utf16to16be(void *pData, int N);











|
|
>

1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
int sqlite3FitsIn32Bits(const char *);

unsigned char *sqlite3utf16to8(const void *pData, int N);
void *sqlite3utf8to16be(const unsigned char *pIn, int N);
void *sqlite3utf8to16le(const unsigned char *pIn, int N);
void sqlite3utf16to16le(void *pData, int N);
void sqlite3utf16to16be(void *pData, int N);
int sqlite3PutVarint(unsigned char *, u64);
int sqlite3GetVarint(unsigned char *, u64 *);
int sqlite3VarintLen(u64 v);

Changes to src/util.c.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.76 2004/05/08 08:23:40 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** If malloc() ever fails, this global variable gets set to 1.







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.77 2004/05/10 07:17:32 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** If malloc() ever fails, this global variable gets set to 1.
1130
1131
1132
1133
1134
1135
1136








1137
1138



















  if( db->pVdbe!=0 ){
    db->magic = SQLITE_MAGIC_ERROR;
    return 1;
  }
  return 0;
}





































>
>
>
>
>
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
  if( db->pVdbe!=0 ){
    db->magic = SQLITE_MAGIC_ERROR;
    return 1;
  }
  return 0;
}

int sqlite3PutVarint(unsigned char *p, u64 v){
  int i = 0;
  do{
    p[i++] = (v & 0x7f) | 0x80;
    v >>= 7;
  }while( v!=0 );
  p[i-1] &= 0x7f;
  return i;
}

int sqlite3GetVarint(unsigned char *p, u64 *v){
  u64 x = p[0] & 0x7f;
  int n = 0;
  while( (p[n++]&0x80)!=0 ){
    x |= (p[n]&0x7f)<<(n*7);
  }
  *v = x;
  return n;
}

int sqlite3VarintLen(u64 v){
  int i = 0;
  do{
    i++;
    v >>= 7;
  }while( v!=0 );
  return i;
}

Changes to src/vdbe.c.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
**
** Various scripts scan this source file in order to generate HTML
** documentation, headers files, or other derived files.  The formatting
** of the code in this file is, therefore, important.  See other comments
** in this file for details.  If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
** $Id: vdbe.c,v 1.271 2004/05/09 23:23:58 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*







|







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
**
** Various scripts scan this source file in order to generate HTML
** documentation, headers files, or other derived files.  The formatting
** of the code in this file is, therefore, important.  See other comments
** in this file for details.  If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
** $Id: vdbe.c,v 1.272 2004/05/10 07:17:32 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*
1802
1803
1804
1805
1806
1807
1808



































































































































































































































































1809
1810
1811
1812
1813
1814
1815
  int i, cnt;
  cnt = pOp->p1;
  if( cnt<0 ) cnt = -cnt;
  assert( &pTos[1-cnt] >= p->aStack );
  for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
  if( i>=cnt ) pc = pOp->p2-1;
  if( pOp->p1>0 ) popStack(&pTos, cnt);



































































































































































































































































  break;
}

/* Opcode: MakeRecord P1 P2 *
**
** Convert the top P1 entries of the stack into a single entry
** suitable for use as a data record in a database table.  The







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
  int i, cnt;
  cnt = pOp->p1;
  if( cnt<0 ) cnt = -cnt;
  assert( &pTos[1-cnt] >= p->aStack );
  for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
  if( i>=cnt ) pc = pOp->p2-1;
  if( pOp->p1>0 ) popStack(&pTos, cnt);
  break;
}

/* Opcode: Column3 P1 P2 *
**
** This opcode (not yet in use) is a replacement for the current
** OP_Column3 that supports the SQLite3 manifest typing feature.
**
** Interpret the data that cursor P1 points to as
** a structure built using the MakeRecord instruction.
** (See the MakeRecord opcode for additional information about
** the format of the data.)
** Push onto the stack the value of the P2-th column contained
** in the data.
**
** If the KeyAsData opcode has previously executed on this cursor,
** then the field might be extracted from the key rather than the
** data.
**
** If P1 is negative, then the record is stored on the stack rather
** than in a table.  For P1==-1, the top of the stack is used.
** For P1==-2, the next on the stack is used.  And so forth.  The
** value pushed is always just a pointer into the record which is
** stored further down on the stack.  The column value is not copied.
*/
case OP_Column3: {
  int payloadSize;
  int i = pOp->p1;
  int p2 = pOp->p2;
  Cursor *pC;
  char *zRec;
  BtCursor *pCrsr;

  char *zHdr = 0;
  int freeZHdr = 0;
  int dataOffsetLen;
  u64 dataOffset;
  char *zIdx = 0;
  int cnt;
  u64 idxN;
  u64 idxN1;

  assert( i<p->nCursor );
  pTos++;
  if( i<0 ){
    assert( &pTos[i]>=p->aStack );
    assert( pTos[i].flags & MEM_Str );
    zRec = pTos[i].z;
    payloadSize = pTos[i].n;
  }else if( (pC = &p->aCsr[i])->pCursor!=0 ){
    sqlite3VdbeCursorMoveto(pC);
    zRec = 0;
    pCrsr = pC->pCursor;
    if( pC->nullRow ){
      payloadSize = 0;
    }else if( pC->keyAsData ){
      u64 payloadSize64;
      sqlite3BtreeKeySize(pCrsr, &payloadSize64);
      payloadSize = payloadSize64;
    }else{
      sqlite3BtreeDataSize(pCrsr, &payloadSize);
    }
  }else if( pC->pseudoTable ){
    payloadSize = pC->nData;
    zRec = pC->pData;
    assert( payloadSize==0 || zRec!=0 );
  }else{
    payloadSize = 0;
  }

  /* If payloadSize is 0, then just push a NULL onto the stack. */
  if( payloadSize==0 ){
    pTos->flags = MEM_Null;
    break;
  }

  /* Read the data-offset for this record */
  if( zRec ){
    dataOffsetLen = sqlite3GetVarint(zRec, &dataOffset);
  }else{
    unsigned char zDataOffset[9];
    if( pC->keyAsData ){
      sqlite3BtreeKey(pCrsr, 0, 9, zDataOffset);
    }else{
      sqlite3BtreeData(pCrsr, 0, 9, zDataOffset);
    }
    dataOffsetLen = sqlite3GetVarint(zDataOffset, &dataOffset);
  }

  /* Set zHdr to point at the start of the Idx() fields of the
  ** record. Set freeZHdr to 1 if we need to sqliteFree(zHdr) later.
  */
  if( zRec ){
    zHdr = zRec + dataOffsetLen;
  }else{
    zHdr = sqliteMalloc(dataOffset);
    if( !zHdr ){
      rc = SQLITE_NOMEM;
      goto abort_due_to_error;
    }
    freeZHdr = 1;
    if( pC->keyAsData ){
      sqlite3BtreeKey(pCrsr, dataOffsetLen, dataOffset, zHdr);
    }else{
      sqlite3BtreeData(pCrsr, dataOffsetLen, dataOffset, zHdr);
    }
  }

  /* Find the Nth byte of zHdr that does not have the 0x80
  ** bit set. The byte after this one is the start of the Idx(N)
  ** varint. Then read Idx(N) and Idx(N+1)
  */
  cnt = p2;
  zIdx = zHdr;
  while( cnt>0 ){
    assert( (zIdx-zHdr)<dataOffset );
    if( !(*zIdx & 0x80) ) cnt--;
    zIdx++;
  }
  zIdx += sqlite3GetVarint(zIdx, &idxN);
  sqlite3GetVarint(zIdx, &idxN1);

  /* Set zHdr to point at the field data */
  if( freeZHdr ){
    sqliteFree(zHdr);
    freeZHdr = 0;
  }
  if( zRec ){
    zHdr = zRec + (dataOffsetLen + dataOffset + idxN);
  }else{
    cnt = idxN1 - idxN;
    assert( cnt>0 );
    zHdr = sqliteMalloc(cnt);
    if( !zHdr ){
      rc = SQLITE_NOMEM;
      goto abort_due_to_error;
    }
    freeZHdr = 1;
    if( pC->keyAsData ){
      sqlite3BtreeKey(pCrsr, dataOffsetLen+dataOffset+idxN, cnt, zHdr);
    }else{
      sqlite3BtreeData(pCrsr, dataOffsetLen+dataOffset+idxN, cnt, zHdr);
    }
  }

  /* Deserialize the field value directory into the top of the
  ** stack. If the deserialized length does not match the expected
  ** length, this indicates corruption.
  */
  if( (idxN1-idxN)!=sqlite3VdbeDeserialize(pTos, zHdr) ){
    if( freeZHdr ){
      sqliteFree(zHdr);
    }
    rc = SQLITE_CORRUPT;
    goto abort_due_to_error;
  }

  if( freeZHdr ){
    sqliteFree(zHdr);
  }
  break;
}

/* Opcode MakeRecord3 P1 * *
**
** This opcode (not yet in use) is a replacement for the current
** OP_MakeRecord that supports the SQLite3 manifest typing feature.
** It drops the (P2==1) option that was never use.
**
** Convert the top P1 entries of the stack into a single entry
** suitable for use as a data record in a database table.  The
** details of the format are irrelavant as long as the OP_Column
** opcode can decode the record later.  Refer to source code
** comments for the details of the record format.
*/
case OP_MakeRecord3: {
  /* Assuming the record contains N fields, the record format looks
  ** like this:
  **
  ** --------------------------------------------------------------------------
  ** | data-offset | idx1 | ... | idx(N-1) | idx(N) | data0 | ... | data(N-1) |
  ** --------------------------------------------------------------------------
  **
  ** Data(0) is taken from the lowest element of the stack and data(N-1) is
  ** the top of the stack.
  **
  ** The data-offset and each of the idx() entries is stored as a 1-9 
  ** byte variable-length integer (see comments in btree.c). The
  ** data-offset contains the offset from the end of itself to the start 
  ** of data(0).
  ** 
  ** Idx(k) contains the offset from the start of data(0) to the first 
  ** byte of data(k). Idx(0) is implicitly 0. Hence:
  ** 
  **    sizeof(data-offset) + data-offset + Idx(N) 
  **
  ** is the number of bytes in the record. The offset to start of data(X)
  ** is sizeof(data-offset) + data-offset + Idx(X
  **
  ** TODO: Even when the record is short enough for Mem::zShort, this opcode
  **   allocates it dynamically.
  */
  int nDataLen = 0;
  int nHdrLen = 0;
  int data_offset = 0;
  int nField = pOp->p1;
  unsigned char *zNewRecord;
  unsigned char *zHdr;
  Mem *pRec;

  Mem *pData0 = &pTos[1-nField];
  assert( pData0>=p->aStack );

  /* Loop through the elements that will make up the record, determining
  ** the aggregate length of the Data() segments and the data_offset.
  */
  for(pRec=pData0; pRec!=pTos; pRec++){
    nDataLen += sqlite3VdbeSerialLen(pRec);
    data_offset += sqlite3VarintLen(nDataLen);
  }
 
  /* The size of the header is the data-offset + the size of the
  ** data-offset as a varint. If the size of the header combined with
  ** the size of the Data() segments is greater than MAX_BYTES_PER_ROW, 
  ** report an error.
  */
  nHdrLen = data_offset + sqlite3VarintLen(data_offset);
  if( (nHdrLen+nDataLen)>MAX_BYTES_PER_ROW ){
    rc = SQLITE_TOOBIG;
    goto abort_due_to_error;
  }

  /* Allocate space for the new row. */
  zNewRecord = sqliteMalloc(nHdrLen+nDataLen);
  if( !zNewRecord ){
    rc = SQLITE_NOMEM;
    goto abort_due_to_error;
  }

  /* Write the data offset */
  zHdr = zNewRecord;
  zHdr += sqlite3PutVarint(zHdr, data_offset);

  /* Loop through the values on the stack writing both the serialized value
  ** and the the Idx() offset for each.
  */
  nDataLen = 0;
  for(pRec=pData0; pRec!=pTos; pRec++){
    nDataLen += sqlite3VdbeSerialize(pRec, &zNewRecord[nDataLen]);
    zHdr += sqlite3PutVarint(zHdr, nDataLen);
  }

  /* Pop nField entries from the stack and push the new entry on */
  popStack(&pTos, nField);
  pTos++;
  pTos->n = nDataLen+nHdrLen;
  pTos->z = zNewRecord;
  pTos->flags = MEM_Str | MEM_Dyn;

  break;
}

/* Opcode: MakeRecord P1 P2 *
**
** Convert the top P1 entries of the stack into a single entry
** suitable for use as a data record in a database table.  The
Changes to src/vdbeInt.h.
297
298
299
300
301
302
303



304
305
306
307
308
void sqlite3VdbeKeylistFree(Keylist*);
void sqliteVdbePopStack(Vdbe*,int);
int sqlite3VdbeCursorMoveto(Cursor*);
int sqlite3VdbeByteSwap(int);
#if !defined(NDEBUG) || defined(VDBE_PROFILE)
void sqlite3VdbePrintOp(FILE*, int, Op*);
#endif




int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);










>
>
>





297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
void sqlite3VdbeKeylistFree(Keylist*);
void sqliteVdbePopStack(Vdbe*,int);
int sqlite3VdbeCursorMoveto(Cursor*);
int sqlite3VdbeByteSwap(int);
#if !defined(NDEBUG) || defined(VDBE_PROFILE)
void sqlite3VdbePrintOp(FILE*, int, Op*);
#endif
int sqlite3VdbeSerialize(const Mem *, unsigned char *);
int sqlite3VdbeSerialLen(const Mem *);
int sqlite3VdbeDeserialize(Mem *, const unsigned char *);

int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);



Changes to src/vdbeaux.c.
1095
1096
1097
1098
1099
1100
1101



























1102
1103
1104
1105
1106
1107
1108

  rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
  *pResult = memcmp(pCellKey, pKey, nKey>nCellKey?nCellKey:nKey);
  sqliteFree(pCellKey);

  return rc;
}




























/*
** The following is the comparison function for (non-integer)
** keys in the btrees.  This function returns negative, zero, or
** positive if the first key is less than, equal to, or greater than
** the second.
**







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135

  rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
  *pResult = memcmp(pCellKey, pKey, nKey>nCellKey?nCellKey:nKey);
  sqliteFree(pCellKey);

  return rc;
}

/*
** Write the serialized form of the value held by pMem into zBuf. Return
** the number of bytes written.
*/
int sqlite3VdbeSerialize(
  const Mem *pMem,      /* Pointer to vdbe value to serialize */
  unsigned char *zBuf   /* Buffer to write to */
){
}

/*
** Return the number of bytes that would be consumed by the serialized
** form of the value held by pMem. Return negative if an error occurs.
*/
int sqlite3VdbeSerialLen(const Mem *pMem){
}

/*
** Deserialize a value from zBuf and store it in *pMem. Return the number
** of bytes written, or negative if an error occurs.
*/
int sqlite3VdbeDeserialize(
  Mem *pMem,                   /* structure to write new value to */
  const unsigned char *zBuf    /* Buffer to read from */
){
}

/*
** The following is the comparison function for (non-integer)
** keys in the btrees.  This function returns negative, zero, or
** positive if the first key is less than, equal to, or greater than
** the second.
**