/* Function */ CREATE OR REPLACE FUNCTION dss_check_path(dss_path varchar) RETURNS void AS $BODY$ BEGIN --test for empty directory names IF dss_path LIKE '%//%' THEN RAISE EXCEPTION '% contains an empty directory name', dss_path; END IF; --test if last directory is empty name IF dss_path LIKE '%/' AND dss_path <> '/' THEN RAISE EXCEPTION '% ends with an empty directory name', dss_path; END IF; --TODO: Check for invalid characters, trailing spaces, etc.... RETURN; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; /* Test Function */ CREATE OR REPLACE FUNCTION test_dss_clean_path() RETURNS int4 AS $BODY$ DECLARE valid_paths varchar[]; invalid_paths varchar[]; current_index int4 = 1; current_path varchar; max_index int4; error_count int4 := 0; test_count int4 := 0; input varchar; BEGIN valid_paths := ARRAY['/', '/topdir', '/topdir/subdir', '/topdir/subdir/subsubdir']; invalid_paths := ARRAY['', '//', '//topdir', '///', '/topdir/', '/topdir/subdir/', '/topdir/subdir/subsubdir/']; /* Test valid paths: should not raise an exception */ max_index := array_upper(valid_paths, 1); current_index := 1; BEGIN WHILE current_index <= max_index LOOP current_path := valid_paths[current_index]; PERFORM dss_check_path(current_path); --exception not expected RAISE NOTICE 'PASS: % did not raise an exception', current_path; current_index := current_index + 1; test_count := test_count + 1; END LOOP; EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'FAIL: % should not have been identified as being an invalid path', current_path; error_count := error_count + 1; END; /* Test invalid paths: should raise an exception */ max_index := array_upper(invalid_paths, 1); current_index := 1; WHILE current_index <= max_index LOOP BEGIN current_path := invalid_paths[current_index]; PERFORM dss_check_path(current_path); RAISE NOTICE 'FAIL: % should have been identified as an invalid path', current_path; error_count := error_count + 1; EXCEPTION WHEN OTHERS THEN --exception expected RAISE NOTICE 'PASS: % did raise an exception', current_path; END; current_index := current_index + 1; test_count := test_count + 1; END LOOP; IF error_count = 0 THEN RAISE NOTICE 'SUCCESS: All % tests passed', test_count; ELSE RAISE NOTICE 'FAILURE: % out of % tests failed', error_count, test_count; END IF; RETURN error_count; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; /* Call Test Function */ SELECT test_dss_clean_path(); /* Drop Test Function */ DROP FUNCTION test_dss_clean_path();