unit Unit_SQLite3Dataset_LanguageSupport;
{
SQLite3LanguageSupport made by totya from LacaK code, see forum:
http://forum.lazarus.freepascal.org/index.php/topic,34259.0.html
Usage: (after table created)
SQLite3LanguageSupport (Sqlite3Dataset1.SqliteHandle);
changelog:
v1.0 (2016.10.05 02:30) initial version
totya remarks:
- Tested with TSQLite3Dataset (rev34598) on Win7 x64, Lazarus 1.6
- The recommended sqlite3dyn unit doesn't need, but if present, I got sigsev error.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
sqlite3,
// sqlite3dyn,
ctypes;
procedure SQLite3Dataset_LanguageSupport (const _Handle: pointer);
implementation
const
SQLITE_UTF8= $01;
SQLITE_DETERMINISTIC =$800;
// http://forum.lazarus.freepascal.org/index.php/topic,34259.msg224029.html#msg224029
procedure UTF8xLower (ctx: psqlite3_context; {%H-}N: cint; V: ppsqlite3_value); cdecl;
var S: AnsiString;
begin
SetString(S, sqlite3_value_text(V[0]), sqlite3_value_bytes(V[0]));
S := UTF8Encode(AnsiLowerCase(UTF8Decode(S)));
sqlite3_result_text(ctx, PAnsiChar(S), Length(S), sqlite3_destructor_type(SQLITE_TRANSIENT));
end;
// utf8 case-sensitive compare callback function
// http://wiki.freepascal.org/SQLite#Creating_user_defined_collations
function UTF8xCompare_CS ({%H-}user: pointer; len1: longint; data1: pointer; len2: longint; data2: pointer): longint; cdecl;
var S1, S2: AnsiString;
begin
SetString(S1, data1, len1);
SetString(S2, data2, len2);
Result := UnicodeCompareStr(UTF8Decode(S1), UTF8Decode(S2));
end;
// utf8 case-insensitive compare callback function
// http://wiki.freepascal.org/SQLite#Creating_user_defined_collations
function UTF8xCompare_CIS ({%H-}user: pointer; len1: longint; data1: pointer; len2: longint; data2: pointer): longint; cdecl;
var S1, S2: AnsiString;
begin
SetString(S1, data1, len1);
SetString(S2, data2, len2);
Result := UnicodeCompareText(UTF8Decode(S1), UTF8Decode(S2));
end;
// http://forum.lazarus.freepascal.org/index.php/topic,34259.msg224029.html#msg224029
// http://forum.lazarus.freepascal.org/index.php/topic,34259.msg224152.html#msg224152
procedure SQLite3Dataset_LanguageSupport (const _Handle: pointer);
begin
if Assigned(_Handle)
then
begin
sqlite3_create_function
(_Handle,
'lower',
1,
SQLITE_UTF8 or SQLITE_DETERMINISTIC,
nil,
@UTF8xLower,
nil, nil);
sqlite3_create_collation(_Handle, 'UTF8_CS', SQLITE_UTF8, nil, @UTF8xCompare_CS);
sqlite3_create_collation(_Handle, 'UTF8_CIS', SQLITE_UTF8, nil, @UTF8xCompare_CIS);
end
else raise Exception.Create('Unassigned handle in SQLite3Dataset_LanguageSupport');
end;
initialization
finalization
end.