Project

General

Profile

« Previous | Next » 

Revision 8107

schemas/functions.sql: Added col_cast and set_col_types()

View differences:

functions.sql
25 25
SET search_path = functions, pg_catalog;
26 26

  
27 27
--
28
-- Name: col_cast; Type: TYPE; Schema: functions; Owner: -
29
--
30

  
31
CREATE TYPE col_cast AS (
32
	col_name text,
33
	type regtype
34
);
35

  
36

  
37
--
28 38
-- Name: col_ref; Type: TYPE; Schema: functions; Owner: -
29 39
--
30 40

  
......
662 672

  
663 673

  
664 674
--
675
-- Name: set_col_types(regclass, col_cast[]); Type: FUNCTION; Schema: functions; Owner: -
676
--
677

  
678
CREATE FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) RETURNS void
679
    LANGUAGE plpgsql STRICT
680
    AS $_$
681
DECLARE
682
    sql text = $$ALTER TABLE $$||table_||$$
683
$$||NULLIF(array_to_string(ARRAY(
684
    SELECT
685
    $$ALTER COLUMN $$||col_name_sql||$$ TYPE $$||target_type
686
    ||$$ USING $$||col_name_sql||$$::$$||target_type
687
    FROM
688
    (
689
        SELECT
690
          quote_ident(col_name) AS col_name_sql
691
        , functions.col_type((table_, col_name)) AS curr_type
692
        , type AS target_type
693
        FROM unnest(col_casts)
694
    ) s
695
    WHERE curr_type != target_type
696
), '
697
, '), '');
698
BEGIN
699
    RAISE NOTICE '%', sql;
700
    EXECUTE COALESCE(sql, '');
701
END;
702
$_$;
703

  
704

  
705
--
706
-- Name: FUNCTION set_col_types(table_ regclass, col_casts col_cast[]); Type: COMMENT; Schema: functions; Owner: -
707
--
708

  
709
COMMENT ON FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) IS 'idempotent';
710

  
711

  
712
--
665 713
-- Name: to_global_col_names(regclass); Type: FUNCTION; Schema: functions; Owner: -
666 714
--
667 715

  

Also available in: Unified diff