Generate remove accents mappings
================================

Use the below plpgsql script to generate the contents of pltsql_remove_accent_map.
The output from this script should be replaced in utils/removeaccent.map.
The mappings are only used when the version being used by the engine matches to
the one used to generate the mappings. Below script will also expose ICU version
used to generate the mappings.

-- Setup
CREATE TABLE all_utf8_chars(col1 BYTEA, col2 TEXT);

-- Insert all possible UNICODE and UTF-8 valid characters into table
DO $$
DECLARE
    cnt INT:= 1;
    var TEXT;
BEGIN
    WHILE cnt <= 1114112 LOOP
        BEGIN
            var := chr(cnt);
            IF var <> '\' THEN
                INSERT INTO all_utf8_chars VALUES (cast(var as bytea), var);
            ELSE
                INSERT INTO all_utf8_chars VALUES (cast('\\' as bytea), '\');
            END IF;
        EXCEPTION
            WHEN program_limit_exceeded THEN
        END;       
        cnt := cnt + 1;
    END LOOP;
END;         
$$;

-- Generate macros
SELECT CONCAT('#define pltsql_remove_accent_map_icu_major_version ', sys.get_icu_major_version());
SELECT CONCAT('#define pltsql_remove_accent_map_icu_min_version ', sys.get_icu_minor_version());

-- Generate a mapping for every required character
-- ex:  /* Æ */    {0xc386, 0x4145}    /* AE */
SELECT REPLACE(FORMAT('/* %s */    {%s, %s},    /* %s */',col2, col1, cast(sys.remove_accents_internal(col2) as bytea), sys.remove_accents_internal(col2)), '\', '0')
    FROM all_utf8_chars
    WHERE col2 != sys.remove_accents_internal(col2);

-- Cleanup
DROP TABLE all_utf8_chars;
