]> SAFE projects GIT repository - jmp/mailleur/commitdiff
pos_lock is implemented (unipos.c)
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 19:20:10 +0000 (15:20 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 19:20:10 +0000 (15:20 -0400)
lib/devsql.c
lib/devsql.h
lib/gessql.c
lib/unimar.c
lib/unimar.h
lib/unipos.c
lib/unipos.h
sql/mailleur.sql

index d0a4189ad4c565ef0c45435250170d3e2abdf5d4..89c016abc0afe71a72d475ff3356437ff65e6bbc 100644 (file)
@@ -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
 }
 /*
 \f
@@ -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;
 }
-/*
-\f
-*/
-/********************************************************/
-/*                                                      */
-/*      Procedure to lock access to a table.            */
-/*                                                      */
-/********************************************************/
-PUBLIC _Bool sql_locktable(SQLPTR *sqlptr,char *tblname,_Bool action)
-
-{
-return action;
-}
index fd04765a18b2e2d357f78f86877f640629fdeb5b..3fd3635b72f3c7799bc454e8c63ea534c65423f5 100644 (file)
@@ -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);
index 89624f62005b2ec62b19ee5f0ce0bb63c67d14f5..465cb3f4120bf58e4452f654879cad9e39ae4854 100644 (file)
@@ -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
         }      
index 103a7b3d11ad89f08d8650e5c22de2d422f248cc..3db99cad67e3d8ad1ebb7c5fda053ea04255f301 100644 (file)
@@ -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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
 /*      procedure to drop/free all result information   */
 /*                                                     */
 /********************************************************/
index f46a7f0e0ff416cc9f7f483900b384047a9a5fa4..5c7a6953357f836f4bcf93c962982e374a16c9e7 100644 (file)
@@ -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);
 
index 640e592deafcfde8d3195876c03238ebeb143693..d01f77e35bcf442a05dcfe30ccded3cfa484ee6d 100644 (file)
@@ -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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
 /*      procedure to drop/free all result information   */
 /*                                                     */
 /********************************************************/
index e57a18ff0568a7196ab7f149c2860b6987b2abb1..a661645196f197139f279206d10d5117760f1b9c 100644 (file)
@@ -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);
 
index 41158f1f879fe5053b4d3423f6eefc78e2bb2018..daaacf5e4542a3304d0ec507157d78fa4ec8e907 100644 (file)
@@ -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)