From b020fb3b7512d655fa4103238fb620bbc4d9ef30 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Mon, 5 May 2025 13:16:54 -0400 Subject: [PATCH] Starting unipos implementation --- app/Makefile | 1 + lib/Makefile | 6 ++++- lib/devsql.c | 53 +++++++++++++++++++++++++++++++++++++++ lib/unipos.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 lib/unipos.c diff --git a/app/Makefile b/app/Makefile index 4ca6c77..d1d2b31 100644 --- a/app/Makefile +++ b/app/Makefile @@ -38,6 +38,7 @@ LD = gcc CFLAGS = -I ../lib -Wall $(OPTIME) LIBMAIL = ../lib/libmail.a LIBS = $(LIBMAIL) \ + -lpq \ -lcrypto \ -lssl \ diff --git a/lib/Makefile b/lib/Makefile index 27b99a0..6de9776 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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 diff --git a/lib/devsql.c b/lib/devsql.c index 2beaff2..3a4fe79 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -123,3 +123,56 @@ while (proceed==true) { return (SQLPTR *)sql; #undef OPEP } +/* + +*/ +/********************************************************/ +/* */ +/* 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 index 0000000..a6af57b --- /dev/null +++ b/lib/unipos.c @@ -0,0 +1,70 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Low level subroutine implementation */ +/* to handle POSTGRES SQL request */ +/* */ +/********************************************************/ +#include +#include "subrou.h" +#include "unipos.h" + +/* + +*/ +/********************************************************/ +/* */ +/* 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 +} +/* + +*/ +/********************************************************/ +/* */ +/* 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 +} + + -- 2.47.3