pllua / pllua-deprecated

[DEPRECATED] This repository is no longer maintained. Please follow https://github.com/pllua/pllua
197 stars 16 forks source link

How do I require the luarocks module? #53

Closed ghost closed 7 years ago

ghost commented 7 years ago

luajit pllua use.

example

luarocks install lua-requests

sample function

CREATE FUNCTION request_test (list text[])
RETURNS text[] AS $$
-- set_paths.lua
local version = _VERSION:match("%d+%.%d+")
package.path = './?.lua;/usr/local/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;' .. package.path
package.cpath = './?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;' .. package.cpath
--requests = require('requests')
local requests = require('/usr/local/share/lua/5.1/requests.lua')
list[7] = 'check'
return list
$$ LANGUAGE pllua;

use.

select request_test(ARRAY['q','w','e']);

execute result.

ERROR:  [string "request_test"]:7: attempt to call global 'require' (a nil value)
CONTEXT:  
stack traceback(trusted):
    [string "request_test"]:7: in function <[string "request_test"]:1>
********** Error **********

ERROR: [string "request_test"]:7: attempt to call global 'require' (a nil value)
SQL state: 38000
Context: 
stack traceback(trusted):
    [string "request_test"]:7: in function <[string "request_test"]:1>

pathcheck

CREATE FUNCTION require_test ()
RETURNS text[] AS $$
local list = {}
list[1] = _VERSION:match("%d+%.%d+")
list[2] = package.path
list[3] = package.cpath
list[4] = 'test'
return list
$$ LANGUAGE pllua;

result

select require_test()

5.1,./?.lua;/usr/local/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;./?.lua;/usr/local/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;./?.lua;/usr/local/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;./?.lua;/usr/local/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;./?.lua;/usr/local/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua,./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so,test

How do I require the luarocks module?

golgote commented 7 years ago

Look at the documentation:

Even though the module system is absent in pllua, PL/Lua allows for modules to be automatically loaded after creating the environment: all entries in table pllua.init are require'd at startup.

ghost commented 7 years ago

@golgote

Thank you for telling me.

The pllua extension installs both trusted and untrusted flavors of PL/Lua and creates the module table pllua.init. Alternatively, a systemwide installation though the PL template facility can be achieved with:

  INSERT INTO pg_catalog.pg_pltemplate
      VALUES ('pllua', true, 'pllua_call_handler', 'pllua_validator', '$libdir/pllua', NULL);

I'm sorry. I can not understand.

Specifically, if lua-requests is in this PATH(/usr/local/share/lua/5.1/requests.lua), how will it be available?

Best regards.

golgote commented 7 years ago

Try: INSERT INTO pllua.init ('module') VALUES ('requests');
This will require the module and make it available for all your scripts.

golgote commented 7 years ago

You might also want to use plluau (untrusted: less secure) instead of pllua (more secure) if you need require specifically, because it gives you access to everything in Lua.

ghost commented 7 years ago

@golgote

Thank you for telling me!! I understand .

this issue is close.