473,473 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pl/tcl trigger question

Hello everyone,

I'm working on a tiny trigger function that needs to ensure that all
values entered in a field are lowercase'd. I can't use pl/pgsql because
I have a dozen different columns (with different names) that need a
trigger that does this and pl'pgsql can't expand variable names to
fieldnames. Writing a dozen functions (one per columnname) is /way/ too
blunt so I tried pl/tcl (which I don't know):

----------------------------------------------------------------
-- first do:
-- createdb test
-- createlang pltcl test

drop function my_lowercase() cascade;
create function my_lowercase() returns trigger as '
set NEW($1) lower(NEW($1))
return [array get NEW]' language 'pltcl';

drop table mytab;
create table mytab (myfield varchar);

create trigger trig_mytab before insert or update on mytab
for each row execute procedure my_lowercase('myfield');

-- let's insert a string, hope it's lowercase'd
insert into mytab (myfield) values ('TEST');
select * from mytab;

-- wrong, myfield contains 'lower(NEW(myfield))'
----------------------------------------------------------------

Can someone please tell me what I'm doing wrong? It's probably
something very simple but I don't know TCL (and I'm planning to keep
the serverside programming on pl'pgsql as much as possible).

TIA!

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 11 '05 #1
4 2119
On Tue, 2003-08-26 at 07:28, Jules Alberts wrote:
Hello everyone,

I'm working on a tiny trigger function that needs to ensure that all
values entered in a field are lowercase'd. I can't use pl/pgsql because
I have a dozen different columns (with different names) that need a
trigger that does this and pl'pgsql can't expand variable names to
fieldnames. Writing a dozen functions (one per columnname) is /way/ too
blunt so I tried pl/tcl (which I don't know):

----------------------------------------------------------------
-- first do:
-- createdb test
-- createlang pltcl test

drop function my_lowercase() cascade;
create function my_lowercase() returns trigger as '
set NEW($1) lower(NEW($1))
return [array get NEW]' language 'pltcl';

drop table mytab;
create table mytab (myfield varchar);

create trigger trig_mytab before insert or update on mytab
for each row execute procedure my_lowercase('myfield');

-- let's insert a string, hope it's lowercase'd
insert into mytab (myfield) values ('TEST');
select * from mytab;

-- wrong, myfield contains 'lower(NEW(myfield))'
----------------------------------------------------------------

Can someone please tell me what I'm doing wrong? It's probably
something very simple but I don't know TCL (and I'm planning to keep
the serverside programming on pl'pgsql as much as possible).


You'll need a function a bit more complex than this, but to do what your
trying to do in the function above the function would be written as:

create or replace function my_lowercase() returns trigger as '
set NEW($1) [string tolower $NEW($1)]
return [array get NEW]' language 'pltcl';

Hope this helps, please post the final results when you get there.

Robert Treat
--
Build A Brighter Lamp :: Linux Apache {middleware} PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 11 '05 #2
Ok the way you could do this is as follows:

create or replace function my_lowercase() returns trigger as '
foreach id [array names NEW] {
set NEW($id) [string tolower $NEW($id)]
}
return [array get NEW]
' language 'pltcl';

HTH
Darren

On 26 Aug 2003, Robert Treat wrote:
On Tue, 2003-08-26 at 07:28, Jules Alberts wrote:
Hello everyone,

I'm working on a tiny trigger function that needs to ensure that all
values entered in a field are lowercase'd. I can't use pl/pgsql because
I have a dozen different columns (with different names) that need a
trigger that does this and pl'pgsql can't expand variable names to
fieldnames. Writing a dozen functions (one per columnname) is /way/ too
blunt so I tried pl/tcl (which I don't know):

----------------------------------------------------------------
-- first do:
-- createdb test
-- createlang pltcl test

drop function my_lowercase() cascade;
create function my_lowercase() returns trigger as '
set NEW($1) lower(NEW($1))
return [array get NEW]' language 'pltcl';

drop table mytab;
create table mytab (myfield varchar);

create trigger trig_mytab before insert or update on mytab
for each row execute procedure my_lowercase('myfield');

-- let's insert a string, hope it's lowercase'd
insert into mytab (myfield) values ('TEST');
select * from mytab;

-- wrong, myfield contains 'lower(NEW(myfield))'
----------------------------------------------------------------

Can someone please tell me what I'm doing wrong? It's probably
something very simple but I don't know TCL (and I'm planning to keep
the serverside programming on pl'pgsql as much as possible).


You'll need a function a bit more complex than this, but to do what your
trying to do in the function above the function would be written as:

create or replace function my_lowercase() returns trigger as '
set NEW($1) [string tolower $NEW($1)]
return [array get NEW]' language 'pltcl';

Hope this helps, please post the final results when you get there.

Robert Treat


--
Darren Ferguson
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 11 '05 #3
Op 26 Aug 2003 (12:38), schreef Robert Treat <xz****@users.sourceforge.net>:
On Tue, 2003-08-26 at 07:28, Jules Alberts wrote:
Hello everyone,

I'm working on a tiny trigger function that needs to ensure that all
values entered in a field are lowercase'd. I can't use pl/pgsql
because I have a dozen different columns (with different names) that
need a trigger that does this and pl'pgsql can't expand variable names
to fieldnames. Writing a dozen functions (one per columnname) is /way/
too blunt so I tried pl/tcl (which I don't know):

<bad attempt snipped>
You'll need a function a bit more complex than this, but to do what your
trying to do in the function above the function would be written as:

create or replace function my_lowercase() returns trigger as '
set NEW($1) [string tolower $NEW($1)]
return [array get NEW]' language 'pltcl';

Hope this helps, please post the final results when you get there.
Hi Robert,

It works great, thanks a lot! There is one little issue though: when I
insert null values, the function fails. I think I can work around this
by giving the columns a default value of '' in my table design, but I
would like a more defensive approach, I.E. having my_lowercase() check
for null values.

Thanks again for any help, and sorry if I'm asking basic TCL questions,
I don't know the language. Do you happen to know a good site where the
language is explained? All I googled was about creating widgets, GUI
stuff :-(
Robert Treat


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #4


Jules Alberts wrote:
Op 26 Aug 2003 (12:38), schreef Robert Treat <xz****@users.sourceforge.net>:
On Tue, 2003-08-26 at 07:28, Jules Alberts wrote:
> Hello everyone,
>
> I'm working on a tiny trigger function that needs to ensure that all
> values entered in a field are lowercase'd. I can't use pl/pgsql
> because I have a dozen different columns (with different names) that
> need a trigger that does this and pl'pgsql can't expand variable names
> to fieldnames. Writing a dozen functions (one per columnname) is /way/
> too blunt so I tried pl/tcl (which I don't know):


<bad attempt snipped>
You'll need a function a bit more complex than this, but to do what your
trying to do in the function above the function would be written as:

create or replace function my_lowercase() returns trigger as '
set NEW($1) [string tolower $NEW($1)]
return [array get NEW]' language 'pltcl';

Hope this helps, please post the final results when you get there.


Hi Robert,

It works great, thanks a lot! There is one little issue though: when I
insert null values, the function fails. I think I can work around this
by giving the columns a default value of '' in my table design, but I
would like a more defensive approach, I.E. having my_lowercase() check
for null values.


Have you tried the scriptics site http://www.scriptics.com/ under
"web-resources->documentation"? There are some tutorials and howto's.
create or replace function force_lower () returns trigger as '
foreach key $args {
if {[info exists NEW($key)]} {
set NEW($key) [string tolower $NEW($key)]
}
}
return [array get NEW]
' language pltcl;

create trigger force_lower before insert or update on mytable
for each row execute procedure force_lower('field_1', 'field_n');
This works for a variable number of fields on every table and ignores
NULL values.
Jan

--
#================================================= =====================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================= = Ja******@Yahoo.com #
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joel Thornton | last post by:
Whenever something is inserted to a given table, I want to run some shell commands using xp_cmdshell. Would it be a bad idea to put this xp_cmdshell in the INSERT trigger of this table? I...
1
by: Matik | last post by:
Hello to all, I have a small question. I call the SP outer the DB. The procedure deletes some record in table T1. The table T1 has a trigger after delete. This is very importand for me, that...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
2
by: robert | last post by:
typed this into the ibm.com search window, but didn't get anything that looked like it would answer a question: +trigger +faster +db2 +cobol the question: are DB2 (390/v6, at the moment)...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
5
by: Bob Stearns | last post by:
I have two (actually many) dates in a table I want to validate on insertion. The following works in the case of only one WHEN clause but fails with two (or more), with the (improper?...
3
by: ChrisN | last post by:
Hello all, I have a quick question. I'm using a C# object to commit new rows to a database. In the database I have an INSERT Trigger watching values come in. If the record to be committed...
3
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code...
9
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger,...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.