Platforms to show: All Mac Windows Linux Cross-Platform
/SQL/SQLDatabaseMBS SQLite custom function
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /SQL/SQLDatabaseMBS SQLite custom function
This example is the version from Tue, 29th Jan 2024.
Project "SQLDatabaseMBS SQLite custom function.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
// use internal sqlite library
call InternalSQLiteLibraryMBS.Use
// a test function with subclass
functions.Append New TestFunction
// and one with handler
Dim f As New SQLiteFunctionMBS
f.name = "SHA256"
f.ArgumentCount = 1
f.Flags = f.kFlagDeterministic OR f.kFlagUTF8 OR f.kFlagInnocuous
// Text encoding UTF8, function is deterministic and can be cached, function is innocuous as it depends only on parameters
AddHandler f.Perform, AddressOf PerformSHA256
functions.Append f // keep reference
dim db as new SQLDatabaseMBS
// where is the library?
'db.Option(SQLConnectionMBS.kOptionLibrarySQLite) = "/usr/lib/libsqlite3.0.dylib"
// connect to database
// in this example it is SQLite,
// but can also be Sybase, Oracle, Informix, DB2, SQLServer, InterBase, MySQL, SQLBase and ODBC
// in-memory database
Dim path As String = ":memory:"
db.DatabaseName = "sqlite:"+path
if db.Connect then
// try our Test function
Dim r As RecordSet = db.SQLSelect("SELECT test()")
if r = nil or r.eof then
MsgBox "Failed to query version."
else
MsgBox "Test function returned: "+r.IdxField(1).StringValue
End If
// try our SHA256 function
r = db.SQLSelect("SELECT hex(SHA256(""Hello World""))")
If r = Nil Or r.eof Then
MsgBox "Failed to query version."
Else
MsgBox "SHA256 function returned: "+r.IdxField(1).StringValue
End If
// should show A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E
end if
End EventHandler
Sub PerformSHA256(f as SQLiteFunctionMBS, ArgumentCount as Integer, Arguments() as Variant)
#Pragma BackgroundTasks False
#Pragma BoundsChecking False
#Pragma StackOverflowChecking False
Try
Dim a As Variant = Arguments(0)
Dim mem As MemoryBlock
If a IsA MemoryBlock Then
mem = a
Else
mem = a.StringValue
End If
Dim Hash As MemoryBlock = Crypto.SHA2_256(mem)
f.ResultBlob hash
Catch r As RuntimeException
f.ResultError "Exception: "+r.message
End Try
End Sub
Property Functions() As SQLiteFunctionMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Sign
End Sign
Class TestFunction Inherits SQLiteFunctionMBS
EventHandler Sub Perform(ArgumentCount as Integer, Arguments() as Variant)
#Pragma BackgroundTasks False
#Pragma BoundsChecking False
#Pragma StackOverflowChecking False
ResultText "Hello World"
End EventHandler
Sub Constructor()
// Calling the overridden superclass constructor.
Super.Constructor
Me.Name = "Test"
Me.ArgumentCount = 0
Me.Flags = Me.kFlagUTF8 OR Me.kFlagDeterministic OR Me.kFlagInnocuous
// Text encoding UTF8, function is deterministic and can be cached, function is innocuous as it depends only on parameters
End Sub
End Class
End Project
See also:
- /SQL/SQLDatabaseMBS CubeSQL select version
- /SQL/SQLDatabaseMBS Microsoft SQL Connect
- /SQL/SQLDatabaseMBS Microsoft SQL cross platform
- /SQL/SQLDatabaseMBS ODBC Connect
- /SQL/SQLDatabaseMBS PostgreSQL
- /SQL/SQLDatabaseMBS PostgreSQL Connect
- /SQL/SQLDatabaseMBS SQL Anywhere Connect
- /SQL/SQLDatabaseMBS SQLite Connect in Memory Database
- /SQL/SQLDatabaseMBS SQLite Encryption Fetch values
- /SQL/SQLDatabaseMBS SQLite ExecuteSQL threaded
The items on this page are in the following plugins: MBS SQL Plugin.