Skip to content

Commit d1af44b

Browse files
added a new lua function, query_db
1 parent 5f5ea88 commit d1af44b

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ There are a few libraries written in go specifically for gopher-lua that are ava
550550
An example plugin is provided under `plugins/rss.lua`.<br/>
551551

552552
```yaml
553+
period: 3600
554+
channel: "#rssfeed"
553555
rssfeeds:
554556
- name: "one"
555557
url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCaiL2GDNpLYH6Wokkk1VNcg"
@@ -677,6 +679,10 @@ milla.send_gemini_request(prompt)
677679
milla.send_chatgpt_request(prompt)
678680
```
679681

682+
```lua
683+
milla.query_db(query)
684+
```
685+
680686
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/>
681687

682688
More of milla's functionality will be available through milla's lua module over time.<br/>'

plugins.go

+35
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ailncode/gluaxmlpath"
1010
"github.com/cjoudrey/gluahttp"
1111
"github.com/google/generative-ai-go/genai"
12+
"github.com/jackc/pgx/v5"
1213
"github.com/kohkimakimoto/gluayaml"
1314
"github.com/lrstanley/girc"
1415
openai "github.com/sashabaranov/go-openai"
@@ -258,6 +259,39 @@ func chatGPTRequestClosure(luaState *lua.LState, appConfig *TomlConfig) func(*lu
258259
}
259260
}
260261

262+
func dbQueryClosure(luaState *lua.LState, appConfig *TomlConfig) func(*lua.LState) int {
263+
return func(luaState *lua.LState) int {
264+
if appConfig.pool == nil {
265+
log.Println("Database connection is not available")
266+
267+
return 0
268+
}
269+
270+
query := luaState.CheckString(1)
271+
272+
rows, err := appConfig.pool.Query(context.Background(), query)
273+
if err != nil {
274+
log.Println(err.Error())
275+
}
276+
defer rows.Close()
277+
278+
logs, err := pgx.CollectRows(rows, pgx.RowToStructByName[LogModel])
279+
if err != nil {
280+
log.Println(err.Error())
281+
}
282+
283+
table := luaState.CreateTable(0, len(logs))
284+
285+
for index, log := range logs {
286+
luaState.SetTable(table, lua.LNumber(index), lua.LString(log.Log))
287+
}
288+
289+
luaState.Push(table)
290+
291+
return 1
292+
}
293+
}
294+
261295
func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client, appConfig *TomlConfig) func(*lua.LState) int {
262296
return func(luaState *lua.LState) int {
263297
exports := map[string]lua.LGFunction{
@@ -267,6 +301,7 @@ func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client, appConf
267301
"send_ollama_request": lua.LGFunction(ollamaRequestClosure(luaState, appConfig)),
268302
"send_gemini_request": lua.LGFunction(geminiRequestClosure(luaState, appConfig)),
269303
"send_chatgpt_request": lua.LGFunction(chatGPTRequestClosure(luaState, appConfig)),
304+
"query_db": lua.LGFunction(dbQueryClosure(luaState, appConfig)),
270305
}
271306
millaModule := luaState.SetFuncs(luaState.NewTable(), exports)
272307

plugins/test.lua

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ local function sleep(n)
55
while os.clock() - t0 <= n do end
66
end
77

8-
local function printer()
9-
8+
local function test_printer()
109
local config = milla.toml_config.new()
1110
print(config:IrcServer())
1211
config:IrcServer("irc.libera.chat")
@@ -18,4 +17,15 @@ local function printer()
1817
end
1918
end
2019

21-
printer()
20+
local function test_query()
21+
local query =
22+
"select log from liberanet_milla_us_market_news order by log desc;"
23+
local result = milla.query_db(query)
24+
25+
print(result)
26+
27+
for _, v in ipairs(result) do print(v) end
28+
end
29+
30+
-- test_printer()
31+
test_query()

0 commit comments

Comments
 (0)