-
Hi, But if i want to add a 'table' parameter in the URL (ie example: 'jsontest.sql?table=article') and to replace in the query the table name with $table: It seem that the first occurence of $table is well replaced, but not the second one and it give me: Error: I suspect again some security reasons, but maybe i forgot something ... PS: And many thanks for this super genial open source project. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Indeed, postgres does not support variable table names in prepared statements (nor do any other database). It's mostly a good thing, both for security reasons, as you mentioned (most people don't want to give open access to all tables, including internal ones, to everyone) and for performance reasons (Postgres needs to know which table it will be querying to make a good query plan). If you don't care about any of that and just want to give everyone access to everything, you can create a postgres function with an In your migrations: CREATE FUNCTION select_all_from(tablename text, OUT table_as_array jsonb)
AS '
BEGIN
EXECUTE format(''SELECT json_agg(row_to_json(%I)) FROM %I '', tablename, tablename) INTO table_as_array;
END
' LANGUAGE plpgsql; in your SELECT
'json' AS component,
select_all_from($table) AS contents; Live example: https://www.db-fiddle.com/f/wrTmdVULH7GC1JAUi4ij7X/1 That said, what I would do is just manually select which tables I want to create an API endpoint for, and create a small sql file for each one. |
Beta Was this translation helpful? Give feedback.
Indeed, postgres does not support variable table names in prepared statements (nor do any other database). It's mostly a good thing, both for security reasons, as you mentioned (most people don't want to give open access to all tables, including internal ones, to everyone) and for performance reasons (Postgres needs to know which table it will be querying to make a good query plan).
If you don't care about any of that and just want to give everyone access to everything, you can create a postgres function with an
EXECUTE
statement.In your migrations: