Changeset 24

Show
Ignore:
Timestamp:
09/09/08 14:50:41 (4 years ago)
Author:
fumanchu
Message:

assert_column tweaks.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/epic/epic.sql

    r23 r24  
    731731-- 
    732732-- 'call' can be any table, view, or procedure that returns records. 
    733 -- 'expected' MUST be an array of the same type as colname. 
     733-- 'expected' MUST be an array of the same type as colname. If necessary, cast it using :: to avoid 
     734-- the error 'type of "record1._assert_column_result" does not match that when preparing the plan'. 
     735-- TODO: can this function detect the type and cast it for the user? 
    734736--  
    735737-- Example: 
     
    745747  firstname     text; 
    746748  found_1       boolean; 
     749  lower_bound   int; 
     750  base_type      text; 
    747751BEGIN 
    748752  -- Dump the call output into a temp table 
     
    761765      'SELECT ' || colname || ' AS _assert_column_result FROM ' || call || ';'; 
    762766  END IF; 
     767  SELECT INTO base_type pgt.typname 
     768    FROM pg_attribute pga LEFT JOIN pg_type pgt ON pga.atttypid = pgt.oid 
     769    WHERE pga.attrelid = (SELECT oid FROM pg_class WHERE relname = '_test_assert_column_base') 
     770    AND pga.attnum = 1; 
     771  -- Casting to ::name doesn't work so well. 
     772  IF base_type = 'name' THEN 
     773    base_type := 'text'; 
     774  END IF; 
    763775   
    764776  -- Dump the provided array into a temp table 
     
    766778  -- doesn't get cached and re-used (or subsequent calls will fail). 
    767779  EXECUTE 'CREATE TEMPORARY TABLE _test_assert_column_expected (LIKE _test_assert_column_base);'; 
    768   IF array_lower(expected, 1) iS NOT NULL THEN 
    769     FOR i IN array_lower(expected, 1)..array_upper(expected, 1) 
     780  lower_bound = array_lower(expected, 1); 
     781  IF lower_bound iS NOT NULL THEN 
     782    FOR i IN lower_bound..array_upper(expected, 1) 
    770783    LOOP 
    771784      IF expected[i] IS NULL THEN 
    772785        EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (NULL);'; 
    773       ELSEIF typename(expected[i]) IN ('text', 'varchar', 'char', 'bytea', 'date', 'timestamp', 'timestamptz', 'time', 'timetz') THEN 
     786      ELSEIF base_type IN ('text', 'varchar', 'char', 'bytea', 'date', 'timestamp', 'timestamptz', 'time', 'timetz') THEN 
    774787        EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (' 
    775788                || quote_literal(expected[i]) || ');';