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