mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-31 22:10:51 +02:00
* Load extension demo by Silvio Clecio
git-svn-id: trunk@40610 -
This commit is contained in:
parent
51995c5cac
commit
c0e680e277
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -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
|
||||
|
49
packages/fcl-db/examples/myext.pp
Normal file
49
packages/fcl-db/examples/myext.pp
Normal 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.
|
40
packages/fcl-db/examples/sqlite3extdemo.pp
Normal file
40
packages/fcl-db/examples/sqlite3extdemo.pp
Normal 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.
|
Loading…
Reference in New Issue
Block a user