aboutsummaryrefslogtreecommitdiffstats
path: root/plugins.go
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2024-06-10 01:13:23 +0000
committerterminaldweller <devi@terminaldweller.com>2024-06-10 01:13:23 +0000
commitd1af44bed4e714799b7dedab7cb2909ec7dffb8b (patch)
treed9349e463203405239956eb66c80ec1a61486ae9 /plugins.go
parentfixes #33 (diff)
downloadmilla-d1af44bed4e714799b7dedab7cb2909ec7dffb8b.tar.gz
milla-d1af44bed4e714799b7dedab7cb2909ec7dffb8b.zip
added a new lua function, query_db
Diffstat (limited to 'plugins.go')
-rw-r--r--plugins.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/plugins.go b/plugins.go
index cd99081..b25d1c4 100644
--- a/plugins.go
+++ b/plugins.go
@@ -9,6 +9,7 @@ import (
"github.com/ailncode/gluaxmlpath"
"github.com/cjoudrey/gluahttp"
"github.com/google/generative-ai-go/genai"
+ "github.com/jackc/pgx/v5"
"github.com/kohkimakimoto/gluayaml"
"github.com/lrstanley/girc"
openai "github.com/sashabaranov/go-openai"
@@ -258,6 +259,39 @@ func chatGPTRequestClosure(luaState *lua.LState, appConfig *TomlConfig) func(*lu
}
}
+func dbQueryClosure(luaState *lua.LState, appConfig *TomlConfig) func(*lua.LState) int {
+ return func(luaState *lua.LState) int {
+ if appConfig.pool == nil {
+ log.Println("Database connection is not available")
+
+ return 0
+ }
+
+ query := luaState.CheckString(1)
+
+ rows, err := appConfig.pool.Query(context.Background(), query)
+ if err != nil {
+ log.Println(err.Error())
+ }
+ defer rows.Close()
+
+ logs, err := pgx.CollectRows(rows, pgx.RowToStructByName[LogModel])
+ if err != nil {
+ log.Println(err.Error())
+ }
+
+ table := luaState.CreateTable(0, len(logs))
+
+ for index, log := range logs {
+ luaState.SetTable(table, lua.LNumber(index), lua.LString(log.Log))
+ }
+
+ luaState.Push(table)
+
+ return 1
+ }
+}
+
func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client, appConfig *TomlConfig) func(*lua.LState) int {
return func(luaState *lua.LState) int {
exports := map[string]lua.LGFunction{
@@ -267,6 +301,7 @@ func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client, appConf
"send_ollama_request": lua.LGFunction(ollamaRequestClosure(luaState, appConfig)),
"send_gemini_request": lua.LGFunction(geminiRequestClosure(luaState, appConfig)),
"send_chatgpt_request": lua.LGFunction(chatGPTRequestClosure(luaState, appConfig)),
+ "query_db": lua.LGFunction(dbQueryClosure(luaState, appConfig)),
}
millaModule := luaState.SetFuncs(luaState.NewTable(), exports)