#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[]) {
 FILE *dbgs;

	         if ((dbgs = fopen("log", "w")) != NULL)
		                 ECPGdebug(1, dbgs);
  EXEC SQL CONNECT TO mm;

  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 FUNCTION My_Table_Check() RETURNS trigger
    AS $test$
    BEGIN
    	RAISE NOTICE 'TG_NAME=%, TG WHEN=%', 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 DROP TRIGGER My_Table_Check_Trigger ON My_Table;
  EXEC SQL DROP FUNCTION My_Table_Check();
  EXEC SQL DROP TABLE My_Table;

  EXEC SQL DISCONNECT ALL;
  if (dbgs != NULL)
                  fclose(dbgs);


  return 0;
}
