From b6477c6295226ae7d7f43185ce0ab55f47bd5f94 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Tue, 30 May 2006 11:54:51 +0000 Subject: Add regexp_replace() to string functions section. Joachim Wieland --- doc/src/sgml/func.sgml | 23 +++++++++++++++++---- doc/src/sgml/plpgsql.sgml | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 5 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 0a9a62b759f..5ce49880b53 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -1112,7 +1112,9 @@ substring(string from pattern) text - Extract substring matching POSIX regular expression + Extract substring matching POSIX regular expression. See + for more information on pattern + matching. substring('Thomas' from '...$') mas @@ -1122,8 +1124,9 @@ substring(string from pattern for escape) text - Extract substring matching SQL regular - expression + Extract substring matching SQL regular expression. + See for more information on + pattern matching. substring('Thomas' from '%#"o_a#"_' for '#') oma @@ -1420,6 +1423,18 @@ 'O''Reilly' + + regexp_replace(string text, pattern text, replacement text [,flags text]) + text + + Replace substring matching POSIX regular expression. See + for more information on pattern + matching. + + regexp_replace('Thomas', '.[mN]a.', 'M') + ThM + + repeat(string text, number int) text diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 2fc998ee909..ef5e9e3ece0 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -1,4 +1,4 @@ - + <application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language @@ -879,6 +879,55 @@ SELECT merge_fields(t.*) FROM table1 t WHERE ... ; field in it will draw a run-time error. + + To obtain the values of the fields the record is made up of, + the record variable can be qualified with the column or field + name. This can be done either by literally using the column name + or the column name for indexing the record can be taken out of a scalar + variable. The syntax for this notation is Record_variable.(IndexVariable). + To get information about the column field names of the record, + a special expression exists that returns all column names as an array: + RecordVariable.(*) . + Thus, the RECORD can be viewed + as an associative array that allows for introspection of it's contents. + This feature is especially useful for writing generic triggers that + operate on records with unknown structure. + Here is an example procedure that shows column names and values + of the predefined record NEW in a trigger procedure: + + +CREATE OR REPLACE FUNCTION show_associative_records() RETURNS TRIGGER AS $$ + DECLARE + colname TEXT; + colcontent TEXT; + colnames TEXT[]; + coln INT4; + coli INT4; + BEGIN +-- obtain an array with all field names of the record + colnames := NEW.(*); + RAISE NOTICE 'All column names of test record: %', colnames; +-- show field names and contents of record + coli := 1; + coln := array_upper(colnames,1); + RAISE NOTICE 'Number of columns in NEW: %', coln; + FOR coli IN 1 .. coln LOOP + colname := colnames[coli]; + colcontent := NEW.(colname); + RAISE NOTICE 'column % of NEW: %', quote_ident(colname), quote_literal(colcontent); + END LOOP; +-- Do it with a fixed field name: +-- will have to know the column name + RAISE NOTICE 'column someint of NEW: %', quote_literal(NEW.someint); + RETURN NULL; + END; +$$ LANGUAGE plpgsql; +--CREATE TABLE test_records (someint INT8, somestring TEXT); +--CREATE TRIGGER tr_test_record BEFORE INSERT ON test_records FOR EACH ROW EXECUTE PROCEDURE show_associative_records(); + + + + Note that RECORD is not a true data type, only a placeholder. One should also realize that when a PL/pgSQL -- cgit v1.2.3