How to add function output to a variable using luasql

335 views Asked by At

I am trying to assign the output of a function to a variable, but I can’t do it. :(

I use Lua 5.3.4 and luarocks 3.2.1 (+luasql-postgres)

My script:

luasql = require "luasql.postgres"
value=arg[1]
current_time=os.date("%Y-%m-%d %H:%M:%S")
env = luasql.postgres()
con = assert (env:connect('postgres', 'postgres', 'postgres', '192.168.241.93','5432'))

function printvalues(res)
    row = res:fetch ({}, "a")
    while row do
            print(string.format("%-12s", row.client_addr))
            row = res:fetch (row, "a")
    end
    print()
end

res = assert (con:execute('select client_addr from pg_stat_replication order by replay_lag asc limit 1'))


--txn = res
a = {myfunc = printvalues(res)}

if a == '192.168.242.41' then
print("backend1")
elseif a == '192.168.241.76' then
print("backend2")
end

print(string.format("%-12s",a))
print(a)

Script result:

root@haproxy:/etc/haproxy# lua scripts/test.lua
192.168.1.76

table: 0x559fadf97080
table: 0x559fadf97080

Please tell me:

  1. How can I assign the result of a function to a variable?

  2. How to remove the empty line in the output of the function printvalues(res)

2

There are 2 answers

0
xxleite On
  1. you need to make a return statement in the function body to return a value
  2. the empty line in your example is the print() statement in the function printvalues

I see you are making a query using limit 1, since you will only get one value, the while loop statement is useless.

I hope this works for you

local luasql = require "luasql.postgres"
local env = luasql.postgres()
local con = assert (env:connect(
  'postgres', 'postgres', 'postgres', '192.168.241.93', '5432'
))
local res = assert(con:execute(
  'select client_addr from pg_stat_replication order by replay_lag asc limit 1'
))
local row = res:fetch({}, 'a')
local a = string.format('%-12s', row.client_addr)

res:close()

if a == '192.168.242.41' then
  print('backend1')
elseif a == '192.168.241.76' then
  print('backend2')
end

print(a)

but if you want an example with function:

local luasql = require "luasql.postgres"
local env = luasql.postgres()
local con = assert (env:connect(
  'postgres', 'postgres', 'postgres', '192.168.241.93', '5432'
))
local res = assert(con:execute(
  'select client_addr from pg_stat_replication order by replay_lag asc limit 1'
))

local function printvalues(res)
  local row = res:fetch({}, 'a')
  res:close()
  return string.format('%-12s', row.client_addr)
end

local a = printvalues(res)

if a == '192.168.242.41' then
  print('backend1')
elseif a == '192.168.241.76' then
  print('backend2')
end

print(a)

try to use local keyword before variables, this will make them scope variables

0
Sergey Mironov On

Thank. I ended up using this script:

luasql = require "luasql.postgres"
env = luasql.postgres()
con = assert (env:connect('postgres', 'postgres', 'postgres','333.333.333.333','5432')) --master

backend = function(txn)
res = assert (con:execute('select client_addr from pg_stat_replication order by replay_lag asc limit 1'))
row = res:fetch ({}, "a")
while row do
    ip = string.format("%-12s", row.client_addr)
    row = res:fetch (row, "a")
end
result = "backend1"
if ip == '111.111.111.111' then
result = "backend1"
elseif ip == '222.222.222.222' then
result = "backend2"
end
return result
end

core.register_fetches("choose_backend", backend)