From: Jean-Marc Pigeon (Delson) Date: Fri, 11 Jul 2025 00:51:40 +0000 (-0400) Subject: Trying to make devsql.c more generic X-Git-Tag: tag-0.14~140 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=a1ad30e81ec949fcf0da20d9df383f7e47ab9fe5;p=jmp%2Fmailleur Trying to make devsql.c more generic --- diff --git a/lib/Makefile b/lib/Makefile index 185a0f4..ea2db41 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -67,6 +67,7 @@ gestcp.o: \ gestcp.h gestcp.c gessql.o: \ + unisql.h \ devsql.h \ gessql.h gessql.c diff --git a/lib/devsql.c b/lib/devsql.c index 678916b..ba8b497 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -31,16 +31,10 @@ typedef struct { }db; }SQLTYP; -typedef struct { - int num; //field number - char *name; //field name; - }FLDTYP; - //SQL database request #define EMAILS "emails" //emails tables #define SESSIONS "sessions" //session tables -#define ACTIONS "actions" //action tables /* @@ -356,106 +350,6 @@ return usr; */ /********************************************************/ /* */ -/* Procedure to create a new session contents */ -/* */ -/********************************************************/ -static _Bool insert_ses(SQLTYP *sql,char *seskey,SESTYP **ses) - -{ -#define OPEP "devseql.c:insert_ses," -#define INSSES "INSERT INTO "SESSIONS" (sessid) VALUES(%s)" - -return (sql_request(sql,INSSES,seskey)==1); - -#undef INSSES -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ -/* Procedure to update a session contents */ -/* */ -/********************************************************/ -static _Bool update_ses(SQLTYP *sql,char *seskey,SESTYP **ses) - -{ -#define OPEP "devseql.c:update_ses," -#define UPDSES "UPDATE "SESSIONS" set %s WHERE sessid=%s" - -//field available in table "sessions" -static const FLDTYP sesfield[]={ - {0,"sessid"}, - {1,"sesstitle"}, - {2,"sessfrom"}, - {3,"emailfrom"}, - {4,"taille"}, - {5,"duration"}, - {6,(char *)0} - }; - -_Bool isok; -char *cmtset; - -isok=true; -cmtset=strdup(""); -for (int i=0;(isok==true)&&(sesfield[i].name!=(char *)0);i++) { - char *data; - char items[50]; - - data=(char *)0; - (void) strcpy(items,""); - switch (sesfield[i].num) { - case 0 : //session ID - break; //nothing to do - case 1 : //title - data=sql_gooddata(sql,(*ses)->title); - break; //nothing to do - case 2 : //sfrom - data=sql_gooddata(sql,(*ses)->sfrom); - break; //nothing to do - case 3 : //efrom - data=sql_gooddata(sql,(*ses)->hfrom); - break; - case 4 : //taille - (void) rou_asprintf(&data,"%lu",(*ses)->taille); - break; - case 5 : //duration - data=sql_gooddata(sql,(*ses)->duration); - break; - default : - isok=false; - (void) rou_alert(0,"%s Unexpected field <%s> (Bug?)",OPEP,sesfield[i]); - break; - } - if (data!=(char *)0) { - char *newset; - char *sep; - - newset=(char *)0; - sep=""; - if (strlen(cmtset)>0) - sep=","; - (void) rou_asprintf(&newset,"%s%s%s=%s",cmtset,sep,sesfield[i].name,data); - cmtset=rou_freestr(cmtset); - cmtset=newset; - data=rou_freestr(data); - } - } -if (isok==true) - isok=(sql_request(sql,UPDSES,cmtset,seskey)==1); -cmtset=rou_freestr(cmtset); -return isok; - -#undef INSSES -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ /* Procedure to establish a link with the */ /* designated SQL server. */ /* */ @@ -766,125 +660,14 @@ return isok; */ /********************************************************/ /* */ -/* Procedure to manage information about email */ -/* exchange session. */ -/* */ -/********************************************************/ -PUBLIC _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses) - -{ -#define OPEP "devsql.c:sql_mngses," - -_Bool isok; -char *gooddata; -SQLTYP *sql; -int phase; -_Bool proceed; - -isok=false; -gooddata=(char *)0; -sql=(SQLTYP *)sqlptr; -phase=0; -proceed=true; -while (proceed==true) { - switch (phase) { - case 0 : //checking SQL - if ((sql==(SQLTYP *)0)||(ses==(SESTYP **)0)||(*ses==(SESTYP *)0)) { - (void) rou_alert(0,"%s SQL pointer is NUll (Bug?)",OPEP); - phase=999; - } - if ((gooddata=sql_gooddata(sql,(*ses)->sessid))==(char *)0) { - (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)", - OPEP,SESSIONS,(*ses)->sessid); - phase=999; - } - break; - case 1 : //getting user information - switch (action) { - case sql_insert : - isok=insert_ses(sql,gooddata,ses); - break; - case sql_update : - isok=update_ses(sql,gooddata,ses); - break; - default : - (void) rou_alert(0,"%s action='%d' not yet implemented! (BUG?)", - OPEP,action); - } - gooddata=rou_freestr(gooddata); - break; - default : - proceed=false; - break; - } - phase++; - } -return isok; - -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ -/* Procedure to update database information about */ -/* emails echange current and last status. */ +/* Procedure to retrieve/store data from/to */ +/* database. */ /* */ /********************************************************/ -PUBLIC _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act) +PUBLIC _Bool sql_mngdata(SQLPTR *sqlptr,FLDTYP *field,SQLENUM action) { -#define OPEP "devsql.c:sql_mngact," -#define DELACT "DELETE FROM "ACTIONS" WHERE sessid=%s AND rcptto=%s" -#define INACT "INSERT INTO "ACTIONS" (%s) VALUES(%s,%s,%s,%d,%s)" - -_Bool isok; - -isok=false; -if (act!=(ACTTYP *)0) { - char strcode[10]; - char *goodid; - char *goodrcpt; - char *goodcode; - - (void) memset(strcode,'\000',sizeof(strcode)); - strcode[0]=act->code; - goodid=sql_gooddata(sqlptr,act->sessid); - goodrcpt=sql_gooddata(sqlptr,act->rcptto); - goodcode=sql_gooddata(sqlptr,strcode); - if (act->resp!=(char **)0) { - char *field; - - field="code,sessid,rcptto,numline,info"; - (void) sql_request(sqlptr,DELACT,goodid,goodrcpt); - if (act->resp!=(char **)0) { - char **resp; - int num; - - resp=act->resp; - num=1; - while (*resp!=(char *)0) { - char *goodinfo; - - goodinfo=sql_gooddata(sqlptr,*resp); - (void) sql_request(sqlptr,INACT,field,goodcode,goodid,goodrcpt,num,goodinfo); - goodinfo=rou_freestr(goodinfo); - resp++; - num++; - } - } - } - goodcode=rou_freestr(goodcode); - goodrcpt=rou_freestr(goodrcpt); - goodid=rou_freestr(goodid); - isok=true; - } -return isok; - -#undef INACT -#undef DELACT -#undef OPEP +return false; } /* diff --git a/lib/gessql.c b/lib/gessql.c index b4f608d..03f22c0 100644 --- a/lib/gessql.c +++ b/lib/gessql.c @@ -4,12 +4,222 @@ /* Module to manage complexe SQL request */ /* */ /********************************************************/ +#include #include "subrou.h" +#include "unisql.h" #include "gessql.h" +//DATABASE Table names #define RMTTBL "remotes" //Table about remotes site +#define ACTTBL "actions" //action tables +#define SESTBL "sessions" //session tables +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to update a session contents */ +/* */ +/********************************************************/ +static _Bool update_ses(SQLPTR *sqlptr,char *seskey,SESTYP **ses) + +{ +#define OPEP "devseql.c:update_ses," + +static const char *upd="UPDATE "SESTBL" set %s WHERE sessid=%s"; + +//field available in table "sessions" +static const FLDTYP sesfield[]={ + {0,"sessid"}, + {1,"sesstitle"}, + {2,"sessfrom"}, + {3,"emailfrom"}, + {4,"taille"}, + {5,"duration"}, + {6,(char *)0} + }; + +_Bool isok; +char *cmtset; + +isok=true; +cmtset=strdup(""); +for (int i=0;(isok==true)&&(sesfield[i].name!=(char *)0);i++) { + char *data; + char items[50]; + + data=(char *)0; + (void) strcpy(items,""); + switch (sesfield[i].num) { + case 0 : //session ID + break; //nothing to do + case 1 : //title + data=sql_gooddata(sqlptr,(*ses)->title); + break; //nothing to do + case 2 : //sfrom + data=sql_gooddata(sqlptr,(*ses)->sfrom); + break; //nothing to do + case 3 : //efrom + data=sql_gooddata(sqlptr,(*ses)->hfrom); + break; + case 4 : //taille + (void) rou_asprintf(&data,"%lu",(*ses)->taille); + break; + case 5 : //duration + data=sql_gooddata(sqlptr,(*ses)->duration); + break; + default : + isok=false; + (void) rou_alert(0,"%s Unexpected field <%s> (Bug?)",OPEP,sesfield[i]); + break; + } + if (data!=(char *)0) { + char *newset; + char *sep; + + newset=(char *)0; + sep=""; + if (strlen(cmtset)>0) + sep=","; + (void) rou_asprintf(&newset,"%s%s%s=%s",cmtset,sep,sesfield[i].name,data); + cmtset=rou_freestr(cmtset); + cmtset=newset; + data=rou_freestr(data); + } + } +if (isok==true) + isok=(sql_request(sqlptr,upd,cmtset,seskey)==1); +cmtset=rou_freestr(cmtset); +return isok; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to manage information about email */ +/* exchange session. */ +/* */ +/********************************************************/ +PUBLIC _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses) + +{ +#define OPEP "devsql.c:sql_mngses," + +static const char *ins="INSERT INTO "SESTBL" (sessid) VALUES(%s)"; + +_Bool isok; +char *gooddata; +int phase; +_Bool proceed; + +isok=false; +gooddata=(char *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //checking SQL + if ((sqlptr==(SQLPTR *)0)||(ses==(SESTYP **)0)||(*ses==(SESTYP *)0)) { + (void) rou_alert(0,"%s SQL pointer is NUll (Bug?)",OPEP); + phase=999; + } + if ((gooddata=sql_gooddata(sqlptr,(*ses)->sessid))==(char *)0) { + (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)", + OPEP,SESTBL,(*ses)->sessid); + phase=999; + } + break; + case 1 : //getting user information + switch (action) { + case sql_insert : + isok=(sql_request(sqlptr,ins,gooddata)==1); + break; + case sql_update : + isok=update_ses(sqlptr,gooddata,ses); + break; + default : + (void) rou_alert(0,"%s action='%d' not yet implemented! (BUG?)", + OPEP,action); + } + gooddata=rou_freestr(gooddata); + break; + default : + proceed=false; + break; + } + phase++; + } +return isok; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to update database information about */ +/* emails echange current and last status. */ +/* */ +/********************************************************/ +PUBLIC _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act) + +{ +#define OPEP "devsql.c:sql_mngact," + +static const char *del="DELETE FROM "ACTTBL" WHERE sessid=%s AND rcptto=%s"; +static const char *ins="INSERT INTO "ACTTBL" (%s) VALUES(%s,%s,%s,%d,%s)"; + +_Bool isok; + +isok=false; +if (act!=(ACTTYP *)0) { + char strcode[10]; + char *goodid; + char *goodrcpt; + char *goodcode; + + (void) memset(strcode,'\000',sizeof(strcode)); + strcode[0]=act->code; + goodid=sql_gooddata(sqlptr,act->sessid); + goodrcpt=sql_gooddata(sqlptr,act->rcptto); + goodcode=sql_gooddata(sqlptr,strcode); + if (act->resp!=(char **)0) { + char *field; + + field="code,sessid,rcptto,numline,info"; + (void) sql_request(sqlptr,del,goodid,goodrcpt); + if (act->resp!=(char **)0) { + char **resp; + int num; + + resp=act->resp; + num=1; + while (*resp!=(char *)0) { + char *goodinfo; + + goodinfo=sql_gooddata(sqlptr,*resp); + (void) sql_request(sqlptr,ins,field,goodcode,goodid,goodrcpt,num,goodinfo); + goodinfo=rou_freestr(goodinfo); + resp++; + num++; + } + } + } + goodcode=rou_freestr(goodcode); + goodrcpt=rou_freestr(goodrcpt); + goodid=rou_freestr(goodid); + isok=true; + } +return isok; + +#undef OPEP +} /* ^L */ diff --git a/lib/gessql.h b/lib/gessql.h index 505c251..4da3505 100644 --- a/lib/gessql.h +++ b/lib/gessql.h @@ -11,6 +11,9 @@ #include "devsql.h" +//procedure to manage action stats on email exhange status +extern _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act); + //Procedure to increment (or decrement) the remoteip //connection number extern _Bool sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta); diff --git a/lib/unisql.h b/lib/unisql.h index dde32c4..e37fd7d 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -17,6 +17,23 @@ typedef enum { sql_uknown //No action }SQLENUM; +typedef enum { + row_integer, //row data is an integer + row_char, //row data is a char + row_unknown //row data unsoecifier + }FLDENUM; + +//defining a database column definition retrieval +typedef struct { + int num; //field number + char *name; //field name; + FLDENUM specie; //field type + union { + u_long entier;//data is a number + char *string; //data is a string + }data; + }FLDTYP; + //structure about user within the database typedef struct { char *email; //user emails