I' trying to add 3 user_defined function to the PostgreSQL-8.4.15. Here are 3 functions:
(1) start_create_profile();
(2) make_profile();
(3) check_anomaly();
All of them are written in file "test.c" located in src/backend/tcop. I want to call (1) and (3) from the middle of exec_simple_query()
. exec_simple_query()
is PostgreSQL function that is written in "postgres.c" located in src/backend/tcop. I want to call (2) directly through my GUI.
here is the my code written in "test.c" :
#include "postgres.h"
#ifndef PROGPROFILE_H_
#define PROGPROFILE_H_
/* interfaces */
extern void start_create_profile(List *querytree_list);
extern void create_profile();
extern void check_anomaly(List *querytree_list);
#endif /* Test ProgProf */
void start_create_profile(List *querytree_list){
ListCell *l;
ListCell *tl;
FILE *f;
//if the file exist just open and write
//else create and write
f = fopen ("QueryParsed.txt", "a+");
Query *query_idr = (Query *)linitial(querytree_list);
// CMD_SELECT=0 CMD_INSERT=1 CMD_UPDATE=2
switch (query_idr->commandType)
{
case CMD_SELECT:
fputs("CMD_SELECT, ", f);
break;
case CMD_INSERT:
fputs("CMD_INSERT, ", f);
break;
case CMD_UPDATE:
fputs("CMD_UPDATE, ", f);
break;
default:
break;
}
//to have the ID of the table
foreach(l, query_idr->rtable){
Oid tab_idT = ((RangeTblEntry *) lfirst(l)) ->relid;
//char temp1[10];
char *tab_idTConverted = itoa(tab_idT);
/* This is not a table */
if (tab_idT == 0)
continue;
fputs(" tab_id: , ", f);
fputs(tab_idTConverted, f);
}
//to have the name of the targer list
foreach(tl, query_idr->targetList){
TargetEntry *tle = (TargetEntry *) lfirst(tl);
Oid tab_id = tle->resorigtbl;
int tab_idCast=(int)tab_id;
//char temp[10];
char *tab_idConverted = itoa(tab_idCast);
char *resname=tle->resname;
fputs("Name of column: ", f);
fputs(resname, f);
fputs(" ID: ", f);
fputs(tab_idConverted, f);
fputs("\n", f);
}
//close the file that we write
fputs("$", f);
fclose (f);
}
void create_profile(){
}
void check_anomaly(List *querytree_list){
}
Now I've created a very simple GUI including 3 buttons(by netbeans in java). Button1, button2, button3 corresponding by order to start_create_profile()
, make_profile, check_anomaly()
.
I want to use a global variable(Let's consider "button" that can be set to 3 different values like 0, 1, 2. I want that whenever I push the button1 or button3 the global variable become set to 1 or 2 in order to use "if" that I've written in exec_simple_query()
. here is "if"
//initially button=0;
//inside exec_simple_query
if(button==1) start_create_profile();
if(button==2) check_anomaly;
and whenever I push the button2, the function(2) has to be called directly. Any idea how do I have to set that variable to make me able choosing one of those 3 function using my GUI?? How do I have to call the function(2) directly through my GUI??