aboutsummaryrefslogtreecommitdiffstats
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
parentfixes #33 (diff)
downloadmilla-d1af44bed4e714799b7dedab7cb2909ec7dffb8b.tar.gz
milla-d1af44bed4e714799b7dedab7cb2909ec7dffb8b.zip
added a new lua function, query_db
Diffstat (limited to '')
-rw-r--r--README.md6
-rw-r--r--plugins.go35
-rw-r--r--plugins/test.lua16
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`.<br/>
```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.<br/>
More of milla's functionality will be available through milla's lua module over time.<br/>'
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()