From 6a48a174354a06e8c6619c5312703246e776b886 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Tue, 24 Jun 2025 20:36:54 -0400 Subject: [PATCH] Implement tupple management --- lib/Makefile | 2 - lib/devsql.c | 123 +++++++++++++++++++++++++++++++++++++++++++++------ lib/unimar.c | 64 +++++++++++++++++++++++++++ lib/unimar.h | 9 ++++ lib/unipos.c | 109 +++++++++++++++++++++++++++++++++++++++++++++ lib/unipos.h | 9 ++++ 6 files changed, 301 insertions(+), 15 deletions(-) diff --git a/lib/Makefile b/lib/Makefile index dff6370..e0f8e00 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -174,8 +174,6 @@ toremake: Makefile CC = gcc LD = gcc CFLAGS = -Wall -D_GNU_SOURCE \ - -Dwith_postgres \ - -Dwith_mysql \ $(OPTIME) LIBMAIL = libmail.a libmar.a libpos.a PAR = -j`/usr/bin/getconf _NPROCESSORS_ONLN` diff --git a/lib/devsql.c b/lib/devsql.c index 4ba5e0d..ddbbe4c 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -14,12 +14,13 @@ #include "unipos.h" #include "devsql.h" +typedef void SQLRES; //Result from database typedef enum { - db_postgres, //Postgres database - db_maria, //Mariadb SQL - db_unknown - }SQLDBTYP; + db_postgres, //Postgres database + db_maria, //Mariadb SQL + db_unknown + }SQLDBTYP; typedef struct { SQLDBTYP sqldb; //database type @@ -28,6 +29,97 @@ typedef struct { MARPTR *msql; //Mariadb|MySQL Reference }db; }SQLTYP; + +//SQL database request +#define SELFROM "SELECT * FROM" +#define EMAILS "emails" //emails tables +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to drop result issued by a successfull*/ +/* command. */ +/* */ +/********************************************************/ +static SQLRES *dropresult(SQLRES *rs) + +{ +return rs; +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to the number of tupple within a */ +/* result issued by a successfull command. */ +/* */ +/********************************************************/ +static int nbrtupple(SQLTYP *sql,SQLRES *rs) + +{ +#define OPEP "devsql.c:nbrtupple," + +int numrow; + +numrow=0; +switch(sql->sqldb) { + case db_postgres : + numrow=pos_nbrtupple((POSRES *)rs); + break; + case db_maria : + numrow=mar_nbrtupple((MARRES *)rs); + break; + default : + (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + break; + } +return numrow; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to create a data request and submit it*/ +/* to the proper database daemon. */ +/* */ +/********************************************************/ +static SQLRES *gettupple(SQLTYP *sql,const char *fmt,...) + +{ +#define OPEP "devsql.c:gettupple," + +SQLRES *rs; +va_list args; +char *cmd; + +rs=(SQLRES *)0; +va_start(args,fmt); +if ((rou_vasprintf(&cmd,fmt,args))>0) { + switch(sql->sqldb) { + case db_postgres : + rs=(SQLRES *)pos_gettupple(sql->db.psql,cmd); + break; + case db_maria : + rs=(SQLRES *)mar_gettupple(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 rs; + +#undef OPEP +} /* */ @@ -251,9 +343,11 @@ PUBLIC USRTYP *sql_getusr(SQLPTR *sqlptr,char *email) { #define OPEP "devsql.c:sql_getuser," +#define SELUSR "%s %s WHERE username=%s" USRTYP *usr; SQLTYP *sql; +SQLRES *rs; int phase; _Bool proceed; @@ -289,16 +383,17 @@ while (proceed==true) { phase=999; } break; - case 3 : //opening specific database - switch (sql->sqldb) { - case db_postgres : - break; - case db_maria : - break; - default : - (void) rou_alert(0,"%s Unexpected type='%d' (bug?)",OPEP,sql->sqldb); - break; + case 3 : //getting user information + if ((rs=gettupple(sql,SELUSR,SELFROM,EMAILS,email))!=(SQLRES *)0) { + (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)", + OPEP,email); + phase=999; //Trouble trouble + } + break; + case 4 : //getting user information + if (nbrtupple(sql,rs)>0) { } + rs=dropresult(rs); break; default : proceed=false; @@ -307,5 +402,7 @@ while (proceed==true) { phase++; } return usr; + +#undef SELUSR #undef OPEP } diff --git a/lib/unimar.c b/lib/unimar.c index 419721b..cc0b807 100644 --- a/lib/unimar.c +++ b/lib/unimar.c @@ -148,4 +148,68 @@ if (dstr!=(char *)0) { (void) strcat(cleanstr,"'"); return cleanstr; } +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to extract data from the database. */ +/* Return POSRES status pointer (can be NULL is not*/ +/* successfull. */ +/* */ +/********************************************************/ +PUBLIC MARRES *mar_gettupple(MARPTR *marptr,char *command) + +{ +#define OPEP "unimar.c:mar_gettupple" + +register MARRES *marres; + +marres=(MARRES *)0; +#ifdef DB_MYSQL + { + MYSQL_RES *mysstatut; + register MYSQL *ms; + + mysstatut=(MYSQL_RES *)0; + ms=(MYSQL *)marptr; + if (mysql_query(ms,command)==0) { + if ((mysstatut=mysql_store_result(ms))==(MYSQL_RES *)0) + (void) rou_alert(3,"%s result empty for cmd=<%s>",OPEP,command); + } + else { + (void) rou_alert(0,"%s pid='%05d' Unable to carry cmd=<%s>, error=<%s>", + OPEP,getpid(),command,mysql_error((MYSQL *)ms)); + } + marres=(MARRES *)mysstatut; + } +#endif +return marres; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to return the number of tupple related*/ +/* related to a previous search. */ +/* */ +/********************************************************/ +PUBLIC int mar_nbrtupple(MARRES *marres) + +{ +#define OPEP "unimar.c:mar_nbrtupple," + +register int numrow; + +numrow=0; +#ifdef DB_MYSQL + numrow=mysql_num_rows((MYSQL_RES *)marres); +#endif +return numrow; + +#undef OPEP +} diff --git a/lib/unimar.h b/lib/unimar.h index efb8c22..9df6398 100644 --- a/lib/unimar.h +++ b/lib/unimar.h @@ -11,6 +11,9 @@ //reference to a SQL pointer reference typedef void MARPTR; +//reference to a MariaDB result +typedef void MARRES; + //Opening a postscript database extern MARPTR *mar_opensql(const char *host,const char *sqlport,const char *dbname); @@ -20,4 +23,10 @@ extern MARPTR *mar_closesql(MARPTR *marptr); //Procedure to detect and 'clean' any single quote within a string extern char *mar_cleanquote(char *sequence); +//procedure to extract data from database +extern MARRES *mar_gettupple(MARPTR *marptr,char *command); + +//procedure to return the number of entry within a result +extern int mar_nbrtupple(MARRES *marres); + #endif diff --git a/lib/unipos.c b/lib/unipos.c index bf59fa2..26a2aea 100644 --- a/lib/unipos.c +++ b/lib/unipos.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "subrou.h" #include "unipos.h" @@ -17,7 +18,57 @@ #if DATABASE==USE_POSTGRESQL #define DB_POSTGRESQL #endif +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to execut a database request by daemon*/ +/* */ +/********************************************************/ +#ifdef DB_POSTGRESQL +static PGresult *request(PGconn *pf,char *directive) + +{ +#define OPEP "unipos.c:request," +#define RELAX 1000000 + +PGresult *statut; +int try; +statut=(PGresult *)0; +try=0; +while (statut==(PGresult *)0) { + try++; + switch(PQstatus(pf)) { + case CONNECTION_OK : + if ((statut=PQexec(pf,directive))!=(PGresult *)0) + break; + else + (void) rou_alert(0,"%s Command <%s> failed, (error=<%s>), retrying", + OPEP,directive,PQerrorMessage(pf)); + // fall through + // NO Break, want DB reset + default : + (void) rou_alert(0,"%s reseting postgres connection (try='%d')", + OPEP,try); + (void) usleep(RELAX/2); + (void) PQreset(pf); + (void) usleep(RELAX/2); + break; + } + if (try>10) { + (void) rou_alert(0,"%s Unable to carry command <%s> Postgres error!", + OPEP,directive); + break; + } + } +return statut; + +#undef RELAX +#undef OPEP +} +#endif /* */ @@ -145,3 +196,61 @@ if (dstr!=(char *)0) { (void) strcat(cleanstr,"'"); return cleanstr; } +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to extract data from the database. */ +/* Return POSRES status pointer (can be NULL is not*/ +/* successfull. */ +/* */ +/********************************************************/ +PUBLIC POSRES *pos_gettupple(POSPTR *posptr,char *command) + +{ +#define OPEP "basmys.c:mys_tupple," + +register POSRES *posres; + +posres=(POSRES *)0; +#ifdef DB_POSTGRESQL + { + PGresult *pgstatut; + register PGconn *pf; + + pf=(PGconn *)posptr; + if ((pgstatut=request(pf,command))!=(PGresult *)0) { + if (PQresultStatus(pgstatut)!=PGRES_TUPLES_OK) { + (void) rou_alert(0,"%s SQL command <%s> failed",OPEP,command); + (void) PQclear(pgstatut); + pgstatut=(PGresult *)0; + } + } + posres=(POSRES *)pgstatut; + } +#endif +return posres; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to retunr the number of tupple related*/ +/* related to a previous search. */ +/* */ +/********************************************************/ +PUBLIC int pos_nbrtupple(POSRES *posres) + +{ +register int numrow; + +numrow=0; +#ifdef DB_POSTGRESQL +numrow=PQntuples((PGresult *)posres); +#endif +return numrow; +} diff --git a/lib/unipos.h b/lib/unipos.h index 1b6d440..0113f9d 100644 --- a/lib/unipos.h +++ b/lib/unipos.h @@ -11,6 +11,9 @@ //reference to a SQL pointer reference typedef void POSPTR; +//reference to a POSTGRESQL result +typedef void POSRES; + //Opening a postscript database extern POSPTR *pos_opensql(const char *host,const char *sqlport,const char *dbname); @@ -20,4 +23,10 @@ extern POSPTR *pos_closesql(POSPTR *posptr); //Procedure to detect and 'clean' any single quote within a string extern char *pos_cleanquote(char *sequence); +//procedure to extract data from 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); + #endif -- 2.47.3