From dd66f2d1110c619798d87f1c3ecb9057c1cc84d1 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Mon, 5 May 2025 12:50:05 -0400 Subject: [PATCH] Start SQL code implementation --- lib/Makefile | 2 + lib/devsql.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/devsql.h | 2 +- lib/lvleml.c | 22 ++++++++--- lib/unimar.h | 14 +++++++ lib/unipos.h | 20 ++++++++++ 6 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 lib/unimar.h create mode 100644 lib/unipos.h diff --git a/lib/Makefile b/lib/Makefile index eae422b..27b99a0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -66,6 +66,8 @@ devsoc.o: \ devsoc.h devsoc.c devsql.o: \ + unipos.h \ + unimar.h \ devsql.h devsql.c unidns.o: \ diff --git a/lib/devsql.c b/lib/devsql.c index 17b4680..2beaff2 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -5,12 +5,121 @@ /* to handle SQL request */ /* */ /********************************************************/ +#include +#include + +#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; /* */ /********************************************************/ /* */ +/* 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; +} +/* + +*/ +/********************************************************/ +/* */ +/* 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 +} diff --git a/lib/devsql.h b/lib/devsql.h index 9d621a3..7507cf0 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -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); diff --git a/lib/lvleml.c b/lib/lvleml.c index e3bd81e..0752e44 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -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 index 0000000..bdf1b89 --- /dev/null +++ b/lib/unimar.h @@ -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 index 0000000..f2436dd --- /dev/null +++ b/lib/unipos.h @@ -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 -- 2.47.3