summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/sql/func.pgc
blob: 5ebcafa11dac6ced0381a9f58e3239a05d11eff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

EXEC SQL INCLUDE ../regression;

int main() {
  EXEC SQL char text[25];

  ECPGdebug(1, stderr);
  EXEC SQL CONNECT TO REGRESSDB1;

  EXEC SQL SET AUTOCOMMIT TO ON;
  EXEC SQL WHENEVER SQLWARNING SQLPRINT;
  EXEC SQL WHENEVER SQLERROR SQLPRINT;

  EXEC SQL CREATE TABLE My_Table ( Item1 int, Item2 text );
  EXEC SQL CREATE TABLE Log (name text, w text);

  EXEC SQL CREATE FUNCTION My_Table_Check() RETURNS trigger
    AS $test$
    BEGIN
	INSERT INTO Log VALUES(TG_NAME, TG_WHEN);
	RETURN NEW;
    END; $test$
    LANGUAGE plpgsql;

  EXEC SQL CREATE TRIGGER My_Table_Check_Trigger
    BEFORE INSERT
    ON My_Table
    FOR EACH ROW
    EXECUTE PROCEDURE My_Table_Check();

  EXEC SQL INSERT INTO My_Table VALUES (1234, 'Some random text');
  EXEC SQL INSERT INTO My_Table VALUES (5678, 'The Quick Brown');
  EXEC SQL SELECT name INTO :text FROM Log LIMIT 1;
  printf("Trigger %s fired.\n", text);

  EXEC SQL DROP TRIGGER My_Table_Check_Trigger ON My_Table;
  EXEC SQL DROP FUNCTION My_Table_Check();
  EXEC SQL DROP TABLE Log;
  EXEC SQL DROP TABLE My_Table;

  EXEC SQL DISCONNECT ALL;

  return 0;
}