-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_utils.go
More file actions
379 lines (326 loc) · 9.2 KB
/
lua_utils.go
File metadata and controls
379 lines (326 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
lua "github.com/yuin/gopher-lua"
luajson "layeh.com/gopher-json"
)
// Global Lua state
var (
globalLuaState *lua.LState
luaStateMutex sync.Mutex
)
// InitializeLuaState initializes the global Lua state if it hasn't been initialized yet
func InitializeLuaState() {
luaStateMutex.Lock()
defer luaStateMutex.Unlock()
if globalLuaState == nil {
globalLuaState = lua.NewState()
luajson.Preload(globalLuaState)
registerSQLFunctions(globalLuaState)
registerHTTPFunctions(globalLuaState)
}
}
// CloseLuaState closes the global Lua state
func CloseLuaState() {
luaStateMutex.Lock()
defer luaStateMutex.Unlock()
if globalLuaState != nil {
globalLuaState.Close()
globalLuaState = nil
}
}
// GetLuaState returns the global Lua state
func GetLuaState() *lua.LState {
luaStateMutex.Lock()
defer luaStateMutex.Unlock()
if globalLuaState == nil {
InitializeLuaState()
}
return globalLuaState
}
// FetchLuaScriptContent reads Lua script content from either a file or URL
func FetchLuaScriptContent(source string) ([]byte, error) {
// Check if the source is a URL
if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") {
resp, err := http.Get(source)
if err != nil {
return nil, fmt.Errorf("failed to fetch URL: %w", err)
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return content, nil
}
// If not a URL, treat as a local file
content, err := os.ReadFile(source)
if err != nil {
return nil, fmt.Errorf("failed to read Lua script file: %w", err)
}
return content, nil
}
// ExecuteLuaScript executes a Lua script with the given arguments
func ExecuteLuaScript(script string, args []string, resultWriter io.Writer) error {
L := GetLuaState()
// Create arg table for Lua script
argTable := L.NewTable()
for i, arg := range args {
argTable.RawSetInt(i+1, lua.LString(arg))
}
L.SetGlobal("args", argTable)
// Execute the Lua script
if err := L.DoString(script); err != nil {
return fmt.Errorf("lua execution error: %v", err)
}
// Get the return value from the Lua script
returnValue := L.Get(-1)
// Print the return value
log.Println("Lua script executed successfully, return value:", returnValue)
return nil
}
// registerSQLFunctions registers SQL functions in the Lua state
func registerSQLFunctions(L *lua.LState) {
sqlTable := L.NewTable()
// Register query function
sqlTable.RawSetString("query", L.NewFunction(func(L *lua.LState) int {
query := L.ToString(1)
result := L.NewTable()
result.RawSetString("ok", lua.LBool(true))
result.RawSetString("error", lua.LString(""))
conn := GetDB()
if conn == nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString("database connection is not available, please connect first using .connect command"))
L.Push(result)
return 1
}
rows, err := conn.Query(query)
if err != nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString(err.Error()))
L.Push(result)
return 1
}
defer rows.Close()
// Get column types
columns, err := rows.Columns()
if err != nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString(err.Error()))
L.Push(result)
return 1
}
// Create a slice of interface{} to hold the values
values := make([]interface{}, len(columns))
valuePtrs := make([]interface{}, len(columns))
for i := range columns {
valuePtrs[i] = &values[i]
}
// Create result table
resultTable := L.NewTable()
// Add header row
headerRow := L.NewTable()
for i, col := range columns {
headerRow.RawSetInt(i+1, lua.LString(col))
}
// Add data rows
rowIndex := 1
for rows.Next() {
err := rows.Scan(valuePtrs...)
if err != nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString(err.Error()))
L.Push(result)
return 1
}
// Create row table
rowTable := L.NewTable()
for i, v := range values {
var luaValue lua.LValue
switch val := v.(type) {
case []byte:
luaValue = lua.LString(string(val))
case nil:
// nil is not a valid Lua value, so we use LNil
luaValue = lua.LNil
case int64:
luaValue = lua.LNumber(val)
case float64:
luaValue = lua.LNumber(val)
case bool:
luaValue = lua.LBool(val)
case time.Time:
luaValue = lua.LString(val.Format("2006-01-02 15:04:05"))
default:
luaValue = lua.LString(fmt.Sprintf("%v", val))
}
rowTable.RawSetInt(i+1, luaValue)
}
resultTable.RawSetInt(rowIndex, rowTable)
rowIndex++
}
// Set the data in the result object
result.RawSetString("data", resultTable)
result.RawSetString("columns", headerRow)
result.RawSetString("row_count", lua.LNumber(rowIndex-1))
L.Push(result)
return 1
}))
// Register execute function
sqlTable.RawSetString("execute", L.NewFunction(func(L *lua.LState) int {
query := L.ToString(1)
result := L.NewTable()
result.RawSetString("ok", lua.LBool(true))
result.RawSetString("error", lua.LString(""))
conn := GetDB()
if conn == nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString("database connection is not available, please connect first using .connect command"))
L.Push(result)
return 1
}
res, err := conn.Exec(query)
if err != nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString(err.Error()))
L.Push(result)
return 1
}
rowsAffected, err := res.RowsAffected()
if err != nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString(err.Error()))
L.Push(result)
return 1
}
lastInsertId, err := res.LastInsertId()
if err != nil {
result.RawSetString("ok", lua.LBool(false))
result.RawSetString("error", lua.LString(err.Error()))
L.Push(result)
return 1
}
// Set the data in the result object
result.RawSetString("rows_affected", lua.LNumber(rowsAffected))
result.RawSetString("last_insert_id", lua.LNumber(lastInsertId))
L.Push(result)
return 1
}))
L.SetGlobal("sql", sqlTable)
}
// registerHTTPFunctions registers HTTP functions in the Lua state
func registerHTTPFunctions(L *lua.LState) {
httpTable := L.NewTable()
// Register do function
httpTable.RawSetString("fetch", L.NewFunction(func(L *lua.LState) int {
// Get parameters
method := L.ToString(1)
url := L.ToString(2)
headers := L.ToTable(3)
body := L.ToString(4)
callback := L.ToFunction(5)
// Create HTTP client
client := &http.Client{}
// Create request
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
L.Push(lua.LBool(false))
L.Push(lua.LString(err.Error()))
return 2
}
// Add headers if provided
if headers != nil {
headers.ForEach(func(k, v lua.LValue) {
req.Header.Add(k.String(), v.String())
})
}
// If callback is provided, do async request
if callback != nil {
// Create new goroutine for async execution
go func() {
// Execute request
resp, err := client.Do(req)
if err != nil {
// Schedule callback execution in main thread
L.Push(callback)
L.Push(lua.LBool(false))
L.Push(lua.LString(err.Error()))
L.CallByParam(lua.P{
Fn: callback,
NRet: 0,
Protect: true,
}, lua.LBool(false), lua.LString(err.Error()))
return
}
defer resp.Body.Close()
// Read response body
respBody, err := io.ReadAll(resp.Body)
if err != nil {
L.CallByParam(lua.P{
Fn: callback,
NRet: 0,
Protect: true,
}, lua.LBool(false), lua.LString(err.Error()))
return
}
// Create response table
responseTable := L.NewTable()
responseTable.RawSetString("status_code", lua.LNumber(resp.StatusCode))
responseTable.RawSetString("body", lua.LString(string(respBody)))
// Create headers table
headersTable := L.NewTable()
for k, v := range resp.Header {
if len(v) > 0 {
headersTable.RawSetString(k, lua.LString(v[0]))
}
}
responseTable.RawSetString("headers", headersTable)
// Schedule callback execution in main thread
L.CallByParam(lua.P{
Fn: callback,
NRet: 0,
Protect: true,
}, lua.LBool(true), responseTable)
}()
return 0
}
// Synchronous execution (no callback)
resp, err := client.Do(req)
if err != nil {
L.Push(lua.LBool(false))
L.Push(lua.LString(err.Error()))
return 2
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
L.Push(lua.LBool(false))
L.Push(lua.LString(err.Error()))
return 2
}
// Create response table
responseTable := L.NewTable()
responseTable.RawSetString("status_code", lua.LNumber(resp.StatusCode))
responseTable.RawSetString("body", lua.LString(string(respBody)))
// Create headers table
headersTable := L.NewTable()
for k, v := range resp.Header {
if len(v) > 0 {
headersTable.RawSetString(k, lua.LString(v[0]))
}
}
responseTable.RawSetString("headers", headersTable)
L.Push(lua.LBool(true))
L.Push(responseTable)
return 2
}))
L.SetGlobal("http", httpTable)
}