SQLite4
Check-in [51c6f56d73]
Not logged in

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

Overview
Comment:Have sqlite4_column_text() and other functions return the size of the returned buffer in bytes via an optional output parameter. Remove sqlite4_column_bytes() and similar.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 51c6f56d730617c77c823d572cea8f749d993bec
User & Date: dan 2013-05-09 18:11:21
Context
2013-05-09
19:12
Add a destructor parameter to sqlite4_create_function() and create_function16(). Remove create_function_v2(). check-in: b7612a4adb user: dan tags: trunk
18:11
Have sqlite4_column_text() and other functions return the size of the returned buffer in bytes via an optional output parameter. Remove sqlite4_column_bytes() and similar. check-in: 51c6f56d73 user: dan tags: trunk
2013-05-08
17:24
Modify sqlite4_column_text() and sqlite4_value_text() to return (const char *) instead of (const unsigned char *). check-in: d1966c57fa user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/alter.c.

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
**     -> 'CREATE INDEX i ON def(a, b, c)'
*/
static void renameTableFunc(
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  char const *zSql = sqlite4_value_text(argv[0]);
  char const *zTableName = sqlite4_value_text(argv[1]);

  int token;
  Token tname;
  char const *zCsr = zSql;
  int len = 0;
  char *zRet;

................................................................................
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  sqlite4 *db = sqlite4_context_db_handle(context);
  char *zOutput = 0;
  char *zResult;
  char const *zInput = sqlite4_value_text(argv[0]);
  char const *zOld = sqlite4_value_text(argv[1]);
  char const *zNew = sqlite4_value_text(argv[2]);

  const char *z;                  /* Pointer to token */
  int n;                          /* Length of token z */
  int token;                      /* Type of token */

  UNUSED_PARAMETER(NotUsed);
  for(z=zInput; *z; z=z+n){
................................................................................
** TRIGGER, not CREATE INDEX and CREATE TABLE.
*/
static void renameTriggerFunc(
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  char const *zSql = sqlite4_value_text(argv[0]);
  char const *zTableName = sqlite4_value_text(argv[1]);

  int token;
  Token tname;
  int dist = 3;
  char const *zCsr = zSql;
  int len = 0;
  char *zRet;







|
|







 







|
|
|







 







|
|







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
**     -> 'CREATE INDEX i ON def(a, b, c)'
*/
static void renameTableFunc(
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  char const *zSql = sqlite4_value_text(argv[0], 0);
  char const *zTableName = sqlite4_value_text(argv[1], 0);

  int token;
  Token tname;
  char const *zCsr = zSql;
  int len = 0;
  char *zRet;

................................................................................
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  sqlite4 *db = sqlite4_context_db_handle(context);
  char *zOutput = 0;
  char *zResult;
  char const *zInput = sqlite4_value_text(argv[0], 0);
  char const *zOld = sqlite4_value_text(argv[1], 0);
  char const *zNew = sqlite4_value_text(argv[2], 0);

  const char *z;                  /* Pointer to token */
  int n;                          /* Length of token z */
  int token;                      /* Type of token */

  UNUSED_PARAMETER(NotUsed);
  for(z=zInput; *z; z=z+n){
................................................................................
** TRIGGER, not CREATE INDEX and CREATE TABLE.
*/
static void renameTriggerFunc(
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  char const *zSql = sqlite4_value_text(argv[0], 0);
  char const *zTableName = sqlite4_value_text(argv[1], 0);

  int token;
  Token tname;
  int dist = 3;
  char const *zCsr = zSql;
  int len = 0;
  char *zRet;

Changes to src/attach.c.

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  char *zErr = 0;
  unsigned int flags;
  Db *aNew;
  char *zErrDyn = 0;

  UNUSED_PARAMETER(NotUsed);

  zFile = (const char *)sqlite4_value_text(argv[0]);
  zName = (const char *)sqlite4_value_text(argv[1]);
  if( zFile==0 ) zFile = "";
  if( zName==0 ) zName = "";

  /* Check for the following errors:
  **
  **     * Too many attached databases,
  **     * Transaction currently open
................................................................................
**     SELECT sqlite_detach(x)
*/
static void detachFunc(
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  const char *zName = (const char *)sqlite4_value_text(argv[0]);
  sqlite4 *db = sqlite4_context_db_handle(context);
  int i;
  Db *pDb = 0;
  char zErr[128];

  UNUSED_PARAMETER(NotUsed);








|
|







 







|







74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  char *zErr = 0;
  unsigned int flags;
  Db *aNew;
  char *zErrDyn = 0;

  UNUSED_PARAMETER(NotUsed);

  zFile = (const char *)sqlite4_value_text(argv[0], 0);
  zName = (const char *)sqlite4_value_text(argv[1], 0);
  if( zFile==0 ) zFile = "";
  if( zName==0 ) zName = "";

  /* Check for the following errors:
  **
  **     * Too many attached databases,
  **     * Transaction currently open
................................................................................
**     SELECT sqlite_detach(x)
*/
static void detachFunc(
  sqlite4_context *context,
  int NotUsed,
  sqlite4_value **argv
){
  const char *zName = (const char *)sqlite4_value_text(argv[0], 0);
  sqlite4 *db = sqlite4_context_db_handle(context);
  int i;
  Db *pDb = 0;
  char zErr[128];

  UNUSED_PARAMETER(NotUsed);

Changes to src/date.c.

759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    return setDateTimeToCurrent(context, p);
  }
  if( (eType = sqlite4_value_type(argv[0]))==SQLITE4_FLOAT
                   || eType==SQLITE4_INTEGER ){
    p->iJD = (sqlite4_int64)(sqlite4_value_double(argv[0])*86400000.0 + 0.5);
    p->validJD = 1;
  }else{
    z = sqlite4_value_text(argv[0]);
    if( !z || parseDateOrTime(context, z, p) ){
      return 1;
    }
  }
  for(i=1; i<argc; i++){
    z = sqlite4_value_text(argv[i]);
    if( z==0 || parseModifier(context, z, p) ) return 1;
  }
  return 0;
}


/*
................................................................................
  sqlite4_value **argv
){
  DateTime x;
  u64 n;
  size_t i,j;
  char *z;
  sqlite4 *db;
  const char *zFmt = (const char*)sqlite4_value_text(argv[0]);
  char zBuf[100];
  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
  db = sqlite4_context_db_handle(context);
  for(i=0, n=1; zFmt[i]; i++, n++){
    if( zFmt[i]=='%' ){
      switch( zFmt[i+1] ){
        case 'd':







|





|







 







|







759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    return setDateTimeToCurrent(context, p);
  }
  if( (eType = sqlite4_value_type(argv[0]))==SQLITE4_FLOAT
                   || eType==SQLITE4_INTEGER ){
    p->iJD = (sqlite4_int64)(sqlite4_value_double(argv[0])*86400000.0 + 0.5);
    p->validJD = 1;
  }else{
    z = sqlite4_value_text(argv[0], 0);
    if( !z || parseDateOrTime(context, z, p) ){
      return 1;
    }
  }
  for(i=1; i<argc; i++){
    z = sqlite4_value_text(argv[i], 0);
    if( z==0 || parseModifier(context, z, p) ) return 1;
  }
  return 0;
}


/*
................................................................................
  sqlite4_value **argv
){
  DateTime x;
  u64 n;
  size_t i,j;
  char *z;
  sqlite4 *db;
  const char *zFmt = sqlite4_value_text(argv[0], 0);
  char zBuf[100];
  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
  db = sqlite4_context_db_handle(context);
  for(i=0, n=1; zFmt[i]; i++, n++){
    if( zFmt[i]=='%' ){
      switch( zFmt[i+1] ){
        case 'd':

Changes to src/fts5.c.

1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
....
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
....
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
....
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
  pStore = db->aDb[pInfo->iDb].pKV;

  memset(&sCtx, 0, sizeof(sCtx));
  sCtx.db = db;
  sCtx.nCol = pInfo->nCol;
  sqlite4HashInit(db->pEnv, &sCtx.hash, 1);

  pPK = (const u8 *)sqlite4_value_blob(pKey);
  nPK = sqlite4_value_bytes(pKey);
  
  nTnum = getVarint32(pPK, dummy);
  nPK -= nTnum;
  pPK += nTnum;

  for(i=0; rc==SQLITE4_OK && i<pInfo->nCol; i++){
    sqlite4_value *pArg = (sqlite4_value *)(&aArg[i]);
    if( pArg->flags & MEM_Str ){
      const char *zText;
      int nText;

      zText = (const char *)sqlite4_value_text(pArg);
      nText = sqlite4_value_bytes(pArg); 
      sCtx.iCol = i;
      rc = pInfo->pTokenizer->xTokenize(
          &sCtx, pInfo->p, zText, nText, fts5TokenizeCb
      );
    }
  }

................................................................................
  u8 const *aVal; int nVal;       /* List of token instances */
  u8 const *aToken; int nToken;   /* Token for this entry */
  u8 const *aPk; int nPk;         /* Entry primary key blob */
  InstanceList sList;             /* Used to iterate through pVal */
  int nTnum;
  u32 tnum;

  aKey = (const u8 *)sqlite4_value_blob(pKey);
  nKey = sqlite4_value_bytes(pKey);
  aVal = (const u8 *)sqlite4_value_blob(pVal);
  nVal = sqlite4_value_bytes(pVal);

  /* Find the token and primary key blobs for this entry. */
  nTnum = getVarint32(aKey, tnum);
  if( aKey[nTnum]!=0 ){
    aToken = &aKey[nTnum+1];
    nToken = sqlite4Strlen30((const char *)aToken);
    aPk = &aToken[nToken+1];
................................................................................
  int rc = SQLITE4_OK;
  CksumCtx sCtx;
  int nTnum = 0;
  u32 dummy = 0;

  sCtx.cksum = 0;

  sCtx.pPK = (const u8 *)sqlite4_value_blob(pKey);
  sCtx.nPK = sqlite4_value_bytes(pKey);
  nTnum = getVarint32(sCtx.pPK, dummy);
  sCtx.nPK -= nTnum;
  sCtx.pPK += nTnum;

  for(i=0; rc==SQLITE4_OK && i<pInfo->nCol; i++){
    sqlite4_value *pArg = (sqlite4_value *)(&aArg[i]);
    if( pArg->flags & MEM_Str ){
      const char *zText;
      int nText;

      zText = (const char *)sqlite4_value_text(pArg);
      nText = sqlite4_value_bytes(pArg);
      sCtx.iCol = i;
      rc = pInfo->pTokenizer->xTokenize(
          &sCtx, pInfo->p, zText, nText, fts5CksumCb
      );
    }
  }

................................................................................
  char *zRet = 0;
  const char **azCol = 0;
  int nCol = 0;
  sqlite4_stmt *pStmt = 0;

  db = sqlite4_context_db_handle(pCtx);
  assert( nVal==3 );
  zTokenizer = (const char *)sqlite4_value_text(aVal[0]);
  zExpr = (const char *)sqlite4_value_text(aVal[1]);
  zTbl = (const char *)sqlite4_value_text(aVal[2]);

  if( sqlite4Strlen30(zTbl)>0 ){
    int i;
    char *zSql = sqlite4MPrintf(db, "SELECT * FROM '%q'", zTbl);
    rc = sqlite4_prepare(db, zSql, -1, &pStmt, 0);
    sqlite4DbFree(db, zSql);
    if( rc!=SQLITE4_OK ){







|
<











|
<







 







|
<
|
<







 







|
<










|
<







 







|
|
|







1517
1518
1519
1520
1521
1522
1523
1524

1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536

1537
1538
1539
1540
1541
1542
1543
....
1805
1806
1807
1808
1809
1810
1811
1812

1813

1814
1815
1816
1817
1818
1819
1820
....
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
....
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
  pStore = db->aDb[pInfo->iDb].pKV;

  memset(&sCtx, 0, sizeof(sCtx));
  sCtx.db = db;
  sCtx.nCol = pInfo->nCol;
  sqlite4HashInit(db->pEnv, &sCtx.hash, 1);

  pPK = (const u8 *)sqlite4_value_blob(pKey, &nPK);

  
  nTnum = getVarint32(pPK, dummy);
  nPK -= nTnum;
  pPK += nTnum;

  for(i=0; rc==SQLITE4_OK && i<pInfo->nCol; i++){
    sqlite4_value *pArg = (sqlite4_value *)(&aArg[i]);
    if( pArg->flags & MEM_Str ){
      const char *zText;
      int nText;

      zText = (const char *)sqlite4_value_text(pArg, &nText);

      sCtx.iCol = i;
      rc = pInfo->pTokenizer->xTokenize(
          &sCtx, pInfo->p, zText, nText, fts5TokenizeCb
      );
    }
  }

................................................................................
  u8 const *aVal; int nVal;       /* List of token instances */
  u8 const *aToken; int nToken;   /* Token for this entry */
  u8 const *aPk; int nPk;         /* Entry primary key blob */
  InstanceList sList;             /* Used to iterate through pVal */
  int nTnum;
  u32 tnum;

  aKey = (const u8 *)sqlite4_value_blob(pKey, &nKey);

  aVal = (const u8 *)sqlite4_value_blob(pVal, &nVal);


  /* Find the token and primary key blobs for this entry. */
  nTnum = getVarint32(aKey, tnum);
  if( aKey[nTnum]!=0 ){
    aToken = &aKey[nTnum+1];
    nToken = sqlite4Strlen30((const char *)aToken);
    aPk = &aToken[nToken+1];
................................................................................
  int rc = SQLITE4_OK;
  CksumCtx sCtx;
  int nTnum = 0;
  u32 dummy = 0;

  sCtx.cksum = 0;

  sCtx.pPK = (const u8 *)sqlite4_value_blob(pKey, &sCtx.nPK);

  nTnum = getVarint32(sCtx.pPK, dummy);
  sCtx.nPK -= nTnum;
  sCtx.pPK += nTnum;

  for(i=0; rc==SQLITE4_OK && i<pInfo->nCol; i++){
    sqlite4_value *pArg = (sqlite4_value *)(&aArg[i]);
    if( pArg->flags & MEM_Str ){
      const char *zText;
      int nText;

      zText = (const char *)sqlite4_value_text(pArg, &nText);

      sCtx.iCol = i;
      rc = pInfo->pTokenizer->xTokenize(
          &sCtx, pInfo->p, zText, nText, fts5CksumCb
      );
    }
  }

................................................................................
  char *zRet = 0;
  const char **azCol = 0;
  int nCol = 0;
  sqlite4_stmt *pStmt = 0;

  db = sqlite4_context_db_handle(pCtx);
  assert( nVal==3 );
  zTokenizer = sqlite4_value_text(aVal[0], 0);
  zExpr = sqlite4_value_text(aVal[1], 0);
  zTbl = sqlite4_value_text(aVal[2], 0);

  if( sqlite4Strlen30(zTbl)>0 ){
    int i;
    char *zSql = sqlite4MPrintf(db, "SELECT * FROM '%q'", zTbl);
    rc = sqlite4_prepare(db, zSql, -1, &pStmt, 0);
    sqlite4DbFree(db, zSql);
    if( rc!=SQLITE4_OK ){

Changes to src/fts5func.c.

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
...
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  int iCol = pSnip->iCol;

  rc = sqlite4_mi_column_value(pCtx, iCol, &pVal);
  if( rc==SQLITE4_OK ){
    SnippetCtx sCtx;
    int nText;

    nText = sqlite4_value_bytes(pVal);
    memset(&sCtx, 0, sizeof(sCtx));
    sCtx.zText = (const char *)sqlite4_value_text(pVal);
    sCtx.db = sqlite4_context_db_handle(pCtx);
    sCtx.nToken = nToken;
    sCtx.iOff = iOff;
    sCtx.mask = mask;
    sCtx.zStart = zStart;
    sCtx.zEnd = zEnd;
    sCtx.zEllipses = zEllipses;
................................................................................
  int rc;
  int nPhrase;

  const char *zStart = "<b>";
  const char *zEnd = "</b>";
  const char *zEllipses = "...";

  if( nArg>0 ) zStart = (const char *)sqlite4_value_text(apArg[0]);
  if( nArg>1 ) zEnd = (const char *)sqlite4_value_text(apArg[1]);
  if( nArg>2 ) zEllipses = (const char *)sqlite4_value_text(apArg[2]);
  if( nArg>3 ) iCol = sqlite4_value_int(apArg[3]);
  if( nArg>4 ) nToken = sqlite4_value_int(apArg[4]);

  rc = sqlite4_mi_phrase_count(pCtx, &nPhrase);
  for(nSnip=1; rc==SQLITE4_OK && nSnip<5; nSnip = ((nSnip==2) ? 3 : (nSnip+1))){
    int nTok;
    int i;







<

|







 







|
|
|







390
391
392
393
394
395
396

397
398
399
400
401
402
403
404
405
...
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
  int iCol = pSnip->iCol;

  rc = sqlite4_mi_column_value(pCtx, iCol, &pVal);
  if( rc==SQLITE4_OK ){
    SnippetCtx sCtx;
    int nText;


    memset(&sCtx, 0, sizeof(sCtx));
    sCtx.zText = sqlite4_value_text(pVal, &nText);
    sCtx.db = sqlite4_context_db_handle(pCtx);
    sCtx.nToken = nToken;
    sCtx.iOff = iOff;
    sCtx.mask = mask;
    sCtx.zStart = zStart;
    sCtx.zEnd = zEnd;
    sCtx.zEllipses = zEllipses;
................................................................................
  int rc;
  int nPhrase;

  const char *zStart = "<b>";
  const char *zEnd = "</b>";
  const char *zEllipses = "...";

  if( nArg>0 ) zStart = (const char *)sqlite4_value_text(apArg[0], 0);
  if( nArg>1 ) zEnd = (const char *)sqlite4_value_text(apArg[1], 0);
  if( nArg>2 ) zEllipses = (const char *)sqlite4_value_text(apArg[2], 0);
  if( nArg>3 ) iCol = sqlite4_value_int(apArg[3]);
  if( nArg>4 ) nToken = sqlite4_value_int(apArg[4]);

  rc = sqlite4_mi_phrase_count(pCtx, &nPhrase);
  for(nSnip=1; rc==SQLITE4_OK && nSnip<5; nSnip = ((nSnip==2) ? 3 : (nSnip+1))){
    int nTok;
    int i;

Changes to src/func.c.

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100



101
102
103
104
105
106


107
108
109

110
111

112
113
114
115
116
117
118
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
...
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
...
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
...
843
844
845
846
847
848
849

850
851
852
853
854
855
856
857
858
859
860
...
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
...
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
...
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
....
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
....
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
** Implementation of the length() function
*/
static void lengthFunc(
  sqlite4_context *context,
  int argc,
  sqlite4_value **argv
){
  int len;

  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  switch( sqlite4_value_type(argv[0]) ){
    case SQLITE4_BLOB:
    case SQLITE4_INTEGER:
    case SQLITE4_FLOAT: {
      sqlite4_result_int(context, sqlite4_value_bytes(argv[0]));
      break;
    }



    case SQLITE4_TEXT: {
      const char *z = sqlite4_value_text(argv[0]);
      if( z==0 ) return;
      len = 0;
      while( *z ){
        len++;


        SQLITE4_SKIP_UTF8(z);
      }
      sqlite4_result_int(context, len);

      break;
    }

    default: {
      sqlite4_result_null(context);
      break;
    }
  }
}

................................................................................
   || (argc==3 && sqlite4_value_type(argv[2])==SQLITE4_NULL)
  ){
    return;
  }
  p0type = sqlite4_value_type(argv[0]);
  p1 = sqlite4_value_int(argv[1]);
  if( p0type==SQLITE4_BLOB ){
    len = sqlite4_value_bytes(argv[0]);
    z = sqlite4_value_blob(argv[0]);
    if( z==0 ) return;
    assert( len==sqlite4_value_bytes(argv[0]) );
  }else{
    z = sqlite4_value_text(argv[0]);
    if( z==0 ) return;
    len = 0;
    if( p1<0 ){
      for(z2=z; *z2; len++){
        SQLITE4_SKIP_UTF8(z2);
      }
    }
................................................................................
** Implementation of the upper() and lower() SQL functions.
*/
static void upperFunc(sqlite4_context *context, int argc, sqlite4_value **argv){
  char *z1;
  const char *z2;
  int i, n;
  UNUSED_PARAMETER(argc);
  z2 = sqlite4_value_text(argv[0]);
  n = sqlite4_value_bytes(argv[0]);
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  assert( z2==sqlite4_value_text(argv[0]) );
  if( z2 ){
    z1 = contextMalloc(context, ((i64)n)+1);
    if( z1 ){
      for(i=0; i<n; i++){
        z1[i] = (char)sqlite4Toupper(z2[i]);
      }
      sqlite4_result_text(context, z1, n, SQLITE4_DYNAMIC, 0);
................................................................................
  }
}
static void lowerFunc(sqlite4_context *context, int argc, sqlite4_value **argv){
  char *z1;
  const char *z2;
  int i, n;
  UNUSED_PARAMETER(argc);
  z2 = sqlite4_value_text(argv[0]);
  n = sqlite4_value_bytes(argv[0]);
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  assert( z2==sqlite4_value_text(argv[0]) );
  if( z2 ){
    z1 = contextMalloc(context, ((i64)n)+1);
    if( z1 ){
      for(i=0; i<n; i++){
        z1[i] = sqlite4Tolower(z2[i]);
      }
      sqlite4_result_text(context, z1, n, SQLITE4_DYNAMIC, 0);
................................................................................
  sqlite4_value **argv
){
  const char *zA, *zB;
  u32 escape = 0;
  int nPat;
  sqlite4 *db = sqlite4_context_db_handle(context);

  zB = sqlite4_value_text(argv[0]);
  zA = sqlite4_value_text(argv[1]);

  /* Limit the length of the LIKE or GLOB pattern to avoid problems
  ** of deep recursion and N*N behavior in patternCompare().
  */
  nPat = sqlite4_value_bytes(argv[0]);
  testcase( nPat==db->aLimit[SQLITE4_LIMIT_LIKE_PATTERN_LENGTH] );
  testcase( nPat==db->aLimit[SQLITE4_LIMIT_LIKE_PATTERN_LENGTH]+1 );
  if( nPat > db->aLimit[SQLITE4_LIMIT_LIKE_PATTERN_LENGTH] ){
    sqlite4_result_error(context, "LIKE or GLOB pattern too complex", -1);
    return;
  }
  assert( zB==sqlite4_value_text(argv[0]) );  /* Encoding did not change */

  if( argc==3 ){
    /* The escape character string must consist of a single UTF-8 character.
    ** Otherwise, return an error.
    */
    const char *zEsc = sqlite4_value_text(argv[2]);
    if( zEsc==0 ) return;
    if( sqlite4Utf8CharLen(zEsc, -1)!=1 ){
      static const char *zErr = "ESCAPE expression must be a single character";
      sqlite4_result_error(context, zErr, -1);
      return;
    }
    escape = sqlite4Utf8Read(zEsc, &zEsc);
................................................................................
static void errlogFunc(
  sqlite4_context *context,
  int argc,
  sqlite4_value **argv
){
  int rclog = sqlite4_value_int(argv[0]);
  sqlite4_env *pEnv = sqlite4_context_env(context);
  sqlite4_log(pEnv, rclog, "%s", sqlite4_value_text(argv[1]));
  UNUSED_PARAMETER(argc);
}

/*
** Implementation of the sqlite_compileoption_used() function.
** The result is an integer that identifies if the compiler option
** was used to build SQLite.
................................................................................
  const char *zOptName;
  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
  ** function is a wrapper around the sqlite4_compileoption_used() C/C++
  ** function.
  */
  if( (zOptName = sqlite4_value_text(argv[0]))!=0 ){
    sqlite4_result_int(context, sqlite4_compileoption_used(zOptName));
  }
}
#endif /* SQLITE4_OMIT_COMPILEOPTION_DIAGS */

/*
** Implementation of the sqlite_compileoption_get() function. 
................................................................................
  switch( sqlite4_value_type(argv[0]) ){
    case SQLITE4_INTEGER:
    case SQLITE4_FLOAT: {
      sqlite4_result_value(context, argv[0]);
      break;
    }
    case SQLITE4_BLOB: {

      char *zText = 0;
      char const *zBlob = sqlite4_value_blob(argv[0]);
      int nBlob = sqlite4_value_bytes(argv[0]);
      assert( zBlob==sqlite4_value_blob(argv[0]) ); /* No encoding change */
      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
      if( zText ){
        int i;
        for(i=0; i<nBlob; i++){
          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
        }
................................................................................
        sqlite4_free(sqlite4_context_env(context), zText);
      }
      break;
    }
    case SQLITE4_TEXT: {
      int i,j;
      u64 n;
      const char *zArg = sqlite4_value_text(argv[0]);
      char *z;

      if( zArg==0 ) return;
      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
      if( z ){
        z[0] = '\'';
................................................................................
  sqlite4_value **argv
){
  int i, n;
  const unsigned char *pBlob;
  char *zHex, *z;
  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  pBlob = sqlite4_value_blob(argv[0]);
  n = sqlite4_value_bytes(argv[0]);
  assert( pBlob==sqlite4_value_blob(argv[0]) );  /* No encoding change */
  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
  if( zHex ){
    for(i=0; i<n; i++, pBlob++){
      unsigned char c = *pBlob;
      *(z++) = hexdigits[(c>>4)&0xf];
      *(z++) = hexdigits[c&0xf];
    }
................................................................................
  int nRep;                /* Size of zRep */
  i64 nOut;                /* Maximum size of zOut */
  int loopLimit;           /* Last zStr[] that might match zPattern[] */
  int i, j;                /* Loop counters */

  assert( argc==3 );
  UNUSED_PARAMETER(argc);
  zStr = sqlite4_value_text(argv[0]);
  if( zStr==0 ) return;
  nStr = sqlite4_value_bytes(argv[0]);
  assert( zStr==sqlite4_value_text(argv[0]) );  /* No encoding change */
  zPattern = sqlite4_value_text(argv[1]);
  if( zPattern==0 ){
    assert( sqlite4_value_type(argv[1])==SQLITE4_NULL
            || sqlite4_context_db_handle(context)->mallocFailed );
    return;
  }
  if( zPattern[0]==0 ){
    assert( sqlite4_value_type(argv[1])!=SQLITE4_NULL );
    sqlite4_result_value(context, argv[0]);
    return;
  }
  nPattern = sqlite4_value_bytes(argv[1]);
  assert( zPattern==sqlite4_value_text(argv[1]) );  /* No encoding change */
  zRep = sqlite4_value_text(argv[2]);
  if( zRep==0 ) return;
  nRep = sqlite4_value_bytes(argv[2]);
  assert( zRep==sqlite4_value_text(argv[2]) );
  nOut = nStr + 1;
  assert( nOut<SQLITE4_MAX_LENGTH );
  zOut = contextMalloc(context, (i64)nOut);
  if( zOut==0 ){
    return;
  }
  loopLimit = nStr - nPattern;  
................................................................................
  u8 *aLen = 0;                   /* Length of each character in zCharSet */
  char **azChar = 0;              /* Individual characters in zCharSet */
  int nChar;                      /* Number of characters in zCharSet */

  if( sqlite4_value_type(argv[0])==SQLITE4_NULL ){
    return;
  }
  zIn = sqlite4_value_text(argv[0]);
  if( zIn==0 ) return;
  nIn = sqlite4_value_bytes(argv[0]);
  assert( zIn==sqlite4_value_text(argv[0]) );
  if( argc==1 ){
    static const unsigned char lenOne[] = { 1 };
    static char * const azOne[] = { " " };
    nChar = 1;
    aLen = (u8*)lenOne;
    azChar = (char**)azOne;
    zCharSet = 0;
  }else if( (zCharSet = sqlite4_value_text(argv[1]))==0 ){
    return;
  }else{
    const char *z;
    for(z=zCharSet, nChar=0; *z; nChar++){
      SQLITE4_SKIP_UTF8(z);
    }
    if( nChar>0 ){
................................................................................
    sqlite4 *db = sqlite4_context_db_handle(context);
    int firstTerm = pAccum->useMalloc==0;
    pAccum->useMalloc = 2;
    pAccum->pEnv = db->pEnv;
    pAccum->mxAlloc = db->aLimit[SQLITE4_LIMIT_LENGTH];
    if( !firstTerm ){
      if( argc==2 ){
        zSep = (char*)sqlite4_value_text(argv[1]);
        nSep = sqlite4_value_bytes(argv[1]);
      }else{
        zSep = ",";
        nSep = 1;
      }
      sqlite4StrAccumAppend(pAccum, zSep, nSep);
    }
    zVal = (char*)sqlite4_value_text(argv[0]);
    nVal = sqlite4_value_bytes(argv[0]);
    sqlite4StrAccumAppend(pAccum, zVal, nVal);
  }
}
static void groupConcatFinalize(sqlite4_context *context){
  StrAccum *pAccum;
  pAccum = sqlite4_aggregate_context(context, 0);
  if( pAccum ){







<
<



|
|
|
|

|
>
>
>

|
<
<
|
<
>
>
|
|
|
>


>







 







<
|

<

|







 







|
<
<
<







 







|
<
<
<







 







|
|




<






<





|







 







|







 







|







 







>

|
<
<







 







|







 







|
<
<







 







|

<
<
|










<
<
|

<
<







 







|

<
<







|







 







|
<






|
<







83
84
85
86
87
88
89


90
91
92
93
94
95
96
97
98
99
100
101
102
103


104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
...
191
192
193
194
195
196
197

198
199

200
201
202
203
204
205
206
207
208
...
322
323
324
325
326
327
328
329



330
331
332
333
334
335
336
...
338
339
340
341
342
343
344
345



346
347
348
349
350
351
352
...
660
661
662
663
664
665
666
667
668
669
670
671
672

673
674
675
676
677
678

679
680
681
682
683
684
685
686
687
688
689
690
691
...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
...
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
...
835
836
837
838
839
840
841
842
843
844


845
846
847
848
849
850
851
...
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
...
899
900
901
902
903
904
905
906


907
908
909
910
911
912
913
...
936
937
938
939
940
941
942
943
944


945
946
947
948
949
950
951
952
953
954
955


956
957


958
959
960
961
962
963
964
....
1013
1014
1015
1016
1017
1018
1019
1020
1021


1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
....
1326
1327
1328
1329
1330
1331
1332
1333

1334
1335
1336
1337
1338
1339
1340

1341
1342
1343
1344
1345
1346
1347
** Implementation of the length() function
*/
static void lengthFunc(
  sqlite4_context *context,
  int argc,
  sqlite4_value **argv
){


  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  switch( sqlite4_value_type(argv[0]) ){
    case SQLITE4_BLOB: {
      int nBlob;
      sqlite4_value_blob(argv[0], &nBlob);
      sqlite4_result_int(context, nBlob);
      break;
    };

    case SQLITE4_INTEGER:
    case SQLITE4_FLOAT:
    case SQLITE4_TEXT: {
      const char *z = sqlite4_value_text(argv[0], 0);


      if( z ){ 

        int nChar;
        for(nChar=0; *z; nChar++){
          SQLITE4_SKIP_UTF8(z);
        }
        sqlite4_result_int(context, nChar);
      }
      break;
    }

    default: {
      sqlite4_result_null(context);
      break;
    }
  }
}

................................................................................
   || (argc==3 && sqlite4_value_type(argv[2])==SQLITE4_NULL)
  ){
    return;
  }
  p0type = sqlite4_value_type(argv[0]);
  p1 = sqlite4_value_int(argv[1]);
  if( p0type==SQLITE4_BLOB ){

    z = sqlite4_value_blob(argv[0], &len);
    if( z==0 ) return;

  }else{
    z = sqlite4_value_text(argv[0], 0);
    if( z==0 ) return;
    len = 0;
    if( p1<0 ){
      for(z2=z; *z2; len++){
        SQLITE4_SKIP_UTF8(z2);
      }
    }
................................................................................
** Implementation of the upper() and lower() SQL functions.
*/
static void upperFunc(sqlite4_context *context, int argc, sqlite4_value **argv){
  char *z1;
  const char *z2;
  int i, n;
  UNUSED_PARAMETER(argc);
  z2 = sqlite4_value_text(argv[0], &n);



  if( z2 ){
    z1 = contextMalloc(context, ((i64)n)+1);
    if( z1 ){
      for(i=0; i<n; i++){
        z1[i] = (char)sqlite4Toupper(z2[i]);
      }
      sqlite4_result_text(context, z1, n, SQLITE4_DYNAMIC, 0);
................................................................................
  }
}
static void lowerFunc(sqlite4_context *context, int argc, sqlite4_value **argv){
  char *z1;
  const char *z2;
  int i, n;
  UNUSED_PARAMETER(argc);
  z2 = sqlite4_value_text(argv[0], &n);



  if( z2 ){
    z1 = contextMalloc(context, ((i64)n)+1);
    if( z1 ){
      for(i=0; i<n; i++){
        z1[i] = sqlite4Tolower(z2[i]);
      }
      sqlite4_result_text(context, z1, n, SQLITE4_DYNAMIC, 0);
................................................................................
  sqlite4_value **argv
){
  const char *zA, *zB;
  u32 escape = 0;
  int nPat;
  sqlite4 *db = sqlite4_context_db_handle(context);

  zB = sqlite4_value_text(argv[0], &nPat);
  zA = sqlite4_value_text(argv[1], 0);

  /* Limit the length of the LIKE or GLOB pattern to avoid problems
  ** of deep recursion and N*N behavior in patternCompare().
  */

  testcase( nPat==db->aLimit[SQLITE4_LIMIT_LIKE_PATTERN_LENGTH] );
  testcase( nPat==db->aLimit[SQLITE4_LIMIT_LIKE_PATTERN_LENGTH]+1 );
  if( nPat > db->aLimit[SQLITE4_LIMIT_LIKE_PATTERN_LENGTH] ){
    sqlite4_result_error(context, "LIKE or GLOB pattern too complex", -1);
    return;
  }


  if( argc==3 ){
    /* The escape character string must consist of a single UTF-8 character.
    ** Otherwise, return an error.
    */
    const char *zEsc = sqlite4_value_text(argv[2], 0);
    if( zEsc==0 ) return;
    if( sqlite4Utf8CharLen(zEsc, -1)!=1 ){
      static const char *zErr = "ESCAPE expression must be a single character";
      sqlite4_result_error(context, zErr, -1);
      return;
    }
    escape = sqlite4Utf8Read(zEsc, &zEsc);
................................................................................
static void errlogFunc(
  sqlite4_context *context,
  int argc,
  sqlite4_value **argv
){
  int rclog = sqlite4_value_int(argv[0]);
  sqlite4_env *pEnv = sqlite4_context_env(context);
  sqlite4_log(pEnv, rclog, "%s", sqlite4_value_text(argv[1], 0));
  UNUSED_PARAMETER(argc);
}

/*
** Implementation of the sqlite_compileoption_used() function.
** The result is an integer that identifies if the compiler option
** was used to build SQLite.
................................................................................
  const char *zOptName;
  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
  ** function is a wrapper around the sqlite4_compileoption_used() C/C++
  ** function.
  */
  if( (zOptName = sqlite4_value_text(argv[0], 0))!=0 ){
    sqlite4_result_int(context, sqlite4_compileoption_used(zOptName));
  }
}
#endif /* SQLITE4_OMIT_COMPILEOPTION_DIAGS */

/*
** Implementation of the sqlite_compileoption_get() function. 
................................................................................
  switch( sqlite4_value_type(argv[0]) ){
    case SQLITE4_INTEGER:
    case SQLITE4_FLOAT: {
      sqlite4_result_value(context, argv[0]);
      break;
    }
    case SQLITE4_BLOB: {
      int nBlob;
      char *zText = 0;
      char const *zBlob = sqlite4_value_blob(argv[0], &nBlob);


      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
      if( zText ){
        int i;
        for(i=0; i<nBlob; i++){
          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
        }
................................................................................
        sqlite4_free(sqlite4_context_env(context), zText);
      }
      break;
    }
    case SQLITE4_TEXT: {
      int i,j;
      u64 n;
      const char *zArg = sqlite4_value_text(argv[0], 0);
      char *z;

      if( zArg==0 ) return;
      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
      if( z ){
        z[0] = '\'';
................................................................................
  sqlite4_value **argv
){
  int i, n;
  const unsigned char *pBlob;
  char *zHex, *z;
  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  pBlob = sqlite4_value_blob(argv[0], &n);


  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
  if( zHex ){
    for(i=0; i<n; i++, pBlob++){
      unsigned char c = *pBlob;
      *(z++) = hexdigits[(c>>4)&0xf];
      *(z++) = hexdigits[c&0xf];
    }
................................................................................
  int nRep;                /* Size of zRep */
  i64 nOut;                /* Maximum size of zOut */
  int loopLimit;           /* Last zStr[] that might match zPattern[] */
  int i, j;                /* Loop counters */

  assert( argc==3 );
  UNUSED_PARAMETER(argc);
  zStr = sqlite4_value_text(argv[0], &nStr);
  if( zStr==0 ) return;


  zPattern = sqlite4_value_text(argv[1], &nPattern);
  if( zPattern==0 ){
    assert( sqlite4_value_type(argv[1])==SQLITE4_NULL
            || sqlite4_context_db_handle(context)->mallocFailed );
    return;
  }
  if( zPattern[0]==0 ){
    assert( sqlite4_value_type(argv[1])!=SQLITE4_NULL );
    sqlite4_result_value(context, argv[0]);
    return;
  }


  zRep = sqlite4_value_text(argv[2], &nRep);
  if( zRep==0 ) return;


  nOut = nStr + 1;
  assert( nOut<SQLITE4_MAX_LENGTH );
  zOut = contextMalloc(context, (i64)nOut);
  if( zOut==0 ){
    return;
  }
  loopLimit = nStr - nPattern;  
................................................................................
  u8 *aLen = 0;                   /* Length of each character in zCharSet */
  char **azChar = 0;              /* Individual characters in zCharSet */
  int nChar;                      /* Number of characters in zCharSet */

  if( sqlite4_value_type(argv[0])==SQLITE4_NULL ){
    return;
  }
  zIn = sqlite4_value_text(argv[0], &nIn);
  if( zIn==0 ) return;


  if( argc==1 ){
    static const unsigned char lenOne[] = { 1 };
    static char * const azOne[] = { " " };
    nChar = 1;
    aLen = (u8*)lenOne;
    azChar = (char**)azOne;
    zCharSet = 0;
  }else if( (zCharSet = sqlite4_value_text(argv[1], 0))==0 ){
    return;
  }else{
    const char *z;
    for(z=zCharSet, nChar=0; *z; nChar++){
      SQLITE4_SKIP_UTF8(z);
    }
    if( nChar>0 ){
................................................................................
    sqlite4 *db = sqlite4_context_db_handle(context);
    int firstTerm = pAccum->useMalloc==0;
    pAccum->useMalloc = 2;
    pAccum->pEnv = db->pEnv;
    pAccum->mxAlloc = db->aLimit[SQLITE4_LIMIT_LENGTH];
    if( !firstTerm ){
      if( argc==2 ){
        zSep = (char*)sqlite4_value_text(argv[1], &nSep);

      }else{
        zSep = ",";
        nSep = 1;
      }
      sqlite4StrAccumAppend(pAccum, zSep, nSep);
    }
    zVal = (char*)sqlite4_value_text(argv[0], &nVal);

    sqlite4StrAccumAppend(pAccum, zVal, nVal);
  }
}
static void groupConcatFinalize(sqlite4_context *context){
  StrAccum *pAccum;
  pAccum = sqlite4_aggregate_context(context, 0);
  if( pAccum ){

Changes to src/legacy.c.

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
            assert( azCols[i]!=0 );
          }
          callbackIsInit = 1;
        }
        if( rc==SQLITE4_ROW ){
          azVals = &azCols[nCol];
          for(i=0; i<nCol; i++){
            azVals[i] = (char *)sqlite4_column_text(pStmt, i);
            if( !azVals[i] && sqlite4_column_type(pStmt, i)!=SQLITE4_NULL ){
              db->mallocFailed = 1;
              goto exec_out;
            }
          }
        }
        if( xCallback(pArg, nCol, azVals, azCols) ){







|







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
            assert( azCols[i]!=0 );
          }
          callbackIsInit = 1;
        }
        if( rc==SQLITE4_ROW ){
          azVals = &azCols[nCol];
          for(i=0; i<nCol; i++){
            azVals[i] = (char *)sqlite4_column_text(pStmt, i, 0);
            if( !azVals[i] && sqlite4_column_type(pStmt, i)!=SQLITE4_NULL ){
              db->mallocFailed = 1;
              goto exec_out;
            }
          }
        }
        if( xCallback(pArg, nCol, azVals, azCols) ){

Changes to src/main.c.

842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  if( !sqlite4SafetyCheckSickOrOk(db) ){
    return sqlite4ErrStr(SQLITE4_MISUSE_BKPT);
  }
  sqlite4_mutex_enter(db->mutex);
  if( db->mallocFailed ){
    z = sqlite4ErrStr(SQLITE4_NOMEM);
  }else{
    z = (char*)sqlite4_value_text(db->pErr);
    assert( !db->mallocFailed );
    if( z==0 ){
      z = sqlite4ErrStr(db->errCode);
    }
  }
  sqlite4_mutex_leave(db->mutex);
  return z;
................................................................................
  if( !sqlite4SafetyCheckSickOrOk(db) ){
    return (void *)misuse;
  }
  sqlite4_mutex_enter(db->mutex);
  if( db->mallocFailed ){
    z = (void *)outOfMem;
  }else{
    z = sqlite4_value_text16(db->pErr);
    if( z==0 ){
      sqlite4ValueSetStr(db->pErr, -1, sqlite4ErrStr(db->errCode),
           SQLITE4_UTF8, SQLITE4_STATIC, 0);
      z = sqlite4_value_text16(db->pErr);
    }
    /* A malloc() may have failed within the call to sqlite4_value_text16()
    ** above. If this is the case, then the db->mallocFailed flag needs to
    ** be cleared before returning. Do this directly, instead of via
    ** sqlite4ApiExit(), to avoid setting the database handle error message.
    */
    db->mallocFailed = 0;







|







 







|



|







842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  if( !sqlite4SafetyCheckSickOrOk(db) ){
    return sqlite4ErrStr(SQLITE4_MISUSE_BKPT);
  }
  sqlite4_mutex_enter(db->mutex);
  if( db->mallocFailed ){
    z = sqlite4ErrStr(SQLITE4_NOMEM);
  }else{
    z = (char*)sqlite4_value_text(db->pErr, 0);
    assert( !db->mallocFailed );
    if( z==0 ){
      z = sqlite4ErrStr(db->errCode);
    }
  }
  sqlite4_mutex_leave(db->mutex);
  return z;
................................................................................
  if( !sqlite4SafetyCheckSickOrOk(db) ){
    return (void *)misuse;
  }
  sqlite4_mutex_enter(db->mutex);
  if( db->mallocFailed ){
    z = (void *)outOfMem;
  }else{
    z = sqlite4_value_text16(db->pErr, 0);
    if( z==0 ){
      sqlite4ValueSetStr(db->pErr, -1, sqlite4ErrStr(db->errCode),
           SQLITE4_UTF8, SQLITE4_STATIC, 0);
      z = sqlite4_value_text16(db->pErr, 0);
    }
    /* A malloc() may have failed within the call to sqlite4_value_text16()
    ** above. If this is the case, then the db->mallocFailed flag needs to
    ** be cleared before returning. Do this directly, instead of via
    ** sqlite4ApiExit(), to avoid setting the database handle error message.
    */
    db->mallocFailed = 0;

Changes to src/shell.c.

832
833
834
835
836
837
838

839

840
841

842
843
844
845
846
847
848
849
850
...
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
....
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
....
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
....
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
....
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
      for(i=0; i<nArg; i++){
        char *zSep = i>0 ? ",": "";
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE4_NULL) ){
          fprintf(p->out,"%sNULL",zSep);
        }else if( aiType && aiType[i]==SQLITE4_TEXT ){
          if( zSep[0] ) fprintf(p->out,"%s",zSep);
          output_quoted_string(p->out, azArg[i]);

        }else if( aiType && (aiType[i]==SQLITE4_INTEGER || aiType[i]==SQLITE4_FLOAT) ){

          fprintf(p->out,"%s%s",zSep, azArg[i]);
        }else if( aiType && aiType[i]==SQLITE4_BLOB && p->pStmt ){

          const void *pBlob = sqlite4_column_blob(p->pStmt, i);
          int nBlob = sqlite4_column_bytes(p->pStmt, i);
          if( zSep[0] ) fprintf(p->out,"%s",zSep);
          output_hex_blob(p->out, pBlob, nBlob);
        }else if( isNumber(azArg[i], 0) ){
          fprintf(p->out,"%s%s",zSep, azArg[i]);
        }else{
          if( zSep[0] ) fprintf(p->out,"%s",zSep);
          output_quoted_string(p->out, azArg[i]);
................................................................................
  rc = sqlite4_step(pSelect);
  nResult = sqlite4_column_count(pSelect);
  while( rc==SQLITE4_ROW ){
    if( zFirstRow ){
      fprintf(p->out, "%s", zFirstRow);
      zFirstRow = 0;
    }
    z = (char const *)sqlite4_column_text(pSelect, 0);
    fprintf(p->out, "%s", z);
    for(i=1; i<nResult; i++){ 
      fprintf(p->out, ",%s", sqlite4_column_text(pSelect, i));
    }
    if( z==0 ) z = "";
    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
    if( z[0] ){
      fprintf(p->out, "\n;\n");
    }else{
      fprintf(p->out, ";\n");
................................................................................
            /* save off ptrs to column names */
            for(i=0; i<nCol; i++){
              azCols[i] = (char *)sqlite4_column_name(pStmt, i);
            }
            do{
              /* extract the data and data types */
              for(i=0; i<nCol; i++){
                azVals[i] = (char *)sqlite4_column_text(pStmt, i);
                aiTypes[i] = sqlite4_column_type(pStmt, i);
                if( !azVals[i] && (aiTypes[i]!=SQLITE4_NULL) ){
                  rc = SQLITE4_NOMEM;
                  break; /* from for */
                }
              } /* end for */

................................................................................
    if( zTmp ){
      zSelect = appendText(zSelect, zTmp, '\'');
      free(zTmp);
    }
    zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
    rc = sqlite4_step(pTableInfo);
    while( rc==SQLITE4_ROW ){
      const char *zText = (const char *)sqlite4_column_text(pTableInfo, 1);
      zSelect = appendText(zSelect, "quote(", 0);
      zSelect = appendText(zSelect, zText, '"');
      rc = sqlite4_step(pTableInfo);
      if( rc==SQLITE4_ROW ){
        zSelect = appendText(zSelect, "), ", 0);
      }else{
        zSelect = appendText(zSelect, ") ", 0);
................................................................................
    if( rc ) return rc;
    zSql = sqlite4_mprintf(0, 
        "SELECT name FROM sqlite_master"
        " WHERE type IN ('table','view')"
        "   AND name NOT LIKE 'sqlite_%%'"
        "   AND name LIKE ?1");
    while( sqlite4_step(pStmt)==SQLITE4_ROW ){
      const char *zDbName = (const char*)sqlite4_column_text(pStmt, 1);
      if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
      if( strcmp(zDbName,"temp")==0 ){
        zSql = sqlite4_mprintf(0, 
                 "%z UNION ALL "
                 "SELECT 'temp.' || name FROM sqlite_temp_master"
                 " WHERE type IN ('table','view')"
                 "   AND name NOT LIKE 'sqlite_%%'"
................................................................................
        if( azNew==0 ){
          fprintf(stderr, "Error: out of memory\n");
          break;
        }
        nAlloc = n;
        azResult = azNew;
      }
      azResult[nRow] = sqlite4_mprintf(0, "%s", sqlite4_column_text(pStmt, 0));
      if( azResult[nRow] ) nRow++;
    }
    sqlite4_finalize(pStmt);        
    if( nRow>0 ){
      int len, maxlen = 0;
      int i, j;
      int nPrintCol, nPrintRow;







>
|
>


>
|
<







 







|


|







 







|







 







|







 







|







 







|







832
833
834
835
836
837
838
839
840
841
842
843
844
845

846
847
848
849
850
851
852
...
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
....
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
....
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
....
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
....
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
      for(i=0; i<nArg; i++){
        char *zSep = i>0 ? ",": "";
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE4_NULL) ){
          fprintf(p->out,"%sNULL",zSep);
        }else if( aiType && aiType[i]==SQLITE4_TEXT ){
          if( zSep[0] ) fprintf(p->out,"%s",zSep);
          output_quoted_string(p->out, azArg[i]);
        }else if( aiType && (
              aiType[i]==SQLITE4_INTEGER || aiType[i]==SQLITE4_FLOAT
        )){
          fprintf(p->out,"%s%s",zSep, azArg[i]);
        }else if( aiType && aiType[i]==SQLITE4_BLOB && p->pStmt ){
          int nBlob;
          const void *pBlob = sqlite4_column_blob(p->pStmt, i, &nBlob);

          if( zSep[0] ) fprintf(p->out,"%s",zSep);
          output_hex_blob(p->out, pBlob, nBlob);
        }else if( isNumber(azArg[i], 0) ){
          fprintf(p->out,"%s%s",zSep, azArg[i]);
        }else{
          if( zSep[0] ) fprintf(p->out,"%s",zSep);
          output_quoted_string(p->out, azArg[i]);
................................................................................
  rc = sqlite4_step(pSelect);
  nResult = sqlite4_column_count(pSelect);
  while( rc==SQLITE4_ROW ){
    if( zFirstRow ){
      fprintf(p->out, "%s", zFirstRow);
      zFirstRow = 0;
    }
    z = sqlite4_column_text(pSelect, 0, 0);
    fprintf(p->out, "%s", z);
    for(i=1; i<nResult; i++){ 
      fprintf(p->out, ",%s", sqlite4_column_text(pSelect, i, 0));
    }
    if( z==0 ) z = "";
    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
    if( z[0] ){
      fprintf(p->out, "\n;\n");
    }else{
      fprintf(p->out, ";\n");
................................................................................
            /* save off ptrs to column names */
            for(i=0; i<nCol; i++){
              azCols[i] = (char *)sqlite4_column_name(pStmt, i);
            }
            do{
              /* extract the data and data types */
              for(i=0; i<nCol; i++){
                azVals[i] = (char *)sqlite4_column_text(pStmt, i, 0);
                aiTypes[i] = sqlite4_column_type(pStmt, i);
                if( !azVals[i] && (aiTypes[i]!=SQLITE4_NULL) ){
                  rc = SQLITE4_NOMEM;
                  break; /* from for */
                }
              } /* end for */

................................................................................
    if( zTmp ){
      zSelect = appendText(zSelect, zTmp, '\'');
      free(zTmp);
    }
    zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
    rc = sqlite4_step(pTableInfo);
    while( rc==SQLITE4_ROW ){
      const char *zText = sqlite4_column_text(pTableInfo, 1, 0);
      zSelect = appendText(zSelect, "quote(", 0);
      zSelect = appendText(zSelect, zText, '"');
      rc = sqlite4_step(pTableInfo);
      if( rc==SQLITE4_ROW ){
        zSelect = appendText(zSelect, "), ", 0);
      }else{
        zSelect = appendText(zSelect, ") ", 0);
................................................................................
    if( rc ) return rc;
    zSql = sqlite4_mprintf(0, 
        "SELECT name FROM sqlite_master"
        " WHERE type IN ('table','view')"
        "   AND name NOT LIKE 'sqlite_%%'"
        "   AND name LIKE ?1");
    while( sqlite4_step(pStmt)==SQLITE4_ROW ){
      const char *zDbName = sqlite4_column_text(pStmt, 1, 0);
      if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
      if( strcmp(zDbName,"temp")==0 ){
        zSql = sqlite4_mprintf(0, 
                 "%z UNION ALL "
                 "SELECT 'temp.' || name FROM sqlite_temp_master"
                 " WHERE type IN ('table','view')"
                 "   AND name NOT LIKE 'sqlite_%%'"
................................................................................
        if( azNew==0 ){
          fprintf(stderr, "Error: out of memory\n");
          break;
        }
        nAlloc = n;
        azResult = azNew;
      }
      azResult[nRow] = sqlite4_mprintf(0, "%s", sqlite4_column_text(pStmt,0,0));
      if( azResult[nRow] ) nRow++;
    }
    sqlite4_finalize(pStmt);        
    if( nRow>0 ){
      int len, maxlen = 0;
      int i, j;
      int nPrintCol, nPrintRow;

Changes to src/sqlite.h.in.

2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231







2232
2233


2234





2235
2236
2237
2238
2239
2240
2241
....
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
....
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
** The value
** returned by sqlite4_column_type() is only meaningful if no type
** conversions have occurred as described below.  After a type conversion,
** the value returned by sqlite4_column_type() is undefined.  Future
** versions of SQLite may change the behavior of sqlite4_column_type()
** following a type conversion.
**
** ^If the result is a BLOB or UTF-8 string then the sqlite4_column_bytes()
** routine returns the number of bytes in that BLOB or string.
** ^If the result is a UTF-16 string, then sqlite4_column_bytes() converts
** the string to UTF-8 and then returns the number of bytes.
** ^If the result is a numeric value then sqlite4_column_bytes() uses
** [sqlite4_snprintf()] to convert that value to a UTF-8 string and returns
** the number of bytes in that string.
** ^If the result is NULL, then sqlite4_column_bytes() returns zero.
**
** ^If the result is a BLOB or UTF-16 string then the sqlite4_column_bytes16()
** routine returns the number of bytes in that BLOB or string.
** ^If the result is a UTF-8 string, then sqlite4_column_bytes16() converts
** the string to UTF-16 and then returns the number of bytes.
** ^If the result is a numeric value then sqlite4_column_bytes16() uses
** [sqlite4_snprintf()] to convert that value to a UTF-16 string and returns
** the number of bytes in that string.
** ^If the result is NULL, then sqlite4_column_bytes16() returns zero.
**
** ^The values returned by [sqlite4_column_bytes()] and 
** [sqlite4_column_bytes16()] do not include the zero terminators at the end
** of the string.  ^For clarity: the values returned by
** [sqlite4_column_bytes()] and [sqlite4_column_bytes16()] are the number of
** bytes in the string, not the number of characters.
**







** ^Strings returned by sqlite4_column_text() and sqlite4_column_text16(),
** even empty strings, are always zero-terminated.  ^The return


** value from sqlite4_column_blob() for a zero-length BLOB is a NULL pointer.





**
** ^The object returned by [sqlite4_column_value()] is an
** [unprotected sqlite4_value] object.  An unprotected sqlite4_value object
** may only be used with [sqlite4_bind_value()] and [sqlite4_result_value()].
** If the [unprotected sqlite4_value] object returned by
** [sqlite4_column_value()] is used in any other way, including calls
** to routines like [sqlite4_value_int()], [sqlite4_value_text()],
................................................................................
** sqlite4_column_text16() may be invalidated.
** Type conversions and pointer invalidations might occur
** in the following cases:
**
** <ul>
** <li> The initial content is a BLOB and sqlite4_column_text() or
**      sqlite4_column_text16() is called.  A zero-terminator might
**      need to be added to the string.</li>
** <li> The initial content is UTF-8 text and sqlite4_column_bytes16() or
**      sqlite4_column_text16() is called.  The content must be converted
**      to UTF-16.</li>
** <li> The initial content is UTF-16 text and sqlite4_column_bytes() or
**      sqlite4_column_text() is called.  The content must be converted
**      to UTF-8.</li>
** </ul>
**
** ^Conversions between UTF-16be and UTF-16le are always done in place and do
** not invalidate a prior pointer, though of course the content of the buffer
** that the prior pointer references will have been modified.  Other kinds
** of conversion are done in place when it is possible, but sometimes they
** are not possible and in those cases prior pointers are invalidated.
**
** The safest and easiest to remember policy is to invoke these routines
** in one of the following ways:
**
** <ul>
**  <li>sqlite4_column_text() followed by sqlite4_column_bytes()</li>
**  <li>sqlite4_column_blob() followed by sqlite4_column_bytes()</li>
**  <li>sqlite4_column_text16() followed by sqlite4_column_bytes16()</li>
** </ul>
**
** In other words, you should call sqlite4_column_text(),
** sqlite4_column_blob(), or sqlite4_column_text16() first to force the result
** into the desired format, then invoke sqlite4_column_bytes() or
** sqlite4_column_bytes16() to find the size of the result.  Do not mix calls
** to sqlite4_column_text() or sqlite4_column_blob() with calls to
** sqlite4_column_bytes16(), and do not mix calls to sqlite4_column_text16()
** with calls to sqlite4_column_bytes().
**
** ^The pointers returned are valid until a type conversion occurs as
** described above, or until [sqlite4_step()] or [sqlite4_reset()] or
** [sqlite4_finalize()] is called.  ^The memory space used to hold strings
** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
** [sqlite4_column_blob()], [sqlite4_column_text()], etc. into
** [sqlite4_free()].
**
** ^(If a memory allocation error occurs during the evaluation of any
** of these routines, a default value is returned.  The default value
** is either the integer 0, the floating point number 0.0, or a NULL
** pointer.  Subsequent calls to [sqlite4_errcode()] will return
** [SQLITE4_NOMEM].)^
*/
const void *sqlite4_column_blob(sqlite4_stmt*, int iCol);
int sqlite4_column_bytes(sqlite4_stmt*, int iCol);
int sqlite4_column_bytes16(sqlite4_stmt*, int iCol);
double sqlite4_column_double(sqlite4_stmt*, int iCol);
int sqlite4_column_int(sqlite4_stmt*, int iCol);
sqlite4_int64 sqlite4_column_int64(sqlite4_stmt*, int iCol);
const char *sqlite4_column_text(sqlite4_stmt*, int iCol);
const void *sqlite4_column_text16(sqlite4_stmt*, int iCol);
int sqlite4_column_type(sqlite4_stmt*, int iCol);
sqlite4_value *sqlite4_column_value(sqlite4_stmt*, int iCol);

/*
** CAPIREF: Destroy A Prepared Statement Object
**
** ^The sqlite4_finalize() function is called to delete a [prepared statement].
................................................................................
** words, if the value is a string that looks like a number)
** then the conversion is performed.  Otherwise no conversion occurs.
** The [SQLITE4_INTEGER | datatype] after conversion is returned.)^
**
** Please pay particular attention to the fact that the pointer returned
** from [sqlite4_value_blob()], [sqlite4_value_text()], or
** [sqlite4_value_text16()] can be invalidated by a subsequent call to
** [sqlite4_value_bytes()], [sqlite4_value_bytes16()], [sqlite4_value_text()],
** or [sqlite4_value_text16()].
**
** These routines must be called from the same thread as
** the SQL function that supplied the [sqlite4_value*] parameters.
*/
const void *sqlite4_value_blob(sqlite4_value*);
int sqlite4_value_bytes(sqlite4_value*);
int sqlite4_value_bytes16(sqlite4_value*);
double sqlite4_value_double(sqlite4_value*);
int sqlite4_value_int(sqlite4_value*);
sqlite4_int64 sqlite4_value_int64(sqlite4_value*);
const char *sqlite4_value_text(sqlite4_value*);
const void *sqlite4_value_text16(sqlite4_value*);
const void *sqlite4_value_text16le(sqlite4_value*);
const void *sqlite4_value_text16be(sqlite4_value*);
int sqlite4_value_type(sqlite4_value*);
int sqlite4_value_numeric_type(sqlite4_value*);

/*
** CAPIREF: Obtain Aggregate Function Context
**
** Implementations of aggregate SQL functions use this







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






>
>
>
>
>
>
>

|
>
>
|
>
>
>
>
>







 







|
|
|
<
|
|
<








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






|
<
<



|
|







 







<
|




|
<
<



|
|
|
|







2201
2202
2203
2204
2205
2206
2207


















2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
....
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286

2287
2288

2289
2290
2291
2292
2293
2294
2295
2296
























2297
2298
2299
2300
2301
2302
2303


2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
....
2530
2531
2532
2533
2534
2535
2536

2537
2538
2539
2540
2541
2542


2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
** The value
** returned by sqlite4_column_type() is only meaningful if no type
** conversions have occurred as described below.  After a type conversion,
** the value returned by sqlite4_column_type() is undefined.  Future
** versions of SQLite may change the behavior of sqlite4_column_type()
** following a type conversion.
**


















** ^The values returned by [sqlite4_column_bytes()] and 
** [sqlite4_column_bytes16()] do not include the zero terminators at the end
** of the string.  ^For clarity: the values returned by
** [sqlite4_column_bytes()] and [sqlite4_column_bytes16()] are the number of
** bytes in the string, not the number of characters.
**
** The sqlite4_column_text(), sqlite4_column_text16() and sqlite4_column_blob()
** functions return a pointer to a buffer containing the text or blob value.
** If the third parameter passed is not NULL, then the location it points to 
** is set to the number of bytes of text or blob content in the returned 
** buffer. To be clear, for a text value, the output parameter is set to the
** number of bytes of content, not the number of characters.
**
** ^Strings returned by sqlite4_column_text() and sqlite4_column_text16(),
** even empty strings, are always zero-terminated. The terminator byte or 
** bytes is not included in the number of bytes of content returned via
** the output parameter. The return value from sqlite4_column_blob() for 
** a zero-length BLOB is a NULL pointer. ^The pointers returned are valid 
** until a type conversion occurs (see below), or until [sqlite4_step()], 
** [sqlite4_reset()] or [sqlite4_finalize()] is called. ^The memory space 
** used to hold strings and BLOBs is freed automatically.  Do <b>not</b> 
** pass the pointers returned [sqlite4_column_blob()], 
** [sqlite4_column_text()], etc. into [sqlite4_free()].
**
** ^The object returned by [sqlite4_column_value()] is an
** [unprotected sqlite4_value] object.  An unprotected sqlite4_value object
** may only be used with [sqlite4_bind_value()] and [sqlite4_result_value()].
** If the [unprotected sqlite4_value] object returned by
** [sqlite4_column_value()] is used in any other way, including calls
** to routines like [sqlite4_value_int()], [sqlite4_value_text()],
................................................................................
** sqlite4_column_text16() may be invalidated.
** Type conversions and pointer invalidations might occur
** in the following cases:
**
** <ul>
** <li> The initial content is a BLOB and sqlite4_column_text() or
**      sqlite4_column_text16() is called.  A zero-terminator might
**      need to be added to the string.
** <li> The initial content is UTF-8 text and sqlite4_column_text16() is 
**      called. The content must be converted to UTF-16.

** <li> The initial content is UTF-16 text and sqlite4_column_text() is 
**      called.  The content must be converted to UTF-8.

** </ul>
**
** ^Conversions between UTF-16be and UTF-16le are always done in place and do
** not invalidate a prior pointer, though of course the content of the buffer
** that the prior pointer references will have been modified.  Other kinds
** of conversion are done in place when it is possible, but sometimes they
** are not possible and in those cases prior pointers are invalidated.
**
























** ^(If a memory allocation error occurs during the evaluation of any
** of these routines, a default value is returned.  The default value
** is either the integer 0, the floating point number 0.0, or a NULL
** pointer.  Subsequent calls to [sqlite4_errcode()] will return
** [SQLITE4_NOMEM].)^
*/
const void *sqlite4_column_blob(sqlite4_stmt*, int iCol, int *pnByte);


double sqlite4_column_double(sqlite4_stmt*, int iCol);
int sqlite4_column_int(sqlite4_stmt*, int iCol);
sqlite4_int64 sqlite4_column_int64(sqlite4_stmt*, int iCol);
const char *sqlite4_column_text(sqlite4_stmt*, int iCol, int *pnByte);
const void *sqlite4_column_text16(sqlite4_stmt*, int iCol, int *pnByte);
int sqlite4_column_type(sqlite4_stmt*, int iCol);
sqlite4_value *sqlite4_column_value(sqlite4_stmt*, int iCol);

/*
** CAPIREF: Destroy A Prepared Statement Object
**
** ^The sqlite4_finalize() function is called to delete a [prepared statement].
................................................................................
** words, if the value is a string that looks like a number)
** then the conversion is performed.  Otherwise no conversion occurs.
** The [SQLITE4_INTEGER | datatype] after conversion is returned.)^
**
** Please pay particular attention to the fact that the pointer returned
** from [sqlite4_value_blob()], [sqlite4_value_text()], or
** [sqlite4_value_text16()] can be invalidated by a subsequent call to

** [sqlite4_value_text()] or [sqlite4_value_text16()].
**
** These routines must be called from the same thread as
** the SQL function that supplied the [sqlite4_value*] parameters.
*/
const void *sqlite4_value_blob(sqlite4_value*, int *pnByte);


double sqlite4_value_double(sqlite4_value*);
int sqlite4_value_int(sqlite4_value*);
sqlite4_int64 sqlite4_value_int64(sqlite4_value*);
const char *sqlite4_value_text(sqlite4_value*, int *pnByte);
const void *sqlite4_value_text16(sqlite4_value*, int *pnByte);
const void *sqlite4_value_text16le(sqlite4_value*, int *pnByte);
const void *sqlite4_value_text16be(sqlite4_value*, int *pnByte);
int sqlite4_value_type(sqlite4_value*);
int sqlite4_value_numeric_type(sqlite4_value*);

/*
** CAPIREF: Obtain Aggregate Function Context
**
** Implementations of aggregate SQL functions use this

Changes to src/tclsqlite.c.

417
418
419
420
421
422
423

424
425
426
427
428
429
430
431
432
...
440
441
442
443
444
445
446

447
448
449
450
451
452
453
454
455
....
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
....
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
....
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
    for(i=0; i<argc; i++){
      sqlite4_value *pIn = argv[i];
      Tcl_Obj *pVal;
            
      /* Set pVal to contain the i'th column of this row. */
      switch( sqlite4_value_type(pIn) ){
        case SQLITE4_BLOB: {

          int bytes = sqlite4_value_bytes(pIn);
          pVal = Tcl_NewByteArrayObj(sqlite4_value_blob(pIn), bytes);
          break;
        }
        case SQLITE4_INTEGER: {
          sqlite4_int64 v = sqlite4_value_int64(pIn);
          if( v>=-2147483647 && v<=2147483647 ){
            pVal = Tcl_NewIntObj((int)v);
          }else{
................................................................................
          break;
        }
        case SQLITE4_NULL: {
          pVal = Tcl_NewStringObj("", 0);
          break;
        }
        default: {

          int bytes = sqlite4_value_bytes(pIn);
          pVal = Tcl_NewStringObj((char *)sqlite4_value_text(pIn), bytes);
          break;
        }
      }
      rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
      if( rc ){
        Tcl_DecrRefCount(pCmd);
        sqlite4_result_error(context, Tcl_GetStringResult(p->interp), -1); 
................................................................................
** the value for the iCol'th column of the row currently pointed to by
** the DbEvalContext structure passed as the first argument.
*/
static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){
  sqlite4_stmt *pStmt = p->pPreStmt->pStmt;
  switch( sqlite4_column_type(pStmt, iCol) ){
    case SQLITE4_BLOB: {
      int bytes = sqlite4_column_bytes(pStmt, iCol);
      const char *zBlob = sqlite4_column_blob(pStmt, iCol);
      if( !zBlob ) bytes = 0;
      return Tcl_NewByteArrayObj((u8*)zBlob, bytes);
    }
    case SQLITE4_INTEGER: {
      sqlite4_int64 v = sqlite4_column_int64(pStmt, iCol);
      if( v>=-2147483647 && v<=2147483647 ){
        return Tcl_NewIntObj((int)v);
................................................................................
      return Tcl_NewDoubleObj(sqlite4_column_double(pStmt, iCol));
    }
    case SQLITE4_NULL: {
      return dbTextToObj(p->pDb->zNull);
    }
  }

  return dbTextToObj((char *)sqlite4_column_text(pStmt, iCol));
}

/*
** If using Tcl version 8.6 or greater, use the NR functions to avoid
** recursive evalution of scripts by the [db eval] and [db trans]
** commands. Even if the headers used while compiling the extension
** are 8.6 or newer, the code still tests the Tcl version at runtime.
................................................................................
  if( argc<1 ) return;
  p = sqlite4_aggregate_context(context, sizeof(*p));
  if( p==0 ) return;
  if( !p->isInit ){
    MD5Init(p);
  }
  for(i=0; i<argc; i++){
    const char *zData = (char*)sqlite4_value_text(argv[i]);
    if( zData ){
      MD5Update(p, (unsigned char*)zData, strlen(zData));
    }
  }
}
static void md5finalize(sqlite4_context *context){
  MD5Context *p;







>
|
|







 







>
|
|







 







|
|







 







|







 







|







417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
...
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
....
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
....
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
....
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
    for(i=0; i<argc; i++){
      sqlite4_value *pIn = argv[i];
      Tcl_Obj *pVal;
            
      /* Set pVal to contain the i'th column of this row. */
      switch( sqlite4_value_type(pIn) ){
        case SQLITE4_BLOB: {
          int nBlob;
          const void *pBlob = sqlite4_value_blob(pIn, &nBlob);
          pVal = Tcl_NewByteArrayObj(pBlob, nBlob);
          break;
        }
        case SQLITE4_INTEGER: {
          sqlite4_int64 v = sqlite4_value_int64(pIn);
          if( v>=-2147483647 && v<=2147483647 ){
            pVal = Tcl_NewIntObj((int)v);
          }else{
................................................................................
          break;
        }
        case SQLITE4_NULL: {
          pVal = Tcl_NewStringObj("", 0);
          break;
        }
        default: {
          int nText;
          const char *zText = sqlite4_value_text(pIn, &nText);
          pVal = Tcl_NewStringObj(zText, nText);
          break;
        }
      }
      rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
      if( rc ){
        Tcl_DecrRefCount(pCmd);
        sqlite4_result_error(context, Tcl_GetStringResult(p->interp), -1); 
................................................................................
** the value for the iCol'th column of the row currently pointed to by
** the DbEvalContext structure passed as the first argument.
*/
static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){
  sqlite4_stmt *pStmt = p->pPreStmt->pStmt;
  switch( sqlite4_column_type(pStmt, iCol) ){
    case SQLITE4_BLOB: {
      int bytes;
      const char *zBlob = sqlite4_column_blob(pStmt, iCol, &bytes);
      if( !zBlob ) bytes = 0;
      return Tcl_NewByteArrayObj((u8*)zBlob, bytes);
    }
    case SQLITE4_INTEGER: {
      sqlite4_int64 v = sqlite4_column_int64(pStmt, iCol);
      if( v>=-2147483647 && v<=2147483647 ){
        return Tcl_NewIntObj((int)v);
................................................................................
      return Tcl_NewDoubleObj(sqlite4_column_double(pStmt, iCol));
    }
    case SQLITE4_NULL: {
      return dbTextToObj(p->pDb->zNull);
    }
  }

  return dbTextToObj(sqlite4_column_text(pStmt, iCol, 0));
}

/*
** If using Tcl version 8.6 or greater, use the NR functions to avoid
** recursive evalution of scripts by the [db eval] and [db trans]
** commands. Even if the headers used while compiling the extension
** are 8.6 or newer, the code still tests the Tcl version at runtime.
................................................................................
  if( argc<1 ) return;
  p = sqlite4_aggregate_context(context, sizeof(*p));
  if( p==0 ) return;
  if( !p->isInit ){
    MD5Init(p);
  }
  for(i=0; i<argc; i++){
    const char *zData = (char*)sqlite4_value_text(argv[i], 0);
    if( zData ){
      MD5Update(p, (unsigned char*)zData, strlen(zData));
    }
  }
}
static void md5finalize(sqlite4_context *context){
  MD5Context *p;

Changes to src/vdbe.c.

1400
1401
1402
1403
1404
1405
1406
1407


1408
1409
1410
1411
1412
1413
1414
....
4389
4390
4391
4392
4393
4394
4395
4396


4397
4398
4399
4400
4401
4402
4403
....
4417
4418
4419
4420
4421
4422
4423
4424


4425
4426
4427
4428
4429
4430
4431
    */
    sqlite4VdbeMemRelease(&ctx.s);
    goto no_mem;
  }

  /* If the function returned an error, throw an exception */
  if( ctx.isError ){
    sqlite4SetString(&p->zErrMsg, db, "%s", sqlite4_value_text(&ctx.s));


    rc = ctx.isError;
  }

  /* Copy the result of the function into register P3 */
  sqlite4VdbeChangeEncoding(&ctx.s, encoding);
  sqlite4VdbeMemMove(pOut, &ctx.s);
  if( sqlite4VdbeMemTooBig(pOut) ){
................................................................................
    assert( pOp>p->aOp );
    assert( pOp[-1].p4type==P4_COLLSEQ );
    assert( pOp[-1].opcode==OP_CollSeq );
    ctx.pColl = pOp[-1].p4.pColl;
  }
  (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  if( ctx.isError ){
    sqlite4SetString(&p->zErrMsg, db, "%s", sqlite4_value_text(&ctx.s));


    rc = ctx.isError;
  }

  sqlite4VdbeMemRelease(&ctx.s);

  break;
}
................................................................................
case OP_AggFinal: {
  Mem *pMem;
  assert( pOp->p1>0 && pOp->p1<=p->nMem );
  pMem = &aMem[pOp->p1];
  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  rc = sqlite4VdbeMemFinalize(pMem, pOp->p4.pFunc);
  if( rc ){
    sqlite4SetString(&p->zErrMsg, db, "%s", sqlite4_value_text(pMem));


  }
  sqlite4VdbeChangeEncoding(pMem, encoding);
  UPDATE_MAX_BLOBSIZE(pMem);
  if( sqlite4VdbeMemTooBig(pMem) ){
    goto too_big;
  }
  break;







|
>
>







 







|
>
>







 







|
>
>







1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
....
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
....
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
    */
    sqlite4VdbeMemRelease(&ctx.s);
    goto no_mem;
  }

  /* If the function returned an error, throw an exception */
  if( ctx.isError ){
    sqlite4SetString(&p->zErrMsg, db, "%s", 
        (const char *)sqlite4ValueText(&ctx.s, SQLITE4_UTF8)
    );
    rc = ctx.isError;
  }

  /* Copy the result of the function into register P3 */
  sqlite4VdbeChangeEncoding(&ctx.s, encoding);
  sqlite4VdbeMemMove(pOut, &ctx.s);
  if( sqlite4VdbeMemTooBig(pOut) ){
................................................................................
    assert( pOp>p->aOp );
    assert( pOp[-1].p4type==P4_COLLSEQ );
    assert( pOp[-1].opcode==OP_CollSeq );
    ctx.pColl = pOp[-1].p4.pColl;
  }
  (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  if( ctx.isError ){
    sqlite4SetString(&p->zErrMsg, db, "%s", 
        (const char *)sqlite4ValueText(&ctx.s, SQLITE4_UTF8)
    );
    rc = ctx.isError;
  }

  sqlite4VdbeMemRelease(&ctx.s);

  break;
}
................................................................................
case OP_AggFinal: {
  Mem *pMem;
  assert( pOp->p1>0 && pOp->p1<=p->nMem );
  pMem = &aMem[pOp->p1];
  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  rc = sqlite4VdbeMemFinalize(pMem, pOp->p4.pFunc);
  if( rc ){
    sqlite4SetString(&p->zErrMsg, db, "%s", 
        (const char *)sqlite4ValueText(pMem, SQLITE4_UTF8)
    );
  }
  sqlite4VdbeChangeEncoding(pMem, encoding);
  UPDATE_MAX_BLOBSIZE(pMem);
  if( sqlite4VdbeMemTooBig(pMem) ){
    goto too_big;
  }
  break;

Changes to src/vdbeapi.c.

117
118
119
120
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136
137
138
...
142
143
144
145
146
147
148
149
150


151
152
153
154


155
156
157


158
159
160


161
162
163
164
165
166
167
...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
...
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
...
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
...
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
}


/**************************** sqlite4_value_  *******************************
** The following routines extract information from a Mem or sqlite4_value
** structure.
*/
const void *sqlite4_value_blob(sqlite4_value *pVal){
  Mem *p = (Mem*)pVal;
  if( p->flags & (MEM_Blob|MEM_Str) ){
    p->flags &= ~MEM_Str;
    p->flags |= MEM_Blob;

    return p->n ? p->z : 0;
  }else{
    return sqlite4_value_text(pVal);
  }
}
int sqlite4_value_bytes(sqlite4_value *pVal){
  return sqlite4ValueBytes(pVal, SQLITE4_UTF8);
}
int sqlite4_value_bytes16(sqlite4_value *pVal){
  return sqlite4ValueBytes(pVal, SQLITE4_UTF16NATIVE);
................................................................................
}
int sqlite4_value_int(sqlite4_value *pVal){
  return (int)sqlite4VdbeIntValue((Mem*)pVal);
}
sqlite4_int64 sqlite4_value_int64(sqlite4_value *pVal){
  return sqlite4VdbeIntValue((Mem*)pVal);
}
const char *sqlite4_value_text(sqlite4_value *pVal){
  return (const char *)sqlite4ValueText(pVal, SQLITE4_UTF8);


}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_value_text16(sqlite4_value* pVal){
  return sqlite4ValueText(pVal, SQLITE4_UTF16NATIVE);


}
const void *sqlite4_value_text16be(sqlite4_value *pVal){
  return sqlite4ValueText(pVal, SQLITE4_UTF16BE);


}
const void *sqlite4_value_text16le(sqlite4_value *pVal){
  return sqlite4ValueText(pVal, SQLITE4_UTF16LE);


}
#endif /* SQLITE4_OMIT_UTF16 */
int sqlite4_value_type(sqlite4_value* pVal){
  return pVal->type;
}

/**************************** sqlite4_result_  *******************************
................................................................................
    ** The error message from the SQL compiler has already been loaded 
    ** into the database handle. This block copies the error message 
    ** from the database handle into the statement and sets the statement
    ** program counter to 0 to ensure that when the statement is 
    ** finalized or reset the parser error message is available via
    ** sqlite4_errmsg() and sqlite4_errcode().
    */
    const char *zErr = (const char *)sqlite4_value_text(db->pErr); 
    sqlite4DbFree(db, v->zErrMsg);
    if( !db->mallocFailed ){
      v->zErrMsg = sqlite4DbStrDup(db, zErr);
      v->rc = rc2;
    } else {
      v->zErrMsg = 0;
      v->rc = rc = SQLITE4_NOMEM;
................................................................................
  }
}

/**************************** sqlite4_column_  *******************************
** The following routines are used to access elements of the current row
** in the result set.
*/
const void *sqlite4_column_blob(sqlite4_stmt *pStmt, int i){
  const void *val;
  val = sqlite4_value_blob( columnMem(pStmt,i) );
  return val;
}
int sqlite4_column_bytes(sqlite4_stmt *pStmt, int i){
  int val = sqlite4_value_bytes( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
int sqlite4_column_bytes16(sqlite4_stmt *pStmt, int i){
  int val = sqlite4_value_bytes16( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
double sqlite4_column_double(sqlite4_stmt *pStmt, int i){
  double val = sqlite4_value_double( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
................................................................................
  return val;
}
sqlite4_int64 sqlite4_column_int64(sqlite4_stmt *pStmt, int i){
  sqlite4_int64 val = sqlite4_value_int64( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
const char *sqlite4_column_text(sqlite4_stmt *pStmt, int i){
  const char *val = sqlite4_value_text( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
sqlite4_value *sqlite4_column_value(sqlite4_stmt *pStmt, int i){
  Mem *pOut = columnMem(pStmt, i);
  if( pOut->flags&MEM_Static ){
    pOut->flags &= ~MEM_Static;
    pOut->flags |= MEM_Ephem;
  }
  columnMallocFailure(pStmt);
  return (sqlite4_value *)pOut;
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_text16(sqlite4_stmt *pStmt, int i){
  const void *val = sqlite4_value_text16( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
#endif /* SQLITE4_OMIT_UTF16 */
int sqlite4_column_type(sqlite4_stmt *pStmt, int i){
  int iType = sqlite4_value_type( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
................................................................................
**
** If the result is not a simple column reference (if it is an expression
** or a constant) then useTypes 2, 3, and 4 return NULL.
*/
static const void *columnName(
  sqlite4_stmt *pStmt,
  int N,
  const void *(*xFunc)(Mem*),
  int useType
){
  const void *ret = 0;
  Vdbe *p = (Vdbe *)pStmt;
  int n;
  sqlite4 *db = p->db;
  
  assert( db!=0 );
  n = sqlite4_column_count(pStmt);
  if( N<n && N>=0 ){
    N += useType*n;
    sqlite4_mutex_enter(db->mutex);
    assert( db->mallocFailed==0 );
    ret = xFunc(&p->aColName[N]);
     /* A malloc may have failed inside of the xFunc() call. If this
    ** is the case, clear the mallocFailed flag and return NULL.
    */
    if( db->mallocFailed ){
      db->mallocFailed = 0;
      ret = 0;
    }
................................................................................

/*
** Return the name of the Nth column of the result set returned by SQL
** statement pStmt.
*/
const char *sqlite4_column_name(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text, COLNAME_NAME);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_name16(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text16, COLNAME_NAME);
}
#endif

/*
** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
** not define OMIT_DECLTYPE.
*/
................................................................................

#ifndef SQLITE4_OMIT_DECLTYPE
/*
** Return the column declaration type (if applicable) of the 'i'th column
** of the result set of SQL statement pStmt.
*/
const char *sqlite4_column_decltype(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text, COLNAME_DECLTYPE);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_decltype16(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text16, COLNAME_DECLTYPE);
}
#endif /* SQLITE4_OMIT_UTF16 */
#endif /* SQLITE4_OMIT_DECLTYPE */

#ifdef SQLITE4_ENABLE_COLUMN_METADATA
/*
** Return the name of the database from which a result column derives.
** NULL is returned if the result column is an expression or constant or
** anything else which is not an unabiguous reference to a database column.
*/
const char *sqlite4_column_database_name(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text, COLNAME_DATABASE);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_database_name16(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text16, COLNAME_DATABASE);
}
#endif /* SQLITE4_OMIT_UTF16 */

/*
** Return the name of the table from which a result column derives.
** NULL is returned if the result column is an expression or constant or
** anything else which is not an unabiguous reference to a database column.
*/
const char *sqlite4_column_table_name(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text, COLNAME_TABLE);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_table_name16(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text16, COLNAME_TABLE);
}
#endif /* SQLITE4_OMIT_UTF16 */

/*
** Return the name of the table column from which a result column derives.
** NULL is returned if the result column is an expression or constant or
** anything else which is not an unabiguous reference to a database column.
*/
const char *sqlite4_column_origin_name(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text, COLNAME_COLUMN);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_origin_name16(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*))sqlite4_value_text16, COLNAME_COLUMN);
}
#endif /* SQLITE4_OMIT_UTF16 */
#endif /* SQLITE4_ENABLE_COLUMN_METADATA */


/******************************* sqlite4_bind_  ***************************
** 







|




>


|







 







|
|
>
>


|
|
>
>

|
|
>
>

|
|
>
>







 







|







 







|

|
<
<
<
<
<
<
<
<
<
<







 







|
|













|
|







 







|













|







 







|




|







 







|
|



|
|











|
|



|
|









|
|



|
|









|
|



|
|







117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
...
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
...
719
720
721
722
723
724
725
726
727
728










729
730
731
732
733
734
735
...
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
...
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
...
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
...
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
}


/**************************** sqlite4_value_  *******************************
** The following routines extract information from a Mem or sqlite4_value
** structure.
*/
const void *sqlite4_value_blob(sqlite4_value *pVal, int *pnByte){
  Mem *p = (Mem*)pVal;
  if( p->flags & (MEM_Blob|MEM_Str) ){
    p->flags &= ~MEM_Str;
    p->flags |= MEM_Blob;
    if( pnByte ) *pnByte = p->n;
    return p->n ? p->z : 0;
  }else{
    return sqlite4_value_text(pVal, pnByte);
  }
}
int sqlite4_value_bytes(sqlite4_value *pVal){
  return sqlite4ValueBytes(pVal, SQLITE4_UTF8);
}
int sqlite4_value_bytes16(sqlite4_value *pVal){
  return sqlite4ValueBytes(pVal, SQLITE4_UTF16NATIVE);
................................................................................
}
int sqlite4_value_int(sqlite4_value *pVal){
  return (int)sqlite4VdbeIntValue((Mem*)pVal);
}
sqlite4_int64 sqlite4_value_int64(sqlite4_value *pVal){
  return sqlite4VdbeIntValue((Mem*)pVal);
}
const char *sqlite4_value_text(sqlite4_value *pVal, int *pnByte){
  const char *zRet = (const char *)sqlite4ValueText(pVal, SQLITE4_UTF8);
  if( pnByte ) *pnByte = ((Mem *)pVal)->n;
  return zRet;
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_value_text16(sqlite4_value* pVal, int *pnByte){
  const void *pRet = sqlite4ValueText(pVal, SQLITE4_UTF16NATIVE);
  if( pnByte ) *pnByte = ((Mem *)pVal)->n;
  return pRet;
}
const void *sqlite4_value_text16be(sqlite4_value *pVal, int *pnByte){
  const void *pRet = sqlite4ValueText(pVal, SQLITE4_UTF16BE);
  if( pnByte ) *pnByte = ((Mem *)pVal)->n;
  return pRet;
}
const void *sqlite4_value_text16le(sqlite4_value *pVal, int *pnByte){
  const void *pRet = sqlite4ValueText(pVal, SQLITE4_UTF16LE);
  if( pnByte ) *pnByte = ((Mem *)pVal)->n;
  return pRet;
}
#endif /* SQLITE4_OMIT_UTF16 */
int sqlite4_value_type(sqlite4_value* pVal){
  return pVal->type;
}

/**************************** sqlite4_result_  *******************************
................................................................................
    ** The error message from the SQL compiler has already been loaded 
    ** into the database handle. This block copies the error message 
    ** from the database handle into the statement and sets the statement
    ** program counter to 0 to ensure that when the statement is 
    ** finalized or reset the parser error message is available via
    ** sqlite4_errmsg() and sqlite4_errcode().
    */
    const char *zErr = sqlite4_value_text(db->pErr, 0); 
    sqlite4DbFree(db, v->zErrMsg);
    if( !db->mallocFailed ){
      v->zErrMsg = sqlite4DbStrDup(db, zErr);
      v->rc = rc2;
    } else {
      v->zErrMsg = 0;
      v->rc = rc = SQLITE4_NOMEM;
................................................................................
  }
}

/**************************** sqlite4_column_  *******************************
** The following routines are used to access elements of the current row
** in the result set.
*/
const void *sqlite4_column_blob(sqlite4_stmt *pStmt, int i, int *pnByte){
  const void *val;
  val = sqlite4_value_blob( columnMem(pStmt,i), pnByte );










  return val;
}
double sqlite4_column_double(sqlite4_stmt *pStmt, int i){
  double val = sqlite4_value_double( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
................................................................................
  return val;
}
sqlite4_int64 sqlite4_column_int64(sqlite4_stmt *pStmt, int i){
  sqlite4_int64 val = sqlite4_value_int64( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}
const char *sqlite4_column_text(sqlite4_stmt *pStmt, int i, int *pnByte){
  const char *val = sqlite4_value_text( columnMem(pStmt,i), pnByte );
  columnMallocFailure(pStmt);
  return val;
}
sqlite4_value *sqlite4_column_value(sqlite4_stmt *pStmt, int i){
  Mem *pOut = columnMem(pStmt, i);
  if( pOut->flags&MEM_Static ){
    pOut->flags &= ~MEM_Static;
    pOut->flags |= MEM_Ephem;
  }
  columnMallocFailure(pStmt);
  return (sqlite4_value *)pOut;
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_text16(sqlite4_stmt *pStmt, int i, int *pnByte){
  const void *val = sqlite4_value_text16( columnMem(pStmt,i), pnByte );
  columnMallocFailure(pStmt);
  return val;
}
#endif /* SQLITE4_OMIT_UTF16 */
int sqlite4_column_type(sqlite4_stmt *pStmt, int i){
  int iType = sqlite4_value_type( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
................................................................................
**
** If the result is not a simple column reference (if it is an expression
** or a constant) then useTypes 2, 3, and 4 return NULL.
*/
static const void *columnName(
  sqlite4_stmt *pStmt,
  int N,
  const void *(*xFunc)(Mem*, int *),
  int useType
){
  const void *ret = 0;
  Vdbe *p = (Vdbe *)pStmt;
  int n;
  sqlite4 *db = p->db;
  
  assert( db!=0 );
  n = sqlite4_column_count(pStmt);
  if( N<n && N>=0 ){
    N += useType*n;
    sqlite4_mutex_enter(db->mutex);
    assert( db->mallocFailed==0 );
    ret = xFunc(&p->aColName[N], 0);
     /* A malloc may have failed inside of the xFunc() call. If this
    ** is the case, clear the mallocFailed flag and return NULL.
    */
    if( db->mallocFailed ){
      db->mallocFailed = 0;
      ret = 0;
    }
................................................................................

/*
** Return the name of the Nth column of the result set returned by SQL
** statement pStmt.
*/
const char *sqlite4_column_name(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*, int*))sqlite4_value_text, COLNAME_NAME);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_name16(sqlite4_stmt *pStmt, int N){
  return columnName(
      pStmt, N, (const void*(*)(Mem*, int*))sqlite4_value_text16, COLNAME_NAME);
}
#endif

/*
** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
** not define OMIT_DECLTYPE.
*/
................................................................................

#ifndef SQLITE4_OMIT_DECLTYPE
/*
** Return the column declaration type (if applicable) of the 'i'th column
** of the result set of SQL statement pStmt.
*/
const char *sqlite4_column_decltype(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text, COLNAME_DECLTYPE);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_decltype16(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text16, COLNAME_DECLTYPE);
}
#endif /* SQLITE4_OMIT_UTF16 */
#endif /* SQLITE4_OMIT_DECLTYPE */

#ifdef SQLITE4_ENABLE_COLUMN_METADATA
/*
** Return the name of the database from which a result column derives.
** NULL is returned if the result column is an expression or constant or
** anything else which is not an unabiguous reference to a database column.
*/
const char *sqlite4_column_database_name(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text, COLNAME_DATABASE);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_database_name16(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text16, COLNAME_DATABASE);
}
#endif /* SQLITE4_OMIT_UTF16 */

/*
** Return the name of the table from which a result column derives.
** NULL is returned if the result column is an expression or constant or
** anything else which is not an unabiguous reference to a database column.
*/
const char *sqlite4_column_table_name(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text, COLNAME_TABLE);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_table_name16(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text16, COLNAME_TABLE);
}
#endif /* SQLITE4_OMIT_UTF16 */

/*
** Return the name of the table column from which a result column derives.
** NULL is returned if the result column is an expression or constant or
** anything else which is not an unabiguous reference to a database column.
*/
const char *sqlite4_column_origin_name(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text, COLNAME_COLUMN);
}
#ifndef SQLITE4_OMIT_UTF16
const void *sqlite4_column_origin_name16(sqlite4_stmt *pStmt, int N){
  return columnName(pStmt, 
      N, (const void*(*)(Mem*, int*))sqlite4_value_text16, COLNAME_COLUMN);
}
#endif /* SQLITE4_OMIT_UTF16 */
#endif /* SQLITE4_ENABLE_COLUMN_METADATA */


/******************************* sqlite4_bind_  ***************************
** 

Changes to src/where.c.

707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
    op = pRight->op2;
  }
  if( op==TK_VARIABLE ){
    Vdbe *pReprepare = pParse->pReprepare;
    int iCol = pRight->iColumn;
    pVal = sqlite4VdbeGetValue(pReprepare, iCol, SQLITE4_AFF_NONE);
    if( pVal && sqlite4_value_type(pVal)==SQLITE4_TEXT ){
      z = (char *)sqlite4_value_text(pVal);
    }
    sqlite4VdbeSetVarmask(pParse->pVdbe, iCol);
    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
  }else if( op==TK_STRING ){
    z = pRight->u.zToken;
  }
  if( z ){







|







707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
    op = pRight->op2;
  }
  if( op==TK_VARIABLE ){
    Vdbe *pReprepare = pParse->pReprepare;
    int iCol = pRight->iColumn;
    pVal = sqlite4VdbeGetValue(pReprepare, iCol, SQLITE4_AFF_NONE);
    if( pVal && sqlite4_value_type(pVal)==SQLITE4_TEXT ){
      z = sqlite4_value_text(pVal, 0);
    }
    sqlite4VdbeSetVarmask(pParse->pVdbe, iCol);
    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
  }else if( op==TK_STRING ){
    z = pRight->u.zToken;
  }
  if( z ){

Changes to test/permutations.test.

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  main.test
  manydb.test
  misc5.test misc6.test
  misuse.test
  notnull.test
  null.test
  printf.test 
  ptrchng.test
  quote.test

  savepoint.test savepoint2.test savepoint5.test 

  select1.test select2.test select3.test select4.test select5.test 
  select6.test select7.test select8.test select9.test selectA.test 
  selectB.test selectC.test 







<







179
180
181
182
183
184
185

186
187
188
189
190
191
192
  main.test
  manydb.test
  misc5.test misc6.test
  misuse.test
  notnull.test
  null.test
  printf.test 

  quote.test

  savepoint.test savepoint2.test savepoint5.test 

  select1.test select2.test select3.test select4.test select5.test 
  select6.test select7.test select8.test select9.test selectA.test 
  selectB.test selectC.test 

Changes to test/test_func.c.

92
93
94
95
96
97
98
99
100

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
...
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  test_destructor_count_var--;
}
static void test_destructor(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  char *zVal;
  int len;

  
  test_destructor_count_var++;
  assert( nArg==1 );
  if( sqlite4_value_type(argv[0])==SQLITE4_NULL ) return;
  len = sqlite4_value_bytes(argv[0]); 
  zVal = testContextMalloc(pCtx, len+3);
  if( !zVal ){
    return;
  }
  zVal[len+1] = 0;
  zVal[len+2] = 0;
  zVal++;
  memcpy(zVal, sqlite4_value_text(argv[0]), len);
  sqlite4_result_text(pCtx, zVal, -1, destructor, 0);
}
#ifndef SQLITE4_OMIT_UTF16
static void test_destructor16(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  char *zVal;
  int len;

  
  test_destructor_count_var++;
  assert( nArg==1 );
  if( sqlite4_value_type(argv[0])==SQLITE4_NULL ) return;
  len = sqlite4_value_bytes16(argv[0]); 
  zVal = testContextMalloc(pCtx, len+3);
  if( !zVal ){
    return;
  }
  zVal[len+1] = 0;
  zVal[len+2] = 0;
  zVal++;
  memcpy(zVal, sqlite4_value_text16(argv[0]), len);
  sqlite4_result_text16(pCtx, zVal, -1, destructor, 0);
}
#endif
static void test_destructor_count(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
................................................................................
static void test_auxdata(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  int i;
  char *zRet = testContextMalloc(pCtx, nArg*2);
  sqlite4_env *pEnv;
  if( !zRet ) return;
  memset(zRet, 0, nArg*2);
  for(i=0; i<nArg; i++){
    char const *z = (char*)sqlite4_value_text(argv[i]);
    if( z ){
      int n;
      char *zAux = sqlite4_get_auxdata(pCtx, i);
      if( zAux ){
        zRet[i*2] = '1';
        assert( strcmp(zAux,z)==0 );
      }else {
................................................................................
** second argument exists, it becomes the error code.
*/
static void test_error(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  sqlite4_result_error(pCtx, (char*)sqlite4_value_text(argv[0]), -1);
  if( nArg==2 ){
    sqlite4_result_error_code(pCtx, sqlite4_value_int(argv[1]));
  }
}

/* A counter object with its destructor.  Used by counterFunc() below.
*/
................................................................................
*/
static void test_isolation(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
#ifndef SQLITE4_OMIT_UTF16
  sqlite4_value_text16(argv[0]);
  sqlite4_value_text(argv[0]);
  sqlite4_value_text16(argv[0]);
  sqlite4_value_text(argv[0]);
#endif
  sqlite4_result_value(pCtx, argv[1]);
}

/*
** Invoke an SQL statement recursively.  The function result is the 
** first column of the first row of the result set.
................................................................................
  sqlite4_value **argv
){
  sqlite4_stmt *pStmt;
  int rc;
  sqlite4 *db = sqlite4_context_db_handle(pCtx);
  const char *zSql;

  zSql = (char*)sqlite4_value_text(argv[0]);
  rc = sqlite4_prepare(db, zSql, -1, &pStmt, 0);
  if( rc==SQLITE4_OK ){
    rc = sqlite4_step(pStmt);
    if( rc==SQLITE4_ROW ){
      sqlite4_result_value(pCtx, sqlite4_column_value(pStmt, 0));
    }
    rc = sqlite4_finalize(pStmt);
................................................................................
  int nArg,
  sqlite4_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );
  n = sqlite4_value_bytes(argv[0]);
  zIn = (const char*)sqlite4_value_text(argv[0]);
  zOut = sqlite4_malloc(sqlite4_context_env(pCtx), n/2 );
  if( zOut==0 ){
    sqlite4_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite4_result_text16be(pCtx, zOut, n/2, SQLITE4_DYNAMIC, 0);
  }
................................................................................
  int nArg,
  sqlite4_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );
  n = sqlite4_value_bytes(argv[0]);
  zIn = (const char*)sqlite4_value_text(argv[0]);
  zOut = sqlite4_malloc(sqlite4_context_env(pCtx), n/2 );
  if( zOut==0 ){
    sqlite4_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite4_result_text(pCtx, zOut, n/2, SQLITE4_DYNAMIC, 0);
  }
................................................................................
  int nArg,
  sqlite4_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );
  n = sqlite4_value_bytes(argv[0]);
  zIn = (const char*)sqlite4_value_text(argv[0]);
  zOut = sqlite4_malloc(sqlite4_context_env(pCtx), n/2 );
  if( zOut==0 ){
    sqlite4_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite4_result_text16le(pCtx, zOut, n/2, SQLITE4_DYNAMIC, 0);
  }







|
|
>




|
|



|
|

|








|
|
>




|
|



|
|

|







 







<



|







 







|







 







|
|
|
|







 







|







 







<
|







 







<
|







 







<
|







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
...
190
191
192
193
194
195
196

197
198
199
200
201
202
203
204
205
206
207
...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
...
371
372
373
374
375
376
377

378
379
380
381
382
383
384
385
...
397
398
399
400
401
402
403

404
405
406
407
408
409
410
411
...
423
424
425
426
427
428
429

430
431
432
433
434
435
436
437
  test_destructor_count_var--;
}
static void test_destructor(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  char *zVal;                     /* Output value */
  const char *zText;              /* Input text */
  int nText;                      /* strlen(zText) */
  
  test_destructor_count_var++;
  assert( nArg==1 );
  if( sqlite4_value_type(argv[0])==SQLITE4_NULL ) return;
  zText = sqlite4_value_text(argv[0], &nText);
  zVal = testContextMalloc(pCtx, nText+3);
  if( !zVal ){
    return;
  }
  zVal[nText+1] = 0;
  zVal[nText+2] = 0;
  zVal++;
  memcpy(zVal, zText, nText);
  sqlite4_result_text(pCtx, zVal, -1, destructor, 0);
}
#ifndef SQLITE4_OMIT_UTF16
static void test_destructor16(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  char *zVal;                     /* Output value */
  const void *pText;              /* Input text */
  int nText;                      /* strlen(zText) */
  
  test_destructor_count_var++;
  assert( nArg==1 );
  if( sqlite4_value_type(argv[0])==SQLITE4_NULL ) return;
  pText = sqlite4_value_text16(argv[0], &nText);
  zVal = testContextMalloc(pCtx, nText+3);
  if( !zVal ){
    return;
  }
  zVal[nText+1] = 0;
  zVal[nText+2] = 0;
  zVal++;
  memcpy(zVal, pText, nText);
  sqlite4_result_text16(pCtx, zVal, -1, destructor, 0);
}
#endif
static void test_destructor_count(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
................................................................................
static void test_auxdata(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  int i;
  char *zRet = testContextMalloc(pCtx, nArg*2);

  if( !zRet ) return;
  memset(zRet, 0, nArg*2);
  for(i=0; i<nArg; i++){
    char const *z = sqlite4_value_text(argv[i], 0);
    if( z ){
      int n;
      char *zAux = sqlite4_get_auxdata(pCtx, i);
      if( zAux ){
        zRet[i*2] = '1';
        assert( strcmp(zAux,z)==0 );
      }else {
................................................................................
** second argument exists, it becomes the error code.
*/
static void test_error(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
  sqlite4_result_error(pCtx, sqlite4_value_text(argv[0], 0), -1);
  if( nArg==2 ){
    sqlite4_result_error_code(pCtx, sqlite4_value_int(argv[1]));
  }
}

/* A counter object with its destructor.  Used by counterFunc() below.
*/
................................................................................
*/
static void test_isolation(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
){
#ifndef SQLITE4_OMIT_UTF16
  sqlite4_value_text16(argv[0], 0);
  sqlite4_value_text(argv[0], 0);
  sqlite4_value_text16(argv[0], 0);
  sqlite4_value_text(argv[0], 0);
#endif
  sqlite4_result_value(pCtx, argv[1]);
}

/*
** Invoke an SQL statement recursively.  The function result is the 
** first column of the first row of the result set.
................................................................................
  sqlite4_value **argv
){
  sqlite4_stmt *pStmt;
  int rc;
  sqlite4 *db = sqlite4_context_db_handle(pCtx);
  const char *zSql;

  zSql = sqlite4_value_text(argv[0], 0);
  rc = sqlite4_prepare(db, zSql, -1, &pStmt, 0);
  if( rc==SQLITE4_OK ){
    rc = sqlite4_step(pStmt);
    if( rc==SQLITE4_ROW ){
      sqlite4_result_value(pCtx, sqlite4_column_value(pStmt, 0));
    }
    rc = sqlite4_finalize(pStmt);
................................................................................
  int nArg,
  sqlite4_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );

  zIn = sqlite4_value_text(argv[0], &n);
  zOut = sqlite4_malloc(sqlite4_context_env(pCtx), n/2 );
  if( zOut==0 ){
    sqlite4_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite4_result_text16be(pCtx, zOut, n/2, SQLITE4_DYNAMIC, 0);
  }
................................................................................
  int nArg,
  sqlite4_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );

  zIn = sqlite4_value_text(argv[0], &n);
  zOut = sqlite4_malloc(sqlite4_context_env(pCtx), n/2 );
  if( zOut==0 ){
    sqlite4_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite4_result_text(pCtx, zOut, n/2, SQLITE4_DYNAMIC, 0);
  }
................................................................................
  int nArg,
  sqlite4_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );

  zIn = sqlite4_value_text(argv[0], &n);
  zOut = sqlite4_malloc(sqlite4_context_env(pCtx), n/2 );
  if( zOut==0 ){
    sqlite4_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite4_result_text16le(pCtx, zOut, n/2, SQLITE4_DYNAMIC, 0);
  }

Changes to test/test_hexio.c.

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    Tcl_WrongNumArgs(interp, 1, objv, "HEX");
    return TCL_ERROR;
  }
  zOrig = (unsigned char *)Tcl_GetStringFromObj(objv[1], &n);
  z = sqlite4_malloc(0, n+3 );
  n = sqlite4TestHexToBin(zOrig, n, z);
  z[n] = 0;
  nOut = sqlite4Utf8To8(z);
  sqlite4TestBinToHex(z,nOut);
  Tcl_AppendResult(interp, (char*)z, 0);
  sqlite4_free(0, z);
  return TCL_OK;
#else
  Tcl_AppendResult(interp, 
      "[utf8_to_utf8] unavailable - SQLITE4_DEBUG not defined", 0
  );







|
|







304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    Tcl_WrongNumArgs(interp, 1, objv, "HEX");
    return TCL_ERROR;
  }
  zOrig = (unsigned char *)Tcl_GetStringFromObj(objv[1], &n);
  z = sqlite4_malloc(0, n+3 );
  n = sqlite4TestHexToBin(zOrig, n, z);
  z[n] = 0;
  nOut = sqlite4Utf8To8((char *)z);
  sqlite4TestBinToHex(z, nOut);
  Tcl_AppendResult(interp, (char*)z, 0);
  sqlite4_free(0, z);
  return TCL_OK;
#else
  Tcl_AppendResult(interp, 
      "[utf8_to_utf8] unavailable - SQLITE4_DEBUG not defined", 0
  );

Changes to test/test_main.c.

12
13
14
15
16
17
18

19
20
21
22
23
24
25
...
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
...
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
...
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
...
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
....
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
....
2097
2098
2099
2100
2101
2102
2103

2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
....
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
....
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
....
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
....
3100
3101
3102
3103
3104
3105
3106

3107



3108
3109
3110
3111
3112
3113
3114
....
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
....
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
....
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
....
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
....
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
** Code for testing all sorts of SQLite interfaces.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
*/
#include "sqliteInt.h"
#include "vdbeInt.h"
#include "tcl.h"

#include <stdlib.h>
#include <string.h>

/*
** This is a copy of the first part of the SqliteDb structure in 
** tclsqlite.c.  We need it here so that the get_sqlite_pointer routine
** can extract the sqlite4* pointer from an existing Tcl SQLite
................................................................................
  sqlite4_context *context,
  int argc,
  sqlite4_value **argv
){
  int i;
  for(i=0; i<argc; i++){
    if( SQLITE4_NULL!=sqlite4_value_type(argv[i]) ){
      int n = sqlite4_value_bytes(argv[i]);
      sqlite4_result_text(context, (char*)sqlite4_value_text(argv[i]),
          n, SQLITE4_TRANSIENT, 0);
      break;
    }
  }
}

/*
** These are test functions.    hex8() interprets its argument as
** UTF8 and returns a hex encoding.  hex16le() interprets its argument
** as UTF16le and returns a hex encoding.
*/
static void hex8Func(sqlite4_context *p, int argc, sqlite4_value **argv){
  const unsigned char *z;
  int i;
  char zBuf[200];
  z = sqlite4_value_text(argv[0]);
  for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
    sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
  }
  zBuf[i*2] = 0;
  sqlite4_result_text(p, (char*)zBuf, -1, SQLITE4_TRANSIENT, 0);
}
#ifndef SQLITE4_OMIT_UTF16
static void hex16Func(sqlite4_context *p, int argc, sqlite4_value **argv){
  const unsigned short int *z;
  int i;
  char zBuf[400];
  z = sqlite4_value_text16(argv[0]);
  for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
    sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
  }
  zBuf[i*4] = 0;
  sqlite4_result_text(p, (char*)zBuf, -1, SQLITE4_TRANSIENT, 0);
}
#endif
................................................................................
  sqlite4_context *context, 
  int argc,  
  sqlite4_value **argv
){
  struct dstr x;
  memset(&x, 0, sizeof(x));
  (void)sqlite4_exec((sqlite4*)sqlite4_user_data(context),
      (char*)sqlite4_value_text(argv[0]),
      execFuncCallback, &x, 0);
  sqlite4_result_text(context, x.z, x.nUsed, SQLITE4_TRANSIENT, 0);
  sqlite4_free(0, x.z);
}

/*
** Implementation of tkt2213func(), a scalar function that takes exactly
................................................................................
*/
static void tkt2213Function(
  sqlite4_context *context, 
  int argc,  
  sqlite4_value **argv
){
  int nText;
  unsigned char const *zText1;
  unsigned char const *zText2;
  unsigned char const *zText3;

  nText = sqlite4_value_bytes(argv[0]);
  zText1 = sqlite4_value_text(argv[0]);
  zText2 = sqlite4_value_text(argv[0]);
  zText3 = sqlite4_value_text(argv[0]);

  if( zText1!=zText2 || zText2!=zText3 ){
    sqlite4_result_error(context, "tkt2213 is not fixed", -1);
  }else{
    char *zCopy = (char *)sqlite4_malloc(sqlite4_context_env(context),nText);
    memcpy(zCopy, zText1, nText);
    sqlite4_result_text(context, zCopy, nText, SQLITE4_DYNAMIC, 0);
  }
}

/*
** The following SQL function takes 4 arguments.  The 2nd and
** 4th argument must be one of these strings:  'text', 'text16',
** or 'blob' corresponding to API functions
**
**      sqlite4_value_text()
**      sqlite4_value_text16()
**      sqlite4_value_blob()
**
** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
** corresponding to APIs:
**
**      sqlite4_value_bytes()
**      sqlite4_value_bytes16()
**      noop
**
** The APIs designated by the 2nd through 4th arguments are applied
** to the first argument in order.  If the pointers returned by the
** second and fourth are different, this routine returns 1.  Otherwise,
** this routine returns 0.
**
** This function is used to test to see when returned pointers from
** the _text(), _text16() and _blob() APIs become invalidated.
*/
static void ptrChngFunction(
  sqlite4_context *context, 
  int argc,  
  sqlite4_value **argv
){
  const void *p1, *p2;
  const char *zCmd;
  if( argc!=4 ) return;
  zCmd = (const char*)sqlite4_value_text(argv[1]);
  if( zCmd==0 ) return;
  if( strcmp(zCmd,"text")==0 ){
    p1 = (const void*)sqlite4_value_text(argv[0]);
#ifndef SQLITE4_OMIT_UTF16
  }else if( strcmp(zCmd, "text16")==0 ){
    p1 = (const void*)sqlite4_value_text16(argv[0]);
#endif
  }else if( strcmp(zCmd, "blob")==0 ){
    p1 = (const void*)sqlite4_value_blob(argv[0]);
  }else{
    return;
  }
  zCmd = (const char*)sqlite4_value_text(argv[2]);
  if( zCmd==0 ) return;
  if( strcmp(zCmd,"bytes")==0 ){
    sqlite4_value_bytes(argv[0]);
#ifndef SQLITE4_OMIT_UTF16
  }else if( strcmp(zCmd, "bytes16")==0 ){
    sqlite4_value_bytes16(argv[0]);
#endif
  }else if( strcmp(zCmd, "noop")==0 ){
    /* do nothing */
  }else{
    return;
  }
  zCmd = (const char*)sqlite4_value_text(argv[3]);
  if( zCmd==0 ) return;
  if( strcmp(zCmd,"text")==0 ){
    p2 = (const void*)sqlite4_value_text(argv[0]);
#ifndef SQLITE4_OMIT_UTF16
  }else if( strcmp(zCmd, "text16")==0 ){
    p2 = (const void*)sqlite4_value_text16(argv[0]);
#endif
  }else if( strcmp(zCmd, "blob")==0 ){
    p2 = (const void*)sqlite4_value_blob(argv[0]);
  }else{
    return;
  }
  sqlite4_result_int(context, p1!=p2);
}


/*
** Usage:  sqlite_test_create_function DB
**
** Call the sqlite4_create_function API on the given database in order
** to create a function named "x_coalesce".  This function does the same thing
** as the "coalesce" function.  This function also registers an SQL function
................................................................................
          hex16Func, 0, 0);
  }
#endif
  if( rc==SQLITE4_OK ){
    rc = sqlite4_create_function(db, "tkt2213func", 1, SQLITE4_ANY, 0, 
          tkt2213Function, 0, 0);
  }
  if( rc==SQLITE4_OK ){
    rc = sqlite4_create_function(db, "pointer_change", 4, SQLITE4_ANY, 0, 
          ptrChngFunction, 0, 0);
  }

#ifndef SQLITE4_OMIT_UTF16
  /* Use the sqlite4_create_function16() API here. Mainly for fun, but also 
  ** because it is not tested anywhere else. */
  if( rc==SQLITE4_OK ){
    const void *zUtf16;
    sqlite4_value *pVal;
................................................................................

/*
** The following routine is a user-defined SQL function whose purpose
** is to test the sqlite_set_result() API.
*/
static void testFunc(sqlite4_context *context, int argc, sqlite4_value **argv){
  while( argc>=2 ){
    const char *zArg0 = (char*)sqlite4_value_text(argv[0]);
    if( zArg0 ){
      if( 0==sqlite4_stricmp(zArg0, "int") ){
        sqlite4_result_int(context, sqlite4_value_int(argv[1]));
      }else if( sqlite4_stricmp(zArg0,"int64")==0 ){
        sqlite4_result_int64(context, sqlite4_value_int64(argv[1]));
      }else if( sqlite4_stricmp(zArg0,"string")==0 ){
        sqlite4_result_text(context, (char*)sqlite4_value_text(argv[1]), -1,
            SQLITE4_TRANSIENT, 0);
      }else if( sqlite4_stricmp(zArg0,"double")==0 ){
        sqlite4_result_double(context, sqlite4_value_double(argv[1]));
      }else if( sqlite4_stricmp(zArg0,"null")==0 ){
        sqlite4_result_null(context);
      }else if( sqlite4_stricmp(zArg0,"value")==0 ){
        sqlite4_result_value(context, argv[sqlite4_value_int(argv[1])]);
................................................................................
    default:
      assert(0);
  }

  sqlite4BeginBenignMalloc(pEnv);
  pVal = sqlite4ValueNew(0);
  if( pVal ){

    sqlite4ValueSetStr(pVal, nA, zA, encin, SQLITE4_STATIC, 0);
    n = sqlite4_value_bytes(pVal);
    Tcl_ListObjAppendElement(i,pX,
        Tcl_NewStringObj((char*)sqlite4_value_text(pVal),n));
    sqlite4ValueSetStr(pVal, nB, zB, encin, SQLITE4_STATIC, 0);
    n = sqlite4_value_bytes(pVal);
    Tcl_ListObjAppendElement(i,pX,
        Tcl_NewStringObj((char*)sqlite4_value_text(pVal),n));
    sqlite4ValueFree(pVal);
  }
  sqlite4EndBenignMalloc(pEnv);

  Tcl_EvalObjEx(i, pX, 0);
  Tcl_DecrRefCount(pX);
  Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
................................................................................
  Tcl_Obj *pX;
  sqlite4_value *pVal;
  interp = (Tcl_Interp *)sqlite4_user_data(pCtx);
  pX = Tcl_NewStringObj("test_function", -1);
  Tcl_IncrRefCount(pX);
  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
  Tcl_ListObjAppendElement(interp, pX, 
      Tcl_NewStringObj((char*)sqlite4_value_text(argv[0]), -1));
  Tcl_EvalObjEx(interp, pX, 0);
  Tcl_DecrRefCount(pX);
  sqlite4_result_text(pCtx, Tcl_GetStringResult(interp), -1,
                      SQLITE4_TRANSIENT, 0);
  pVal = sqlite4ValueNew(0);
  sqlite4ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
      SQLITE4_UTF8, SQLITE4_STATIC, 0);
  sqlite4_result_text16be(pCtx, sqlite4_value_text16be(pVal),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4ValueFree(pVal);
}
static void test_function_utf16le(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
................................................................................
  Tcl_Obj *pX;
  sqlite4_value *pVal;
  interp = (Tcl_Interp *)sqlite4_user_data(pCtx);
  pX = Tcl_NewStringObj("test_function", -1);
  Tcl_IncrRefCount(pX);
  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
  Tcl_ListObjAppendElement(interp, pX, 
      Tcl_NewStringObj((char*)sqlite4_value_text(argv[0]), -1));
  Tcl_EvalObjEx(interp, pX, 0);
  Tcl_DecrRefCount(pX);
  pVal = sqlite4ValueNew(0);
  sqlite4ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
      SQLITE4_UTF8, SQLITE4_STATIC, 0);
  sqlite4_result_text(pCtx, (char*)sqlite4_value_text(pVal), -1,
                      SQLITE4_TRANSIENT, 0);
  sqlite4ValueFree(pVal);
}
static void test_function_utf16be(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
................................................................................
  Tcl_Obj *pX;
  sqlite4_value *pVal;
  interp = (Tcl_Interp *)sqlite4_user_data(pCtx);
  pX = Tcl_NewStringObj("test_function", -1);
  Tcl_IncrRefCount(pX);
  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
  Tcl_ListObjAppendElement(interp, pX, 
      Tcl_NewStringObj((char*)sqlite4_value_text(argv[0]), -1));
  Tcl_EvalObjEx(interp, pX, 0);
  Tcl_DecrRefCount(pX);
  pVal = sqlite4ValueNew(0);
  sqlite4ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
      SQLITE4_UTF8, SQLITE4_STATIC, 0);
  sqlite4_result_text16(pCtx, sqlite4_value_text16le(pVal),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4_result_text16be(pCtx, sqlite4_value_text16le(pVal),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4_result_text16le(pCtx, sqlite4_value_text16le(pVal),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4ValueFree(pVal);
}
#endif /* SQLITE4_OMIT_UTF16 */
static int test_function(
  void * clientData,
  Tcl_Interp *interp,
................................................................................
       Tcl_GetString(objv[0]), " filename options-list", 0);
    return TCL_ERROR;
  }

  zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
  rc = sqlite4_open(0, zFilename, &db, 0);
  

  if( sqlite4TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;



  Tcl_AppendResult(interp, zBuf, 0);
  return TCL_OK;
}

/*
** Usage: sqlite4_open_v2 FILENAME FLAGS VFS
*/
................................................................................
       Tcl_GetString(objv[0]), " STMT column", 0);
    return TCL_ERROR;
  }

  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;

  len = sqlite4_column_bytes(pStmt, col);
  pBlob = sqlite4_column_blob(pStmt, col);
  Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
  return TCL_OK;
}

/*
** Usage: sqlite4_column_double STMT column
**
................................................................................
  void * clientData,        /* Pointer to SQLite API function to be invoke */
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_stmt *pStmt;
  int col;
  const char *(*xFunc)(sqlite4_stmt*, int);
  const char *zRet;

  xFunc = (const char *(*)(sqlite4_stmt*, int))clientData;
  if( objc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", 
       Tcl_GetString(objv[0]), " STMT column", 0);
    return TCL_ERROR;
  }

  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  zRet = xFunc(pStmt, col);
  if( zRet ){
    Tcl_SetResult(interp, (char *)zRet, 0);
  }
  return TCL_OK;
}

/*
................................................................................
#endif /* SQLITE4_OMIT_UTF16 */

  return TCL_OK;
}

/*
** Usage: sqlite4_column_int STMT column
**
** Usage: sqlite4_column_bytes STMT column
**
** Usage: sqlite4_column_bytes16 STMT column
**
*/
static int test_stmt_int(
  void * clientData,    /* Pointer to SQLite API function to be invoked */
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
................................................................................
  sqlite4_free(0, zExplain);
  if( rc!=SQLITE4_OK ) return rc;

  while( SQLITE4_ROW==sqlite4_step(pExplain) ){
    int iSelectid = sqlite4_column_int(pExplain, 0);
    int iOrder = sqlite4_column_int(pExplain, 1);
    int iFrom = sqlite4_column_int(pExplain, 2);
    const char *zDetail = (const char *)sqlite4_column_text(pExplain, 3);

    printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
  }

  return sqlite4_finalize(pExplain);
}

................................................................................
     { "sqlite4_column_type",           test_column_type   ,0 },
     { "sqlite4_column_blob",           test_column_blob   ,0 },
     { "sqlite4_column_double",         test_column_double ,0 },
     { "sqlite4_column_int64",          test_column_int64  ,0 },
     { "sqlite4_column_text",   test_stmt_utf8,  (void*)sqlite4_column_text },
     { "sqlite4_column_name",   test_stmt_utf8,  (void*)sqlite4_column_name },
     { "sqlite4_column_int",    test_stmt_int,   (void*)sqlite4_column_int  },
     { "sqlite4_column_bytes",  test_stmt_int,   (void*)sqlite4_column_bytes},
#ifndef SQLITE4_OMIT_DECLTYPE
     { "sqlite4_column_decltype",test_stmt_utf8,(void*)sqlite4_column_decltype},
#endif
#ifdef SQLITE4_ENABLE_COLUMN_METADATA
{ "sqlite4_column_database_name",test_stmt_utf8,(void*)sqlite4_column_database_name},
{ "sqlite4_column_table_name",test_stmt_utf8,(void*)sqlite4_column_table_name},
{ "sqlite4_column_origin_name",test_stmt_utf8,(void*)sqlite4_column_origin_name},
#endif

#ifndef SQLITE4_OMIT_UTF16
     { "sqlite4_column_bytes16", test_stmt_int, (void*)sqlite4_column_bytes16 },
     { "sqlite4_column_text16",  test_stmt_utf16, (void*)sqlite4_column_text16},
     { "sqlite4_column_name16",  test_stmt_utf16, (void*)sqlite4_column_name16},
     { "add_alignment_test_collations", add_alignment_test_collations, 0      },
#ifndef SQLITE4_OMIT_DECLTYPE
     { "sqlite4_column_decltype16",test_stmt_utf16,(void*)sqlite4_column_decltype16},
#endif
#ifdef SQLITE4_ENABLE_COLUMN_METADATA







>







 







|
|
|











|


|




|






|







 







|







 







|
|
|

<
|
|
|









<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
<
<
<







 







|






|







 







>

|
|
<

|
|
<







 







|







|







 







|





|







 







|





|

|

|







 







>
|
>
>
>







 







<
|







 







|


|








|







 







<
<
<
<
<







 







|







 







<










<







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
...
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
...
798
799
800
801
802
803
804
805
806
807
808

809
810
811
812
813
814
815
816
817
818
819
820











































































821
822
823
824
825
826
827
...
861
862
863
864
865
866
867




868
869
870
871
872
873
874
....
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
....
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028

2029
2030
2031

2032
2033
2034
2035
2036
2037
2038
....
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
....
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
....
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
....
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
....
3256
3257
3258
3259
3260
3261
3262

3263
3264
3265
3266
3267
3268
3269
3270
....
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
....
3394
3395
3396
3397
3398
3399
3400





3401
3402
3403
3404
3405
3406
3407
....
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
....
4448
4449
4450
4451
4452
4453
4454

4455
4456
4457
4458
4459
4460
4461
4462
4463
4464

4465
4466
4467
4468
4469
4470
4471
** Code for testing all sorts of SQLite interfaces.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
*/
#include "sqliteInt.h"
#include "vdbeInt.h"
#include "tcl.h"
#include "testInt.h"
#include <stdlib.h>
#include <string.h>

/*
** This is a copy of the first part of the SqliteDb structure in 
** tclsqlite.c.  We need it here so that the get_sqlite_pointer routine
** can extract the sqlite4* pointer from an existing Tcl SQLite
................................................................................
  sqlite4_context *context,
  int argc,
  sqlite4_value **argv
){
  int i;
  for(i=0; i<argc; i++){
    if( SQLITE4_NULL!=sqlite4_value_type(argv[i]) ){
      int n;
      const char *z = sqlite4_value_text(argv[i], &n);
      sqlite4_result_text(context, z, n, SQLITE4_TRANSIENT, 0);
      break;
    }
  }
}

/*
** These are test functions.    hex8() interprets its argument as
** UTF8 and returns a hex encoding.  hex16le() interprets its argument
** as UTF16le and returns a hex encoding.
*/
static void hex8Func(sqlite4_context *p, int argc, sqlite4_value **argv){
  const char *z;
  int i;
  char zBuf[200];
  z = sqlite4_value_text(argv[0], 0);
  for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
    sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
  }
  zBuf[i*2] = 0;
  sqlite4_result_text(p, zBuf, -1, SQLITE4_TRANSIENT, 0);
}
#ifndef SQLITE4_OMIT_UTF16
static void hex16Func(sqlite4_context *p, int argc, sqlite4_value **argv){
  const unsigned short int *z;
  int i;
  char zBuf[400];
  z = sqlite4_value_text16(argv[0], 0);
  for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
    sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
  }
  zBuf[i*4] = 0;
  sqlite4_result_text(p, (char*)zBuf, -1, SQLITE4_TRANSIENT, 0);
}
#endif
................................................................................
  sqlite4_context *context, 
  int argc,  
  sqlite4_value **argv
){
  struct dstr x;
  memset(&x, 0, sizeof(x));
  (void)sqlite4_exec((sqlite4*)sqlite4_user_data(context),
      (char*)sqlite4_value_text(argv[0], 0),
      execFuncCallback, &x, 0);
  sqlite4_result_text(context, x.z, x.nUsed, SQLITE4_TRANSIENT, 0);
  sqlite4_free(0, x.z);
}

/*
** Implementation of tkt2213func(), a scalar function that takes exactly
................................................................................
*/
static void tkt2213Function(
  sqlite4_context *context, 
  int argc,  
  sqlite4_value **argv
){
  int nText;
  char const *zText1;
  char const *zText2;
  char const *zText3;


  zText1 = sqlite4_value_text(argv[0], &nText);
  zText2 = sqlite4_value_text(argv[0], 0);
  zText3 = sqlite4_value_text(argv[0], 0);

  if( zText1!=zText2 || zText2!=zText3 ){
    sqlite4_result_error(context, "tkt2213 is not fixed", -1);
  }else{
    char *zCopy = (char *)sqlite4_malloc(sqlite4_context_env(context),nText);
    memcpy(zCopy, zText1, nText);
    sqlite4_result_text(context, zCopy, nText, SQLITE4_DYNAMIC, 0);
  }
}












































































/*
** Usage:  sqlite_test_create_function DB
**
** Call the sqlite4_create_function API on the given database in order
** to create a function named "x_coalesce".  This function does the same thing
** as the "coalesce" function.  This function also registers an SQL function
................................................................................
          hex16Func, 0, 0);
  }
#endif
  if( rc==SQLITE4_OK ){
    rc = sqlite4_create_function(db, "tkt2213func", 1, SQLITE4_ANY, 0, 
          tkt2213Function, 0, 0);
  }





#ifndef SQLITE4_OMIT_UTF16
  /* Use the sqlite4_create_function16() API here. Mainly for fun, but also 
  ** because it is not tested anywhere else. */
  if( rc==SQLITE4_OK ){
    const void *zUtf16;
    sqlite4_value *pVal;
................................................................................

/*
** The following routine is a user-defined SQL function whose purpose
** is to test the sqlite_set_result() API.
*/
static void testFunc(sqlite4_context *context, int argc, sqlite4_value **argv){
  while( argc>=2 ){
    const char *zArg0 = (char*)sqlite4_value_text(argv[0], 0);
    if( zArg0 ){
      if( 0==sqlite4_stricmp(zArg0, "int") ){
        sqlite4_result_int(context, sqlite4_value_int(argv[1]));
      }else if( sqlite4_stricmp(zArg0,"int64")==0 ){
        sqlite4_result_int64(context, sqlite4_value_int64(argv[1]));
      }else if( sqlite4_stricmp(zArg0,"string")==0 ){
        sqlite4_result_text(context, (char*)sqlite4_value_text(argv[1], 0), -1,
            SQLITE4_TRANSIENT, 0);
      }else if( sqlite4_stricmp(zArg0,"double")==0 ){
        sqlite4_result_double(context, sqlite4_value_double(argv[1]));
      }else if( sqlite4_stricmp(zArg0,"null")==0 ){
        sqlite4_result_null(context);
      }else if( sqlite4_stricmp(zArg0,"value")==0 ){
        sqlite4_result_value(context, argv[sqlite4_value_int(argv[1])]);
................................................................................
    default:
      assert(0);
  }

  sqlite4BeginBenignMalloc(pEnv);
  pVal = sqlite4ValueNew(0);
  if( pVal ){
    const char *z;
    sqlite4ValueSetStr(pVal, nA, zA, encin, SQLITE4_STATIC, 0);
    z = sqlite4_value_text(pVal, &n);
    Tcl_ListObjAppendElement(i,pX, Tcl_NewStringObj(z, n));

    sqlite4ValueSetStr(pVal, nB, zB, encin, SQLITE4_STATIC, 0);
    z = sqlite4_value_text(pVal, &n);
    Tcl_ListObjAppendElement(i,pX, Tcl_NewStringObj(z, n));

    sqlite4ValueFree(pVal);
  }
  sqlite4EndBenignMalloc(pEnv);

  Tcl_EvalObjEx(i, pX, 0);
  Tcl_DecrRefCount(pX);
  Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
................................................................................
  Tcl_Obj *pX;
  sqlite4_value *pVal;
  interp = (Tcl_Interp *)sqlite4_user_data(pCtx);
  pX = Tcl_NewStringObj("test_function", -1);
  Tcl_IncrRefCount(pX);
  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
  Tcl_ListObjAppendElement(interp, pX, 
      Tcl_NewStringObj((char*)sqlite4_value_text(argv[0], 0), -1));
  Tcl_EvalObjEx(interp, pX, 0);
  Tcl_DecrRefCount(pX);
  sqlite4_result_text(pCtx, Tcl_GetStringResult(interp), -1,
                      SQLITE4_TRANSIENT, 0);
  pVal = sqlite4ValueNew(0);
  sqlite4ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
      SQLITE4_UTF8, SQLITE4_STATIC, 0);
  sqlite4_result_text16be(pCtx, sqlite4_value_text16be(pVal, 0),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4ValueFree(pVal);
}
static void test_function_utf16le(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
................................................................................
  Tcl_Obj *pX;
  sqlite4_value *pVal;
  interp = (Tcl_Interp *)sqlite4_user_data(pCtx);
  pX = Tcl_NewStringObj("test_function", -1);
  Tcl_IncrRefCount(pX);
  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
  Tcl_ListObjAppendElement(interp, pX, 
      Tcl_NewStringObj(sqlite4_value_text(argv[0], 0), -1));
  Tcl_EvalObjEx(interp, pX, 0);
  Tcl_DecrRefCount(pX);
  pVal = sqlite4ValueNew(0);
  sqlite4ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
      SQLITE4_UTF8, SQLITE4_STATIC, 0);
  sqlite4_result_text(pCtx, sqlite4_value_text(pVal, 0), -1,
                      SQLITE4_TRANSIENT, 0);
  sqlite4ValueFree(pVal);
}
static void test_function_utf16be(
  sqlite4_context *pCtx, 
  int nArg,
  sqlite4_value **argv
................................................................................
  Tcl_Obj *pX;
  sqlite4_value *pVal;
  interp = (Tcl_Interp *)sqlite4_user_data(pCtx);
  pX = Tcl_NewStringObj("test_function", -1);
  Tcl_IncrRefCount(pX);
  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
  Tcl_ListObjAppendElement(interp, pX, 
      Tcl_NewStringObj(sqlite4_value_text(argv[0], 0), -1));
  Tcl_EvalObjEx(interp, pX, 0);
  Tcl_DecrRefCount(pX);
  pVal = sqlite4ValueNew(0);
  sqlite4ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
      SQLITE4_UTF8, SQLITE4_STATIC, 0);
  sqlite4_result_text16(pCtx, sqlite4_value_text16le(pVal, 0),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4_result_text16be(pCtx, sqlite4_value_text16le(pVal, 0),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4_result_text16le(pCtx, sqlite4_value_text16le(pVal, 0),
      -1, SQLITE4_TRANSIENT, 0);
  sqlite4ValueFree(pVal);
}
#endif /* SQLITE4_OMIT_UTF16 */
static int test_function(
  void * clientData,
  Tcl_Interp *interp,
................................................................................
       Tcl_GetString(objv[0]), " filename options-list", 0);
    return TCL_ERROR;
  }

  zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
  rc = sqlite4_open(0, zFilename, &db, 0);
  
  if( rc!=SQLITE4_OK 
   || sqlite4TestMakePointerStr(interp, zBuf, db) 
  ){
    return TCL_ERROR;
  }
  Tcl_AppendResult(interp, zBuf, 0);
  return TCL_OK;
}

/*
** Usage: sqlite4_open_v2 FILENAME FLAGS VFS
*/
................................................................................
       Tcl_GetString(objv[0]), " STMT column", 0);
    return TCL_ERROR;
  }

  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;


  pBlob = sqlite4_column_blob(pStmt, col, &len);
  Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
  return TCL_OK;
}

/*
** Usage: sqlite4_column_double STMT column
**
................................................................................
  void * clientData,        /* Pointer to SQLite API function to be invoke */
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_stmt *pStmt;
  int col;
  const char *(*xFunc)(sqlite4_stmt*, int, int *);
  const char *zRet;

  xFunc = (const char *(*)(sqlite4_stmt*, int, int*))clientData;
  if( objc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", 
       Tcl_GetString(objv[0]), " STMT column", 0);
    return TCL_ERROR;
  }

  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  zRet = xFunc(pStmt, col, 0);
  if( zRet ){
    Tcl_SetResult(interp, (char *)zRet, 0);
  }
  return TCL_OK;
}

/*
................................................................................
#endif /* SQLITE4_OMIT_UTF16 */

  return TCL_OK;
}

/*
** Usage: sqlite4_column_int STMT column





*/
static int test_stmt_int(
  void * clientData,    /* Pointer to SQLite API function to be invoked */
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
................................................................................
  sqlite4_free(0, zExplain);
  if( rc!=SQLITE4_OK ) return rc;

  while( SQLITE4_ROW==sqlite4_step(pExplain) ){
    int iSelectid = sqlite4_column_int(pExplain, 0);
    int iOrder = sqlite4_column_int(pExplain, 1);
    int iFrom = sqlite4_column_int(pExplain, 2);
    const char *zDetail = (const char *)sqlite4_column_text(pExplain, 3, 0);

    printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
  }

  return sqlite4_finalize(pExplain);
}

................................................................................
     { "sqlite4_column_type",           test_column_type   ,0 },
     { "sqlite4_column_blob",           test_column_blob   ,0 },
     { "sqlite4_column_double",         test_column_double ,0 },
     { "sqlite4_column_int64",          test_column_int64  ,0 },
     { "sqlite4_column_text",   test_stmt_utf8,  (void*)sqlite4_column_text },
     { "sqlite4_column_name",   test_stmt_utf8,  (void*)sqlite4_column_name },
     { "sqlite4_column_int",    test_stmt_int,   (void*)sqlite4_column_int  },

#ifndef SQLITE4_OMIT_DECLTYPE
     { "sqlite4_column_decltype",test_stmt_utf8,(void*)sqlite4_column_decltype},
#endif
#ifdef SQLITE4_ENABLE_COLUMN_METADATA
{ "sqlite4_column_database_name",test_stmt_utf8,(void*)sqlite4_column_database_name},
{ "sqlite4_column_table_name",test_stmt_utf8,(void*)sqlite4_column_table_name},
{ "sqlite4_column_origin_name",test_stmt_utf8,(void*)sqlite4_column_origin_name},
#endif

#ifndef SQLITE4_OMIT_UTF16

     { "sqlite4_column_text16",  test_stmt_utf16, (void*)sqlite4_column_text16},
     { "sqlite4_column_name16",  test_stmt_utf16, (void*)sqlite4_column_name16},
     { "add_alignment_test_collations", add_alignment_test_collations, 0      },
#ifndef SQLITE4_OMIT_DECLTYPE
     { "sqlite4_column_decltype16",test_stmt_utf16,(void*)sqlite4_column_decltype16},
#endif
#ifdef SQLITE4_ENABLE_COLUMN_METADATA

Changes to test/test_utf.c.

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  val.flags = MEM_Str|MEM_Term|MEM_Static;
  val.z = "hello world";
  val.type = SQLITE4_TEXT;
  val.enc = SQLITE4_UTF8;

  for(i=0; i<repeat_count; i++){
    if( do_calls ){
      zVal = (char*)sqlite4_value_text(&val);
    }
  }

  return TCL_OK;
}

static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){







|







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  val.flags = MEM_Str|MEM_Term|MEM_Static;
  val.z = "hello world";
  val.type = SQLITE4_TEXT;
  val.enc = SQLITE4_UTF8;

  for(i=0; i<repeat_count; i++){
    if( do_calls ){
      zVal = sqlite4_value_text(&val, 0);
    }
  }

  return TCL_OK;
}

static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){