SQLite4
Check-in [7f314c9a71]
Not logged in

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

Overview
Comment:Remove the encoding argument from sqlite4_create_collation(). Pass sqlite4_value objects to the collation sequence callbacks instead.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7f314c9a714cb5265da0b021ca6645e92a696e57
User & Date: dan 2013-06-13 20:20:43
Context
2013-06-14
17:16
Update create_collation() documentation. Have the create_collation() function invoke the destructor (if one was specified) before returning if it fails. check-in: bcf7a78f8b user: dan tags: trunk
2013-06-13
20:20
Remove the encoding argument from sqlite4_create_collation(). Pass sqlite4_value objects to the collation sequence callbacks instead. check-in: 7f314c9a71 user: dan tags: trunk
16:32
Remove the no longer required FuncDestructor object. check-in: e5d82c92f0 user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/build.c.

1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
....
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
** invokes the collation factory if the named collation cannot be found
** and generates an error message.
**
** See also: sqlite4FindCollSeq(), sqlite4GetCollSeq()
*/
CollSeq *sqlite4LocateCollSeq(Parse *pParse, const char *zName){
  sqlite4 *db = pParse->db;
  u8 enc = ENC(db);
  u8 initbusy = db->init.busy;
  CollSeq *pColl;

  pColl = sqlite4FindCollSeq(db, enc, zName, initbusy);
  if( !initbusy && (!pColl || !pColl->xCmp) ){
    pColl = sqlite4GetCollSeq(db, enc, pColl, zName);
    if( !pColl ){
      sqlite4ErrorMsg(pParse, "no such collation sequence: %s", zName);
    }
  }

  return pColl;
}
................................................................................
    reindexDatabases(pParse, 0);
    return;
  }else if( NEVER(pName2==0) || pName2->z==0 ){
    char *zColl;
    assert( pName1->z );
    zColl = sqlite4NameFromToken(pParse->db, pName1);
    if( !zColl ) return;
    pColl = sqlite4FindCollSeq(db, ENC(db), zColl, 0);
    if( pColl ){
      reindexDatabases(pParse, zColl);
      sqlite4DbFree(db, zColl);
      return;
    }
    sqlite4DbFree(db, zColl);
  }







<



|

|







 







|







1173
1174
1175
1176
1177
1178
1179

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
....
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
** invokes the collation factory if the named collation cannot be found
** and generates an error message.
**
** See also: sqlite4FindCollSeq(), sqlite4GetCollSeq()
*/
CollSeq *sqlite4LocateCollSeq(Parse *pParse, const char *zName){
  sqlite4 *db = pParse->db;

  u8 initbusy = db->init.busy;
  CollSeq *pColl;

  pColl = sqlite4FindCollSeq(db, zName, initbusy);
  if( !initbusy && (!pColl || !pColl->xCmp) ){
    pColl = sqlite4GetCollSeq(db, pColl, zName);
    if( !pColl ){
      sqlite4ErrorMsg(pParse, "no such collation sequence: %s", zName);
    }
  }

  return pColl;
}
................................................................................
    reindexDatabases(pParse, 0);
    return;
  }else if( NEVER(pName2==0) || pName2->z==0 ){
    char *zColl;
    assert( pName1->z );
    zColl = sqlite4NameFromToken(pParse->db, pName1);
    if( !zColl ) return;
    pColl = sqlite4FindCollSeq(db, zColl, 0);
    if( pColl ){
      reindexDatabases(pParse, zColl);
      sqlite4DbFree(db, zColl);
      return;
    }
    sqlite4DbFree(db, zColl);
  }

Changes to src/callback.c.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
..
77
78
79
80
81
82
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
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
...
134
135
136
137
138
139
140
141
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
168
169
170
171

172
173
174
175


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

#include "sqliteInt.h"

/*
** Invoke the 'collation needed' callback to request a collation sequence
** in the encoding enc of name zName, length nName.
*/
static void callCollNeeded(sqlite4 *db, int enc, const char *zName){
  assert( !db->xCollNeeded || !db->xCollNeeded16 );
  if( db->xCollNeeded ){
    char *zExternal = sqlite4DbStrDup(db, zName);
    if( !zExternal ) return;
    db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
    sqlite4DbFree(db, zExternal);
  }
#ifndef SQLITE4_OMIT_UTF16
  if( db->xCollNeeded16 ){
    char const *zExternal;
    sqlite4_value *pTmp = sqlite4ValueNew(db);
    sqlite4ValueSetStr(pTmp, -1, zName, SQLITE4_UTF8, SQLITE4_STATIC, 0);
    zExternal = sqlite4ValueText(pTmp, SQLITE4_UTF16NATIVE);
    if( zExternal ){
      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
    }
    sqlite4ValueFree(pTmp);
  }
#endif
}

/*
** This routine is called if the collation factory fails to deliver a
** collation function in the best encoding but there may be other versions
** of this collation function (for other text encodings) available. Use one
** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
** possible.
*/
static int synthCollSeq(sqlite4 *db, CollSeq *pColl){
  CollSeq *pColl2;
  char *z = pColl->zName;
  int i;
  static const u8 aEnc[] = { SQLITE4_UTF16BE, SQLITE4_UTF16LE, SQLITE4_UTF8 };
  for(i=0; i<3; i++){
    pColl2 = sqlite4FindCollSeq(db, aEnc[i], z, 0);
    if( pColl2->xCmp!=0 ){
      memcpy(pColl, pColl2, sizeof(CollSeq));
      pColl->xDel = 0;         /* Do not copy the destructor */
      return SQLITE4_OK;
    }
  }
  return SQLITE4_ERROR;
}

/*
** This function is responsible for invoking the collation factory callback
** or substituting a collation sequence of a different encoding when the
** requested collation sequence is not available in the desired encoding.
** 
................................................................................
** db for collation type name zName, length nName, or NULL, if no collation
** sequence can be found.
**
** See also: sqlite4LocateCollSeq(), sqlite4FindCollSeq()
*/
CollSeq *sqlite4GetCollSeq(
  sqlite4* db,          /* The database connection */
  u8 enc,               /* The desired encoding for the collating sequence */
  CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
  const char *zName     /* Collating sequence name */
){
  CollSeq *p;

  p = pColl;
  if( !p ){
    p = sqlite4FindCollSeq(db, enc, zName, 0);
  }
  if( !p || !p->xCmp ){
    /* No collation sequence of this type for this encoding is registered.
    ** Call the collation factory to see if it can supply us with one.
    */
    callCollNeeded(db, enc, zName);
    p = sqlite4FindCollSeq(db, enc, zName, 0);
  }
  if( p && !p->xCmp && synthCollSeq(db, p) ){
    p = 0;
  }
  assert( !p || p->xCmp );
  return p;
}

/*
** This routine is called on a collation sequence before it is used to
** check that it is defined. An undefined collation sequence exists when
................................................................................
** an equivalent collating sequence that uses a text encoding different
** from the main database is substituted, if one is available.
*/
int sqlite4CheckCollSeq(Parse *pParse, CollSeq *pColl){
  if( pColl ){
    const char *zName = pColl->zName;
    sqlite4 *db = pParse->db;
    CollSeq *p = sqlite4GetCollSeq(db, ENC(db), pColl, zName);
    if( !p ){
      sqlite4ErrorMsg(pParse, "no such collation sequence: %s", zName);
      pParse->nErr++;
      return SQLITE4_ERROR;
    }
    assert( p==pColl );
  }
................................................................................


/*
** Locate and return an entry from the db.aCollSeq hash table. If the entry
** specified by zName and nName is not found and parameter 'create' is
** true, then create a new entry. Otherwise return NULL.
**
** Each pointer stored in the sqlite4.aCollSeq hash table contains an
** array of three CollSeq structures. The first is the collation sequence
** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
**
** Stored immediately after the three collation sequences is a copy of
** the collation sequence name. A pointer to this string is stored in
** each collation sequence structure.

*/
static CollSeq *findCollSeqEntry(
  sqlite4 *db,          /* Database connection */
  const char *zName,    /* Name of the collating sequence */
  int create            /* Create a new entry if true */
){
  CollSeq *pColl;
  int nName = sqlite4Strlen30(zName);
  pColl = sqlite4HashFind(&db->aCollSeq, zName, nName);

  if( 0==pColl && create ){


    pColl = sqlite4DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
    if( pColl ){
      CollSeq *pDel = 0;
      pColl[0].zName = (char*)&pColl[3];
      pColl[0].enc = SQLITE4_UTF8;
      pColl[1].zName = (char*)&pColl[3];
      pColl[1].enc = SQLITE4_UTF16LE;
      pColl[2].zName = (char*)&pColl[3];
      pColl[2].enc = SQLITE4_UTF16BE;
      memcpy(pColl[0].zName, zName, nName);
      pColl[0].zName[nName] = 0;
      pDel = sqlite4HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);


      /* If a malloc() failure occurred in sqlite4HashInsert(), it will 
      ** return the pColl pointer to be deleted (because it wasn't added
      ** to the hash table).
      */


      assert( pDel==0 || pDel==pColl );
      if( pDel!=0 ){
        db->mallocFailed = 1;
        sqlite4DbFree(db, pDel);
        pColl = 0;
      }
    }
  }
  return pColl;
}

/*
** Parameter zName points to a UTF-8 encoded string nName bytes long.
** Return the CollSeq* pointer for the collation sequence named zName
** for the encoding 'enc' from the database 'db'.
**
** If the entry specified is not found and 'create' is true, then create a
** new entry.  Otherwise return NULL.
**
** A separate function sqlite4LocateCollSeq() is a wrapper around
** this routine.  sqlite4LocateCollSeq() invokes the collation factory
** if necessary and generates an error message if the collating sequence
** cannot be found.
**
** See also: sqlite4LocateCollSeq(), sqlite4GetCollSeq()
*/
CollSeq *sqlite4FindCollSeq(
  sqlite4 *db,
  u8 enc,
  const char *zName,
  int create
){
  CollSeq *pColl;
  if( zName ){
    pColl = findCollSeqEntry(db, zName, create);
  }else{
    pColl = db->pDfltColl;
  }
  assert( SQLITE4_UTF8==1 && SQLITE4_UTF16LE==2 && SQLITE4_UTF16BE==3 );
  assert( enc>=SQLITE4_UTF8 && enc<=SQLITE4_UTF16BE );
  if( pColl ) pColl += enc-1;
  return pColl;
}

/* During the search for the best function definition, this procedure
** is called to test how well the function passed as the first argument
** matches the request for a function with nArg arguments in a system
** that uses encoding enc. The value returned indicates how well the







|
<



|


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







 







<
|






|



|
<
|
|

|
<
|







 







|







 







<
<
<
<
|

<
>











>
>
|

<
|
<
<
<
<
<
|
<
<
|
>
|
<
<
<
>
>
|
|
|
|
|
|
|
|





|
<













<









<
<
<







16
17
18
19
20
21
22
23

24
25
26
27
28
29



































30
31
32
33
34
35
36
..
41
42
43
44
45
46
47

48
49
50
51
52
53
54
55
56
57
58
59

60
61
62
63

64
65
66
67
68
69
70
71
..
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
..
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
146
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162



163
164
165
166
167
168
169

#include "sqliteInt.h"

/*
** Invoke the 'collation needed' callback to request a collation sequence
** in the encoding enc of name zName, length nName.
*/
static void callCollNeeded(sqlite4 *db, const char *zName){

  if( db->xCollNeeded ){
    char *zExternal = sqlite4DbStrDup(db, zName);
    if( !zExternal ) return;
    db->xCollNeeded(db->pCollNeededArg, db, zExternal);
    sqlite4DbFree(db, zExternal);
  }



































}

/*
** This function is responsible for invoking the collation factory callback
** or substituting a collation sequence of a different encoding when the
** requested collation sequence is not available in the desired encoding.
** 
................................................................................
** db for collation type name zName, length nName, or NULL, if no collation
** sequence can be found.
**
** See also: sqlite4LocateCollSeq(), sqlite4FindCollSeq()
*/
CollSeq *sqlite4GetCollSeq(
  sqlite4* db,          /* The database connection */

  CollSeq *pColl,       /* Collating sequence object, or NULL */
  const char *zName     /* Collating sequence name */
){
  CollSeq *p;

  p = pColl;
  if( !p ){
    p = sqlite4FindCollSeq(db, zName, 0);
  }
  if( !p || !p->xCmp ){
    /* No collation sequence of this type for this encoding is registered.
    ** Call the collation factory to see if it can supply us with one. */

    callCollNeeded(db, zName);
    p = sqlite4FindCollSeq(db, zName, 0);
  }
  if( p && !p->xCmp ) p = 0;


  assert( !p || p->xCmp );
  return p;
}

/*
** This routine is called on a collation sequence before it is used to
** check that it is defined. An undefined collation sequence exists when
................................................................................
** an equivalent collating sequence that uses a text encoding different
** from the main database is substituted, if one is available.
*/
int sqlite4CheckCollSeq(Parse *pParse, CollSeq *pColl){
  if( pColl ){
    const char *zName = pColl->zName;
    sqlite4 *db = pParse->db;
    CollSeq *p = sqlite4GetCollSeq(db, pColl, zName);
    if( !p ){
      sqlite4ErrorMsg(pParse, "no such collation sequence: %s", zName);
      pParse->nErr++;
      return SQLITE4_ERROR;
    }
    assert( p==pColl );
  }
................................................................................


/*
** Locate and return an entry from the db.aCollSeq hash table. If the entry
** specified by zName and nName is not found and parameter 'create' is
** true, then create a new entry. Otherwise return NULL.
**




** Stored immediately after the CollSeq object in memory is a copy of
** the collation sequence name. A pointer to this string is stored in

** CollSeq.zName.
*/
static CollSeq *findCollSeqEntry(
  sqlite4 *db,          /* Database connection */
  const char *zName,    /* Name of the collating sequence */
  int create            /* Create a new entry if true */
){
  CollSeq *pColl;
  int nName = sqlite4Strlen30(zName);
  pColl = sqlite4HashFind(&db->aCollSeq, zName, nName);

  if( 0==pColl && create ){
    CollSeq *pDel;

    pColl = (CollSeq*)sqlite4DbMallocZero(db, sizeof(CollSeq)+nName+1);
    if( pColl ){

      pColl->zName = (char *)&pColl[1];





      memcpy(pColl->zName, zName, nName);


    }
      
    /* If a malloc() failure occurs in sqlite4HashInsert(), a copy of 



    ** pColl is returned. Free it if this happens. */
    pDel = sqlite4HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
    assert( pDel==0 || pDel==pColl );
    if( pDel!=0 ){
      db->mallocFailed = 1;
      sqlite4DbFree(db, pDel);
      pColl = 0;
    }
  }

  return pColl;
}

/*
** Parameter zName points to a UTF-8 encoded string nName bytes long.
** Return the CollSeq* pointer for the collation sequence named zName.

**
** If the entry specified is not found and 'create' is true, then create a
** new entry.  Otherwise return NULL.
**
** A separate function sqlite4LocateCollSeq() is a wrapper around
** this routine.  sqlite4LocateCollSeq() invokes the collation factory
** if necessary and generates an error message if the collating sequence
** cannot be found.
**
** See also: sqlite4LocateCollSeq(), sqlite4GetCollSeq()
*/
CollSeq *sqlite4FindCollSeq(
  sqlite4 *db,

  const char *zName,
  int create
){
  CollSeq *pColl;
  if( zName ){
    pColl = findCollSeqEntry(db, zName, create);
  }else{
    pColl = db->pDfltColl;
  }



  return pColl;
}

/* During the search for the best function definition, this procedure
** is called to test how well the function passed as the first argument
** matches the request for a function with nArg arguments in a system
** that uses encoding enc. The value returned indicates how well the

Changes to src/expr.c.

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
....
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
      ** a TK_COLUMN but was previously evaluated and cached in a register */
      const char *zColl;
      int j = p->iColumn;
      if( j>=0 ){
        sqlite4 *db = pParse->db;
        zColl = p->pTab->aCol[j].zColl;
        pColl = sqlite4FindCollSeq(db, ENC(db), zColl, 0);
        pExpr->pColl = pColl;
      }
      break;
    }
    if( op!=TK_CAST && op!=TK_UPLUS ){
      break;
    }
................................................................................
    ** comparison is the same as the affinity of the column. If
    ** it is not, it is not possible to use any index.  */
    aff = comparisonAffinity(pX);
    if( aff!=SQLITE4_AFF_NONE && aff!=pTab->aCol[iCol].affinity ) return 0;

    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
      if( (pIdx->aiColumn[0]==iCol)
       && sqlite4FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
       && (!bReqUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
      ){
        break;
      }
    }
  }








|







 







|







103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
....
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
      ** a TK_COLUMN but was previously evaluated and cached in a register */
      const char *zColl;
      int j = p->iColumn;
      if( j>=0 ){
        sqlite4 *db = pParse->db;
        zColl = p->pTab->aCol[j].zColl;
        pColl = sqlite4FindCollSeq(db, zColl, 0);
        pExpr->pColl = pColl;
      }
      break;
    }
    if( op!=TK_CAST && op!=TK_UPLUS ){
      break;
    }
................................................................................
    ** comparison is the same as the affinity of the column. If
    ** it is not, it is not possible to use any index.  */
    aff = comparisonAffinity(pX);
    if( aff!=SQLITE4_AFF_NONE && aff!=pTab->aCol[iCol].affinity ) return 0;

    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
      if( (pIdx->aiColumn[0]==iCol)
       && sqlite4FindCollSeq(db, pIdx->azColl[0], 0)==pReq
       && (!bReqUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
      ){
        break;
      }
    }
  }

Changes to src/main.c.

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

248



249
250
251
252
253
254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273




274
275
276
277


278
279
280
281
282
283
284
285

286


287
288
289
290
291
292
293
294


295
296
297
298
299
300
301
302
...
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
...
780
781
782
783
784
785
786
787
788
789
790
791
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
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
....
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236

1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
....
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374

1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
}

/*
** This is the default collating function named "BINARY" which is always
** available.
**
** If the padFlag argument is not NULL then space padding at the end
** of strings is ignored.  This implements the RTRIM collation.
*/
static int binCollFunc(
  void *padFlag,
  int nKey1, const void *pKey1,
  int nKey2, const void *pKey2,
  int *pRes
){
  int res, n;
  n = nKey1<nKey2 ? nKey1 : nKey2;
  res = memcmp(pKey1, pKey2, n);
  if( res==0 ){
    if( padFlag
     && allSpaces(((char*)pKey1)+n, nKey1-n)
     && allSpaces(((char*)pKey2)+n, nKey2-n)
    ){
      /* Leave res unchanged at 0 */
    }else{
      res = nKey1 - nKey2;



    }
  }
  *pRes = res;
  return SQLITE4_OK;
}

/*
** The xMakeKey callback for the built-in RTRIM collation. The output
** is the same as the input, with any trailing ' ' characters removed.
** (e.g.  " abc   "  ->   " abc").
*/
static int collRtrimMkKey(
  void *NotUsed,                  /* Not used */
  int nIn, const void *pIn,       /* Input text. UTF-8. */
  int nOut, void *pOut            /* Output buffer */

){



  int nCopy = nIn;
  while( nCopy>0 && ((const char *)pIn)[nCopy-1]==' ' ) nCopy--;
  if( nCopy<=nOut ){
    memcpy(pOut, pIn, nCopy);
  }
  return nCopy;

}

/*
** Another built-in collating sequence: NOCASE. 
**
** This collating sequence is intended to be used for "case independant
** comparison". SQLite's knowledge of upper and lower case equivalents
** extends only to the 26 characters used in the English language.
**
** At the moment there is only a UTF-8 implementation.
*/
static int collNocaseCmp(
  void *NotUsed,
  int nKey1, const void *pKey1,
  int nKey2, const void *pKey2,
  int *pRes
){
  int r = sqlite4_strnicmp(
      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);




  UNUSED_PARAMETER(NotUsed);
  if( 0==r ){
    r = nKey1-nKey2;
  }


  *pRes = r;
  return SQLITE4_OK;
}

static int collNocaseMkKey(
  void *NotUsed,
  int nIn, const void *pKey1,
  int nOut, void *pKey2

){


  if( nOut>=nIn ){
    int i;
    u8 *aIn = (u8 *)pKey1;
    u8 *aOut = (u8 *)pKey2;
    for(i=0; i<nIn; i++){
      aOut[i] = sqlite4_tolower(aIn[i]);
    }
  }


  return nIn;
}

/*
** Return the number of changes in the most recent call to sqlite4_exec().
*/
int sqlite4_changes(sqlite4 *db){
  return db->nChange;
................................................................................
        sqlite4DbFree(db, p);
        p = pSame;
      }
    }
  }
  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
    /* Invoke any destructors registered for collation sequence user data. */
    for(j=0; j<3; j++){
      if( pColl[j].xDel ){
        pColl[j].xDel(pColl[j].pUser);
      }
    }
    sqlite4DbFree(db, pColl);
  }
  sqlite4HashClear(&db->aCollSeq);
#ifndef SQLITE4_OMIT_VIRTUALTABLE
  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
    Module *pMod = (Module *)sqliteHashData(i);
    if( pMod->xDestroy ){
................................................................................
  if( !db || db->mallocFailed ){
    return SQLITE4_NOMEM;
  }
  return db->errCode;
}

/*
** Create a new collating function for database "db".  The name is zName
** and the encoding is enc.
*/
static int createCollation(
  sqlite4* db,
  const char *zName, 
  u8 enc,
  void* pCtx,
  int(*xCompare)(void*,int,const void*,int,const void*,int*),

  int(*xMakeKey)(void*,int,const void*,int,void*),
  void(*xDel)(void*)
){
  CollSeq *pColl;
  int enc2;
  int nName = sqlite4Strlen30(zName);
  
  assert( sqlite4_mutex_held(db->mutex) );

  /* If SQLITE4_UTF16 is specified as the encoding type, transform this
  ** to one of SQLITE4_UTF16LE or SQLITE4_UTF16BE using the
  ** SQLITE4_UTF16NATIVE macro. SQLITE4_UTF16 is not used internally.
  */
  enc2 = enc;
  testcase( enc2==SQLITE4_UTF16 );
  testcase( enc2==SQLITE4_UTF16_ALIGNED );
  if( enc2==SQLITE4_UTF16 || enc2==SQLITE4_UTF16_ALIGNED ){
    enc2 = SQLITE4_UTF16NATIVE;
  }
  if( enc2<SQLITE4_UTF8 || enc2>SQLITE4_UTF16BE ){
    return SQLITE4_MISUSE_BKPT;
  }

  /* Check if this call is removing or replacing an existing collation 
  ** sequence. If so, and there are active VMs, return busy. If there
  ** are no active VMs, invalidate any pre-compiled statements.
  */
  pColl = sqlite4FindCollSeq(db, (u8)enc2, zName, 0);
  if( pColl && pColl->xCmp ){
    if( db->activeVdbeCnt ){
      sqlite4Error(db, SQLITE4_BUSY, 
        "unable to delete/modify collation sequence due to active statements");
      return SQLITE4_BUSY;
    }
    sqlite4ExpirePreparedStatements(db);

    /* If collation sequence pColl was created directly by a call to
    ** sqlite4_create_collation, and not generated by synthCollSeq(),
    ** then any copies made by synthCollSeq() need to be invalidated.
    ** Also, collation destructor - CollSeq.xDel() - function may need
    ** to be called.
    */ 
    if( (pColl->enc & ~SQLITE4_UTF16_ALIGNED)==enc2 ){
      CollSeq *aColl = sqlite4HashFind(&db->aCollSeq, zName, nName);
      int j;
      for(j=0; j<3; j++){
        CollSeq *p = &aColl[j];
        if( p->enc==pColl->enc ){
          if( p->xDel ){
            p->xDel(p->pUser);
          }

          p->xCmp = 0;


        }
      }
    }
  }

  pColl = sqlite4FindCollSeq(db, (u8)enc2, zName, 1);
  if( pColl==0 ) return SQLITE4_NOMEM;
  pColl->xCmp = xCompare;
  pColl->xMkKey = xMakeKey;
  pColl->pUser = pCtx;
  pColl->xDel = xDel;
  pColl->enc = (u8)(enc2 | (enc & SQLITE4_UTF16_ALIGNED));
  sqlite4Error(db, SQLITE4_OK, 0);
  return SQLITE4_OK;
}


/*
** This array defines hard upper bounds on limit values.  The
** initializer must be kept in sync with the SQLITE4_LIMIT_*
................................................................................
            ;

  sqlite4HashInit(pEnv, &db->aCollSeq, 0);
#ifndef SQLITE4_OMIT_VIRTUALTABLE
  sqlite4HashInit(pEnv, &db->aModule, 0);
#endif

  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
  ** and UTF-16, so add a version for each to avoid any unnecessary
  ** conversions. The only error that can occur here is a malloc() failure.
  */
  createCollation(db, "BINARY", SQLITE4_UTF8, 0, binCollFunc, 0, 0);
  createCollation(db, "BINARY", SQLITE4_UTF16BE, 0, binCollFunc, 0, 0);
  createCollation(db, "BINARY", SQLITE4_UTF16LE, 0, binCollFunc, 0, 0);
  createCollation(
      db, "RTRIM", SQLITE4_UTF8, (void*)1, binCollFunc, collRtrimMkKey, 0
  );

  if( db->mallocFailed ){
    goto opendb_out;
  }
  db->pDfltColl = sqlite4FindCollSeq(db, SQLITE4_UTF8, "BINARY", 0);
  assert( db->pDfltColl!=0 );

  /* Also add a UTF-8 case-insensitive collation sequence. */
  createCollation(db, 
      "NOCASE", SQLITE4_UTF8, 0, collNocaseCmp, collNocaseMkKey, 0
  );

  /* Parse the filename/URI argument. */
  rc = sqlite4ParseUri(pEnv, zFilename, &flags, &zOpen, &zErrMsg);
  if( rc!=SQLITE4_OK ){
    if( rc==SQLITE4_NOMEM ) db->mallocFailed = 1;
    sqlite4Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
    sqlite4_free(pEnv, zErrMsg);
    goto opendb_out;
................................................................................

/*
** Register a new collation sequence with the database handle db.
*/
int sqlite4_create_collation(
  sqlite4* db, 
  const char *zName, 
  int enc, 
  void* pCtx,
  int(*xCompare)(void*,int,const void*,int,const void*,int*),

  int(*xMakeKey)(void*,int,const void*,int,void*),
  void(*xDel)(void*)
){
  int rc;
  sqlite4_mutex_enter(db->mutex);
  assert( !db->mallocFailed );
  rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xMakeKey, xDel);
  rc = sqlite4ApiExit(db, rc);
  sqlite4_mutex_leave(db->mutex);
  return rc;
}

/*
** Register a collation sequence factory callback with the database handle
** db. Replace any previously installed collation sequence factory.
*/
int sqlite4_collation_needed(
  sqlite4 *db, 
  void(*xCollNeeded)(void*,sqlite4*,int eTextRep,const char*),
  void *pCollNeededArg
){
  sqlite4_mutex_enter(db->mutex);
  db->xCollNeeded = xCollNeeded;
  db->xCollNeeded16 = 0;
  db->pCollNeededArg = pCollNeededArg;
  sqlite4_mutex_leave(db->mutex);
  return SQLITE4_OK;
}

/*
** Test to see whether or not the database connection is currently within







|



|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>













|
|
>

>
>
>

|

|

|
>













|
|


|
|
>
>
>
>

<
<
|
>
>
|





|
|
>

>
>


|
|




>
>
|







 







|
<
<
<
<
<







 







|
<




<
|
<
>
|



<
<



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




|








<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
>
|
>
>
|
|
<
<
<
|





<
<







 







|
<
<
<
|
<
<
<
|
<
>



|


<
<
<
<
<







 







<

<
>
|





|











|




<







207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
...
425
426
427
428
429
430
431
432





433
434
435
436
437
438
439
...
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
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
....
1200
1201
1202
1203
1204
1205
1206
1207



1208



1209

1210
1211
1212
1213
1214
1215
1216





1217
1218
1219
1220
1221
1222
1223
....
1334
1335
1336
1337
1338
1339
1340

1341

1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365

1366
1367
1368
1369
1370
1371
1372
}

/*
** This is the default collating function named "BINARY" which is always
** available.
**
** If the padFlag argument is not NULL then space padding at the end
** of strings is ignored. This implements the RTRIM collation.
*/
static int binCollFunc(
  void *padFlag,
  sqlite4_value *p1,
  sqlite4_value *p2,
  int *pRes
){
  int n;
  int n1;
  int n2;
  int res;
  const char *z1 = sqlite4_value_text(p1, &n1);
  const char *z2 = sqlite4_value_text(p2, &n2);

  n = (n1<n2 ? n1 : n2);
  res = memcmp(z1, z2, n);
  if( res==0 ){
    if( padFlag && allSpaces(z1+n, n1-n) && allSpaces(z2+n, n2-n) ){
      /* Leave res unchanged at 0 */
    }else{
      res = n1 - n2;
    }
  }
  *pRes = res;
  return SQLITE4_OK;
}

/*
** The xMakeKey callback for the built-in RTRIM collation. The output
** is the same as the input, with any trailing ' ' characters removed.
** (e.g.  " abc   "  ->   " abc").
*/
static int collRtrimMkKey(
  void *NotUsed,                  /* Not used */
  sqlite4_value *pVal,            /* Text value to make a key of */
  int nOut, void *pOut,           /* Output buffer */
  int *pnOut
){
  int nIn;
  const char *zIn = sqlite4_value_text(pVal, &nIn);

  int nCopy = nIn;
  while( nCopy>0 && zIn[nCopy-1]==' ' ) nCopy--;
  if( nCopy<=nOut ){
    memcpy(pOut, zIn, nCopy);
  }
  *pnOut = nCopy;
  return SQLITE4_OK;
}

/*
** Another built-in collating sequence: NOCASE. 
**
** This collating sequence is intended to be used for "case independant
** comparison". SQLite's knowledge of upper and lower case equivalents
** extends only to the 26 characters used in the English language.
**
** At the moment there is only a UTF-8 implementation.
*/
static int collNocaseCmp(
  void *NotUsed,
  sqlite4_value *p1,
  sqlite4_value *p2,
  int *pRes
){
  int n1;
  int n2;
  int res;
  const char *z1 = sqlite4_value_text(p1, &n1);
  const char *z2 = sqlite4_value_text(p2, &n2);

  UNUSED_PARAMETER(NotUsed);



  res = sqlite4_strnicmp(z1, z2, (n1 < n2 ? n1 : n2));
  if( res==0 ) res = n1 - n2;
  *pRes = res;
  return SQLITE4_OK;
}

static int collNocaseMkKey(
  void *NotUsed,
  sqlite4_value *pVal,
  int nOut, void *pOut,
  int *pnOut
){
  int nIn;
  const char *zIn = sqlite4_value_text(pVal, &nIn);
  if( nOut>=nIn ){
    int i;
    u8 *aIn = (u8 *)zIn;
    u8 *aOut = (u8 *)pOut;
    for(i=0; i<nIn; i++){
      aOut[i] = sqlite4_tolower(aIn[i]);
    }
  }

  *pnOut = nIn;
  return SQLITE4_OK;
}

/*
** Return the number of changes in the most recent call to sqlite4_exec().
*/
int sqlite4_changes(sqlite4 *db){
  return db->nChange;
................................................................................
        sqlite4DbFree(db, p);
        p = pSame;
      }
    }
  }
  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
    if( pColl->xDel ) pColl->xDel(pColl->pUser);





    sqlite4DbFree(db, pColl);
  }
  sqlite4HashClear(&db->aCollSeq);
#ifndef SQLITE4_OMIT_VIRTUALTABLE
  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
    Module *pMod = (Module *)sqliteHashData(i);
    if( pMod->xDestroy ){
................................................................................
  if( !db || db->mallocFailed ){
    return SQLITE4_NOMEM;
  }
  return db->errCode;
}

/*
** Create a new collating function named zName for database db.

*/
static int createCollation(
  sqlite4* db,
  const char *zName, 

  void *pCtx,

  int(*xCompare)(void*, sqlite4_value*, sqlite4_value*, int*),
  int(*xMakeKey)(void*, sqlite4_value*, int, void*, int*),
  void(*xDel)(void*)
){
  CollSeq *pColl;


  
  assert( sqlite4_mutex_held(db->mutex) );















  /* Check if this call is removing or replacing an existing collation 
  ** sequence. If so, and there are active VMs, return busy. If there
  ** are no active VMs, invalidate any pre-compiled statements.
  */
  pColl = sqlite4FindCollSeq(db, zName, 0);
  if( pColl && pColl->xCmp ){
    if( db->activeVdbeCnt ){
      sqlite4Error(db, SQLITE4_BUSY, 
        "unable to delete/modify collation sequence due to active statements");
      return SQLITE4_BUSY;
    }
    sqlite4ExpirePreparedStatements(db);













    if( pColl->xDel ){
      pColl->xDel(pColl->pUser);
    }
    pColl->xDel = 0;
    pColl->xCmp = 0;
    pColl->xMkKey = 0;
    pColl->pUser = 0;
  }




  pColl = sqlite4FindCollSeq(db, zName, 1);
  if( pColl==0 ) return SQLITE4_NOMEM;
  pColl->xCmp = xCompare;
  pColl->xMkKey = xMakeKey;
  pColl->pUser = pCtx;
  pColl->xDel = xDel;


  return SQLITE4_OK;
}


/*
** This array defines hard upper bounds on limit values.  The
** initializer must be kept in sync with the SQLITE4_LIMIT_*
................................................................................
            ;

  sqlite4HashInit(pEnv, &db->aCollSeq, 0);
#ifndef SQLITE4_OMIT_VIRTUALTABLE
  sqlite4HashInit(pEnv, &db->aModule, 0);
#endif

  /* Add the built-in collation sequences BINARY, RTRIM and NOCASE. */



  createCollation(db, "BINARY", 0, binCollFunc, 0, 0);



  createCollation(db, "RTRIM", (void*)1, binCollFunc, collRtrimMkKey, 0);

  createCollation(db, "NOCASE", 0, collNocaseCmp, collNocaseMkKey, 0);
  if( db->mallocFailed ){
    goto opendb_out;
  }
  db->pDfltColl = sqlite4FindCollSeq(db, "BINARY", 0);
  assert( db->pDfltColl!=0 );






  /* Parse the filename/URI argument. */
  rc = sqlite4ParseUri(pEnv, zFilename, &flags, &zOpen, &zErrMsg);
  if( rc!=SQLITE4_OK ){
    if( rc==SQLITE4_NOMEM ) db->mallocFailed = 1;
    sqlite4Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
    sqlite4_free(pEnv, zErrMsg);
    goto opendb_out;
................................................................................

/*
** Register a new collation sequence with the database handle db.
*/
int sqlite4_create_collation(
  sqlite4* db, 
  const char *zName, 

  void* pCtx,

  int(*xCompare)(void*, sqlite4_value*, sqlite4_value*, int*),
  int(*xMakeKey)(void*, sqlite4_value*, int, void*, int*),
  void(*xDel)(void*)
){
  int rc;
  sqlite4_mutex_enter(db->mutex);
  assert( !db->mallocFailed );
  rc = createCollation(db, zName, pCtx, xCompare, xMakeKey, xDel);
  rc = sqlite4ApiExit(db, rc);
  sqlite4_mutex_leave(db->mutex);
  return rc;
}

/*
** Register a collation sequence factory callback with the database handle
** db. Replace any previously installed collation sequence factory.
*/
int sqlite4_collation_needed(
  sqlite4 *db, 
  void(*xCollNeeded)(void*,sqlite4*,const char*),
  void *pCollNeededArg
){
  sqlite4_mutex_enter(db->mutex);
  db->xCollNeeded = xCollNeeded;

  db->pCollNeededArg = pCollNeededArg;
  sqlite4_mutex_leave(db->mutex);
  return SQLITE4_OK;
}

/*
** Test to see whether or not the database connection is currently within

Changes to src/sqlite.h.in.

2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
....
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748

2749
2750
2751
2752
2753
2754
2755
2756
....
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
** These constant define integer codes that represent the various
** text encodings supported by SQLite.
*/
#define SQLITE4_UTF8           1
#define SQLITE4_UTF16LE        2
#define SQLITE4_UTF16BE        3
#define SQLITE4_UTF16          4    /* Use native byte order */
#define SQLITE4_UTF16_ALIGNED  8    /* sqlite4_create_collation only */

/*
** CAPIREF: Obtaining SQL Function Parameter Values
**
** The C-language implementation of SQL functions and aggregates uses
** this set of interface routines to access the parameter values on
** the function or aggregate.
................................................................................
** compatibility.
**
** See also:  [sqlite4_collation_needed()] and [sqlite4_collation_needed16()].
*/
int sqlite4_create_collation(
  sqlite4*, 
  const char *zName, 
  int eTextRep, 
  void *pArg,
  int(*xCompare)(void*,int,const void*,int,const void*,int*),

  int(*xMakeKey)(void*,int,const void*,int,void*),
  void(*xDestroy)(void*)
);

/*
** CAPIREF: Collation Needed Callbacks
**
** ^To avoid having to register all collation sequences before a database
................................................................................
** required collation sequence.)^
**
** The callback function should register the desired collation using
** [sqlite4_create_collation()].
*/
int sqlite4_collation_needed(
  sqlite4*, 
  void(*)(void*,sqlite4*,int eTextRep,const char*),
  void* 
);

/*
** CAPIREF: Suspend Execution For A Short Time
**
** The sqlite4_sleep() function causes the current thread to suspend execution







<







 







<

<
>
|







 







|







2339
2340
2341
2342
2343
2344
2345

2346
2347
2348
2349
2350
2351
2352
....
2738
2739
2740
2741
2742
2743
2744

2745

2746
2747
2748
2749
2750
2751
2752
2753
2754
....
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
** These constant define integer codes that represent the various
** text encodings supported by SQLite.
*/
#define SQLITE4_UTF8           1
#define SQLITE4_UTF16LE        2
#define SQLITE4_UTF16BE        3
#define SQLITE4_UTF16          4    /* Use native byte order */


/*
** CAPIREF: Obtaining SQL Function Parameter Values
**
** The C-language implementation of SQL functions and aggregates uses
** this set of interface routines to access the parameter values on
** the function or aggregate.
................................................................................
** compatibility.
**
** See also:  [sqlite4_collation_needed()] and [sqlite4_collation_needed16()].
*/
int sqlite4_create_collation(
  sqlite4*, 
  const char *zName, 

  void *pArg,

  int(*xCompare)(void*, sqlite4_value*, sqlite4_value*, int*),
  int(*xMakeKey)(void*, sqlite4_value*, int, void*, int*),
  void(*xDestroy)(void*)
);

/*
** CAPIREF: Collation Needed Callbacks
**
** ^To avoid having to register all collation sequences before a database
................................................................................
** required collation sequence.)^
**
** The callback function should register the desired collation using
** [sqlite4_create_collation()].
*/
int sqlite4_collation_needed(
  sqlite4*, 
  void(*)(void*,sqlite4*,const char*),
  void* 
);

/*
** CAPIREF: Suspend Execution For A Short Time
**
** The sqlite4_sleep() function causes the current thread to suspend execution

Changes to src/sqliteInt.h.

820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
....
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060

1061
1062
1063
1064
1065
1066
1067
1068
....
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
....
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
  void *pTraceArg;                          /* Argument to the trace function */
  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  void *pProfileArg;                        /* Argument to profile function */
#ifndef SQLITE4_OMIT_WAL
  int (*xWalCallback)(void *, sqlite4 *, const char *, int);
  void *pWalArg;
#endif
  void(*xCollNeeded)(void*,sqlite4*,int eTextRep,const char*);
  void(*xCollNeeded16)(void*,sqlite4*,int eTextRep,const void*);
  void *pCollNeededArg;
  sqlite4_value *pErr;          /* Most recent error message */
  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
  char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
  union {
    volatile int isInterrupted; /* True if sqlite4_interrupt has been called */
    double notUsed1;            /* Spacer */
................................................................................
**
** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
** collating sequence is undefined.  Indices built on an undefined
** collating sequence may not be read or written.
*/
struct CollSeq {
  char *zName;          /* Name of the collating sequence, UTF-8 encoded */
  u8 enc;               /* Text encoding handled by xCmp() */
  void *pUser;          /* First argument to xCmp() */
  int (*xCmp)(void*,int, const void*, int, const void*, int *);

  int (*xMkKey)(void*,int, const void*, int, void*);
  void (*xDel)(void*);  /* Destructor for pUser */
};

/*
** A sort order can be either ASC or DESC.
*/
#define SQLITE4_SO_ASC       0  /* Sort in ascending order */
................................................................................
int sqlite4Atoi64(const char*, i64*, int, u8);
void sqlite4Error(sqlite4*, int, const char*,...);
void *sqlite4HexToBlob(sqlite4*, const char *z, int n);
u8 sqlite4HexToInt(int h);
int sqlite4TwoPartName(Parse *, Token *, Token *, Token **);
const char *sqlite4ErrStr(int);
int sqlite4ReadSchema(Parse *pParse);
CollSeq *sqlite4FindCollSeq(sqlite4*,u8 enc, const char*,int);
CollSeq *sqlite4LocateCollSeq(Parse *pParse, const char*zName);
CollSeq *sqlite4ExprCollSeq(Parse *pParse, Expr *pExpr);
Expr *sqlite4ExprSetColl(Expr*, CollSeq*);
Expr *sqlite4ExprSetCollByToken(Parse *pParse, Expr*, Token*);
int sqlite4CheckCollSeq(Parse *, CollSeq *);
int sqlite4CheckObjectName(Parse *, const char *);
void sqlite4VdbeSetChanges(sqlite4 *, int);
................................................................................
void sqlite4SelectPrep(Parse*, Select*, NameContext*);
int sqlite4ResolveExprNames(NameContext*, Expr*);
void sqlite4ResolveSelectNames(Parse*, Select*, NameContext*);
int sqlite4ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
void sqlite4ColumnDefault(Vdbe *, Table *, int, int);
void sqlite4AlterFinishAddColumn(Parse *, Token *);
void sqlite4AlterBeginAddColumn(Parse *, SrcList *);
CollSeq *sqlite4GetCollSeq(sqlite4*, u8, CollSeq *, const char*);
char sqlite4AffinityType(const char*);
void sqlite4Analyze(Parse*, Token*, Token*);
int sqlite4FindDb(sqlite4*, Token*);
int sqlite4FindDbName(sqlite4 *, const char *);
int sqlite4AnalysisLoad(sqlite4*,int iDB);
void sqlite4DeleteIndexSamples(sqlite4*,Index*);
void sqlite4DefaultRowEst(Index*);







|
<







 







<

<
>
|







 







|







 







|







820
821
822
823
824
825
826
827

828
829
830
831
832
833
834
....
1050
1051
1052
1053
1054
1055
1056

1057

1058
1059
1060
1061
1062
1063
1064
1065
1066
....
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
....
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
  void *pTraceArg;                          /* Argument to the trace function */
  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  void *pProfileArg;                        /* Argument to profile function */
#ifndef SQLITE4_OMIT_WAL
  int (*xWalCallback)(void *, sqlite4 *, const char *, int);
  void *pWalArg;
#endif
  void(*xCollNeeded)(void*,sqlite4*,const char*);

  void *pCollNeededArg;
  sqlite4_value *pErr;          /* Most recent error message */
  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
  char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
  union {
    volatile int isInterrupted; /* True if sqlite4_interrupt has been called */
    double notUsed1;            /* Spacer */
................................................................................
**
** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
** collating sequence is undefined.  Indices built on an undefined
** collating sequence may not be read or written.
*/
struct CollSeq {
  char *zName;          /* Name of the collating sequence, UTF-8 encoded */

  void *pUser;          /* First argument to xCmp() */

  int (*xCmp)(void*, sqlite4_value*, sqlite4_value*, int*);
  int (*xMkKey)(void*, sqlite4_value*, int, void*, int*);
  void (*xDel)(void*);  /* Destructor for pUser */
};

/*
** A sort order can be either ASC or DESC.
*/
#define SQLITE4_SO_ASC       0  /* Sort in ascending order */
................................................................................
int sqlite4Atoi64(const char*, i64*, int, u8);
void sqlite4Error(sqlite4*, int, const char*,...);
void *sqlite4HexToBlob(sqlite4*, const char *z, int n);
u8 sqlite4HexToInt(int h);
int sqlite4TwoPartName(Parse *, Token *, Token *, Token **);
const char *sqlite4ErrStr(int);
int sqlite4ReadSchema(Parse *pParse);
CollSeq *sqlite4FindCollSeq(sqlite4*,const char*,int);
CollSeq *sqlite4LocateCollSeq(Parse *pParse, const char*zName);
CollSeq *sqlite4ExprCollSeq(Parse *pParse, Expr *pExpr);
Expr *sqlite4ExprSetColl(Expr*, CollSeq*);
Expr *sqlite4ExprSetCollByToken(Parse *pParse, Expr*, Token*);
int sqlite4CheckCollSeq(Parse *, CollSeq *);
int sqlite4CheckObjectName(Parse *, const char *);
void sqlite4VdbeSetChanges(sqlite4 *, int);
................................................................................
void sqlite4SelectPrep(Parse*, Select*, NameContext*);
int sqlite4ResolveExprNames(NameContext*, Expr*);
void sqlite4ResolveSelectNames(Parse*, Select*, NameContext*);
int sqlite4ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
void sqlite4ColumnDefault(Vdbe *, Table *, int, int);
void sqlite4AlterFinishAddColumn(Parse *, Token *);
void sqlite4AlterBeginAddColumn(Parse *, SrcList *);
CollSeq *sqlite4GetCollSeq(sqlite4*, CollSeq *, const char*);
char sqlite4AffinityType(const char*);
void sqlite4Analyze(Parse*, Token*, Token*);
int sqlite4FindDb(sqlite4*, Token*);
int sqlite4FindDbName(sqlite4 *, const char *);
int sqlite4AnalysisLoad(sqlite4*,int iDB);
void sqlite4DeleteIndexSamples(sqlite4*,Index*);
void sqlite4DefaultRowEst(Index*);

Changes to src/tclsqlite.c.

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
...
313
314
315
316
317
318
319
320
321
322
323
324
325




326
327
328



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

346



347
348
349
350


351
352
353
354
355
356
357
358
359
360
361


362
363
364
365
366
367
368
369
....
1422
1423
1424
1425
1426
1427
1428
1429
1430


1431
1432
1433
1434
1435
1436
1437
  Tcl_ResetResult(pDb->interp);
}
#endif

static void tclCollateNeeded(
  void *pCtx,
  sqlite4 *db,
  int enc,
  const char *zName
){
  SqliteDb *pDb = (SqliteDb *)pCtx;
  Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
  Tcl_IncrRefCount(pScript);
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
  Tcl_EvalObjEx(pDb->interp, pScript, 0);
................................................................................

/*
** This routine is called to evaluate an SQL collation function implemented
** using TCL script.
*/
static int tclSqlCollate(
  void *pCtx,
  int nA,
  const void *zA,
  int nB,
  const void *zB,
  int *pRes
){




  SqlCollate *p = (SqlCollate *)pCtx;
  Tcl_Obj *pCmd;
  int rc;




  pCmd = Tcl_NewStringObj(p->zCmp, -1);
  Tcl_IncrRefCount(pCmd);
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
  rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);
  *pRes = (atoi(Tcl_GetStringResult(p->interp)));
  return (rc==TCL_OK ? SQLITE4_OK : SQLITE4_ERROR);
}

static int tclSqlMkkey(
  void *pCtx,
  int nIn,
  const void *zIn,
  int nOut,
  void *zOut

){



  SqlCollate *p = (SqlCollate *)pCtx;
  Tcl_Obj *pCmd;
  int nRes;
  char *zRes;



  pCmd = Tcl_NewStringObj(p->zMkkey, -1);
  Tcl_IncrRefCount(pCmd);
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zIn, nIn));
  Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);

  zRes = Tcl_GetStringFromObj(Tcl_GetObjResult(p->interp), &nRes);
  if( nRes<=nOut ){
    memcpy(zOut, zRes, nRes);
  }


  return nRes;
}

/*
** This routine is called to evaluate an SQL function implemented
** using TCL script.
*/
static void tclSqlFunc(sqlite4_context *context, int argc, sqlite4_value**argv){
................................................................................
    pCollate->pNext = pDb->pCollate;
    pCollate->zCmp = (char*)&pCollate[1];
    pCollate->zMkkey = &pCollate->zCmp[nCmp + 1];
    pDb->pCollate = pCollate;

    memcpy(pCollate->zCmp, zCmp, nCmp+1);
    memcpy(pCollate->zMkkey, zMkkey, nMkkey+1);
    if( sqlite4_create_collation(pDb->db, zName, SQLITE4_UTF8, 
        pCollate, tclSqlCollate, tclSqlMkkey, 0) ){


      Tcl_SetResult(interp, (char *)sqlite4_errmsg(pDb->db), TCL_VOLATILE);
      return TCL_ERROR;
    }
    break;
  }

  /*







<







 







|
|
<
<


>
>
>
>



>
>
>



|
|








|
<

|
>

>
>
>




>
>




|




|

>
>
|







 







|
|
>
>







296
297
298
299
300
301
302

303
304
305
306
307
308
309
...
312
313
314
315
316
317
318
319
320


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
....
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
  Tcl_ResetResult(pDb->interp);
}
#endif

static void tclCollateNeeded(
  void *pCtx,
  sqlite4 *db,

  const char *zName
){
  SqliteDb *pDb = (SqliteDb *)pCtx;
  Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
  Tcl_IncrRefCount(pScript);
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
  Tcl_EvalObjEx(pDb->interp, pScript, 0);
................................................................................

/*
** This routine is called to evaluate an SQL collation function implemented
** using TCL script.
*/
static int tclSqlCollate(
  void *pCtx,
  sqlite4_value *p1,
  sqlite4_value *p2,


  int *pRes
){
  int n1;
  int n2;
  const char *z1;
  const char *z2;
  SqlCollate *p = (SqlCollate *)pCtx;
  Tcl_Obj *pCmd;
  int rc;

  z1 = sqlite4_value_text(p1, &n1);
  z2 = sqlite4_value_text(p2, &n2);

  pCmd = Tcl_NewStringObj(p->zCmp, -1);
  Tcl_IncrRefCount(pCmd);
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(z1, n1));
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(z2, n2));
  rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);
  *pRes = (atoi(Tcl_GetStringResult(p->interp)));
  return (rc==TCL_OK ? SQLITE4_OK : SQLITE4_ERROR);
}

static int tclSqlMkkey(
  void *pCtx,
  sqlite4_value *pVal,

  int nOut,
  void *pOut,
  int *pnOut
){
  int nIn;
  int rc;
  const char *zIn;
  SqlCollate *p = (SqlCollate *)pCtx;
  Tcl_Obj *pCmd;
  int nRes;
  char *zRes;

  zIn = sqlite4_value_text(pVal, &nIn);

  pCmd = Tcl_NewStringObj(p->zMkkey, -1);
  Tcl_IncrRefCount(pCmd);
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zIn, nIn));
  rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);

  zRes = Tcl_GetStringFromObj(Tcl_GetObjResult(p->interp), &nRes);
  if( nRes<=nOut ){
    memcpy(pOut, zRes, nRes);
  }
  *pnOut = nRes;

  return rc;
}

/*
** This routine is called to evaluate an SQL function implemented
** using TCL script.
*/
static void tclSqlFunc(sqlite4_context *context, int argc, sqlite4_value**argv){
................................................................................
    pCollate->pNext = pDb->pCollate;
    pCollate->zCmp = (char*)&pCollate[1];
    pCollate->zMkkey = &pCollate->zCmp[nCmp + 1];
    pDb->pCollate = pCollate;

    memcpy(pCollate->zCmp, zCmp, nCmp+1);
    memcpy(pCollate->zMkkey, zMkkey, nMkkey+1);
    rc = sqlite4_create_collation(
        pDb->db, zName, pCollate, tclSqlCollate, tclSqlMkkey, 0
    );
    if( rc!=SQLITE4_OK ){
      Tcl_SetResult(interp, (char *)sqlite4_errmsg(pDb->db), TCL_VOLATILE);
      return TCL_ERROR;
    }
    break;
  }

  /*

Changes to src/vdbeInt.h.

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  u8 **pzOut,                  /* Write the resulting key here */
  int *pnOut,                  /* Number of bytes in the key */
  int nExtra                   /* Append extra bytes on end of key */
);
int sqlite4VdbeEncodeIntKey(u8 *aBuf,sqlite4_int64 v);
int sqlite4VdbeDecodeIntKey(const KVByteArray*, KVSize, sqlite4_int64*);
int sqlite4VdbeShortKey(const u8 *, int, int);
int sqlite4MemCompare(const Mem*, const Mem*, const CollSeq*,int*);
int sqlite4VdbeExec(Vdbe*);
int sqlite4VdbeList(Vdbe*);
int sqlite4VdbeHalt(Vdbe*);
int sqlite4VdbeChangeEncoding(Mem *, int);
int sqlite4VdbeMemTooBig(Mem*);
int sqlite4VdbeMemCopy(Mem*, const Mem*);
void sqlite4VdbeMemShallowCopy(Mem*, const Mem*, int);







|







381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  u8 **pzOut,                  /* Write the resulting key here */
  int *pnOut,                  /* Number of bytes in the key */
  int nExtra                   /* Append extra bytes on end of key */
);
int sqlite4VdbeEncodeIntKey(u8 *aBuf,sqlite4_int64 v);
int sqlite4VdbeDecodeIntKey(const KVByteArray*, KVSize, sqlite4_int64*);
int sqlite4VdbeShortKey(const u8 *, int, int);
int sqlite4MemCompare(Mem*, Mem*, const CollSeq*,int*);
int sqlite4VdbeExec(Vdbe*);
int sqlite4VdbeList(Vdbe*);
int sqlite4VdbeHalt(Vdbe*);
int sqlite4VdbeChangeEncoding(Mem *, int);
int sqlite4VdbeMemTooBig(Mem*);
int sqlite4VdbeMemCopy(Mem*, const Mem*);
void sqlite4VdbeMemShallowCopy(Mem*, const Mem*, int);

Changes to src/vdbecodec.c.

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491


492
493

494




495
496

497
498
499
500
501
502
503
504
505
506

507
508
509
510
511
512
513
    if( enlargeEncoderAllocation(p, 1) ) return SQLITE4_NOMEM;
    p->aOut[p->nOut++] = 0x05;   /* NULL */
  }else
  if( flags & (MEM_Real|MEM_Int) ){
    if( enlargeEncoderAllocation(p, 16) ) return SQLITE4_NOMEM;
    encodeNumericKey(p, pMem->u.num);
  }else if( flags & MEM_Str ){
    Mem *pEnc;                    /* Pointer to memory cell in correct enc. */
    Mem sMem;                     /* Value converted to different encoding */
    int enc;                      /* Required encoding */

    /* Figure out the current encoding of pMem, and the encoding required
    ** (either the encoding specified by the collation sequence, or utf-8
    ** if there is no collation sequence).  */
    enc = ((pColl && pColl->xMkKey) ? pColl->enc : SQLITE4_UTF8);
    assert( enc==SQLITE4_UTF8 || enc==SQLITE4_UTF16LE || enc==SQLITE4_UTF16BE );
    assert( pMem->enc==SQLITE4_UTF8 || pMem->enc==SQLITE4_UTF16LE 

         || pMem->enc==SQLITE4_UTF16BE 
    );
    
    /* If necessary, convert the encoding of the input text. */
    if( pMem->enc!=enc ){
      memset(&sMem, 0, sizeof(sMem));
      sqlite4VdbeMemShallowCopy(&sMem, pMem, MEM_Static);
      sqlite4VdbeMemTranslate(&sMem, enc);
      pEnc = &sMem;
    }else{
      pEnc = pMem;
    }

    /* Write the encoded key to the output buffer. */
    if( enlargeEncoderAllocation(p, pMem->n*4 + 2) ) return SQLITE4_NOMEM;
    p->aOut[p->nOut++] = 0x24;   /* Text */
    if( pColl==0 || pColl->xMkKey==0 ){


      memcpy(p->aOut+p->nOut, pEnc->z, pEnc->n);
      p->nOut += pEnc->n;

    }else{




      int nSpc = p->nAlloc-p->nOut;
      n = pColl->xMkKey(pColl->pUser, pEnc->n, pEnc->z, nSpc, p->aOut+p->nOut);

      if( n+1>nSpc ){
        if( enlargeEncoderAllocation(p, n+1) ) return SQLITE4_NOMEM;
        n = pColl->xMkKey(pColl->pUser, pEnc->n, pEnc->z, n, p->aOut+p->nOut);
      }
      p->nOut += n;
    }
    p->aOut[p->nOut++] = 0x00;

    /* Release any memory allocated to hold the translated text */
    if( pEnc==&sMem ) sqlite4VdbeMemRelease(&sMem);


  }else if( isLastValue ){
    /* A BLOB value that is the right-most value of a key */
    assert( flags & MEM_Blob );
    if( enlargeEncoderAllocation(p, pMem->n+1) ) return SQLITE4_NOMEM;
    p->aOut[p->nOut++] = 0x26;
    memcpy(p->aOut+p->nOut, pMem->z, pMem->n);







<
<
|

<
<
<
<
<
|
>
|

<
<
|
<
<
<
<
<
|
<





>
>
|
|
>

>
>
>
>
|
|
>
|
|
|

|



|
|
>







458
459
460
461
462
463
464


465
466





467
468
469
470


471





472

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    if( enlargeEncoderAllocation(p, 1) ) return SQLITE4_NOMEM;
    p->aOut[p->nOut++] = 0x05;   /* NULL */
  }else
  if( flags & (MEM_Real|MEM_Int) ){
    if( enlargeEncoderAllocation(p, 16) ) return SQLITE4_NOMEM;
    encodeNumericKey(p, pMem->u.num);
  }else if( flags & MEM_Str ){


    int enc;                      /* Initial encoding of pMem */






    assert( pMem->enc==SQLITE4_UTF8 
         || pMem->enc==SQLITE4_UTF16LE
         || pMem->enc==SQLITE4_UTF16BE
    );


    assert( pMem->db );





    enc = pMem->enc;


    /* Write the encoded key to the output buffer. */
    if( enlargeEncoderAllocation(p, pMem->n*4 + 2) ) return SQLITE4_NOMEM;
    p->aOut[p->nOut++] = 0x24;   /* Text */
    if( pColl==0 || pColl->xMkKey==0 ){
      const char *z = (const char *)sqlite4ValueText(pMem, SQLITE4_UTF8);
      if( z ){
        memcpy(p->aOut+p->nOut, z, pMem->n);
        p->nOut += pMem->n;
      }
    }else{
      int rc;                     /* xMkKey() return code */
      int nReq;                   /* Space required by xMkKey() */
      int nSpc;                   /* Space available */

      nSpc = p->nAlloc-p->nOut;
      rc = pColl->xMkKey(pColl->pUser, pMem, nSpc, p->aOut+p->nOut, &nReq);
      if( rc!=SQLITE4_OK ) return rc;
      if( nReq+1>nSpc ){
        if( enlargeEncoderAllocation(p, nReq+1) ) return SQLITE4_NOMEM;
        rc = pColl->xMkKey(pColl->pUser, pMem, nReq, p->aOut+p->nOut, &nReq);
      }
      p->nOut += nReq;
    }
    p->aOut[p->nOut++] = 0x00;

    /* If the operations above changed the encoding of pMem, change it back.
    ** This call is a no-op if pMem was not modified by the code above.  */
    sqlite4VdbeChangeEncoding(pMem, enc);

  }else if( isLastValue ){
    /* A BLOB value that is the right-most value of a key */
    assert( flags & MEM_Blob );
    if( enlargeEncoderAllocation(p, pMem->n+1) ) return SQLITE4_NOMEM;
    p->aOut[p->nOut++] = 0x26;
    memcpy(p->aOut+p->nOut, pMem->z, pMem->n);

Changes to src/vdbemem.c.

636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
...
697
698
699
700
701
702
703

704
705
706
707
708
709
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
...
738
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
** than pMem2. Sorting order is NULL's first, followed by numbers (integers
** and reals) sorted numerically, followed by text ordered by the collating
** sequence pColl and finally blob's ordered by memcmp().
**
** Two NULL values are considered equal by this function.
*/
int sqlite4MemCompare(
  const Mem *pMem1, 
  const Mem *pMem2, 
  const CollSeq *pColl,
  int *pRes                       /* OUT: Result of comparison operation */
){
  int rc = SQLITE4_OK;
  int f1, f2;
  int combined_flags;

................................................................................
    /* The collation sequence must be defined at this point, even if
    ** the user deletes the collation sequence after the vdbe program is
    ** compiled (this was not always the case).
    */
    assert( !pColl || pColl->xCmp );

    if( pColl ){

      void *pUser = pColl->pUser;
      if( pMem1->enc==pColl->enc ){
        /* The strings are already in the correct encoding.  Call the
        ** comparison function directly */
        return pColl->xCmp(pUser, pMem1->n, pMem1->z, pMem2->n, pMem2->z, pRes);
      }else{
        const void *v1, *v2;
        int n1, n2;
        Mem c1;
        Mem c2;
        memset(&c1, 0, sizeof(c1));
        memset(&c2, 0, sizeof(c2));
        sqlite4VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
        sqlite4VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
        v1 = sqlite4ValueText((sqlite4_value*)&c1, pColl->enc);
        n1 = v1==0 ? 0 : c1.n;
        v2 = sqlite4ValueText((sqlite4_value*)&c2, pColl->enc);
        n2 = v2==0 ? 0 : c2.n;
        rc = pColl->xCmp(pUser, n1, v1, n2, v2, pRes);
        sqlite4VdbeMemRelease(&c1);
        sqlite4VdbeMemRelease(&c2);


        return rc;
      }
    }
    /* If a NULL pointer was passed as the collate function, fall through
    ** to the blob case and use memcmp().  */
  }
 
  /* Both values must be blobs.  Compare using memcmp().  */
  *pRes = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
  if( *pRes==0 ){
................................................................................
}

/* This function is only available internally, it is not part of the
** external API. It works in a similar way to sqlite4_value_text(),
** except the data returned is in the encoding specified by the second
** parameter, which must be one of SQLITE4_UTF16BE, SQLITE4_UTF16LE or
** SQLITE4_UTF8.
**
** (2006-02-16:)  The enc value can be or-ed with SQLITE4_UTF16_ALIGNED.
** If that is the case, then the result must be aligned on an even byte
** boundary.
*/
const void *sqlite4ValueText(sqlite4_value* pVal, u8 enc){
  if( !pVal ) return 0;

  assert( pVal->db==0 || sqlite4_mutex_held(pVal->db->mutex) );
  assert( (enc&3)==(enc&~SQLITE4_UTF16_ALIGNED) );
  assert( (pVal->flags & MEM_RowSet)==0 );

  if( pVal->flags&MEM_Null ){
    return 0;
  }
  assert( (MEM_Blob>>3) == MEM_Str );
  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
  if( pVal->flags&MEM_Str ){
    sqlite4VdbeChangeEncoding(pVal, enc & ~SQLITE4_UTF16_ALIGNED);
    if( (enc & SQLITE4_UTF16_ALIGNED)!=0 && 1==(1&SQLITE4_PTR_TO_INT(pVal->z)) ){
      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
      if( sqlite4VdbeMemMakeWriteable(pVal)!=SQLITE4_OK ){
        return 0;
      }
    }
    sqlite4VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
  }else{
    assert( (pVal->flags&MEM_Blob)==0 );
    sqlite4VdbeMemStringify(pVal, enc);
    assert( 0==(1&SQLITE4_PTR_TO_INT(pVal->z)) );
  }
  assert(pVal->enc==(enc & ~SQLITE4_UTF16_ALIGNED) || pVal->db==0
              || pVal->db->mallocFailed );
  if( pVal->enc==(enc & ~SQLITE4_UTF16_ALIGNED) ){
    return pVal->z;
  }else{
    return 0;
  }
}

/*







|
|







 







>

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
>
>
|
|
|







 







<
<
<
<





<








|
<
<
<
<
<
<






|
<
|







636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
...
697
698
699
700
701
702
703
704
705

















706


707
708
709
710
711
712
713
714
715
716
717
718
...
722
723
724
725
726
727
728




729
730
731
732
733

734
735
736
737
738
739
740
741
742






743
744
745
746
747
748
749

750
751
752
753
754
755
756
757
** than pMem2. Sorting order is NULL's first, followed by numbers (integers
** and reals) sorted numerically, followed by text ordered by the collating
** sequence pColl and finally blob's ordered by memcmp().
**
** Two NULL values are considered equal by this function.
*/
int sqlite4MemCompare(
  Mem *pMem1, 
  Mem *pMem2, 
  const CollSeq *pColl,
  int *pRes                       /* OUT: Result of comparison operation */
){
  int rc = SQLITE4_OK;
  int f1, f2;
  int combined_flags;

................................................................................
    /* The collation sequence must be defined at this point, even if
    ** the user deletes the collation sequence after the vdbe program is
    ** compiled (this was not always the case).
    */
    assert( !pColl || pColl->xCmp );

    if( pColl ){
      int enc = pMem1->enc;
      void *pUser = pColl->pUser;

















      rc = pColl->xCmp(pUser, pMem1, pMem2, pRes);


      sqlite4VdbeChangeEncoding(pMem1, enc);
      sqlite4VdbeChangeEncoding(pMem2, enc);
      return rc;
    }

    /* If a NULL pointer was passed as the collate function, fall through
    ** to the blob case and use memcmp().  */
  }
 
  /* Both values must be blobs.  Compare using memcmp().  */
  *pRes = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
  if( *pRes==0 ){
................................................................................
}

/* This function is only available internally, it is not part of the
** external API. It works in a similar way to sqlite4_value_text(),
** except the data returned is in the encoding specified by the second
** parameter, which must be one of SQLITE4_UTF16BE, SQLITE4_UTF16LE or
** SQLITE4_UTF8.




*/
const void *sqlite4ValueText(sqlite4_value* pVal, u8 enc){
  if( !pVal ) return 0;

  assert( pVal->db==0 || sqlite4_mutex_held(pVal->db->mutex) );

  assert( (pVal->flags & MEM_RowSet)==0 );

  if( pVal->flags&MEM_Null ){
    return 0;
  }
  assert( (MEM_Blob>>3) == MEM_Str );
  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
  if( pVal->flags&MEM_Str ){
    sqlite4VdbeChangeEncoding(pVal, enc);






    sqlite4VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
  }else{
    assert( (pVal->flags&MEM_Blob)==0 );
    sqlite4VdbeMemStringify(pVal, enc);
    assert( 0==(1&SQLITE4_PTR_TO_INT(pVal->z)) );
  }
  assert(pVal->enc==enc || pVal->db==0 || pVal->db->mallocFailed );

  if( pVal->enc==enc ){
    return pVal->z;
  }else{
    return 0;
  }
}

/*

Changes to src/where.c.

626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
....
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
....
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
            zColl = pTab->aCol[iColumn].zColl;
          }else{
            zColl = pIdx->azColl[j];
          }

          /* If the collation sequence used by the index is not the same as
          ** that used by the expression, then this term is not a match.  */
          if( pColl!=sqlite4FindCollSeq(db, ENC(db), zColl, 0) ) continue;
        }
        return pTerm;
      }
    }
  }
  return 0;
}
................................................................................
        if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */


        c = sqlite4UpperToLower[c];
      }
      *pC = c + 1;
    }
    pColl = sqlite4FindCollSeq(db, SQLITE4_UTF8, noCase ? "NOCASE" : "BINARY",0);
    pNewExpr1 = sqlite4PExpr(pParse, TK_GE, 
                     sqlite4ExprSetColl(sqlite4ExprDup(db,pLeft,0), pColl),
                     pStr1, 0);
    idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
    testcase( idxNew1==0 );
    exprAnalyze(pSrc, pWC, idxNew1);
    pNewExpr2 = sqlite4PExpr(pParse, TK_LT,
................................................................................
    if( iIdxCol<pIdx->nColumn ){
      zColl = pIdx->azColl[iIdxCol];
    }else if( iColumn>=0 ) {
      zColl = pTab->aCol[iColumn].zColl;
    }else{
      zColl = 0;
    }
    if( pColl!=sqlite4FindCollSeq(db, ENC(db), zColl, 0) ) break;

    if( iIdxCol==iNext ){
      u8 reqSortOrder;
      u8 idxSortOrder = SQLITE4_SO_ASC;
      if( iIdxCol<pIdx->nColumn ) idxSortOrder = pIdx->aSortOrder[iIdxCol];
      assert( idxSortOrder==SQLITE4_SO_ASC || idxSortOrder==SQLITE4_SO_DESC );








|







 







|







 







|







626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
....
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
....
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
            zColl = pTab->aCol[iColumn].zColl;
          }else{
            zColl = pIdx->azColl[j];
          }

          /* If the collation sequence used by the index is not the same as
          ** that used by the expression, then this term is not a match.  */
          if( pColl!=sqlite4FindCollSeq(db, zColl, 0) ) continue;
        }
        return pTerm;
      }
    }
  }
  return 0;
}
................................................................................
        if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */


        c = sqlite4UpperToLower[c];
      }
      *pC = c + 1;
    }
    pColl = sqlite4FindCollSeq(db, noCase ? "NOCASE" : "BINARY",0);
    pNewExpr1 = sqlite4PExpr(pParse, TK_GE, 
                     sqlite4ExprSetColl(sqlite4ExprDup(db,pLeft,0), pColl),
                     pStr1, 0);
    idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
    testcase( idxNew1==0 );
    exprAnalyze(pSrc, pWC, idxNew1);
    pNewExpr2 = sqlite4PExpr(pParse, TK_LT,
................................................................................
    if( iIdxCol<pIdx->nColumn ){
      zColl = pIdx->azColl[iIdxCol];
    }else if( iColumn>=0 ) {
      zColl = pTab->aCol[iColumn].zColl;
    }else{
      zColl = 0;
    }
    if( pColl!=sqlite4FindCollSeq(db, zColl, 0) ) break;

    if( iIdxCol==iNext ){
      u8 reqSortOrder;
      u8 idxSortOrder = SQLITE4_SO_ASC;
      if( iIdxCol<pIdx->nColumn ) idxSortOrder = pIdx->aSortOrder[iIdxCol];
      assert( idxSortOrder==SQLITE4_SO_ASC || idxSortOrder==SQLITE4_SO_DESC );

Changes to test/test_main.c.

1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371





1372
1373
1374
1375
1376




1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
....
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
....
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
....
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
....
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
....
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
....
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
....
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236

  Tcl_DecrRefCount(p->pCmp);
  Tcl_DecrRefCount(p->pDel);
  sqlite4_free(0, (void *)p);
}
static int testCreateCollationCmp(
  void *pCtx,
  int nLeft,
  const void *zLeft,
  int nRight,
  const void *zRight,
  int *pRes
){





  TestCollationX *p = (TestCollationX *)pCtx;
  Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
  int iRes = 0;
  int rc;





  Tcl_IncrRefCount(pScript);
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));

  rc = Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  if( rc==TCL_OK ){
   rc = Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes);
  }
  Tcl_DecrRefCount(pScript);

................................................................................
  p = (TestCollationX *)sqlite4_malloc(0, sizeof(TestCollationX));
  p->pCmp = objv[3];
  p->pDel = objv[4];
  p->interp = interp;
  Tcl_IncrRefCount(p->pCmp);
  Tcl_IncrRefCount(p->pDel);

  rc = sqlite4_create_collation(db, Tcl_GetString(objv[2]), 16, 
      (void *)p, testCreateCollationCmp, 0, testCreateCollationDel
  );
  if( rc!=SQLITE4_MISUSE ){
    Tcl_AppendResult(interp, "sqlite4_create_collate() failed to detect "
      "an invalid encoding", (char*)0);
    return TCL_ERROR;
  }
  rc = sqlite4_create_collation(db, Tcl_GetString(objv[2]), SQLITE4_UTF8, 
      (void *)p, testCreateCollationCmp, 0, testCreateCollationDel
  );
  return TCL_OK;
}

/*
** USAGE: sqlite4_create_function_v2 DB NAME NARG ENC ?SWITCHES?
**
** Available switches are:
................................................................................
static int uses_stmt_journal(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_stmt *pStmt;
  int rc;

  if( objc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
        Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
    return TCL_ERROR;
  }

  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  rc = sqlite4_stmt_readonly(pStmt);
  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->needSavepoint));
  return TCL_OK;
}


/*
** Usage:  sqlite4_reset  STMT 
................................................................................
    sprintf(zBuf, "(%d) ", rc);
    Tcl_AppendResult(interp, zBuf, sqlite4ErrStr(rc), 0);
    return TCL_ERROR;
  }
  return TCL_OK;
}

#ifndef SQLITE4_OMIT_UTF16
/*
** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
**
** This function is used to test that SQLite selects the correct collation
** sequence callback when multiple versions (for different text encodings)
** are available.
**
** Calling this routine registers the collation sequence "test_collate"
** with database handle <db>. The second argument must be a list of three
** boolean values. If the first is true, then a version of test_collate is
** registered for UTF-8, if the second is true, a version is registered for
** UTF-16le, if the third is true, a UTF-16be version is available.
** Previous versions of test_collate are deleted.
**
** The collation sequence test_collate is implemented by calling the
** following TCL script:
**
**   "test_collate <enc> <lhs> <rhs>"
**
** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
** The <enc> parameter is the encoding of the collation function that
** SQLite selected to call. The TCL test script implements the
** "test_collate" proc.
**
** Note that this will only work with one intepreter at a time, as the
** interp pointer to use when evaluating the TCL script is stored in
** pTestCollateInterp.
*/
static Tcl_Interp* pTestCollateInterp;
static int test_collate_func(
  void *pCtx, 
  int nA, const void *zA,
  int nB, const void *zB,
  int *pRes
){
  Tcl_Interp *i = pTestCollateInterp;
  int encin = SQLITE4_PTR_TO_INT(pCtx);
  int res;
  int n;
  sqlite4_env *pEnv = sqlite4_context_env(pCtx);

  sqlite4_value *pVal;
  Tcl_Obj *pX;

  pX = Tcl_NewStringObj("test_collate", -1);
  Tcl_IncrRefCount(pX);

  switch( encin ){
    case SQLITE4_UTF8:
      Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
      break;
    case SQLITE4_UTF16LE:
      Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
      break;
    case SQLITE4_UTF16BE:
      Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
      break;
    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);
  *pRes = res;
  return SQLITE4_OK;
}
static int test_collate(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4 *db;
  int val;
  int rc;

  if( objc!=5 ) goto bad_args;
  pTestCollateInterp = interp;
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;

  if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
  rc = sqlite4_create_collation(db, "test_collate", SQLITE4_UTF8, 
          (void *)SQLITE4_UTF8, val?test_collate_func:0, 0, 0);
  if( rc==SQLITE4_OK ){
    if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
    rc = sqlite4_create_collation(db, "test_collate", SQLITE4_UTF16LE, 
            (void *)SQLITE4_UTF16LE, val?test_collate_func:0, 0, 0);
    if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
    rc = sqlite4_create_collation(db, "test_collate", SQLITE4_UTF16BE,
            (void *)SQLITE4_UTF16BE, val?test_collate_func:0, 0, 0); 
  }
  if( sqlite4TestErrCode(interp, db, rc) ) return TCL_ERROR;
  
  if( rc!=SQLITE4_OK ){
    Tcl_AppendResult(interp, sqlite4TestErrorName(rc), 0);
    return TCL_ERROR;
  }
  return TCL_OK;

bad_args:
  Tcl_AppendResult(interp, "wrong # args: should be \"",
      Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  return TCL_ERROR;
}

/*
** When the collation needed callback is invoked, record the name of 
** the requested collating function here.  The recorded name is linked
** to a TCL variable and used to make sure that the requested collation
** name is correct.
*/
static char zNeededCollation[200];
static char *pzNeededCollation = zNeededCollation;


/*
** Called when a collating sequence is needed.  Registered using
** sqlite4_collation_needed16().
*/
static void test_collate_needed_cb(
  void *pCtx, 
  sqlite4 *db,
  int eTextRep,
  const char *zName
){
  int enc = ENC(db);
  strcpy(zNeededCollation, zName);
  sqlite4_create_collation(
      db, "test_collate", enc, SQLITE4_INT_TO_PTR(enc), test_collate_func, 0, 0
  );
}

/*
** Usage: add_test_collate_needed DB
*/
static int test_collate_needed(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4 *db;
  int rc;

  if( objc!=2 ) goto bad_args;
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  rc = sqlite4_collation_needed(db, test_collate_needed_cb, 0);
  if( sqlite4TestErrCode(interp, db, rc) ) return TCL_ERROR;
  return TCL_OK;

bad_args:
  Tcl_WrongNumArgs(interp, 1, objv, "DB");
  return TCL_ERROR;
}

/*
** tclcmd:   add_alignment_test_collations  DB
**
** Add two new collating sequences to the database DB
**
**     utf16_aligned
**     utf16_unaligned
**
** Both collating sequences use the same sort order as BINARY.
** The only difference is that the utf16_aligned collating
** sequence is declared with the SQLITE4_UTF16_ALIGNED flag.
** Both collating functions increment the unaligned utf16 counter
** whenever they see a string that begins on an odd byte boundary.
*/
static int unaligned_string_counter = 0;
static int alignmentCollFunc(
  void *NotUsed,
  int nKey1, const void *pKey1,
  int nKey2, const void *pKey2,
  int *pRes
){
  int res, n;
  n = nKey1<nKey2 ? nKey1 : nKey2;
  if( nKey1>0 && 1==(1&(SQLITE4_PTR_TO_INT(pKey1))) ){
    unaligned_string_counter++;
  }
  if( nKey2>0 && 1==(1&(SQLITE4_PTR_TO_INT(pKey2))) ){
    unaligned_string_counter++;
  }

  res = memcmp(pKey1, pKey2, n);
  if( res==0 ){
    res = nKey1 - nKey2;
  }
  *pRes = res;
  return SQLITE4_OK;
}
static int add_alignment_test_collations(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4 *db;
  if( objc>=2 ){
    if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
    sqlite4_create_collation(db, "utf16_unaligned", SQLITE4_UTF16, 
        0, alignmentCollFunc, 0, 0);
    sqlite4_create_collation(db, "utf16_aligned", SQLITE4_UTF16_ALIGNED, 
        0, alignmentCollFunc, 0, 0);
  }
  return SQLITE4_OK;
}
#endif /* !defined(SQLITE4_OMIT_UTF16) */

/*
** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
**
** This function is used to test that SQLite selects the correct user
** function callback when multiple versions (for different text encodings)
** are available.
**
................................................................................
  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  return TCL_OK;
}

/*
** Usage: sqlite_delete_collation DB collation-name
**
** Delete the collation sequence 'collation-name' from database handle 
** DB. It is assumed that the collation sequence was created as UTF8 (the 
** way the TCL interface does it).
*/
static int delete_collation(
  void * clientData,
  Tcl_Interp *interp,
  int argc,
  char **argv
){
................................................................................
  sqlite4 *db;
  if( argc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
        " DB function-name", 0);
    return TCL_ERROR;
  }
  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  rc = sqlite4_create_collation(db, argv[2], SQLITE4_UTF8, 0, 0, 0, 0);
  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  return TCL_OK;
}

/*
** Usage: sqlite4_db_transaction_status DB
**
................................................................................
#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
     { "add_alignment_test_collations", add_alignment_test_collations, 0      },
#endif
     { "sqlite4_create_collation",   test_create_collation, 0 },
     { "working_64bit_int",          working_64bit_int,   0   },
     { "sqlite4_create_function_v2", test_create_function_v2, 0 },

     /* Functions from os.h */
#ifndef SQLITE4_OMIT_UTF16
     { "add_test_collate",        test_collate, 0            },
     { "add_test_collate_needed", test_collate_needed, 0     },
     { "add_test_function",       test_function, 0           },
#endif
     { "sqlite4_test_errstr",     test_errstr, 0             },
     { "tcl_variable_type",       tcl_variable_type, 0       },
     { "sqlite4_libversion_number", test_libversion_number, 0  },
     { "test_sqlite4_log",         test_sqlite4_log, 0  },
#ifndef SQLITE4_OMIT_EXPLAIN
................................................................................
      (char*)&sqlite4_current_time, TCL_LINK_INT);
#if SQLITE4_OS_UNIX && defined(__APPLE__) && SQLITE4_ENABLE_LOCKING_STYLE
  Tcl_LinkVar(interp, "sqlite_hostid_num", 
      (char*)&sqlite4_hostid_num, TCL_LINK_INT);
#endif
  Tcl_LinkVar(interp, "sqlite4_xferopt_count",
      (char*)&sqlite4_xferopt_count, TCL_LINK_INT);
#ifndef SQLITE4_OMIT_UTF16
  Tcl_LinkVar(interp, "unaligned_string_counter",
      (char*)&unaligned_string_counter, TCL_LINK_INT);
#endif
#ifndef SQLITE4_OMIT_UTF16
  Tcl_LinkVar(interp, "sqlite_last_needed_collation",
      (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
#endif
#if SQLITE4_OS_WIN
  Tcl_LinkVar(interp, "sqlite_os_type",
      (char*)&sqlite4_os_type, TCL_LINK_INT);
#endif
#ifdef SQLITE4_TEST
  Tcl_LinkVar(interp, "sqlite_query_plan",
      (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);







|
<
|
<


>
>
>
>
>





>
>
>
>

|
|







 







|


|
<
<


<
<
<







 







<








|







 







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







 







|
<
<







 







|







 







<
<
<






<
<







 







<
<
<
<
<
<
<
<







1359
1360
1361
1362
1363
1364
1365
1366

1367

1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
....
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426


1427
1428



1429
1430
1431
1432
1433
1434
1435
....
1793
1794
1795
1796
1797
1798
1799

1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
....
1925
1926
1927
1928
1929
1930
1931


































































































































































































































1932
1933
1934
1935
1936
1937
1938
....
3214
3215
3216
3217
3218
3219
3220
3221


3222
3223
3224
3225
3226
3227
3228
....
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
....
3915
3916
3917
3918
3919
3920
3921



3922
3923
3924
3925
3926
3927


3928
3929
3930
3931
3932
3933
3934
....
3983
3984
3985
3986
3987
3988
3989








3990
3991
3992
3993
3994
3995
3996

  Tcl_DecrRefCount(p->pCmp);
  Tcl_DecrRefCount(p->pDel);
  sqlite4_free(0, (void *)p);
}
static int testCreateCollationCmp(
  void *pCtx,
  sqlite4_value *pLeft,

  sqlite4_value *pRight,

  int *pRes
){
  int nLeft;
  int nRight;
  const char *zLeft;
  const char *zRight;

  TestCollationX *p = (TestCollationX *)pCtx;
  Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
  int iRes = 0;
  int rc;

  zLeft = sqlite4_value_text(pLeft, &nLeft);
  zRight = sqlite4_value_text(pRight, &nRight);
  if( !zLeft || !zRight ) return SQLITE4_NOMEM;

  Tcl_IncrRefCount(pScript);
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zLeft, nLeft));
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zRight,nRight));

  rc = Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  if( rc==TCL_OK ){
   rc = Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes);
  }
  Tcl_DecrRefCount(pScript);

................................................................................
  p = (TestCollationX *)sqlite4_malloc(0, sizeof(TestCollationX));
  p->pCmp = objv[3];
  p->pDel = objv[4];
  p->interp = interp;
  Tcl_IncrRefCount(p->pCmp);
  Tcl_IncrRefCount(p->pDel);

  rc = sqlite4_create_collation(db, Tcl_GetString(objv[2]),
      (void *)p, testCreateCollationCmp, 0, testCreateCollationDel
  );
  if( rc!=SQLITE4_OK ){


    return TCL_ERROR;
  }



  return TCL_OK;
}

/*
** USAGE: sqlite4_create_function_v2 DB NAME NARG ENC ?SWITCHES?
**
** Available switches are:
................................................................................
static int uses_stmt_journal(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_stmt *pStmt;


  if( objc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
        Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
    return TCL_ERROR;
  }

  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  sqlite4_stmt_readonly(pStmt);
  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->needSavepoint));
  return TCL_OK;
}


/*
** Usage:  sqlite4_reset  STMT 
................................................................................
    sprintf(zBuf, "(%d) ", rc);
    Tcl_AppendResult(interp, zBuf, sqlite4ErrStr(rc), 0);
    return TCL_ERROR;
  }
  return TCL_OK;
}



































































































































































































































/*
** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
**
** This function is used to test that SQLite selects the correct user
** function callback when multiple versions (for different text encodings)
** are available.
**
................................................................................
  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  return TCL_OK;
}

/*
** Usage: sqlite_delete_collation DB collation-name
**
** Delete the collation sequence 'collation-name' from database handle DB. 


*/
static int delete_collation(
  void * clientData,
  Tcl_Interp *interp,
  int argc,
  char **argv
){
................................................................................
  sqlite4 *db;
  if( argc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
        " DB function-name", 0);
    return TCL_ERROR;
  }
  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  rc = sqlite4_create_collation(db, argv[2], 0, 0, 0, 0);
  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  return TCL_OK;
}

/*
** Usage: sqlite4_db_transaction_status DB
**
................................................................................
#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




     { "sqlite4_create_collation",   test_create_collation, 0 },
     { "working_64bit_int",          working_64bit_int,   0   },
     { "sqlite4_create_function_v2", test_create_function_v2, 0 },

     /* Functions from os.h */
#ifndef SQLITE4_OMIT_UTF16


     { "add_test_function",       test_function, 0           },
#endif
     { "sqlite4_test_errstr",     test_errstr, 0             },
     { "tcl_variable_type",       tcl_variable_type, 0       },
     { "sqlite4_libversion_number", test_libversion_number, 0  },
     { "test_sqlite4_log",         test_sqlite4_log, 0  },
#ifndef SQLITE4_OMIT_EXPLAIN
................................................................................
      (char*)&sqlite4_current_time, TCL_LINK_INT);
#if SQLITE4_OS_UNIX && defined(__APPLE__) && SQLITE4_ENABLE_LOCKING_STYLE
  Tcl_LinkVar(interp, "sqlite_hostid_num", 
      (char*)&sqlite4_hostid_num, TCL_LINK_INT);
#endif
  Tcl_LinkVar(interp, "sqlite4_xferopt_count",
      (char*)&sqlite4_xferopt_count, TCL_LINK_INT);








#if SQLITE4_OS_WIN
  Tcl_LinkVar(interp, "sqlite_os_type",
      (char*)&sqlite4_os_type, TCL_LINK_INT);
#endif
#ifdef SQLITE4_TEST
  Tcl_LinkVar(interp, "sqlite_query_plan",
      (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);

Changes to test/test_misc1.c.

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  /* Open a database. */
  rc = sqlite4_open(0, ":memory:", &db, 0);
  if( rc!=SQLITE4_OK ){
    zErrFunction = "sqlite4_open";
    goto error_out;
  }

  rc = sqlite4_create_collation(db, "collate", 456, 0, 0, 0, 0);
  if( rc!=SQLITE4_MISUSE ){
    sqlite4_close(db, 0);
    zErrFunction = "sqlite4_create_collation";
    goto error_out;
  }

  sqlite4_close(db, 0);
  return TCL_OK;

error_out:
  Tcl_ResetResult(interp);
  Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
  return TCL_ERROR;







<
<
<
<
<
<
<







40
41
42
43
44
45
46







47
48
49
50
51
52
53
  /* Open a database. */
  rc = sqlite4_open(0, ":memory:", &db, 0);
  if( rc!=SQLITE4_OK ){
    zErrFunction = "sqlite4_open";
    goto error_out;
  }








  sqlite4_close(db, 0);
  return TCL_OK;

error_out:
  Tcl_ResetResult(interp);
  Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
  return TCL_ERROR;