From: Jean-Marc Pigeon (Delson) Date: Mon, 14 Jul 2025 20:15:48 +0000 (-0400) Subject: Able to chose data-base according binary used X-Git-Tag: tag-0.14~96 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=3ffbc3f53c56b05b7e34abc856cd37c31f9e8df5;p=jmp%2Fmailleur Able to chose data-base according binary used --- diff --git a/conf/mailleur.conf.dvl b/conf/mailleur.conf.dvl index bdd0ffb..f834b3e 100644 --- a/conf/mailleur.conf.dvl +++ b/conf/mailleur.conf.dvl @@ -41,11 +41,11 @@ CA_VERIFY_CLT=0 #to check PEER/server remote certificate #------------------------------------------------ #Configured for Postgresql database #DB_TYPE can be either POSTGRESQL,MYSQL, default POSTGRESQL -DB_NAME=mailleur -DB_HOST=localhost -DB_LANG="UTF-8" -DB_TYPE=POSTGRESQL -DB_PORT=5432 +#DB_NAME=mailleur +#DB_HOST=localhost +#DB_LANG="UTF-8" +#DB_TYPE=POSTGRESQL +#DB_PORT=5432 #DB_TYPE=MYSQL #DB_PORT=3306 diff --git a/lib/devsql.c b/lib/devsql.c index bf68dc4..9fa6c81 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -217,7 +217,7 @@ PUBLIC SQLPTR *sql_opensql() static const char *dbenv[]={"DB_TYPE","DB_HOST","DB_PORT","DB_NAME"}; SQLTYP *sql; -const char *db[sizeof(dbenv)/sizeof(char *)]; +const char *db[SQLENV]; int phase; _Bool proceed; @@ -228,23 +228,38 @@ proceed=true; while (proceed==true) { //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); 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 + case 0 : //get the preset sql env variable + for (int i=0;i<2;i++) { + switch (i) { + case 0 : //Postgresql + if (posenv[0]!=(char *)0) + (void) memcpy(db,posenv,sizeof(db)); + break; + case 1 : //MySQL/Mariadb + if (marenv[0]!=(char *)0) + (void) memcpy(db,marenv,sizeof(db)); + break; + default : //Trouble trouble + break; } } break; - case 1 : //getting dbtype + case 1 : //get database parameters + for (int i=0;i<(sizeof(dbenv)/sizeof(char *));i++) { + char *ptr; + + ptr=getenv(dbenv[i]); + if (ptr!=(char *)0) + db[i]=ptr; //Variable overwrite + } + break; + case 2 : //getting dbtype if ((sql=gettyp(db[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 + case 3 : //opening specific database switch (sql->sqldb) { case db_postgres : sql->db.psql=pos_opensql(db[1],db[2],db[3]); @@ -258,7 +273,7 @@ while (proceed==true) { break; } break; - case 3 : //checking if the database is properly open + case 4 : //checking if the database is properly open _Bool isok; isok=false; diff --git a/lib/subrou.h b/lib/subrou.h index 298d7a8..d66f26d 100644 --- a/lib/subrou.h +++ b/lib/subrou.h @@ -22,6 +22,9 @@ #define USE_POSTGRESQL 1 #define USE_MYSQL 2 +#define SQLENV 5 //number of ENV parameters for + //SQL definition + typedef void (*genfree_t)(void *); typedef struct timespec TIMESPEC; diff --git a/lib/unimar.c b/lib/unimar.c index 2c817c2..f296c21 100644 --- a/lib/unimar.c +++ b/lib/unimar.c @@ -21,6 +21,24 @@ #define DB_MYSQL #endif +#ifdef DB_MYSQL +char *marenv[SQLENV]= { + "MYSQL", + "localhost", + "3306", + "mailleur", + (char *)0 + }; +#else +char *marenv[SQLENV]= { + (char *)0, + (char *)0, + (char *)0, + (char *)0, + (char *)0 + }; +#endif + /* */ @@ -92,7 +110,7 @@ PUBLIC char *mar_caldate(char *expression,int second) char *seq; seq=(char *)0; -(void) rou_asprintf(&seq,"(addtime(%s,%d)",expression,second); +(void) rou_asprintf(&seq,"(addtime(%s,%d))",expression,second); return seq; } /* diff --git a/lib/unimar.h b/lib/unimar.h index ff70733..613ace9 100644 --- a/lib/unimar.h +++ b/lib/unimar.h @@ -14,6 +14,9 @@ typedef void MARPTR; //reference to a MariaDB result typedef void MARRES; +//Database env variable +extern char *marenv[SQLENV]; + //Procedure to detect and 'clean' any single quote within a string extern char *mar_cleanquote(char *sequence); diff --git a/lib/unipos.c b/lib/unipos.c index bb73b21..b972bc2 100644 --- a/lib/unipos.c +++ b/lib/unipos.c @@ -14,10 +14,31 @@ #include "subrou.h" #include "unipos.h" + + //checking if MYSQL database need to compiled #if DATABASE==USE_POSTGRESQL - #define DB_POSTGRESQL + #define DB_POSTGRESQL +#endif + +#ifdef DB_POSTGRESQL +char *posenv[SQLENV]= { + "POSTGRESQL", + "localhost", + "5432", + "mailleur", + (char *)0 + }; +#else +char *posenv[SQLENV]= { + (char *)0, + (char *)0, + (char *)0, + (char *)0, + (char *)0 + }; #endif + /* */ diff --git a/lib/unipos.h b/lib/unipos.h index 89dc2a9..5a5ffcd 100644 --- a/lib/unipos.h +++ b/lib/unipos.h @@ -14,6 +14,9 @@ typedef void POSPTR; //reference to a POSTGRESQL result typedef void POSRES; +//Database env variable +extern char *posenv[SQLENV]; + //Procedure to detect and 'clean' any single quote within a string extern char *pos_cleanquote(char *sequence); diff --git a/lib/unisql.h b/lib/unisql.h index 379bdd7..6254cf6 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -9,6 +9,9 @@ #include +#define SQLENV 5 //number of ENV parameters for + //SQL definition + typedef enum { sql_select, //select a record sql_insert, //create a record