* Load extension demo by Silvio Clecio

git-svn-id: trunk@40610 -
This commit is contained in:
michael 2018-12-21 23:09:40 +00:00
parent 51995c5cac
commit c0e680e277
3 changed files with 91 additions and 0 deletions

2
.gitattributes vendored
View File

@ -2098,8 +2098,10 @@ packages/fcl-db/examples/loadlibdemo.lpi svneol=native#text/plain
packages/fcl-db/examples/loadlibdemo.pp svneol=native#text/plain
packages/fcl-db/examples/logsqldemo.lpi svneol=native#text/plain
packages/fcl-db/examples/logsqldemo.pas svneol=native#text/plain
packages/fcl-db/examples/myext.pp svneol=native#text/plain
packages/fcl-db/examples/pqeventstest.pp svneol=native#text/plain
packages/fcl-db/examples/showcsv.pp svneol=native#text/plain
packages/fcl-db/examples/sqlite3extdemo.pp svneol=native#text/plain
packages/fcl-db/examples/sqlite3loadlib.lpr svneol=native#text/plain
packages/fcl-db/examples/sqlparser.pp svneol=native#text/plain
packages/fcl-db/examples/tsamytable.pp svneol=native#text/plain

View File

@ -0,0 +1,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.

View File

@ -0,0 +1,40 @@
program test;
{$mode objfpc}{$H+}
uses
sysutils,
sqlite3conn,
sqlite3ext,
sqldb;
const
SharedPrefix = {$ifdef mswindows}''{$else}'lib'{$endif};
var
con: TSQLite3Connection;
trans: TSQLTransaction;
q: TSQLQuery;
begin
con := TSQLite3Connection.Create(nil);
trans := TSQLTransaction.Create(con);
q := TSQLQuery.Create(con);
try
trans.DataBase := con;
q.DataBase := con;
q.Transaction := trans;
con.DatabaseName := 'test.sqlite3';
con.Open;
con.LoadExtension(ExtractFilePath(ParamStr(0)) +
SharedPrefix + 'myext.' + SharedSuffix);
q.SQL.Text := 'SELECT mysum(2, 3);';
q.Open;
WriteLn('MYSUM: ', q.Fields[0].AsInteger); // prints "MYSUM: 5"
q.Close;
q.SQL.Text := 'SELECT myconcat(''abc'', ''123'');';
q.Open;
WriteLn('MYCONCAT: ', q.Fields[0].AsString); // prints "MYCONCAT: abc123"
finally
con.Free;
end;
end.