1*b65b3e42Sdrh /*
2*b65b3e42Sdrh ** 2017-04-16
3*b65b3e42Sdrh **
4*b65b3e42Sdrh ** The author disclaims copyright to this source code. In place of
5*b65b3e42Sdrh ** a legal notice, here is a blessing:
6*b65b3e42Sdrh **
7*b65b3e42Sdrh ** May you do good and not evil.
8*b65b3e42Sdrh ** May you find forgiveness for yourself and forgive others.
9*b65b3e42Sdrh ** May you share freely, never taking more than you give.
10*b65b3e42Sdrh **
11*b65b3e42Sdrh *************************************************************************
12*b65b3e42Sdrh **
13*b65b3e42Sdrh ** This file implements a run-time loadable extension to SQLite that
14*b65b3e42Sdrh ** registers a sqlite3_collation_needed() callback to register a fake
15*b65b3e42Sdrh ** collating function for any unknown collating sequence. The fake
16*b65b3e42Sdrh ** collating function works like BINARY.
17*b65b3e42Sdrh **
18*b65b3e42Sdrh ** This extension can be used to load schemas that contain one or more
19*b65b3e42Sdrh ** unknown collating sequences.
20*b65b3e42Sdrh */
21*b65b3e42Sdrh #include "sqlite3ext.h"
22*b65b3e42Sdrh SQLITE_EXTENSION_INIT1
23*b65b3e42Sdrh #include <string.h>
24*b65b3e42Sdrh
anyCollFunc(void * NotUsed,int nKey1,const void * pKey1,int nKey2,const void * pKey2)25*b65b3e42Sdrh static int anyCollFunc(
26*b65b3e42Sdrh void *NotUsed,
27*b65b3e42Sdrh int nKey1, const void *pKey1,
28*b65b3e42Sdrh int nKey2, const void *pKey2
29*b65b3e42Sdrh ){
30*b65b3e42Sdrh int rc, n;
31*b65b3e42Sdrh n = nKey1<nKey2 ? nKey1 : nKey2;
32*b65b3e42Sdrh rc = memcmp(pKey1, pKey2, n);
33*b65b3e42Sdrh if( rc==0 ) rc = nKey1 - nKey2;
34*b65b3e42Sdrh return rc;
35*b65b3e42Sdrh }
36*b65b3e42Sdrh
anyCollNeeded(void * NotUsed,sqlite3 * db,int eTextRep,const char * zCollName)37*b65b3e42Sdrh static void anyCollNeeded(
38*b65b3e42Sdrh void *NotUsed,
39*b65b3e42Sdrh sqlite3 *db,
40*b65b3e42Sdrh int eTextRep,
41*b65b3e42Sdrh const char *zCollName
42*b65b3e42Sdrh ){
43*b65b3e42Sdrh sqlite3_create_collation(db, zCollName, eTextRep, 0, anyCollFunc);
44*b65b3e42Sdrh }
45*b65b3e42Sdrh
46*b65b3e42Sdrh #ifdef _WIN32
47*b65b3e42Sdrh __declspec(dllexport)
48*b65b3e42Sdrh #endif
sqlite3_anycollseq_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)49*b65b3e42Sdrh int sqlite3_anycollseq_init(
50*b65b3e42Sdrh sqlite3 *db,
51*b65b3e42Sdrh char **pzErrMsg,
52*b65b3e42Sdrh const sqlite3_api_routines *pApi
53*b65b3e42Sdrh ){
54*b65b3e42Sdrh int rc = SQLITE_OK;
55*b65b3e42Sdrh SQLITE_EXTENSION_INIT2(pApi);
56*b65b3e42Sdrh rc = sqlite3_collation_needed(db, 0, anyCollNeeded);
57*b65b3e42Sdrh return rc;
58*b65b3e42Sdrh }
59