]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Starting to implement database lock
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 15:04:07 +0000 (11:04 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 15:04:07 +0000 (11:04 -0400)
lib/devsql.c
lib/devsql.h
lib/gessql.c
sql/mailleur.sql

index c9961902412384be648edeaaced7b07465f7720c..d0a4189ad4c565ef0c45435250170d3e2abdf5d4 100644 (file)
@@ -245,6 +245,34 @@ return (SQLPTR *)sql;
 */
 /********************************************************/
 /*                                                      */
+/*      Procedure to get an exclusive access to a       */
+/*      specific database table.                        */
+/*                                                      */
+/********************************************************/
+PUBLIC int sql_lock(SQLPTR *sqlptr,char *tablename)
+
+{
+return 1;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to release an exclusive access to a   */
+/*      specific database table.                        */
+/*                                                      */
+/********************************************************/
+PUBLIC int sql_unlock(SQLPTR *sqlptr,_Bool commit)
+
+{
+return 1;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
 /*      Procedure to make sure a string to be use to    */
 /*      access database is clean.                       */
 /*      (no character to be misunderstood by database)  */
index 62d3a042e6c12477426bd729c8d6f73f8278bb5f..fd04765a18b2e2d357f78f86877f640629fdeb5b 100644 (file)
@@ -22,6 +22,12 @@ extern SQLPTR *sql_opensql();
 //procedure to close an previously opened SQL channel
 extern SQLPTR *sql_closesql(SQLPTR *sqlptr);
 
+//procedure to LOCK access to a database table
+extern int sql_lock(SQLPTR *sqlptr,char *tablename);
+
+//procedure to UNLOCK access to a database table
+extern int sql_unlock(SQLPTR *sqlptr,_Bool commit);
+
 //procedure to make sure a string is acceptable
 //as key search by database
 extern char *sql_gooddata(SQLPTR *sqlptr,char *key);
index b0f8448add74d0edb08537a4e4c60600b51f44dd..fb376fab0682b72e08853cd2ed265d97176cc53c 100644 (file)
@@ -468,7 +468,8 @@ PUBLIC _Bool sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta)
 {
 #define OPEP    "gessql.c:sql_newconnect,"
 
-static const char *updfmt="UPDATE %s SET links=links+%d,lastupdate=now() WHERE remoteip='%s'";
+static const char *upd="UPDATE %s SET links=links+%d,lastupdate=now() WHERE remoteip='%s'";
+static const char *ins="INSERT INTO %s (remoteip) values('%s')";
 
 _Bool done;
 int phase;
@@ -480,8 +481,34 @@ proceed=true;
 while (proceed==true) {
   switch (phase) {
     case 0      :       //updating remotes record with links
-      if (sql_request(sqlptr,updfmt,RMTTBL,delta,rmtip)==1)
-        phase=999;
+      if (sql_request(sqlptr,upd,RMTTBL,delta,rmtip)==1) {
+        phase=999;      //done no probleme
+        }
+      break; 
+    case 1      :       //record does not exist
+      done=false;
+      if (sql_lock(sqlptr,RMTTBL)<0) {
+        (void) rou_alert(0,"%s Unable to dbd_lockhard %s table",OPEP,RMTTBL);
+        phase=999;      //Unable to lock table
+        }      
+      break; 
+    case 2      :       //was record created in the meantime?
+      done=true;
+      if (sql_request(sqlptr,upd,RMTTBL,delta,rmtip)==1) {
+        phase++;        //yes, no need to create it again
+        }
+      break; 
+    case 3      :       //Let create a record
+      if (sql_request(sqlptr,ins,RMTTBL,rmtip)!=1) {
+        (void) rou_alert(0,"%s Unable to create entry "
+                           "for remote ip <%s> in table <%s> (DB trouble?)",
+                            OPEP,rmtip,RMTTBL);
+        done=false;
+        phase=999;      //Unable to lock table
+        }
+      break; 
+    case 4      :       //unlock database
+      (void) sql_unlock(sqlptr,true);
       break; 
     default     :       //SAFE Guard
       proceed=false;
index d1c7df6af74c3b8b817e74fd141fc61c31db1bec..41158f1f879fe5053b4d3423f6eefc78e2bb2018 100644 (file)
@@ -103,7 +103,7 @@ CREATE TABLE remotes        (
                        DFLT NOW(),
        remoteip        TEXTUNIQUE,     //remote IP number
        links           INTEGER
-                       DFLT 0,         //how many time the remote connected
+                       DFLT 1,         //how many time the remote connected
        credit          INTEGER //Remote IP current credit (-100..+100)
                        DFLT -100,
        status          TEXT            //'toscan','inscan','valide'