Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flex] Provide conninfo to Lua script #1212

Closed
mmd-osm opened this issue Jun 13, 2020 · 3 comments
Closed

[Flex] Provide conninfo to Lua script #1212

mmd-osm opened this issue Jun 13, 2020 · 3 comments

Comments

@mmd-osm
Copy link
Contributor

mmd-osm commented Jun 13, 2020

This relates a bit to #1211. Assuming we want to create additional indices, or use database lookups, etc., it would be useful to have access to the same database connection information that the C++ code uses, and leverage a library like luasql.postgres :

luasql = require "luasql.postgres"
envv = assert (luasql.postgres())
con = assert (envv:connect(osm2pgsql.conninfo))

--- example only
res = assert (con:execute[[
        CREATE INDEX title_idx_nulls_low ON films (title NULLS FIRST)
]])

Maybe this one liner would be sufficient? There's one caveat: conninfo might contain a password. Not sure, if we can assume Lua scripts to be trustworthy enough here.

diff --git a/src/output-flex.cpp b/src/output-flex.cpp
index 4c7afd3e..ad43d349 100644
--- a/src/output-flex.cpp
+++ b/src/output-flex.cpp
@@ -1273,6 +1273,7 @@ void output_flex_t::init_lua(std::string const &filename)
     luaX_add_table_func(lua_state(), "define_table",
                         lua_trampoline_app_define_table);
     luaX_add_table_func(lua_state(), "mark", lua_trampoline_app_mark);
+    luaX_add_table_str(lua_state(), "conninfo",  m_options.database_options.conninfo().c_str());
 
     lua_setglobal(lua_state(), "osm2pgsql");

@joto
Copy link
Collaborator

joto commented Jun 15, 2020

Do you have an actual, current use case for this?

We should not just add features that seem nice or useful, because we have to support them until the end of times...

@mmd-osm
Copy link
Contributor Author

mmd-osm commented Jun 17, 2020

I added a number of use cases to #1211 (comment), which all depend on database access, and are equally relevant to this issue.

My assumption is that osm2pgsql will not provide a generic interface to execute database queries (both SELECTs, UPDATES, INSERT, etc.), hence the need for an external Lua library to access the database.

To avoid hard-coding database names, or doing any strange tricks to extracts the relevant parameters from the osm2pgsql command line, having the connection parameters readily available in Lua seemed like the least intrusive option.

@lonvia
Copy link
Collaborator

lonvia commented Jun 20, 2020

Closing as per same reason as #1211.

@lonvia lonvia closed this as completed Jun 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants