From d1af44bed4e714799b7dedab7cb2909ec7dffb8b Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sun, 9 Jun 2024 21:13:23 -0400 Subject: added a new lua function, query_db --- README.md | 6 ++++++ plugins.go | 35 +++++++++++++++++++++++++++++++++++ plugins/test.lua | 16 +++++++++++++--- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 393ea5b..a450612 100644 --- a/README.md +++ b/README.md @@ -550,6 +550,8 @@ There are a few libraries written in go specifically for gopher-lua that are ava An example plugin is provided under `plugins/rss.lua`.
```yaml +period: 3600 +channel: "#rssfeed" rssfeeds: - name: "one" url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCaiL2GDNpLYH6Wokkk1VNcg" @@ -677,6 +679,10 @@ milla.send_gemini_request(prompt) milla.send_chatgpt_request(prompt) ``` +```lua +milla.query_db(query) +``` + The example rss plugin, accepts a yaml file as input, reeds the provided rss feeds once, extracts the title, author name and link to the resource, sends the feed over to the `#rssfeed` irc channel and exits.
More of milla's functionality will be available through milla's lua module over time.
' 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) diff --git a/plugins/test.lua b/plugins/test.lua index afd998c..fee865e 100644 --- a/plugins/test.lua +++ b/plugins/test.lua @@ -5,8 +5,7 @@ local function sleep(n) while os.clock() - t0 <= n do end end -local function printer() - +local function test_printer() local config = milla.toml_config.new() print(config:IrcServer()) config:IrcServer("irc.libera.chat") @@ -18,4 +17,15 @@ local function printer() end end -printer() +local function test_query() + local query = + "select log from liberanet_milla_us_market_news order by log desc;" + local result = milla.query_db(query) + + print(result) + + for _, v in ipairs(result) do print(v) end +end + +-- test_printer() +test_query() -- cgit v1.2.3