SQLite4
Check-in [bcf7a78f8b]
Not logged in

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

Overview
Comment:Update create_collation() documentation. Have the create_collation() function invoke the destructor (if one was specified) before returning if it fails.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: bcf7a78f8b2e332d5a7480950df33e7b7f577825
User & Date: dan 2013-06-14 17:16:04
Context
2013-06-14
18:28
Have sqlite4_authorizer_push() invoke any destructor passed to it if an error (i.e. a malloc failure) occurs. check-in: cb28262fc8 user: dan tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/main.c.

1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350

1351
1352
1353
1354
1355
1356
1357

/*
** 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.







|









>







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

/*
** 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);
  if( rc!=SQLITE4_OK && xDel ) xDel(pCtx);
  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.

Changes to src/sqlite.h.in.

2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745

/*
** CAPIREF: Define New Collation Sequences
**
** ^This function adds, removes, or modifies a [collation] associated
** with the [database connection] specified as the first argument.
**
** ^The name of the collation is a UTF-8 string.
** ^Collation names that compare equal according to [sqlite4_strnicmp()] are
** considered to be the same name.
**
** ^(The third argument (eTextRep) must be one of the constants:
** <ul>
** <li> [SQLITE4_UTF8],
** <li> [SQLITE4_UTF16LE],
** <li> [SQLITE4_UTF16BE],
** <li> [SQLITE4_UTF16], or
** <li> [SQLITE4_UTF16_ALIGNED].
** </ul>)^
** ^The eTextRep argument determines the encoding of strings passed
** to the collating function callback, xCallback.
** ^The [SQLITE4_UTF16] and [SQLITE4_UTF16_ALIGNED] values for eTextRep
** force strings to be UTF16 with native byte order.
** ^The [SQLITE4_UTF16_ALIGNED] value for eTextRep forces strings to begin
** on an even byte address.
**
** ^The fourth argument, pArg, is an application data pointer that is passed
** through as the first argument to the collating function callback.
**
** ^The fifth argument, xCallback, is a pointer to the comparision function.
** ^The sixth argument, xMakeKey, is a pointer to a function that generates
** a sort key.
** ^Multiple functions can be registered using the same name but
** with different eTextRep parameters and SQLite will use whichever
** function requires the least amount of data transformation.
** ^If the xCallback argument is NULL then the collating function is
** deleted.  ^When all collating functions having the same name are deleted,
** that collation is no longer usable.
**
** ^The collating function callback is invoked with a copy of the pArg 
** application data pointer and with two strings in the encoding specified
** by the eTextRep argument.  The collating function must return an
** integer that is negative, zero, or positive
** if the first string is less than, equal to, or greater than the second,
** respectively.  A collating function must always return the same answer
** given the same inputs.  If two or more collating functions are registered
** to the same collation name (using different eTextRep values) then all
** must give an equivalent answer when invoked with equivalent strings.
** The collating function must obey the following properties for all
** strings A, B, and C:
**
** <ol>
** <li> If A==B then B==A.
** <li> If A==B and B==C then A==C.
** <li> If A&lt;B THEN B&gt;A.
** <li> If A&lt;B and B&lt;C then A&lt;C.
** </ol>
**
** If a collating function fails any of the above constraints and that
** collating function is  registered and used, then the behavior of SQLite
** is undefined.
**
** ^Collating functions are deleted when they are overridden by later
** calls to the collation creation functions or when the
** [database connection] is closed using [sqlite4_close()].
**
** ^The xDestroy callback is <u>not</u> called if the 
** sqlite4_create_collation() function fails.  Applications that invoke
** sqlite4_create_collation() with a non-NULL xDestroy argument should 
** check the return code and dispose of the application data pointer
** themselves rather than expecting SQLite to deal with it for them.
** This is different from every other SQLite interface.  The inconsistency 
** is unfortunate but cannot be changed without breaking backwards 
** compatibility.
**
** See also:  [sqlite4_collation_needed()] and [sqlite4_collation_needed16()].
*/
int sqlite4_create_collation(
  sqlite4*, 
  const char *zName, 
  void *pArg,







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745

/*
** CAPIREF: Define New Collation Sequences
**
** ^This function adds, removes, or modifies a [collation] associated
** with the [database connection] specified as the first argument.
**
** ^The name of the collation is passed as the second argument. Collation
** names are case-independent, any two names that are equal according 
** to [sqlite4_strnicmp()] are considered to refer to the same collation.
**
** ^The third argument, pArg, is an application data pointer. A copy of
** this pointer is passed as the first argument to the two collation
** callback functions whenever they are invoked.
**
** The fourth argument passed to sqlite4_create_collation() is a comparison
** routine. The two sqlite4_value* arguments passed always point to objects
** containing values of type TEXT. The final argument (type "int*") is an
** output parameter. Assuming no error occurs, the callback implementation
** is expected to set the output parameter to a negative value, zero, or
** a positive value if the first sqlite4_value argument is smaller, equal
** to, or larger than the second, respectively, before returning SQLITE4_OK.
** Or, if an error does occur, an SQLite error code should be returned.
** The value of the output paramete is ignored by SQLite if any error occurs.
**
** The fifth argument, xMakeKey, is a pointer to a function that generates
** a sort key based on the sqlite4_value* passed as its second parameter.
** The second parameter always points to an object containing a value of
** type text. The fifth parameter passed to xMakeKey is an output parameter.
** Assuming no error occurs, the xMakeKey implementation should set the 
** value of the output parameter to the size of the sort key in bytes and
** return SQLITE4_OK. Or, if an error does occur, an error code should be
** returned. The output parameter is ignored in this case.
**
** The third and fourth parameters passed to each invocation of the xMakeKey
** function are the size of and a pointer to an output buffer, respectively.
** If the output buffer is large enough to store the entire sort key, then
** the xMakeKey implementation should populate it with the sort key value
** before returning. Otherwise, if it is not large enough, SQLite will
** detect this and invoke the xMakeKey callback a second time with a larger
** buffer.
**
** The xCompare and xMakeKey callbacks must return consistent results.
** Specifically, according to xCompare, for any text values A, B and C:
**
** <ol>
**   <li> If A==B then B==A.
**   <li> If A==B and B==C then A==C.
**   <li> If A&lt;B then B&gt;A.
**   <li> If A&lt;B and B&lt;C then A&lt;C.
** </ol>
**
** Additionally, for two text values A and B where A is smaller than, equal 
** to, or larger than B, the sort key of A must also be smaller than, equal
** to, or larger than the sort key of B, respectively. Sort keys are 
** compared using memcmp(). If one sort key is a prefix of another, it is
** considered to be smaller.
**
** If a collation does not behave as described above, the behaviour of
** SQLite is undefined.
**
** If it is not NULL, the final argument passed to sqlite4_create_collation()
** is a pointer to a destructor function. The destructor function is invoked
** to indicate that SQLite is no longer using the collation, either because
** it has been replaced by a new collation of the same name or because the
** database connection is being closed. The caller should use the destructor
** function to dispose of any dynamically allocated resources associated with
** the collation. The destructor function is invoked exactly once for each
** call to sqlite4_create_collation.
**
** If the sqlite4_create_collation call fails for any reason, an error code
** is returned and the collation sequence is not registered with the database
** handle. If a destructor function was provided, then in this case it is
** invoked before the sqlite4_create_collation() function returns.
**
** See also:  [sqlite4_collation_needed()] and [sqlite4_collation_needed16()].
*/
int sqlite4_create_collation(
  sqlite4*, 
  const char *zName, 
  void *pArg,

Changes to test/collate7.test.

5
6
7
8
9
10
11
12
13
14
15
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


# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is the experimental sqlite4_create_collation()
# API.
#
# $Id: collate7.test,v 1.2 2008/07/12 14:52:20 drh Exp $


set testdir [file dirname $argv0]
source $testdir/tester.tcl


set ::caseless_del 0



proc caseless_cmp {zLeft zRight} {
  string compare -nocase $zLeft $zRight
}







do_test collate7-1.1 {

  set cmd [list incr ::caseless_del]
  sqlite4_create_collation db CASELESS caseless_cmp $cmd
  set ::caseless_del
} {0}

do_test collate7-1.2 {

  sqlite_delete_collation db CASELESS
  set ::caseless_del
} {1}
do_test collate7-1.3 {

  catchsql {
    CREATE TABLE abc(a COLLATE CASELESS, b, c);
  }
} {1 {no such collation sequence: CASELESS}}

do_test collate7-1.4 {

  sqlite4_create_collation db CASELESS caseless_cmp {incr ::caseless_del}
  db close
  set ::caseless_del
} {2}




do_test collate7-2.1 {
  forcedelete test.db test.db-journal
  sqlite4 db test.db


  sqlite4_create_collation db CASELESS caseless_cmp {incr ::caseless_del}
  execsql {
    PRAGMA encoding='utf-16';
    CREATE TABLE abc16(a COLLATE CASELESS, b, c);
  } db

  set ::caseless_del
} {2}








do_test collate7-2.2 {



  execsql {
    SELECT * FROM abc16 WHERE a < 'abc';



  }






  set ::caseless_del
} {2}
do_test collate7-2.3 {
  sqlite_delete_collation db CASELESS
  set ::caseless_del


} {3}





do_test collate7-2.4 {
  catchsql {
    SELECT * FROM abc16 WHERE a < 'abc';


  }
} {1 {no such collation sequence: CASELESS}}




finish_test









|
|
|
|
|
>



>

<
>
>
>



>
>
|
>
>
>
>
|
>
|
|


>
|
>
|


<
>
|
|
<

>
|
>
|
<

|

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

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

<
>
>
>

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


>
>
5
6
7
8
9
10
11
12
13
14
15
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
73
74
75
76
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
111
112
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements regression tests for SQLite library. The
# focus of this script is the sqlite4_create_collation() API. More
# specifically, the focus is on testing that collation destructor
# functions are invoked correctly.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set ::testprefix collate7



# Implementation of collation sequence callbacks.
#
proc caseless_cmp {zLeft zRight} {
  string compare -nocase $zLeft $zRight
}
proc caseless_mkkey {zIn} {
  string tolower $zIn
}

#-------------------------------------------------------------------------
# Test the destructor is invoked when a collation sequence is overridden.
#
do_test 1.1 {
  set ::caseless_del 0
  set del [list incr ::caseless_del]
  sqlite4_create_collation db CASELESS caseless_cmp caseless_mkkey $del
  set ::caseless_del
} {0}

do_test 1.2 {
  set ::caseless_del 0
  sqlite4_create_collation db CASELESS {} {} {}
  set ::caseless_del
} {1}


do_catchsql_test 1.3 { 
  CREATE TABLE abc(a COLLATE CASELESS, b, c); 

} {1 {no such collation sequence: CASELESS}}

do_test 1.4 {
  set ::caseless_del 0
  sqlite4_create_collation db CASELESS {} {} {}

  set ::caseless_del
} {0}

#-------------------------------------------------------------------------
# Test the destructor is invoked when the database handle is closed.
#
do_test 2.1 {


  set ::caseless_del 0
  set del [list incr ::caseless_del]
  sqlite4_create_collation db CASELESS caseless_cmp caseless_mkkey $del




  db close
  set ::caseless_del
} {1}

#-------------------------------------------------------------------------
# Test the destructor is invoked if an error occurs within
# sqlite4_create_collation(). The easiest error to provoke here is 
# SQLITE4_BUSY - which is returned if an attempt is made to override a
# collation while there are active statements.
#
reset_db
do_test 3.1 {
  set ::caseless_del 0
  set del [list incr ::caseless_del]
  sqlite4_create_collation db CASELESS caseless_cmp caseless_mkkey $del
  execsql {

    CREATE TABLE t1(a COLLATE caseless);
    INSERT INTO t1 VALUES('abc');
    INSERT INTO t1 VALUES('def');
  }

  set ::stmt [sqlite4_prepare db "SELECT a FROM t1" -1 DUMMY]
  sqlite4_step $::stmt
} {SQLITE4_ROW}

do_test 3.2 {
  set ::caseless_del2 0



  set del [list incr ::caseless_del2]
  set rc [catch {
    sqlite4_create_collation db CASELESS caseless_cmp caseless_mkkey $del
  } msg]
  
  list $::caseless_del $caseless_del2 $rc $msg
} {0 1 1 SQLITE4_BUSY}

do_test 3.3 { sqlite4_errcode db } {SQLITE4_BUSY}
do_test 3.4 { 


  sqlite4_errmsg db 
} {unable to delete/modify collation sequence due to active statements}


do_test 3.5 { sqlite4_finalize $::stmt } {SQLITE4_OK}
do_test 3.6 { sqlite4_errcode db       } {SQLITE4_OK}
do_test 3.7 { sqlite4_errmsg db        } {not an error}

finish_test


Changes to test/test_main.c.

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
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383

1384
1385
1386
1387
1388
1389
1390
....
1393
1394
1395
1396
1397
1398
1399





































1400
1401
1402

1403
1404
1405




1406
1407
1408

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
1434
1435
1436
1437

  return TCL_OK;
}
#endif


/*
** Usage: sqlite4_create_collation DB-HANDLE NAME CMP-PROC DEL-PROC
**
**   This Tcl proc is used for testing the experimental
**   sqlite4_create_collation() interface.
*/
struct TestCollationX {
  Tcl_Interp *interp;
  Tcl_Obj *pCmp;

  Tcl_Obj *pDel;
};
typedef struct TestCollationX TestCollationX;

static void testCreateCollationDel(void *pCtx){
  TestCollationX *p = (TestCollationX *)pCtx;

  int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  if( rc!=TCL_OK ){
    Tcl_BackgroundError(p->interp);
  }

  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);
................................................................................

  if( rc!=TCL_OK ){
    return SQLITE4_ERROR;
  }
  *pRes = iRes;
  return SQLITE4_OK;
}





































static int test_create_collation(
  ClientData clientData, /* Not used */
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */

  int objc,              /* Number of arguments */
  Tcl_Obj *CONST objv[]  /* Command arguments */
){




  TestCollationX *p;
  sqlite4 *db;
  int rc;


  if( objc!=5 ){
    Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
    return TCL_ERROR;
  }
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;









  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:
**
**   -func    SCRIPT







|
<
<
<




>



>










>


>












|
|
|





>







 







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

|
<
>
|
|

>
>
>
>
|
|
<
>

|
|




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

|
|
|
>
>

>


>


>
>







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
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
....
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
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
1434
1435
1436
1437
1438
1439
1440

1441
1442
1443
1444
1445
1446
1447
1448
1449
1450

1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496

  return TCL_OK;
}
#endif


/*
** Usage: sqlite4_create_collation DB NAME CMP-PROC MKKEY-PROC DEL-PROC



*/
struct TestCollationX {
  Tcl_Interp *interp;
  Tcl_Obj *pCmp;
  Tcl_Obj *pMkkey;
  Tcl_Obj *pDel;
};
typedef struct TestCollationX TestCollationX;

static void testCreateCollationDel(void *pCtx){
  TestCollationX *p = (TestCollationX *)pCtx;

  int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  if( rc!=TCL_OK ){
    Tcl_BackgroundError(p->interp);
  }

  Tcl_DecrRefCount(p->pCmp);
  Tcl_DecrRefCount(p->pDel);
  Tcl_DecrRefCount(p->pMkkey);
  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 script to evaluate */
  int iRes = 0;                   /* Result of comparison */
  int rc;                         /* Tcl_Eval() return code */

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

  pScript = Tcl_DuplicateObj(p->pCmp);
  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);
................................................................................

  if( rc!=TCL_OK ){
    return SQLITE4_ERROR;
  }
  *pRes = iRes;
  return SQLITE4_OK;
}

static int testCreateCollationMkkey(
  void *pCtx,                     /* Context pointer */
  sqlite4_value *pVal,            /* Value to create sort-key for */
  int nBuf,                       /* Size of output buffer in bytes */
  void *pBuf,                     /* Output buffer */
  int *pnOut                      /* Size of sort-key in bytes */
){
  TestCollationX *p = (TestCollationX *)pCtx;
  Tcl_Obj *pScript;
  const char *zIn;
  int nIn;
  int rc;
  const char *zOut;
  int nOut;

  zIn = sqlite4_value_text(pVal, &nIn);
  if( !zIn ) return SQLITE4_NOMEM;
  
  pScript = Tcl_DuplicateObj(p->pMkkey);
  Tcl_IncrRefCount(pScript);
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zIn, nIn));

  rc = Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  Tcl_DecrRefCount(pScript);
  if( rc!=TCL_OK ){
    return SQLITE4_ERROR;
  }

  zOut = Tcl_GetStringFromObj(Tcl_GetObjResult(p->interp), &nOut);
  if( nOut<=nBuf ){
    memcpy(pBuf, zOut, nOut);
  }
  *pnOut = nOut;
  return SQLITE4_OK;
}

static int test_create_collation(
  ClientData clientData,          /* Not used */

  Tcl_Interp *interp,             /* The TCL interpreter */
  int objc,                       /* Number of arguments */
  Tcl_Obj *CONST objv[]           /* Command arguments */
){
  /* Callback functions */
  int(*xCompare)(void*, sqlite4_value*, sqlite4_value*, int*);
  int(*xMakeKey)(void*, sqlite4_value*, int, void*, int*);
  int nByte;                      /* Size of callback scripts in bytes */
  TestCollationX *p;              /* New context object */
  sqlite4 *db;                    /* Database handle */

  int rc;                         /* create_collation() return code */

  if( objc!=6 ){
    Tcl_WrongNumArgs(interp, 1, objv, "DB NAME CMP-PROC MKKEY-PROC DEL-PROC");
    return TCL_ERROR;
  }
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;

  Tcl_GetStringFromObj(objv[3], &nByte);
  xCompare = (nByte>0 ? testCreateCollationCmp : 0);
  Tcl_GetStringFromObj(objv[4], &nByte);
  xMakeKey = (nByte>0 ? testCreateCollationMkkey : 0);

  if( xCompare==0 && xMakeKey==0 ){
    rc = sqlite4_create_collation(db, Tcl_GetString(objv[2]), 0, 0, 0, 0);
  }else{
    p = (TestCollationX *)sqlite4_malloc(0, sizeof(TestCollationX));
    p->pCmp = objv[3];
    p->pMkkey = objv[4];
    p->pDel = objv[5];
    p->interp = interp;
    Tcl_IncrRefCount(p->pCmp);
    Tcl_IncrRefCount(p->pMkkey);
    Tcl_IncrRefCount(p->pDel);

    rc = sqlite4_create_collation(db, Tcl_GetString(objv[2]), (void *)p,
        testCreateCollationCmp, testCreateCollationMkkey, testCreateCollationDel
    );
  }

  if( rc!=SQLITE4_OK ){
    Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite4TestErrorName(rc), -1));
    return TCL_ERROR;
  }
  Tcl_ResetResult(interp);
  return TCL_OK;
}
/* End of [sqlite4_create_collation] implementation.
********************************************************************/

/*
** USAGE: sqlite4_create_function_v2 DB NAME NARG ENC ?SWITCHES?
**
** Available switches are:
**
**   -func    SCRIPT