]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Implement tupple management
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 25 Jun 2025 00:36:54 +0000 (20:36 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 25 Jun 2025 00:36:54 +0000 (20:36 -0400)
lib/Makefile
lib/devsql.c
lib/unimar.c
lib/unimar.h
lib/unipos.c
lib/unipos.h

index dff637052b549ff7debfb4c30520ec4f1399e0aa..e0f8e0038683a388c01674c73450107d8e4641c3 100644 (file)
@@ -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`
index 4ba5e0dbf7408307b518d085d55b5a84f6d75c9d..ddbbe4cc637db81e36ad42b13f4bc08de235abbf 100644 (file)
 #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
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to drop result issued by a successfull*/
+/*      command.                                        */
+/*                                                      */
+/********************************************************/
+static SQLRES *dropresult(SQLRES *rs)
+
+{
+return rs;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      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
+}
 /*
 \f
 */
@@ -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
 }
index 419721b74b49634091e9b4020d86cb4b7dbc0ba9..cc0b807cfd6ea8988e8102bc53d034a6bf45c083 100644 (file)
@@ -148,4 +148,68 @@ if (dstr!=(char *)0) {
 (void) strcat(cleanstr,"'");
 return cleanstr;
 }
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     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
+}
 
index efb8c22cca2c35723ef3c3ea19cb974e0cf03ea3..9df6398791dceae0f532011cb9d08e73b76dc0a0 100644 (file)
@@ -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
index bf59fa2e84e9f354701d2cd64057dcdb333765f7..26a2aeadbd43f7ed6077d31afd0620561a2e55d8 100644 (file)
@@ -9,6 +9,7 @@
 #include        <stdio.h>
 #include        <stdlib.h>
 #include        <string.h>
+#include        <unistd.h>
 
 #include        "subrou.h"
 #include        "unipos.h"
 #if DATABASE==USE_POSTGRESQL
   #define DB_POSTGRESQL
 #endif
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      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
 /*
 \f
 */
@@ -145,3 +196,61 @@ if (dstr!=(char *)0) {
 (void) strcat(cleanstr,"'");
 return cleanstr;
 }
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     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;
+}
index 1b6d440735779b5b70130754567dc73b8b8e441e..0113f9dadaa2633063722c394a71d9d629fca35a 100644 (file)
@@ -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