*** DRAFT ***

SQLite C Interface

Define New Collating Sequences

int sqlite3_create_collation(
  sqlite3*,
  const char *zName,
  int eTextRep,
  void *pArg,
  int(*xCompare)(void*,int,const void*,int,const void*)
);
int sqlite3_create_collation_v2(
  sqlite3*,
  const char *zName,
  int eTextRep,
  void *pArg,
  int(*xCompare)(void*,int,const void*,int,const void*),
  void(*xDestroy)(void*)
);
int sqlite3_create_collation16(
  sqlite3*,
  const void *zName,
  int eTextRep,
  void *pArg,
  int(*xCompare)(void*,int,const void*,int,const void*)
);

R-40390-09039:[These functions add, remove, or modify a collation associated with the database connection specified as the first argument. ]

R-59626-52827:[The name of the collation is a UTF-8 string for sqlite3_create_collation() and sqlite3_create_collation_v2() and a UTF-16 string in native byte order for sqlite3_create_collation16(). ] R-04232-24861:[Collation names that compare equal according to sqlite3_strnicmp() are considered to be the same name. ]

R-21508-14355:[The third argument (eTextRep) must be one of the constants:

] R-08137-02757:[The eTextRep argument determines the encoding of strings passed to the collating function callback, xCompare. ] R-33414-20006:[The SQLITE_UTF16 and SQLITE_UTF16_ALIGNED values for eTextRep force strings to be UTF16 with native byte order. ] R-62291-37402:[The SQLITE_UTF16_ALIGNED value for eTextRep forces strings to begin on an even byte address. ]

R-32806-65233:[The fourth argument, pArg, is an application data pointer that is passed through as the first argument to the collating function callback. ]

R-06154-44456:[The fifth argument, xCompare, is a pointer to the collating function. ] R-17004-26469:[Multiple collating 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. ] R-00811-45908:[If the xCompare argument is NULL then the collating function is deleted. ] R-63294-00538:[When all collating functions having the same name are deleted, that collation is no longer usable. ]

R-42196-45693:[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 two integer parameters to the collating function callback are the length of the two strings, in bytes. 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:

  1. If A==B then B==A.
  2. If A==B and B==C then A==C.
  3. If A<B THEN B>A.
  4. If A<B and B<C then A<C.

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.

R-05416-57569:[The sqlite3_create_collation_v2() works like sqlite3_create_collation() with the addition that the xDestroy callback is invoked on pArg when the collating function is deleted. ] R-59553-21705:[Collating functions are deleted when they are overridden by later calls to the collation creation functions or when the database connection is closed using sqlite3_close(). ]

R-16618-15294:[The xDestroy callback is not called if the sqlite3_create_collation_v2() function fails. ] Applications that invoke sqlite3_create_collation_v2() 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: sqlite3_collation_needed() and sqlite3_collation_needed16().

See also lists of Objects, Constants, and Functions.

*** DRAFT ***