From ad264d41e4960bb9c7cea5bbc5f76d8aa7f4907f Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Sat, 28 Jun 2025 22:16:31 -0400 Subject: [PATCH] ready to check database record insert command --- lib/devsql.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++-- lib/devsql.h | 5 +- lib/lvleml.c | 6 ++- lib/unimar.c | 32 ++++++++++++ lib/unimar.h | 4 ++ lib/unipos.c | 46 ++++++++++++++++- lib/unipos.h | 6 ++- 7 files changed, 231 insertions(+), 9 deletions(-) diff --git a/lib/devsql.c b/lib/devsql.c index 9bf939b..be44341 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -32,9 +32,10 @@ typedef struct { }SQLTYP; //SQL database request -#define EMAILS "emails" //emails tables +#define EMAILS "emails" //emails tables +#define SESSIONS "sessions" //session tabbles -//field available in table "EMAILS" +//field available in table "emails" const char *usrfield[]={ "email", "password", @@ -44,6 +45,15 @@ const char *usrfield[]={ (char *)0 }; +//field available in table "sessions" +const char *sesfield[]={ + "sessid", + "sessfrom", + "emailfrom", + "taille", + (char *)0 + }; + /* */ @@ -184,8 +194,8 @@ return numrow; */ /********************************************************/ /* */ -/* Procedure to create a data request and submit it*/ -/* to the proper database daemon. */ +/* Procedure to create a data search request and */ +/* submit it to the to the proper database daemon. */ /* */ /********************************************************/ static SQLRES *gettupple(SQLTYP *sql,const char *fmt,...) @@ -217,6 +227,46 @@ if ((rou_vasprintf(&cmd,fmt,args))>0) { va_end(args); return rs; +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to do some action to change database */ +/* contents. */ +/* */ +/********************************************************/ +static int sqlrequest(SQLTYP *sql,const char *fmt,...) + +{ +#define OPEP "devsql.c:sqlrequest," + +int number; +va_list args; +char *cmd; + +number=-1; +va_start(args,fmt); +if ((rou_vasprintf(&cmd,fmt,args))>0) { + switch(sql->sqldb) { + case db_postgres : + number=pos_request(sql->db.psql,cmd); + break; + case db_maria : + number=mar_request(sql->db.msql,cmd); + break; + default : + (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + break; + } + (void) free(cmd); + } +va_end(args); +return number; + #undef OPEP } /* @@ -336,6 +386,29 @@ while (proceed==true) { return usr; #undef SELUSR +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* 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)" + +_Bool isok; + +isok=true; +(void) rou_alert(0,"%s JMPDBG Insertin session <%s>",OPEP,seskey); +isok=(sqlrequest(sql,INSSES,seskey)==1); +return isok; + #undef OPEP } /* @@ -580,7 +653,7 @@ while (proceed==true) { phase=999; } if ((gooddata=sql_gooddata(sql,email))==(char *)0) { - (void) rou_alert(0,"%s %s table, key <%s> not acceptable!", + (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)", OPEP,EMAILS,email); phase=999; } @@ -606,3 +679,61 @@ return isok; #undef OPEP } +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to manage information about email */ +/* exchange session. */ +/* */ +/********************************************************/ +PUBLIC _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,char *seskey,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) { + (void) rou_alert(0,"%s SQL pointer is NUll (Bug?)",OPEP); + phase=999; + } + if ((gooddata=sql_gooddata(sql,seskey))==(char *)0) { + (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)", + OPEP,SESSIONS,seskey); + phase=999; + } + break; + case 1 : //getting user information + switch (action) { + case sql_insert : + isok=insert_ses(sql,gooddata,ses); + break; + default : + (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action); + } + gooddata=rou_freestr(gooddata); + break; + default : + proceed=false; + break; + } + phase++; + } +return isok; + +#undef OPEP +} diff --git a/lib/devsql.h b/lib/devsql.h index 785e7e5..65b7b84 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -23,7 +23,10 @@ extern SQLPTR *sql_closesql(SQLPTR *sqlptr); //as key search by database extern char *sql_gooddata(SQLPTR *sqlptr,char *key); -//procedure to get information on exiting user +//procedure to manage information on exiting user extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr); +//procedure to manage information on email exchange session +extern _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,char *seskey,SESTYP **ses); + #endif diff --git a/lib/lvleml.c b/lib/lvleml.c index 6812c2e..7e9f70d 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -38,9 +38,13 @@ static void getsessid(CONTYP *contact) { if (contact!=(CONTYP *)0) { + char *newsid; + + newsid=eml_getcursesid(contact->mainsesid,contact->numreset); contact->session=sql_freeses(contact->session); contact->session=(SESTYP *)calloc(1,sizeof(SESTYP)); - contact->session->sessid=eml_getcursesid(contact->mainsesid,contact->numreset); + contact->session->sessid=newsid; + (void) sql_mngses(contact->sqlptr,sql_insert,newsid,&(contact->session)); } } /* diff --git a/lib/unimar.c b/lib/unimar.c index 8dfa2e5..103a7b3 100644 --- a/lib/unimar.c +++ b/lib/unimar.c @@ -310,4 +310,36 @@ return numrow; #undef OPEP } +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to request an action (insert,update */ +/* delete) to the database. */ +/* return the number of tupple affected by the */ +/* command. */ +/* */ +/********************************************************/ +PUBLIC int mar_request(MARPTR *marptr,char *command) + +{ +#define OPEP "unipos.c:pos_action," + +int number; +number=-1; +#ifdef DB_MYSQL + { + register MYSQL *ms; + + ms=(MYSQL *)marptr; + if (mysql_query(ms,command)==0) + number=mysql_affected_rows(ms); + else + (void) rou_alert(0,"%s pid='%05d' Unable to carry cmd=<%s>, error=<%s>", + OPEP,getpid(),command,mysql_error(ms)); + } +#endif +return number; +} diff --git a/lib/unimar.h b/lib/unimar.h index c22ab03..f46a7f0 100644 --- a/lib/unimar.h +++ b/lib/unimar.h @@ -38,4 +38,8 @@ extern MARRES *mar_gettupple(MARPTR *marptr,char *command); //procedure to return the number of entry within a result extern int mar_nbrtupple(MARRES *marres); +//procedure to submit an action (insert,update,delete) +//to a MySAL database +extern int mar_request(MARPTR *marptr,char *command); + #endif diff --git a/lib/unipos.c b/lib/unipos.c index f260111..640e592 100644 --- a/lib/unipos.c +++ b/lib/unipos.c @@ -306,7 +306,7 @@ return posres; */ /********************************************************/ /* */ -/* Procedure to retunr the number of tupple related*/ +/* Procedure to return the number of tupple related*/ /* related to a previous search. */ /* */ /********************************************************/ @@ -321,3 +321,47 @@ numrow=PQntuples((PGresult *)posres); #endif return numrow; } +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to request an action (insert,update */ +/* delete) to the database. */ +/* return the number of tupple affected by the */ +/* command. */ +/* */ +/********************************************************/ +PUBLIC int pos_request(POSPTR *posptr,char *command) + +{ +#define OPEP "unipos.c:pos_action," + +int number; + +number=-1; +#ifdef DB_POSTGRESQL + { + register PGconn *pf; + PGresult *pgstat; + + pf=(PGconn *)posptr; + if ((pgstat=request((PGconn *)pf,command))!=(PGresult *)0) { + switch (PQresultStatus(pgstat)) { + case PGRES_COMMAND_OK : + number=atoi(PQcmdTuples(pgstat)); + break; + default : + (void) rou_alert(0,"%s Command <%s> failed, (error=<%s> pgstatut='%d')", + OPEP,command,PQerrorMessage(pf), + PQresultStatus(pgstat)); + break; + } + (void) PQclear(pgstat); + } + } +#endif +return number; + +#undef OPEP +} diff --git a/lib/unipos.h b/lib/unipos.h index 8bb2994..e57a18f 100644 --- a/lib/unipos.h +++ b/lib/unipos.h @@ -32,10 +32,14 @@ extern char *pos_getfield(POSRES *rs,int tuple,int position); //procedure to extract specific field value within database extern char *pos_getvalue(POSRES *rs,int tuple,const char *fieldname); -//procedure to extract data from database +//procedure to extract data from postgresql database extern POSRES *pos_gettupple(POSPTR *posptr,char *command); //procedure to return the number of entry within a result extern int pos_nbrtupple(POSRES *posres); +//procedure to submit an action (insert,update,delete) +//to a postgresql database +extern int pos_request(POSPTR *posptr,char *command); + #endif -- 2.47.3