mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
/contrib patch from Karel.
This commit is contained in:
@ -1,29 +1,64 @@
|
||||
#
|
||||
# $Header: /cvsroot/pgsql/contrib/spi/Makefile,v 1.13 2000/06/15 18:55:17 momjian Exp $
|
||||
#
|
||||
|
||||
SRCDIR= ../../src
|
||||
TOPDIR=../..
|
||||
|
||||
include $(SRCDIR)/Makefile.global
|
||||
include ../Makefile.global
|
||||
|
||||
CFLAGS+= $(CFLAGS_SL)
|
||||
NAME =
|
||||
|
||||
PROGRAM =
|
||||
OBJS = autoinc.o insert_username.o moddatetime.o refint.o timetravel.o
|
||||
DOCS = spi.doc
|
||||
SQLS = $(OBJS:.o=.sql)
|
||||
BINS =
|
||||
EXAMPLES= $(OBJS:.o=.example)
|
||||
MODS = $(OBJS:.o=$(DLSUFFIX))
|
||||
|
||||
CFLAGS += -I. $(CFLAGS_SL)
|
||||
|
||||
ifdef REFINT_VERBOSE
|
||||
CFLAGS+= -DREFINT_VERBOSE
|
||||
endif
|
||||
|
||||
TARGETS= refint$(DLSUFFIX) refint.sql \
|
||||
timetravel$(DLSUFFIX) timetravel.sql \
|
||||
autoinc$(DLSUFFIX) autoinc.sql \
|
||||
moddatetime$(DLSUFFIX) moddatetime.sql \
|
||||
insert_username$(DLSUFFIX) insert_username.sql
|
||||
OTHER_CLEAN = $(SQLS)
|
||||
|
||||
CLEANFILES+= $(TARGETS)
|
||||
all: $(MODS) $(SQLS)
|
||||
|
||||
all:: $(TARGETS)
|
||||
%.sql: %.sql.in
|
||||
$(SED) "s|MODULE_PATHNAME|$(CONTRIB_MODDIR)/$@|" < $< > $@
|
||||
|
||||
%.sql: %.source
|
||||
rm -f $@; \
|
||||
C=`pwd`; \
|
||||
sed -e "s:_OBJWD_:$$C:g" \
|
||||
-e "s:_DLSUFFIX_:$(DLSUFFIX):g" < $< > $@
|
||||
install: install_doc install_sql install_mod install_example
|
||||
|
||||
install_doc:
|
||||
for inst_file in $(DOCS); do \
|
||||
$(INSTALL) $(INSTL_LIB_OPTS) $$inst_file $(CONTRIB_DOCDIR); \
|
||||
done
|
||||
|
||||
install_sql:
|
||||
for inst_file in $(SQLS); do \
|
||||
$(INSTALL) $(INSTL_LIB_OPTS) $$inst_file $(CONTRIB_SQLDIR); \
|
||||
done
|
||||
|
||||
install_mod:
|
||||
for inst_file in $(MODS); do \
|
||||
$(INSTALL) $(INSTL_SHLIB_OPTS) $$inst_file $(CONTRIB_MODDIR); \
|
||||
done
|
||||
|
||||
install_example:
|
||||
for inst_file in $(EXAMPLES); do \
|
||||
$(INSTALL) $(INSTL_LIB_OPTS) $$inst_file $(CONTRIB_EXAMPLESDIR); \
|
||||
done
|
||||
|
||||
depend dep:
|
||||
$(CC) -MM -MG $(CFLAGS) *.c > depend
|
||||
|
||||
clean:
|
||||
rm -f $(TARGETS) *.o
|
||||
$(RM) *~ $(OBJS) $(MODS) $(PROGRAM) depend $(OTHER_CLEAN) core log
|
||||
|
||||
ifeq (depend,$(wildcard depend))
|
||||
include depend
|
||||
endif
|
||||
|
||||
|
||||
|
@ -1,109 +0,0 @@
|
||||
|
||||
Here are general trigger functions provided as workable examples
|
||||
of using SPI and triggers. "General" means that functions may be
|
||||
used for defining triggers for any tables but you have to specify
|
||||
table/field names (as described below) while creating a trigger.
|
||||
|
||||
1. refint.c - functions for implementing referential integrity.
|
||||
|
||||
check_primary_key () is to used for foreign keys of a table.
|
||||
|
||||
You are to create trigger (BEFORE INSERT OR UPDATE) using this
|
||||
function on a table referencing another table. You are to specify
|
||||
as function arguments: triggered table column names which correspond
|
||||
to foreign key, referenced table name and column names in referenced
|
||||
table which correspond to primary/unique key.
|
||||
You may create as many triggers as you need - one trigger for
|
||||
one reference.
|
||||
|
||||
check_foreign_key () is to used for primary/unique keys of a table.
|
||||
|
||||
You are to create trigger (BEFORE DELETE OR UPDATE) using this
|
||||
function on a table referenced by another table(s). You are to specify
|
||||
as function arguments: number of references for which function has to
|
||||
performe checking, action if referencing key found ('cascade' - to delete
|
||||
corresponding foreign key, 'restrict' - to abort transaction if foreign keys
|
||||
exist, 'setnull' - to set foreign key referencing primary/unique key
|
||||
being deleted to null), triggered table column names which correspond
|
||||
to primary/unique key, referencing table name and column names corresponding
|
||||
to foreign key (, ... - as many referencing tables/keys as specified
|
||||
by first argument).
|
||||
Note, that NOT NULL constraint and unique index have to be defined by
|
||||
youself.
|
||||
|
||||
There are examples in refint.example and regression tests
|
||||
(sql/triggers.sql).
|
||||
|
||||
To CREATE FUNCTIONs use refint.sql (will be made by gmake from
|
||||
refint.source).
|
||||
|
||||
|
||||
|
||||
|
||||
# Excuse me for my bad english. Massimo Lambertini
|
||||
#
|
||||
#
|
||||
# New check foreign key
|
||||
#
|
||||
I think that cascade mode is to be considered like that the operation over
|
||||
main table is to be made also in referenced table .
|
||||
When i Delete , i must delete from referenced table ,
|
||||
but when i update , i update referenced table and not delete like unmodified refint.c .
|
||||
|
||||
I made a new version of refint.c that when i update it check the type of modified key ( if is a text , char() i
|
||||
added '') and then create a update query that do the right thing .
|
||||
|
||||
For my point of view that policy is helpfull because i do not have in referenced table
|
||||
loss of information .
|
||||
|
||||
|
||||
In preprocessor subdir i have placed a little utility that from a SQL92 table definition,
|
||||
it create all trigger for foreign key .
|
||||
|
||||
|
||||
the schema that i use to analyze the problem is this
|
||||
|
||||
create table
|
||||
A
|
||||
( key int4 not null primary key ,...,
|
||||
) ;
|
||||
|
||||
create table
|
||||
REFERENCED_B
|
||||
( key int 4 , ... ,
|
||||
foreign key ( key ) references A --
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Trigger for REFERENCED_B
|
||||
--
|
||||
|
||||
CREATE INDEX I_REFERENCED_B_KEY ON REFERENCED_B ( KEY ) ;
|
||||
|
||||
CREATE TRIGGER T_P_REFERENCED_B_A BEFORE INSERT OR UPDATE ON REFERENCED_B FOR EACH ROW
|
||||
EXECUTE PROCEDURE
|
||||
check_primary_key('KEY','A','KEY' );
|
||||
|
||||
CREATE TRIGGER T_F_D_A_REFERENCED_B BEFORE DELETE ON A FOR EACH ROW
|
||||
EXECUTE PROCEDURE
|
||||
check_foreign_key(1,'cascade','KEY','REFERENCED_B ','KEY' );
|
||||
|
||||
CREATE TRIGGER T_F_U_A_REFERENCED_B AFTER UPDATE ON A FOR EACH ROW
|
||||
EXECUTE PROCEDURE
|
||||
check_foreign_key(1,'cascade','KEY','REFERENCED_B ','KEY' );
|
||||
|
||||
-- ********************************
|
||||
|
||||
I write TRIGGER T_F_U_A_REFERENCED_B ( AFTER ) and not BEFORE because if i set
|
||||
BEFORE , when i try to modify ( update ) a key of A , i start a execution of TRIGGER T_P_REFERENCED_B_A
|
||||
( check_primary_key) before the real modification of key in A , then the execution of ( check_primary_key) return
|
||||
not ok.
|
||||
With AFTER Clausole i modify first key of A then a update the value of referenced table REFERENCED_B.
|
||||
|
||||
Try also the new_example.sql to view the modified policy.
|
||||
I wish that my explain of problem is quite clear .
|
||||
If there is miss understanding ( cause my bad english ) please send email to massimo.lambertini@everex.it
|
||||
|
||||
|
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
DROP FUNCTION autoinc();
|
||||
|
||||
CREATE FUNCTION autoinc()
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/autoinc_DLSUFFIX_'
|
||||
LANGUAGE 'newC';
|
||||
|
@ -1,6 +0,0 @@
|
||||
DROP FUNCTION insert_username();
|
||||
|
||||
CREATE FUNCTION insert_username()
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/insert_username_DLSUFFIX_'
|
||||
LANGUAGE 'newC';
|
||||
|
@ -1,6 +0,0 @@
|
||||
DROP FUNCTION moddatetime();
|
||||
|
||||
CREATE FUNCTION moddatetime()
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/moddatetime_DLSUFFIX_'
|
||||
LANGUAGE 'newC';
|
||||
|
@ -1,14 +0,0 @@
|
||||
DROP FUNCTION check_primary_key ();
|
||||
DROP FUNCTION check_foreign_key ();
|
||||
|
||||
CREATE FUNCTION check_primary_key ()
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/refint_DLSUFFIX_'
|
||||
LANGUAGE 'newC'
|
||||
;
|
||||
|
||||
CREATE FUNCTION check_foreign_key ()
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/refint_DLSUFFIX_'
|
||||
LANGUAGE 'newC'
|
||||
;
|
||||
|
@ -1,12 +0,0 @@
|
||||
DROP FUNCTION timetravel();
|
||||
DROP FUNCTION set_timetravel(name, int4);
|
||||
|
||||
CREATE FUNCTION timetravel()
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/timetravel_DLSUFFIX_'
|
||||
LANGUAGE 'newC';
|
||||
|
||||
CREATE FUNCTION set_timetravel(name, int4)
|
||||
RETURNS int4
|
||||
AS '_OBJWD_/timetravel_DLSUFFIX_'
|
||||
LANGUAGE 'newC' WITH (isStrict);
|
||||
|
Reference in New Issue
Block a user