]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Start SQL code implementation
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 5 May 2025 16:50:05 +0000 (12:50 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 5 May 2025 16:50:05 +0000 (12:50 -0400)
lib/Makefile
lib/devsql.c
lib/devsql.h
lib/lvleml.c
lib/unimar.h [new file with mode: 0644]
lib/unipos.h [new file with mode: 0644]

index eae422b3059e49e30b74da15c1523632379fb905..27b99a05661d108c8f551c87294372db8e7fbf9f 100644 (file)
@@ -66,6 +66,8 @@ devsoc.o:                                     \
           devsoc.h devsoc.c
 
 devsql.o:                                      \
+          unipos.h                             \
+          unimar.h                             \
           devsql.h devsql.c
 
 unidns.o:                                      \
index 17b46808d47682e522bebf56a4cbc9ab95cd0e9d..2beaff206b8295d2a1f135865572cb34e92dca54 100644 (file)
 /*     to handle SQL request                           */
 /*                                                     */
 /********************************************************/
+#include        <stdlib.h>
+#include        <string.h>
+
+#include       "subrou.h"
+#include       "unimar.h"
+#include       "unipos.h"
 #include       "devsql.h"
 
+
+typedef enum {
+              db_postgres,      //Postgres database
+              db_maria,         //Mariadb SQL
+              db_unknown
+             }SQLDBTYP;
+
+typedef struct  {
+        SQLDBTYP sqldb;         //database type
+        union   {
+          POSPTR *psql;         //postgreSQL reference/
+          MYSPTR *msql;         //MySQL Reference
+          }db;
+        }SQLTYP;
 /*
 \f
 */
 /********************************************************/
 /*                                                      */
+/*      Procedure to create an SQL pointer for proper   */
+/*      database type.                                  */
 /*                                                      */
 /********************************************************/
+static SQLTYP *gettyp(const char *dbtype)
+
+{
+const char *db[]={"POSTGRES","MYSQL",(const char *)0};
+
+SQLTYP *sql;
+
+sql=(SQLTYP *)0;
+for (int i=0;db[i]!=(const char *)0;i++) {
+  if (strcmp(db[i],dbtype)==0) {
+    sql=(SQLTYP *)calloc(1,sizeof(SQLTYP));
+    sql->sqldb=(SQLDBTYP)i;
+    break;      //data base type found
+    }
+  }
+return sql;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to establish a link with the          */
+/*      designated SQL server.                          */
+/*                                                      */
+/********************************************************/
+PUBLIC SQLPTR *sql_opensql()
+
+{
+#define OPEP    "devsql.c:sql_opensql,"
+
+static const char *dbenv[]={"DB_TYPE","DB_NAME","DB_HOST","DB_PORT"};
+
+SQLTYP *sql;
+const char *db[sizeof(dbenv)/sizeof(char *)];
+int phase;
+_Bool proceed;
+
+sql=(SQLTYP *)0;
+(void) memset(db,'\000',sizeof(db));
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //get database parameters
+      for (int i=0;i<(sizeof(dbenv)/sizeof(char *));i++) {
+        db[i]=getenv(dbenv[i]);
+        if (db[i]==(char *)0) {
+          (void) rou_alert(0,"%s Missing <%s> environment variable (config?)",
+                              OPEP,dbenv[i]);
+          phase=999;    //missing database information
+          }
+        }
+      break;
+    case 1      :       //getting dbtype
+      if ((sql=gettyp(dbenv[0]))==(SQLTYP *)0) {
+        (void) rou_alert(0,"%s %s type unknown (config?)",OPEP,dbenv[0]);
+        phase=999;    //missing database information
+        }
+      break;
+    case 2      :       //opening specific database
+      switch (sql->sqldb) {
+        case db_postgres        :
+          sql->db.psql=pos_opensql(dbenv[1],dbenv[2],dbenv[3]);
+          break;
+        default                 :
+          (void) rou_alert(0,"%s Unexpected type='%d' (bug?)",
+                              OPEP,(int)sql->sqldb);
+          break;
+        }
+      break;
+    case 3      :       //checking if the database is properly open
+      if (sql->db.psql==(POSPTR *)0) {
+        (void) free(sql);
+        sql=(SQLTYP *)0;
+        phase=999;      //Trouble trouble
+        }
+      break;
+    default     :       //SAFE guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return (SQLPTR *)sql;
+#undef  OPEP
+}
index 9d621a3fc6a0bf415e2fcf312a37d4cdf57b20b2..7507cf0bafb08881c78c8e71d1b646564a2099b3 100644 (file)
@@ -12,7 +12,7 @@
 typedef void SQLPTR;
 
 //to connect a remote SQL server
-extern SQLPTR *soql_opensql();
+extern SQLPTR *sql_opensql();
 
 //procedure to close an previously opened SQL channel
 extern SQLPTR *sql_closesql(SQLPTR *sqlptr);
index e3bd81e584c03e375854a67d14f808e0f43cf57f..0752e443adada13ef9aec53daa4e3c9fd46836d4 100644 (file)
@@ -30,6 +30,7 @@ static CONTYP *freecontact(CONTYP *contact)
 #define OPEP    "gestcp.c:freecontact"
 
 if (contact!=(CONTYP *)0) {
+  contact->sqlptr=sql_closesql(contact->sqlptr);
   contact->logptr=log_closelog(contact->logptr);
   contact->cursesid=rou_freestr(contact->cursesid);
   contact->mainsesid=rou_freestr(contact->mainsesid);
@@ -481,15 +482,26 @@ while (proceed==true) {
         phase=999;      //not going further
         }
       break;
-    case 1      :       //waiting from contact
-      contact=calloc(1,sizeof(CONTYP));
+    case 1      :       //connecting to database
+      contact=(CONTYP *)calloc(1,sizeof(CONTYP));
+      contact->sqlptr=sql_opensql();
+      if (contact->sqlptr==(SQLPTR *)0) {
+        (void) rou_alert(0,"%s Unable to contact database",OPEP);
+        (void) sleep(2);//delay to avoid avalanche
+        (void) free(contact);
+        contact=(CONTYP *)0;
+        phase=999;      //no contact possible.
+        }
+      break;
+    case 2      :       //waiting from contact
       if ((contact->socptr=soc_accept(socptr,pos))==(SOCPTR *)0) {
         (void) rou_alert(0,"%s Unable to open contact",OPEP);
+        contact->sqlptr=sql_closesql(contact->sqlptr);
         contact=freecontact(contact);
         phase=999;      //no contact 
         }
       break;
-    case 2      :       //Preparing contact
+    case 3      :       //Preparing contact
       contact->mainsesid=eml_getmainsesid();
       contact->cursesid=eml_getcursesid(contact->mainsesid,contact->numreset);
       contact->locname=soc_getaddrinfo(contact->socptr,true,true);
@@ -500,14 +512,14 @@ while (proceed==true) {
       (void) rou_alert(0,"Contact from peer <%s> to port <%s> started",
                           contact->peerip,contact->locserv);
       break;
-    case 3      :       //check contact validity
+    case 4      :       //check contact validity
       if ((contact->locname==(char *)0)||(contact->peerip==(char *)0)) {
         (void) rou_alert(0,"%s Unable to establish contact entities",OPEP);
         contact=freecontact(contact);
         phase=999;      //no identity
         }
       break;
-    case 4      :       //contact is good, then sending a signon
+    case 5      :       //contact is good, then sending a signon
       (void) log_fprintlog(contact->logptr,false,"SID: %s -> Contact open",
                                                       contact->mainsesid);
       break;
diff --git a/lib/unimar.h b/lib/unimar.h
new file mode 100644 (file)
index 0000000..bdf1b89
--- /dev/null
@@ -0,0 +1,14 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     base level subroutine declaration               */
+/*     to handle MariaDB SQL request.                  */
+/*                                                     */
+/********************************************************/
+#ifndef        UNIMAR
+#define UNIMAR
+
+//reference to a SQL pointer reference
+typedef void MYSPTR;
+
+#endif
diff --git a/lib/unipos.h b/lib/unipos.h
new file mode 100644 (file)
index 0000000..f2436dd
--- /dev/null
@@ -0,0 +1,20 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     base level subroutine declaration               */
+/*     to handle Postscript SQL request.               */
+/*                                                     */
+/********************************************************/
+#ifndef        UNIPOS
+#define UNIPOS
+
+//reference to a SQL pointer reference
+typedef void POSPTR;
+
+//Opening a postscript database
+extern POSPTR *pos_opensql(const char *host,const char *sqlport,const char *dbname);
+
+//closing a postscript database
+extern POSPTR *pos_closesql(POSPTR *posptr);
+
+#endif