[Gambas-user] create custom user functions in SQLITE3 from Gambas

Benoit Minisini benoit.minisini at gambas-basic.org
Tue Apr 4 21:29:14 CEST 2023


Le 04/04/2023 à 20:51, Angel a écrit :
> hello
> 
> I am trying to create custom user functions in SQLITE3 from Gambas but 
> given my ignorance of C it is impossible.
> 
> Has anyone tried?The function in C is:
> 
> int sqlite3_create_function(
>    sqlite3 *db,
>    const char *zFunctionName,
>    int nArg,
>    int eTextRep,
>    void *pApp,
>    void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
>    void (*xStep)(sqlite3_context*,int,sqlite3_value**),
>    void (*xFinal)(sqlite3_context*)
> );
> 
> The explanation at: https://www.sqlite.org/c3ref/create_function.html
> 
> I've done:
> 
> 
> ' Gambas module file
> 
> Library "libsqlite3"
> Private Extern sqlite3_create_function(db As Pointer, name As String, 
> arg As Integer, eTextRep As Integer, pApp As Pointer, xFun As Pointer, 
> xStep As Pointer, xFinal As Pointer)
> Private BD As Connection
> 
> Public Sub Main()
> 
>      '#define SQLITE_UTF8     1
> 
>      Dim punt1 As Pointer
>      Dim punt2 As Pointer
>      Dim ia As Integer
> 
>      BD = New Connection
>      With BD
>          .Type = "sqlite3"
>          .Host = Application.Path
>          'verificating if db exist
>          .Name = ""
>          .Open
>      End With
> 
>      punt1 = VarPtr(BD)
>      Debug sepTelef("666777888")
>      Debug punt1
> 
>      ia = sqlite3_create_function(punt1, "sepTelef", 1, 1, Null, 
> sepTelef, Null, Null)
> 
> End
> 
> Function sepTelef(telefono As String) As String
> 
>      Dim scad As String
> 
>      scad = Mid(telefono, 1, 3) & "-" & Mid(telefono, 4, 3) & "-" & 
> Mid(telefono, 7, 3)
>      Return scad
> 
> End
> 
> 
> result
> 
> Segment violation 11
> 

'VarPtr(BD)' is the memory address where a pointer to a Gambas object 
(BD) is stored. It has nothing to do with the "sqlite3 *" pointer of the 
underlying connection. So It cannot work.

The Handle property of BD returns a pointer that points at a structure 
that represents a SQLite database to Gambas. But it's still not the 
"sqlite3 *" pointer you need.

Fortunately, this pointer is stored at the beginning of that structure. 
(you can know that by reading the source code of the 'gb.db.sqlite3' 
component).

So Try 'Pointer@(BD.Handle)' instead of 'VarPtr(BD)'.

And try a bit to understand how C works otherwise you won't really 
understand how external function management works in Gambas!

Regards,

-- 
Benoît Minisini.



More information about the User mailing list