Query System by C
Posted in 1995
Topics: Stored Procedures & SPL, Server Administration
I have built a database engin of query system implemented by Prolog and SQL, but I'm having difficulties in C. Here is an example of query format to retrieve such that: QUERY: (Name, John) OR (Sex, F) AND [NOT {NOT (Name, Tom) OR (Eyes, blue)} Using an algorithms of Binary Tree, the above query can be translated into an internal representation such that: QUERY: (Name, John) OR (S,ex, F) AND (Name, Tom) AND {NOT (Eyes, blue)} As see an example that NOT {NOT (Name, Tom)} = (Name, Tom) NOT {OR (Eyes, blue)} = AND {NOT (Eyes, blue)} , NOT has to be a leaves in the Binary Tree. If there is a buddy who has programmed a query system in C or has seen a book shows an example implemented in C language, could you email me jkoh@unccsun.uncc.edu directly to help me? Any response would be SINCERELY appreciated. Attached are a unfinished source program works well as a part the engin I expect, and a data file(Table) to test it's query. /*********************** source prog. ***************************************/ #include <stdio.h> #include <string.h> #include <malloc.h> #include <unistd.h> #include <stdlib.h> /* define the lengths of the database attributes */ #define ID_L 3 #define NAME_L 8 #define AGE_L 4 #define HGT_L 5 #define WGT_L 4 #define SEX_L 4 #define EYES_L 6 #define RACE_L 10 #define PROF_L 10 #define ADDR_L 14 #define CRIME_L 11 /* define the structure of the link node */ typedef struct node { char id[ID_L+1]; char name[NAME_L+1]; char age[AGE_L+1]; char hgt[HGT_L+1]; char wgt[WGT_L+1]; char sex[SEX_L+1]; char eyes[EYES_L+1]; char race[RACE_L+1]; char prof[PROF_L+1]; char addr[ADDR_L+1]; char crime[CRIME_L+1]; struct node *next; } query; /* function to print one member of the structure 'query' at a time */ void padz( char *s, int iL) { char fmt[10]; char s1[30]; memcpy(s1,s,iL); sprintf(fmt,"%c-%ds ",'%',iL); s1[iL]=0; printf(fmt,s1); } int match(char *str1, char *str2, int iL,int ifg) { /* function to find out if any match is found with the user given query * and the data base. It returns 1 if match is found, otherwise it returns * 0 if no match is found */ char *p; char str[20]; int retcode = 0; int negation = 0,i=0; if(ifg == 0) return 1; /* returns 1 if user has not given any input */ /* corresponding to attribute 'str1' */ /* ie, no attempt is made to match this attribute */ while(*str1==' ')str1++; while(*str2==' ')str2++; memcpy(str, str1, iL); i = iL-1; /* trailing spaces or '\\n' characters are removed */ while(str[i] == ' ' || str[i]=='\\n' ) str[i--]=0; p = strstr(str2, str); /* searches 'str1' anywhere in the string 'str2' */ if(p) if(*(p-1)=='~') retcode = 0; if((p==NULL) && strchr(str2,'~'))retcode = 1; /* check if negation */ if(p) if( *(p-1) != '~') retcode = 1; return retcode; } /* function to read user query line. If no input is given for this attribute * set ifg=0, otherwise ifg=1 */ void rinput(char *str, int *ifg) { fflush(stdin); gets(str); if(strlen(str) ==0) *ifg = 0; else *ifg = 1; } int matchid(char *str1, char *str2, int iL, int ifg) { /* function to find if user given ID is found in the database */ /* return 1 if user is interested for this ID */ /* otherwise return 0. */ int iL1,i,j, negation=0; int id1, id2; char str[10], str3[10]; if(ifg == 0) return 1; while(*str1==' ')str1++; while(*str2==' ')str2++; memcpy(str3, str1, iL); str3[iL]=0; sscanf(str3,"%d",&id1); /* convert id into a integer */ j=0; iL1 = strlen(str2); while(j < iL1) { sscanf((str2+j),"%s",str); j = j + strlen(str); while(str[j]==' ')j++; negation = 0; if(str[0]=='~') { negation =1; /* set ngation =1 if negation is asked */ str[0]=' '; /* mask ~ character if exist */ } sscanf(str,"%d",&id2); /* id2 is user given id expressed in integer */ if(negation && id1 == id2 ) return 0; if(negation && id1 != id2 ) return 1; if (!negation && id1 == id2 )return 1; } return 0; } /* define global variables */ int idf,namef,agef,hgtf,wgtf,sexf,eyesf,racef,proff,addrf,crimef; query *head,*db; static int cur = 0; int answer_query(void); void initialize_dbase(void) { typedef struct ABC { char id[ID_L+1]; char name[NAME_L+1]; char age[AGE_L+1]; char hgt[HGT_L+1]; char wgt[WGT_L+1]; char sex[SEX_L+1]; char eyes[EYES_L+1]; char race[RACE_L+1]; char prof[PROF_L+1]; char addr[ADDR_L+1]; char crime[CRIME_L+1]; } PERSON; PERSON info; char dummy[100]; int count,iP; FILE *fp; query *temp; /* open the database file */ if ((fp = fopen("db_file", "r")) == NULL) { printf("initialize_temp: CANNOT OPEN! db_file\\n"); exit(1); } fgets(dummy,100,fp); /* skip the first line of the file which is */ /* name of the attributes */ /* printf("size of query = %d\\n",sizeof(query)); */ while(!feof(fp)) /* main while loop to build the linklist */ { temp=(query *)calloc(1,sizeof(query)); if(temp == NULL) { printf("initialize_temp: NO MORE STORAGE AVAILABLE !\\n"); exit(1); } fgets(dummy,100,fp); /* read the entire line upto the new line character */ /* copy different fields into the structure 'query' */ iP=0; memcpy(temp->id, &dummy[iP], ID_L); iP += ID_L; memcpy(temp->name, &dummy[iP], NAME_L); iP += NAME_L; memcpy(temp->age, &dummy[iP], AGE_L); iP += AGE_L; memcpy(temp->hgt, &dummy[iP], HGT_L); iP += HGT_L; memcpy(temp->wgt, &dummy[iP], WGT_L); iP += WGT_L; memcpy(temp->sex, &dummy[iP], SEX_L); iP += SEX_L; memcpy(temp->eyes, &dummy[iP], EYES_L); iP += EYES_L; memcpy(temp->race, &dummy[iP], RACE_L); iP += RACE_L; memcpy(temp->prof, &dummy[iP], PROF_L); iP += PROF_L; memcpy(temp->addr, &dummy[iP], ADDR_L); iP += ADDR_L; memcpy(temp->crime, &dummy[iP], CRIME_L); if(!feof(fp)) { if(head == NULL) { head = temp; /* remember the link head */ db = temp; } else { db->next = temp; /* store the new node into the link list */ db = db->next; /* advance the link pointer */ } } } fclose(fp); } int showlist(void) { /* function to display