Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd489fdf

Browse files
committed
I send you a attach of my modified refint.c that
works with a new policy in cascade mode .Please Read README.MAX .I do not know if you are the author of refint.c ,but if not please tell me who is .Thank you ( excuse me for my bad english) .Massimo Lambertini massimo.lambertini@everex.it
1 parent9e4e33c commitd489fdf

File tree

9 files changed

+424
-156
lines changed

9 files changed

+424
-156
lines changed

‎contrib/spi/Makefile

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,23 @@ SRCDIR= ../../src
33

44
include$(SRCDIR)/Makefile.global
55

6-
CONTRIBDIR=$(LIBDIR)/modules
7-
86
CFLAGS+=$(CFLAGS_SL) -I$(SRCDIR)/include
97

108
ifdefREFINT_VERBOSE
119
CFLAGS+= -DREFINT_VERBOSE
1210
endif
1311

14-
TARGETS= refint$(DLSUFFIX) refint.sql timetravel$(DLSUFFIX) timetravel.sql\
15-
autoinc$(DLSUFFIX) autoinc.sql moddatetime$(DLSUFFIX) moddatetime.sql\
16-
insert_username$(DLSUFFIX) insert_username.sql
12+
TARGETS= refint$(DLSUFFIX) refint.sql
1713

1814
CLEANFILES+=$(TARGETS)
1915

2016
all::$(TARGETS)
2117

22-
install:: all$(CONTRIBDIR)
23-
$(INSTALL) -c README$(CONTRIBDIR)/README.spi
24-
forfin*.example*.sql*$(DLSUFFIX);do$(INSTALL) -c$$f$(CONTRIBDIR)/$$f;done
25-
26-
$(CONTRIBDIR):
27-
mkdir -p$(CONTRIBDIR)
28-
2918
%.sql:%.source
3019
rm -f$@;\
3120
C=`pwd`;\
32-
sed -e"s:_OBJWD_:$(CONTRIBDIR):g"\
21+
sed -e"s:_OBJWD_:$$C:g"\
3322
-e"s:_DLSUFFIX_:$(DLSUFFIX):g"<$<>$@
3423

3524
clean:
36-
rm -f$(TARGETS)*.o
25+
rm -f$(TARGETS)

‎contrib/spi/README

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ table/field names (as described below) while creating a trigger.
88

99
check_primary_key () is to used for foreign keys of a table.
1010

11-
Youhave to create trigger (BEFORE INSERT OR UPDATE) using this
12-
function on a table referencing another table. Youhave to specify
11+
Youare to create trigger (BEFORE INSERT OR UPDATE) using this
12+
function on a table referencing another table. Youare to specify
1313
as function arguments: triggered table column names which correspond
1414
to foreign key, referenced table name and column names in referenced
1515
table which correspond to primary/unique key.
@@ -18,8 +18,8 @@ one reference.
1818

1919
check_foreign_key () is to used for primary/unique keys of a table.
2020

21-
Youhave to create trigger (BEFORE DELETE OR UPDATE) using this
22-
function on a table referenced by another table(s). Youhave to specify
21+
Youare to create trigger (BEFORE DELETE OR UPDATE) using this
22+
function on a table referenced by another table(s). Youare to specify
2323
as function arguments: number of references for which function has to
2424
performe checking, action if referencing key found ('cascade' - to delete
2525
corresponding foreign key, 'restrict' - to abort transaction if foreign keys
@@ -42,26 +42,20 @@ refint.source).
4242

4343
Old internally supported time-travel (TT) used insert/delete
4444
transaction commit times. To get the same feature using triggers
45-
youhave to add to a table two columns of abstime type to store
45+
youare to add to a table two columns of abstime type to store
4646
date when a tuple was inserted (start_date) and changed/deleted
4747
(stop_date):
4848

4949
CREATE TABLE XXX (
5050
......
51-
date_onabstime,
52-
date_offabstime
51+
date_onabstime default currabstime(),
52+
date_offabstime default 'infinity'
5353
......
5454
);
5555

56-
CREATE TRIGGER timetravel
57-
BEFORE INSERT OR DELETE OR UPDATE ON tttest
58-
FOR EACH ROW
59-
EXECUTE PROCEDURE
60-
timetravel (date_on, date_off);
61-
62-
Tuples being inserted with NULLs in date_on/date_off will get current
63-
date in date_on (name of start_date column in XXX) and INFINITY in date_off
64-
(name of stop_date column in XXX).
56+
- so, tuples being inserted with NULLs in date_on/date_off will get
57+
_current_date_ in date_on (name of start_date column in XXX) and INFINITY in
58+
date_off (name of stop_date column in XXX).
6559

6660
Tuples with stop_date equal INFINITY are "valid now": when trigger will
6761
be fired for UPDATE/DELETE of a tuple with stop_date NOT equal INFINITY then
@@ -78,7 +72,7 @@ DELETE: new tuple will be inserted with stop_date setted to current date
7872
(and with the same data in other columns as in tuple being deleted).
7973

8074
NOTE:
81-
1. To get tuples "valid now" youhave to add _stop_date_ = 'infinity'
75+
1. To get tuples "valid now" youare to add _stop_date_ = 'infinity'
8276
to WHERE. Internally supported TT allowed to avoid this...
8377
Fixed rewriting RULEs could help here...
8478
As work arround you may use VIEWs...
@@ -89,61 +83,22 @@ DELETE: new tuple will be inserted with stop_date setted to current date
8983

9084
timetravel() is general trigger function.
9185

92-
You have to create trigger BEFORE (!!!) INSERT OR UPDATE OR DELETE using
93-
this function on a time-traveled table. You have to specify two arguments:
94-
name of start_date column and name of stop_date column in triggered table.
86+
You are to create trigger BEFORE (!!!) UPDATE OR DELETE using this
87+
function on a time-traveled table. You are to specify two arguments: name of
88+
start_date column and name of stop_date column in triggered table.
89+
90+
currabstime() may be used in DEFAULT for start_date column to get
91+
current date.
9592

9693
set_timetravel() allows you turn time-travel ON/OFF for a table:
9794

9895
set_timetravel('XXX', 1) will turn TT ON for table XXX (and report
9996
old status).
10097
set_timetravel('XXX', 0) will turn TT OFF for table XXX (-"-).
10198

102-
Turning TT OFF allows you do with a table ALL what you want!
99+
Turning TT OFF allows you do with a table ALL what you want.
103100

104101
There is example in timetravel.example.
105102

106103
To CREATE FUNCTIONs use timetravel.sql (will be made by gmake from
107104
timetravel.source).
108-
109-
110-
3. autoinc.c - function for implementing AUTOINCREMENT/IDENTITY feature.
111-
112-
You have to create BEFORE INSERT OR UPDATE trigger using function
113-
autoinc(). You have to specify as function arguments: column name
114-
(of int4 type) for which you want to get this feature and name of
115-
SEQUENCE from which next value has to be fetched when NULL or 0
116-
value is being inserted into column (, ... - you are able to specify
117-
as many column/sequence pairs as you need).
118-
119-
There is example in autoinc.example.
120-
121-
To CREATE FUNCTION use autoinc.sql (will be made by gmake from
122-
autoinc.source).
123-
124-
125-
4. insert_username.c - function for inserting user names.
126-
127-
You have to create BEFORE INSERT OR UPDATE trigger using the function
128-
insert_username(). You have to specify as a function argument: the column
129-
name (of text type) in which user names will be inserted. Note that user
130-
names will be inserted irregardless of the initial value of the field, so
131-
that users cannot bypass this functionality by simply defining the field to
132-
be NOT NULL.
133-
134-
There is an example in insert_username.example.
135-
136-
To CREATE FUNCTION use insert_username.sql (will be made by gmake from
137-
insert_username.source).
138-
139-
140-
5. moddatetime.c - function for maintaining a modification datetime stamp.
141-
142-
You have to create a BEFORE UPDATE trigger using the function moddatetime().
143-
One argument must be given, that is the name of the field that is of type
144-
datetime that is to be used as the modification time stamp.
145-
146-
There is an example in moddatetime.example.
147-
148-
To CREATE FUNCTION use moddatetime.sql ( will be made by gmake from
149-
moddatetime.source).

‎contrib/spi/new_example.sql

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--Column ID of table A is primary key:
2+
3+
CREATETABLEA (
4+
IDint4not null,
5+
id1int4not null,
6+
primary key (ID,ID1)
7+
);
8+
9+
--Columns REFB of table B and REFC of C are foreign keys referenting ID of A:
10+
11+
CREATETABLEB (
12+
REFBint4,
13+
REFB1INT4
14+
);
15+
CREATEINDEXBION B (REFB);
16+
17+
CREATETABLEC (
18+
REFCint4,
19+
REFC1int4
20+
);
21+
CREATEINDEXCION C (REFC);
22+
23+
--Trigger for table A:
24+
25+
CREATETRIGGERAT BEFOREDELETEON A FOR EACH ROW
26+
EXECUTE PROCEDURE
27+
check_foreign_key (2,'cascade','ID','id1','B','REFB','REFB1','C','REFC','REFC1');
28+
29+
30+
CREATETRIGGERAT1 AFTERUPDATEON A FOR EACH ROW
31+
EXECUTE PROCEDURE
32+
check_foreign_key (2,'cascade','ID','id1','B','REFB','REFB1','C','REFC','REFC1');
33+
34+
35+
CREATETRIGGERBT BEFORE INSERTORUPDATEON B FOR EACH ROW
36+
EXECUTE PROCEDURE
37+
check_primary_key ('REFB','REFB1','A','ID','ID1');
38+
39+
CREATETRIGGERCT BEFORE INSERTORUPDATEON C FOR EACH ROW
40+
EXECUTE PROCEDURE
41+
check_primary_key ('REFC','REFC1','A','ID','ID1');
42+
43+
44+
45+
-- Now try
46+
47+
INSERT INTO AVALUES (10,10);
48+
INSERT INTO AVALUES (20,20);
49+
INSERT INTO AVALUES (30,30);
50+
INSERT INTO AVALUES (40,41);
51+
INSERT INTO AVALUES (50,50);
52+
53+
INSERT INTO BVALUES (1);-- invalid reference
54+
INSERT INTO BVALUES (10,10);
55+
INSERT INTO BVALUES (30,30);
56+
INSERT INTO BVALUES (30,30);
57+
58+
INSERT INTO CVALUES (11);-- invalid reference
59+
INSERT INTO CVALUES (20,20);
60+
INSERT INTO CVALUES (20,21);
61+
INSERT INTO CVALUES (30,30);
62+
63+
-- now update work well
64+
update Aset ID=100 , ID1=199where ID=30 ;
65+
66+
SELECT*FROM A;
67+
SELECT*FROM B;
68+
SELECT*FROM C;

‎contrib/spi/preprocessor/README.MAX

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
Here are general trigger functions provided as workable examples
3+
of using SPI and triggers. "General" means that functions may be
4+
used for defining triggers for any tables but you have to specify
5+
table/field names (as described below) while creating a trigger.
6+
7+
1. refint.c - functions for implementing referential integrity.
8+
9+
check_primary_key () is to used for foreign keys of a table.
10+
11+
You are to create trigger (BEFORE INSERT OR UPDATE) using this
12+
function on a table referencing another table. You are to specify
13+
as function arguments: triggered table column names which correspond
14+
to foreign key, referenced table name and column names in referenced
15+
table which correspond to primary/unique key.
16+
You may create as many triggers as you need - one trigger for
17+
one reference.
18+
19+
check_foreign_key () is to used for primary/unique keys of a table.
20+
21+
You are to create trigger (BEFORE DELETE OR UPDATE) using this
22+
function on a table referenced by another table(s). You are to specify
23+
as function arguments: number of references for which function has to
24+
performe checking, action if referencing key found ('cascade' - to delete
25+
corresponding foreign key, 'restrict' - to abort transaction if foreign keys
26+
exist, 'setnull' - to set foreign key referencing primary/unique key
27+
being deleted to null), triggered table column names which correspond
28+
to primary/unique key, referencing table name and column names corresponding
29+
to foreign key (, ... - as many referencing tables/keys as specified
30+
by first argument).
31+
Note, that NOT NULL constraint and unique index have to be defined by
32+
youself.
33+
34+
There are examples in refint.example and regression tests
35+
(sql/triggers.sql).
36+
37+
To CREATE FUNCTIONs use refint.sql (will be made by gmake from
38+
refint.source).
39+
40+
# Excuse me for my bad english. Massimo Lambertini
41+
#
42+
#
43+
# New check foreign key
44+
#
45+
I think that cascade mode is to be considered like that the operation over
46+
main table is to be made also in referenced table .
47+
When i Delete , i must delete from referenced table ,
48+
but when i update , i update referenced table and not delete like unmodified refint.c .
49+
50+
I made a patch that when i update it check the type of modified key ( if is a text , char() i
51+
added '') and then create a update query that do the right think .
52+
53+
For my point of view that policy is helpfull because i do not have in referenced table
54+
loss of information .
55+
56+
57+
In preprocessor subdir i have placed a little utility that from a SQL92 table definition,
58+
it create all trigger for foreign key .
59+
60+
61+
the schema that i use to analyze the problem is this
62+
63+
create table
64+
A
65+
( key int4 not null primary key ,...) ;
66+
create table
67+
REFERENCED_B
68+
( key int 4 , ... ,
69+
foreign key ( key ) references A --
70+
);
71+
72+
73+
74+
75+
76+

‎contrib/spi/preprocessor/example.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- Note the syntax is strict because i have no time to write better perl filter.
2+
--
3+
-- [blank] is 1 blank
4+
-- at the end of an interesting line must be a [,] or [--]
5+
-- [ending] must be a , or --
6+
--
7+
-- foreign[blank]key[blank]([blank]keyname,..,keyname[blank])[blank]references[blank]table[blank][ending]
8+
--
9+
-- step1.e < example.sql | step2.pl > foreign_key_triggers.sql
10+
--
11+
-- step1.e is a simple program that UPPERCASE ALL . I know that is simple implementing in Perl
12+
-- bu i haven't time
13+
14+
15+
CREATE TABLE
16+
gruppo
17+
(
18+
codice_gruppo int4NOT NULL,
19+
descrizionevarchar(32)NOT NULL
20+
primary key ( codice_gruppo )
21+
22+
) ;
23+
24+
--
25+
-- fa_parte : Appartenenza di una Azienda Conatto o Cliente ad un certo GRUPPO
26+
--
27+
28+
CREATE TABLE
29+
fa_parte
30+
(
31+
codice_gruppo int4NOT NULL,
32+
codice_contatto int4NOT NULL,
33+
34+
primary key ( codice_gruppo,codice_contatto ) ,
35+
foreign key ( codice_gruppo )references gruppo--
36+
);
37+

‎contrib/spi/preprocessor/step1.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<stdio.h>
2+
3+
char*strtoupper(char*string)
4+
{
5+
inti ;
6+
for (i=0;i<strlen(string);i++)
7+
{
8+
string[i]=toupper(string[i]);
9+
}
10+
returnstring;
11+
}
12+
13+
14+
15+
voidmain (charargc ,char**argv )
16+
{
17+
charstr[250];
18+
intsw=0 ;
19+
while (fgets (str,240,stdin) )
20+
{
21+
if (sw==0 )printf("%s",strtoupper(str));
22+
}
23+
24+
}

‎contrib/spi/preprocessor/step1.e

8.06 KB
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp