create_optional_types_and_functions.pgsql
changeset 74 67a454ea5472
parent 73 11da3d9298b4
child 75 af813ede1e19
equal deleted inserted replaced
73:11da3d9298b4 74:67a454ea5472
   105 EXTERNAL SECURITY INVOKER;
   105 EXTERNAL SECURITY INVOKER;
   106 
   106 
   107 -- ########################################################################## --
   107 -- ########################################################################## --
   108 
   108 
   109 -- ---
   109 -- ---
   110 -- Data type for function postfix_virtual_alias_map(varchar, varchar)
   110 -- Data type for functions: postfix_relocated_map(varchar, varchar)
       
   111 --                          postfix_virtual_alias_map(varchar, varchar)
       
   112 --                          
   111 -- ---
   113 -- ---
   112 CREATE TYPE recipient_destination AS (
   114 CREATE TYPE recipient_destination AS (
   113     recipient   varchar(320),
   115     recipient   varchar(320),
   114     destination text
   116     destination text
   115 );
   117 );
   119 --      varchar localpart
   121 --      varchar localpart
   120 --      varchar the_domain
   122 --      varchar the_domain
   121 -- Returns: recipient_destination records
   123 -- Returns: recipient_destination records
   122 --
   124 --
   123 -- Required access privileges for your postfix database user:
   125 -- Required access privileges for your postfix database user:
   124 --      GRANT SELECT ON alias TO postfix;
   126 --      GRANT SELECT ON alias, domain_name TO postfix;
   125 --
   127 --
   126 -- For more details see postconf(5) section virtual_alias_maps and virtual(5)
   128 -- For more details see postconf(5) section virtual_alias_maps and virtual(5)
   127 -- ---
   129 -- ---
   128 CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
   130 CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
   129     IN localpart varchar, IN the_domain varchar)
   131     IN localpart varchar, IN the_domain varchar)
   145         RETURN;
   147         RETURN;
   146     END;
   148     END;
   147 $$ LANGUAGE plpgsql STABLE
   149 $$ LANGUAGE plpgsql STABLE
   148 RETURNS NULL ON NULL INPUT
   150 RETURNS NULL ON NULL INPUT
   149 EXTERNAL SECURITY INVOKER;
   151 EXTERNAL SECURITY INVOKER;
       
   152 
       
   153 -- ########################################################################## --
       
   154 
       
   155 -- ---
       
   156 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   157 --      varchar localpart
       
   158 --      varchar the_domain
       
   159 -- Returns: recipient_destination records
       
   160 --
       
   161 -- Required access privileges for your postfix database user:
       
   162 --      GRANT SELECT ON domain_name, relocated TO postfix;
       
   163 --
       
   164 -- For more details see postconf(5) section relocated_maps and relocated(5)
       
   165 -- ---
       
   166 CREATE OR REPLACE FUNCTION postfix_relocated_map(
       
   167     IN localpart varchar, IN the_domain varchar)
       
   168     RETURNS SETOF recipient_destination
       
   169 AS $$
       
   170     DECLARE
       
   171         record recipient_destination;
       
   172         recipient varchar(320) := localpart || '@' || the_domain;
       
   173         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   174     BEGIN
       
   175         FOR record IN
       
   176             SELECT recipient, destination
       
   177               FROM relocated
       
   178              WHERE gid = did
       
   179                AND address = localpart
       
   180             LOOP
       
   181                 RETURN NEXT record;
       
   182             END LOOP;
       
   183         RETURN;
       
   184     END;
       
   185 $$ LANGUAGE plpgsql STABLE
       
   186 RETURNS NULL ON NULL INPUT
       
   187 EXTERNAL SECURITY INVOKER;
       
   188 
       
   189 -- ########################################################################## --
       
   190 
       
   191 -- ---
       
   192 -- Data type for function postfix_transport_map(varchar, varchar)
       
   193 -- ---
       
   194 CREATE TYPE recipient_transport AS (
       
   195     recipient   varchar(320),
       
   196     transport   text
       
   197 );
       
   198 
       
   199 -- ---
       
   200 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   201 --      varchar localpart
       
   202 --      varchar the_domain
       
   203 -- Returns: recipient_transport records
       
   204 --
       
   205 -- Required access privileges for your postfix database user:
       
   206 --      GRANT SELECT ON users, transport, domain_name TO postfix;
       
   207 --
       
   208 -- For more details see postconf(5) section transport_maps and transport(5)
       
   209 -- ---
       
   210 CREATE OR REPLACE FUNCTION postfix_transport_map(
       
   211     IN localpart varchar, IN the_domain varchar)
       
   212     RETURNS SETOF recipient_transport
       
   213 AS $$
       
   214     DECLARE
       
   215         record recipient_transport;
       
   216         recipient varchar(320) := localpart || '@' || the_domain;
       
   217     BEGIN
       
   218         FOR record IN
       
   219             SELECT recipient, transport
       
   220               FROM transport
       
   221              WHERE tid = (SELECT tid
       
   222                             FROM users
       
   223                            WHERE gid = (SELECT gid
       
   224                                           FROM domain_name
       
   225                                          WHERE domainname = the_domain)
       
   226                AND local_part = localpart)
       
   227             LOOP
       
   228                 RETURN NEXT record;
       
   229             END LOOP;
       
   230         RETURN;
       
   231     END;
       
   232 $$ LANGUAGE plpgsql STABLE
       
   233 RETURNS NULL ON NULL INPUT
       
   234 EXTERNAL SECURITY INVOKER;