Monday, November 16, 2009

plpgsql style



CREATE OR REPLACE function _getSchema(_table text) RETURNS integer AS $$

DECLARE

_record RECORD;
_column TEXT;
_type TEXT;

BEGIN
RAISE NOTICE 'select ';

FOR _record in
SELECT column_name,data_type from information_schema.columns where table_name = _table

LOOP

_column := _record.column_name;
_type := _record.data_type;

IF
_record.data_type = 'text'
THEN
RAISE NOTICE 'count(distinct(%)) as dist_%,', _column, _column;
ELSIF
_record.data_type = 'numeric' or _record.data_type = 'integer'
THEN
RAISE NOTICE 'sum (%) as sum_%,', _column, _column;
END IF;

END LOOP;

RAISE NOTICE 'count (*) as rows';
RAISE NOTICE 'from % ', _table;


RETURN 0;

END;
$$ LANGUAGE 'plpgsql';

No comments: