From 3b7fb01cd75cf7f5dff52d4a07ccf859cb356806 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Fri, 11 Jul 2025 15:20:10 -0400 Subject: [PATCH] pos_lock is implemented (unipos.c) --- lib/devsql.c | 115 +++++++++++++++++++++++++++-------------------- lib/devsql.h | 2 +- lib/gessql.c | 2 +- lib/unimar.c | 26 +++++++++++ lib/unimar.h | 3 ++ lib/unipos.c | 61 +++++++++++++++++++++++++ lib/unipos.h | 3 ++ sql/mailleur.sql | 2 +- 8 files changed, 163 insertions(+), 51 deletions(-) diff --git a/lib/devsql.c b/lib/devsql.c index d0a4189..89c016a 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -249,10 +249,34 @@ return (SQLPTR *)sql; /* specific database table. */ /* */ /********************************************************/ -PUBLIC int sql_lock(SQLPTR *sqlptr,char *tablename) +PUBLIC _Bool sql_lock(SQLPTR *sqlptr,char *tablename) { -return 1; +#define OPEP "devsql.c:sql_lock," + +_Bool locked; + +locked=false; +if (sqlptr!=(SQLPTR *)0) { + SQLTYP *sql; + + sql=(SQLTYP *)sqlptr; + switch(sql->sqldb) { + case db_postgres : + locked=pos_lock(sql->db.psql,tablename); + break; + case db_maria : + locked=mar_lock(sql->db.psql,tablename); + break; + default : + (void) rou_alert(0,"%s Unexpected db type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + break; + } + } +return locked; + +#undef OPEP } /* @@ -428,29 +452,32 @@ PUBLIC SQLRES *sql_gettupple(SQLPTR *sqlptr,const char *fmt,...) #define OPEP "devsql.c:sql_gettupple," SQLRES *rs; -SQLTYP *sql; -va_list args; -char *cmd; rs=(SQLRES *)0; -sql=(SQLTYP *)sqlptr; -va_start(args,fmt); -if ((rou_vasprintf(&cmd,fmt,args))>0) { - switch(sql->sqldb) { - case db_postgres : - rs=(SQLRES *)pos_gettupple(sql->db.psql,cmd); - break; - case db_maria : - rs=(SQLRES *)mar_gettupple(sql->db.msql,cmd); - break; - default : - (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)", - OPEP,(int)sql->sqldb); - break; +if (sqlptr!=(SQLPTR *)0) { + SQLTYP *sql; + va_list args; + char *cmd; + + sql=(SQLTYP *)sqlptr; + va_start(args,fmt); + if ((rou_vasprintf(&cmd,fmt,args))>0) { + switch(sql->sqldb) { + case db_postgres : + rs=(SQLRES *)pos_gettupple(sql->db.psql,cmd); + break; + case db_maria : + rs=(SQLRES *)mar_gettupple(sql->db.msql,cmd); + break; + default : + (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + break; + } + (void) free(cmd); } - (void) free(cmd); + va_end(args); } -va_end(args); return rs; #undef OPEP @@ -471,20 +498,24 @@ PUBLIC int sql_getnbrtupple(SQLPTR *sqlptr,SQLRES *rs) int numrow; -numrow=0; -SQLTYP *sql; -sql=(SQLTYP *)sqlptr; -switch(sql->sqldb) { - case db_postgres : - numrow=pos_nbrtupple((POSRES *)rs); - break; - case db_maria : - numrow=mar_nbrtupple((MARRES *)rs); - break; - default : - (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)", - OPEP,(int)sql->sqldb); - break; +numrow=-1; +if (sqlptr!=(SQLPTR *)0) { + SQLTYP *sql; + + sql=(SQLTYP *)sqlptr; + switch(sql->sqldb) { + case db_postgres : + numrow=pos_nbrtupple((POSRES *)rs); + break; + case db_maria : + numrow=mar_nbrtupple((MARRES *)rs); + break; + default : + (void) rou_alert(0,"%s Unexpected db type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + numrow=0; + break; + } } return numrow; @@ -537,18 +568,6 @@ return rs; PUBLIC _Bool sql_mngdata(SQLPTR *sqlptr,FLDTYP *field,SQLENUM action) { +(void) rou_alert(0,"JMPDBG sql_mngdata need to be implemented!"); return false; } -/* - -*/ -/********************************************************/ -/* */ -/* Procedure to lock access to a table. */ -/* */ -/********************************************************/ -PUBLIC _Bool sql_locktable(SQLPTR *sqlptr,char *tblname,_Bool action) - -{ -return action; -} diff --git a/lib/devsql.h b/lib/devsql.h index fd04765..3fd3635 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -23,7 +23,7 @@ extern SQLPTR *sql_opensql(); extern SQLPTR *sql_closesql(SQLPTR *sqlptr); //procedure to LOCK access to a database table -extern int sql_lock(SQLPTR *sqlptr,char *tablename); +extern _Bool sql_lock(SQLPTR *sqlptr,char *tablename); //procedure to UNLOCK access to a database table extern int sql_unlock(SQLPTR *sqlptr,_Bool commit); diff --git a/lib/gessql.c b/lib/gessql.c index 89624f6..465cb3f 100644 --- a/lib/gessql.c +++ b/lib/gessql.c @@ -601,7 +601,7 @@ while (proceed==true) { break; case 1 : //record does not exist done=false; - if (sql_lock(sqlptr,RMTTBL)<0) { + if (sql_lock(sqlptr,RMTTBL)==false) { (void) rou_alert(0,"%s Unable to dbd_lockhard %s table",OPEP,RMTTBL); phase=999; //Unable to lock table } diff --git a/lib/unimar.c b/lib/unimar.c index 103a7b3..3db99ca 100644 --- a/lib/unimar.c +++ b/lib/unimar.c @@ -153,6 +153,32 @@ return cleanstr; */ /********************************************************/ /* */ +/* procedure to lock access to a specific table */ +/* within database. */ +/* */ +/********************************************************/ +PUBLIC _Bool mar_lock(MARPTR *maerptr,char *tablename) + +{ +#define OPEP "unimar.c:mar_lock," + +_Bool locked; + +locked=false; +#ifdef DB_MYSQL + { + (void) rou_alert(0,"%s not implemented yet",OPEP); + } +#endif +return locked; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* procedure to drop/free all result information */ /* */ /********************************************************/ diff --git a/lib/unimar.h b/lib/unimar.h index f46a7f0..5c7a695 100644 --- a/lib/unimar.h +++ b/lib/unimar.h @@ -23,6 +23,9 @@ extern MARPTR *mar_closesql(MARPTR *marptr); //Procedure to detect and 'clean' any single quote within a string extern char *mar_cleanquote(char *sequence); +//locking database one table access +extern _Bool mar_lock(MARPTR *marptr,char *tablename); + //procedure to drop/free all result information extern MARRES *mar_dropresult(MARRES *rs); diff --git a/lib/unipos.c b/lib/unipos.c index 640e592..d01f77e 100644 --- a/lib/unipos.c +++ b/lib/unipos.c @@ -201,6 +201,67 @@ return cleanstr; */ /********************************************************/ /* */ +/* procedure to lock access to a specific table */ +/* within database. */ +/* */ +/********************************************************/ +PUBLIC _Bool pos_lock(POSPTR *posptr,char *tablename) + +{ +#define OPEP "unipos.c:pos_lock," + +_Bool locked; + +locked=false; +#ifdef DB_POSTGRESQL + { + static const char *cmd="LOCK TABLE %s IN SHARE ROW EXCLUSIVE MODE"; + + PGconn *pf; + char fullcmd[300]; + int phase; + _Bool proceed; + + pf=(PGconn *)posptr; + (void) snprintf(fullcmd,sizeof(fullcmd),cmd,tablename); + phase=0; + proceed=true; + while (proceed==true) { + switch (phase) { + case 0 : //Starting lock + if (pos_request(posptr,"BEGIN")<0) { + (void) rou_alert(0,"%s Unable to BEGIN to lock table <%s>", + OPEP,tablename); + phase=999; //no need to go further + } + break; + case 1 : //lock table itself + if (pos_request(posptr,fullcmd)<0) { + (void) rou_alert(0,"%s Unable to lock table <%s>", + OPEP,tablename); + phase=999; //no need to go further + } + break; + case 2 : //lock done + locked=true; + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } + } +#endif +return locked; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* procedure to drop/free all result information */ /* */ /********************************************************/ diff --git a/lib/unipos.h b/lib/unipos.h index e57a18f..a661645 100644 --- a/lib/unipos.h +++ b/lib/unipos.h @@ -23,6 +23,9 @@ extern POSPTR *pos_closesql(POSPTR *posptr); //Procedure to detect and 'clean' any single quote within a string extern char *pos_cleanquote(char *sequence); +//locking database one table access +extern _Bool pos_lock(POSPTR *marptr,char *tablename); + //procedure to drop/free all result information extern POSRES *pos_dropresult(POSRES *rs); diff --git a/sql/mailleur.sql b/sql/mailleur.sql index 41158f1..daaacf5 100644 --- a/sql/mailleur.sql +++ b/sql/mailleur.sql @@ -97,11 +97,11 @@ GRANT SELECT ON sessions TO mailapache; //defining table about remote server CREATE TABLE remotes ( + remoteip TEXTUNIQUE, //remote IP number lastscan DBTIMESTAMP //record laste update DFLT NOW(), lastupdate DBTIMESTAMP //record creation DFLT NOW(), - remoteip TEXTUNIQUE, //remote IP number links INTEGER DFLT 1, //how many time the remote connected credit INTEGER //Remote IP current credit (-100..+100) -- 2.47.3