Re: Localizing stored functions by replacing placeholders in their body

2021-03-03 Thread Alexander Farber
Thanks for your input Actually, yes, that is what I have right now a translate() like stored function, with format %s sometimes. But that is "at runtime" and I would like to have a "at compile time"/"deploy once and forget" solution, that is why I have asked about approaches for modifying the bod

RE: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Kevin Brannen
-Original Message- From: Alvaro Herrera Sent: Tuesday, March 2, 2021 2:19 PM To: Alexander Farber Cc: pgsql-general Subject: Re: Localizing stored functions by replacing placeholders in their body On 2021-Mar-02, Alexander Farber wrote: > CREATE OR REPLACE FUNCTION localize_he

Aw: Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Karsten Hilbert
> I'm not sure this is a great approach to in-database translations: you > have one function per string, which is cumbersome, bloated and probably > slow. I would suggest having a function that takes a string and returns > its translation, which is obtained from a couple of tables: one where > the

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alvaro Herrera
On 2021-Mar-02, Alexander Farber wrote: > CREATE OR REPLACE FUNCTION localize_hello() > RETURNS text AS > $func$ > SELECT '$(hello)'; > $func$ LANGUAGE sql IMMUTABLE; I'm not sure this is a great approach to in-database translations: you have one function per string, which is cumb

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alexander Farber
I think I will try this approach: \set localized_declaration `sed 's/this/that/' my_func.sql` :localized_declaration Thank you for your input

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alexander Farber
Ah, I understand, that was the wrong EXECUTE, thank you. Another idea: can't I use \set command for my purpose of localizing stored functions? \set my_func_declaration `sed 's/this/that/' my_func.sql` But how to execute the declaration? I can only echo it with select (:'my_func_declaration');

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Pavel Stehule
út 2. 3. 2021 v 17:55 odesílatel Alexander Farber < alexander.far...@gmail.com> napsal: > Thank you for the \! hint, Pavel, didn't know about that! > > Is it possible to have a pure SQL solution? (To avoid having to install > "sed" on my Win 10 PC) > You should to use PLpgSQL EXECUTE statement, n

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alexander Farber
Thank you for the \! hint, Pavel, didn't know about that! Is it possible to have a pure SQL solution? (To avoid having to install "sed" on my Win 10 PC) Maybe by using EXECUTE? EXECUTE REGEXP_REPLACE( $localize$ CREATE OR REPLACE FUNCTION my_func() RETURNS text AS $func$ SELECT

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Pavel Stehule
út 2. 3. 2021 v 17:18 odesílatel Alexander Farber < alexander.far...@gmail.com> napsal: > Or is it possible to call external commands from an sql script, like > > \i "sed 's/this/that/' some.sql" > you can use \! statement for execution of external statements Pavel

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alexander Farber
Or is it possible to call external commands from an sql script, like \i "sed 's/this/that/' some.sql"

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alexander Farber
Yes, good point about the '\$', thank you Tom. The reason I am trying not to use sed, is because I deploy my database by executing a single command: psql words_en < words_en.sql And the file words_en.sql has the contents: \i words_hash.sql \i words_all_letters.sql \i words_get_hint.sql \i words

Re: Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Tom Lane
Alexander Farber writes: > update pg_proc set prosrc = regexp_replace(prosrc, '$\(\w+\)','Hi > english','g') where proname='localize_hello'; "$" is a metacharacter in regexes ... writing \$ might help. (The idea of direct updates on the system catalogs seems like a really bad one. Why not pass

Localizing stored functions by replacing placeholders in their body

2021-03-02 Thread Alexander Farber
Hello, I have an app using PostgreSQL 13.2, in 6 different human languages (each using different database, but same source code). Currently to localize strings return/set by the stored functions I either get localized strings from a table or maintain stored function source code in 6 different lan