summaryrefslogtreecommitdiff
path: root/packages/sqlite/examples/myext.pp
blob: 7a5bc97e5ed90346c48c7e400d7cae423fad7fec (plain)
1
2
3
4
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
library myext;

{$mode objfpc}{$h+}

uses
  sysutils,
  ctypes,
  sqlite3,
  sqlite3ext;

procedure mysum(ctx: psqlite3_context; n: cint; v: ppsqlite3_value); cdecl;
var
  a, b, r: cint;
begin
  a := sqlite3_value_int(v[0]);
  b := sqlite3_value_int(v[1]);
  r := a + b;
  sqlite3_result_int(ctx, r);
end;

procedure myconcat(ctx: psqlite3_context; n: cint; v: ppsqlite3_value); cdecl;
var
  a, b, r: ansistring;
begin
  a := sqlite3_value_text(v[0]);
  b := sqlite3_value_text(v[1]);
  r := a + b;
  sqlite3_result_text(ctx, @r[1], length(r), nil);
end;

function sqlite3_extension_init(db: Psqlite3; pzErrMsg: Ppcchar;
  const pApi: Psqlite3_api_routines): cint; cdecl; export;
var
  rc: cint;
begin
  SQLITE_EXTENSION_INIT2(pApi);
  rc := sqlite3_create_function(db, 'mysum', 2, SQLITE_UTF8, nil,
    @mysum, nil, nil);
  if rc = SQLITE_OK then
    Result := sqlite3_create_function(db, 'myconcat', 2, SQLITE_UTF8, nil,
      @myconcat, nil, nil);
  Result := rc;
end;

exports
  sqlite3_extension_init;

begin
end.