#include "postgres.h"

#include "plpgsql.h"


#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

static void func_setup(PLpgSQL_execstate * estate, PLpgSQL_function * func);
static void func_beg(PLpgSQL_execstate *estate, PLpgSQL_function *func);
static void func_end(PLpgSQL_execstate *estate, PLpgSQL_function *func);
static void stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
static void stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);

static PLpgSQL_plugin plugin_funcs = { func_setup,
									   func_beg,
									   func_end,
									   stmt_beg,
									   stmt_end,
									   NULL,
									   NULL};

void			_PG_init(void);
void			_PG_fini(void);

static void
func_setup(PLpgSQL_execstate * estate, PLpgSQL_function * func)
{
	elog(NOTICE, "func setup: \"%s\"", func->fn_signature);
}

static void
func_beg(PLpgSQL_execstate *estate, PLpgSQL_function *func)
{
	elog(NOTICE, "func beg: \"%s\"", func->fn_signature);
}

static void
func_end(PLpgSQL_execstate *estate, PLpgSQL_function *func)
{
	elog(NOTICE, "func end: \"%s\"", func->fn_signature);
}

static void
stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
{
	elog(NOTICE, "stmt beg - ln: %d", stmt->lineno);
}

static void
stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
{
	elog(NOTICE, "stmt end - ln: %d", stmt->lineno);
}

void 
_PG_init(void)
{
	PLpgSQL_plugin ** var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable( "PLpgSQL_plugin" );

	static bool inited = false;

	if (inited)
		return;

	*var_ptr = &plugin_funcs;
}

/*
 * Module unload callback
 */
void
_PG_fini(void)
{
}
