From: | Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl> |
---|---|
To: | Andrus <kobruleht2(at)hot(dot)ee> |
Cc: | <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: How to perform text merge |
Date: | 2010-03-28 18:22:26 |
Message-ID: | 48EB810E-9F5B-44F7-8D4C-0C4856C9DF4B@solfertje.student.utwente.nl |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 28 Mar 2010, at 19:43, Andrus wrote:
> Database column contains merge data in text column.
> Expressions are between << and >> separators.
> How to replace them with database values ?
>
> For example, code below should return:
>
> Hello Tom Lane!
>
> How to implement textmerge procedure or other idea ?
>
> Andrus.
>
> create temp table person ( firstname text, lastname text ) on commit drop;
> insert into person values ('Tom', 'Lane');
> create temp table mergedata ( template text ) on commit drop;
> insert into mergedata values ('Hello <<firstname||'' ''||lastname>>!');
>
> select textmerge(template,'select * from person') from mergedata;
Since you pretty much invented your own language you're probably best suited with writing your own parser. Have a look at flex/yacc or whatever it's equivalent is on Windows if that has your preference.
If you "dumb it down" a bit by replacing the expressions by simple tokens then you could handle this with regular expressions, for example:
insert into mergedata values ('Hello <<firstname>> <<lastname>>!');
You replace <<firstname>> and <<lastname>> with their respective values using regexp_replace. You'll need to nest a few calls to that function to get the result you want.
You could be a bit smarter about this and create a <<fullname>> macro that you fill from a function result that uses (firstname, lastname) as input parameters and returns firstname || ' ' || lastname. Views are useful for providing such data too.
Personally I think you're using a bad example here, as usually names don't just involve firstname and surname, but frequently have infixes, suffixes and titles and such. Not all of those fields are going to have values for every person in your database. What happens if you don't have a Tom Lane, but a mr. Lane, or if you have both but want to address a person more politely?
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.
!DSPAM:737,4baf9e7810416492686854!
From | Date | Subject | |
---|---|---|---|
Next Message | Andre Lopes | 2010-03-28 18:27:32 | How to generate a valid postgre TIMESTAMP with PHP? |
Previous Message | Pavel Stehule | 2010-03-28 18:03:44 | Re: Splitting text column to multiple rows |