]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Trying to make devsql.c more generic
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 00:51:40 +0000 (20:51 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 00:51:40 +0000 (20:51 -0400)
lib/Makefile
lib/devsql.c
lib/gessql.c
lib/gessql.h
lib/unisql.h

index 185a0f40612a59276e021a935c8640b601a35bfb..ea2db41fbcf9ad8278053be248302e8dcd29355f 100644 (file)
@@ -67,6 +67,7 @@ gestcp.o:                                     \
           gestcp.h gestcp.c
 
 gessql.o:                                      \
+          unisql.h                             \
           devsql.h                             \
           gessql.h gessql.c
 
index 678916be58e877530587d657d6bcf704a1e4ba41..ba8b497e9a0d75d551d1356a76f4025b9d343553 100644 (file)
@@ -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
 
 /*
 \f
@@ -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
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                      */
-/*      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
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                      */
 /*      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
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                      */
-/*      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;
 }
 /*
 \f
index b4f608de20772b96cd3bf745c504ef56d4fa8dd5..03f22c08b075ac0aaf908d77cc870f556e3603a3 100644 (file)
 /*     Module to manage complexe SQL request           */
 /*                                                     */
 /********************************************************/
+#include        <string.h>
 
 #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
 
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      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
 */
index 505c2515cec2b12dc478de26490eadd52b985cf1..4da35059eb38ac6b73fcbbfde191e7ce0a2f52ba 100644 (file)
@@ -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);
index dde32c438e741064c6a1d97cce5d70ef6b9bbdc4..e37fd7d79a1434507363d03b87c9176637f9e1b1 100644 (file)
@@ -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