]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Starting unipos implementation
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 5 May 2025 17:16:54 +0000 (13:16 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 5 May 2025 17:16:54 +0000 (13:16 -0400)
app/Makefile
lib/Makefile
lib/devsql.c
lib/unipos.c [new file with mode: 0644]

index 4ca6c77f400fd4e96c79266da23d81c92a3f10e7..d1d2b31781be5990715c36834ac4d525a9a0aa96 100644 (file)
@@ -38,6 +38,7 @@ LD    =  gcc
 CFLAGS =  -I ../lib -Wall $(OPTIME)
 LIBMAIL        =  ../lib/libmail.a
 LIBS   =       $(LIBMAIL)                      \
+               -lpq                            \
                -lcrypto                        \
                -lssl                           \
 
index 27b99a05661d108c8f551c87294372db8e7fbf9f..6de9776320904c8d343cacafb16cc2095f32fffc 100644 (file)
@@ -20,7 +20,7 @@ OBJS=                                         \
          lvleml.o                              \
          gesspf.o gestcp.o                     \
          devlog.o devsoc.o devsql.o            \
-         unidns.o unieml.o                     \
+         unidns.o unieml.o unipos.o            \
          unipar.o uniprc.o unisig.o unitls.o   \
          subafn.o subrou.o 
 
@@ -82,6 +82,10 @@ unipar.o:                                    \
           subrou.h                             \
           unipar.h unipar.c
 
+unios.o:                                       \
+          subrou.h                             \
+          unios.h unios.c
+
 uniprc.o:                                      \
           subrou.h                             \
           uniprc.h uniprc.c
index 2beaff206b8295d2a1f135865572cb34e92dca54..3a4fe79aa4597db7ed75ebc9a8793baaacafe4bc 100644 (file)
@@ -123,3 +123,56 @@ while (proceed==true) {
 return (SQLPTR *)sql;
 #undef  OPEP
 }
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to closed previously establish link   */
+/*      with the designated SQL server.                 */
+/*                                                      */
+/********************************************************/
+PUBLIC SQLPTR *sql_closesql(SQLPTR *sqlptr)
+
+{
+#define OPEP    "devsql.c:sql_closesql,"
+
+SQLTYP *sql;
+int phase;
+_Bool proceed;
+
+sql=(SQLPTR *)sqlptr;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //check if database available
+      if (sql==(SQLTYP *)0) {
+        (void) rou_alert(0,"%s %s Datbase pointer null (bug?)",OPEP);
+        phase=999;
+        }
+      break;
+    case 1      :       //opening specific database
+      switch (sql->sqldb) {
+        case db_postgres        :
+          sql->db.psql=pos_closesql(sql->db.psql);
+          break;
+        default                 :
+          (void) rou_alert(0,"%s Unexpected type='%d' (bug?)",
+                              OPEP,(int)sql->sqldb);
+          break;
+        }
+      break;
+    case 2      :       //freeing memory used by SQL
+      (void) free(sql);
+      sql=(SQLTYP *)0;
+      break;
+    default     :       //SAFE guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return (SQLPTR *)sql;
+#undef  OPEP
+}
diff --git a/lib/unipos.c b/lib/unipos.c
new file mode 100644 (file)
index 0000000..a6af57b
--- /dev/null
@@ -0,0 +1,70 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     Low level subroutine implementation             */
+/*     to handle POSTGRES SQL request                  */
+/*                                                     */
+/********************************************************/
+#include        <libpq-fe.h>
+#include        "subrou.h"
+#include        "unipos.h"
+
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to establish a link with the          */
+/*      postgresq SQL server.                           */
+/*                                                      */
+/********************************************************/
+PUBLIC POSPTR *pos_opensql(const char *host,const char *sqlport,const char *dbname)
+
+{
+#define OPEP    "unipos.c:pos_opensql,"
+
+PGconn *pf;
+char *z;        //parameter null
+
+z=(char *)0;
+pf=PQsetdbLogin(host,sqlport,z,z,dbname,z,z);
+// Check if the connection is successful
+if (PQstatus(pf)!=CONNECTION_OK) {
+  (void) rou_alert(0,"%s Connection to database '%s' failed, cause '%s'",
+                      OPEP,dbname,PQerrorMessage(pf));
+  (void) PQfinish(pf);
+  pf=(PGconn *)0;
+  }
+return (POSPTR *)pf;
+#undef  OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to close the link with the designated */
+/*      postgresql SQL server.                          */
+/*                                                      */
+/********************************************************/
+PUBLIC POSPTR *pos_closesql(POSPTR *posptr)
+
+{
+#define OPEP    "unipos.c:pos_closesql,"
+
+PGconn *pf;
+
+pf=(PGconn *)posptr;
+if (pf==(PGconn *)0) {
+  (void) rou_alert(0,"%s Database link already closedi (Bug?)",OPEP);
+  (void) PQfinish(pf);
+  }
+else {
+  (void) PQfinish((PGconn *)pf);
+  pf=(PGconn *)0;
+  }
+return (POSPTR *)pf;
+#undef  OPEP
+}
+
+