]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Improving SQL handling
authorJean-Marc Pigeon <jmp@safe.c>
Thu, 11 Dec 2025 20:46:22 +0000 (15:46 -0500)
committerJean-Marc Pigeon <jmp@safe.c>
Thu, 11 Dec 2025 20:46:42 +0000 (15:46 -0500)
www/devsql.php
www/gessql.php
www/lvlmai.php

index 07e1f054e684708c0b4d3888aa94654ffe27c0e7..e28e60d774cd2fe4cdd8c9aff160666ee50612c7 100644 (file)
@@ -10,6 +10,7 @@ include_once "unienv.php";
 
 class devsql     {
   public $connection = null;
+  private string $OPEP="devsql.php:class_devsql";
   
   // this function is called everytime this class is instantiated
   // connecting to database
@@ -37,7 +38,7 @@ class devsql     {
   // Select a row/s in a Database Table
   public function Select($statement="",$parameters=[]) {
     try {
-      rou_alert(0,"JMPDBG selec=<$statement>");
+      rou_alert(0,"$this->OPEP JMPDBG select=<$statement>");
       return $this->executeStatement($statement,$parameters);
     } catch(Exception $e) {
       throw new Exception($e->getMessage());
@@ -111,7 +112,7 @@ function sql_close(devsql $dbinfo)
 $OPEP="devsql.php, sql_close";
 
 rou_alert(0,"$OPEP JMPDBG disconnect database");
-$dbinfo->connection=null;
+$dbinfo->connection=NULL;
 }
 
 //==============================================================
index 7782ce134325e55364fffc9571981e85878b892c..272201e04b3ce59ff68d3d0fb8f30324090ec835 100644 (file)
@@ -17,96 +17,95 @@ class probe       {
     private ?string $order = null;      // ORDER BY clause
     private ?int $limit = null;         // LIMIT for pagination
     private int $offset = 0;            // OFFSET for pagination, internal only
+    private string $OPEP="gessql.php:class_probe";
 
     /**
      * Constructor
-     */
-    public function __construct(string $table,int $limit,int $offset)
-    {
-        $this->db = sql_connect();
-        $this->table = $table;
-        $this->limit = $limit;
-        $this->offset = $offset;
+     **/
+    public function __construct(string $table,int $limit,int $offset) {
+      $this->db = sql_connect();
+      $this->table = $table;
+      $this->limit = $limit;
+      $this->offset = $offset;
     }
 
     /**
-     * Add a CLOSE condition
-     */
-    public function close(): void
-    {
-    $this->db->connection=NULL;
-    }
+     * Add the release function
+     **/
+    public function release(): void {
+      if ($this->db !== NULL) {
+        sql_close($this->db);
+        }
+      }
 
     /**
      * Add a WHERE condition
-     */
-    public function where(string $datawhere): self
-    {
-        $this->where = $datawhere;
-        return $this;
-    }
+     **/
+    public function where(string $datawhere): self {
+
+      $this->where = $datawhere;
+      return $this;
+     }
 
     /**
      * Set ORDER BY clause
-     */
-    public function order(string $order): self
-    {
-        $this->order = $order;
-        return $this;
-    }
+     **/
+    public function order(string $order): self {
+
+      $this->order = $order;
+      return $this;
+      }
 
     /**
      * Set LIMIT for pagination
-     */
-    public function limit(int $n): self
-    {
-        $this->limit = $n;
-        return $this;
-    }
+     **/
+    public function limit(int $n): self {
+
+      $this->limit = $n;
+      return $this;
+      }
 
     /**
      * Execute the query and return results
      */
-    public function get(): array
-    {
-        rou_alert(0,"JMPDBG starting GET");
-        $sql = "SELECT * FROM {$this->table}";
+    public function get(): array {
 
-        if ($this->where) {
-            $sql .= " WHERE {$this->where}";
-        }
+     $sql = "SELECT * FROM {$this->table}";
+     if ($this->where) {
+       $sql .= " WHERE {$this->where}";
+       }
 
-        if ($this->order) {
-            $sql .= " ORDER BY {$this->order}";
-        }
+     if ($this->order) {
+       $sql .= " ORDER BY {$this->order}";
+       }
 
-        if ($this->limit !== null) {
-            $sql .= " LIMIT {$this->limit}";
-        }
+    if ($this->limit !== null) {
+      $sql .= " LIMIT {$this->limit}";
+      }
 
-        if ($this->offset > 0) {
-            $sql .= " OFFSET {$this->offset}";
-        }
+    if ($this->offset > 0) {
+      $sql .= " OFFSET {$this->offset}";
+      }
 
-        rou_alert(0,"JMPDBG rqst sql=<$sql>");
-        $stmt = $this->db->Select($sql);
-        return $stmt->fetchAll();
+    rou_alert(0,"$this->OPEP, JMPDBG rqst sql=<$sql>");
+    $stmt = $this->db->Select($sql);
+    return $stmt->fetchAll();
     }
 
     /**
      * Count total number of items matching the current WHERE conditions
      */
-    public function getnumrec(): int
-    {
-        $sql = "SELECT COUNT(*) AS cnt FROM {$this->table}";
+    public function getnumrec(): int {
 
-        if ($this->where) {
-            $sql .= " WHERE {$this->where}";
-        }
+    $sql = "SELECT COUNT(*) AS cnt FROM {$this->table}";
+
+    if ($this->where) {
+      $sql .= " WHERE {$this->where}";
+      }
 
-        $stmt = $this->db->Select($sql);
-        $row = $stmt->fetch();
-        return (int) $row['cnt'];
+    $stmt = $this->db->Select($sql);
+    $row = $stmt->fetch();
+    return (int) $row['cnt'];
     }
 
     /** ----------------------------- **/
@@ -117,56 +116,56 @@ class probe       {
      * Move offset to next page
      * Does NOT fetch data
      */
-    public function goNext(): int
-    {
-      if ($this->limit !== null) {
-          $last=max($this->getnumrec(),$this->limit);
-          $this->offset = min($last-$this->limit,$this->offset+$this->limit);
-          }
-        return $this->offset;
+    public function goNext(): int {
+
+    if ($this->limit !== null) {
+      $last=max($this->getnumrec(),$this->limit);
+      $this->offset = min($last-$this->limit,$this->offset+$this->limit);
+      }
+    return $this->offset;
     }
 
     /**
      * Move offset to previous page
      * Does NOT fetch data
      */
-    public function goPrevious(): int
-    {
-        if ($this->limit !== null) {
-            $this->offset = max(0, $this->offset - $this->limit);
-        }
-        return $this->offset;
+    public function goPrevious(): int {
+
+      if ($this->limit !== null) {
+        $this->offset = max(0, $this->offset - $this->limit);
+      }
+    return $this->offset;
     }
 
     /**
      * Move offset to first page
      * Does NOT fetch data
      */
-    public function goFirst(): int
-    {
-        $this->offset = 0;
-        return $this->offset;
+    public function goFirst(): int {
+
+    $this->offset = 0;
+    return $this->offset;
     }
 
     /**
      * Move offset to last page
      * Does NOT fetch data
      */
-    public function goLast(): int
-    {
-        if ($this->limit !== null) {
-            $total = $this->getnumrec();
-            $this->offset = max(0, $total - $this->limit);
-        }
-        return $this->offset;
+    public function goLast(): int {
+
+    if ($this->limit !== null) {
+      $total = $this->getnumrec();
+      $this->offset = max(0, $total - $this->limit);
+      }
+    return $this->offset;
     }
 
     /**
      * Get current offset value
      */
-    public function getOffset(): int
-    {
-        return $this->offset;
+    public function getOffset(): int {
+
+    return $this->offset;
     }
 }
 
@@ -290,24 +289,24 @@ while ($proceed==true) {
         $logname=NULL;
         $phase=999;
         }
-      else {
-        $userlang=lng::en;
-        $language=$stmt->fetch(PDO::FETCH_ASSOC)['lang'];
-        rou_alert(0,"$OPEP, JMPDBG langage=$language");
-        if ($language!=NULL) {
-          switch ($language) {
-            case "FRA"  :
-              $userlang=lng::fr;
-              break;
-            case "ENG"  :
-            default     :       //NO BREAK
-              $userlang=lng::en;
-              break;
-            }
+      break;
+    case 5      :       //getting user working language
+      $userlang=lng::en;
+      $language=$stmt->fetch(PDO::FETCH_ASSOC)['lang'];
+      rou_alert(0,"$OPEP, JMPDBG langage=$language");
+      if ($language!=NULL) {
+        switch ($language) {
+          case "FRA"  :
+            $userlang=lng::fr;
+            break;
+          case "ENG"  :
+          default     :       //NO BREAK
+            $userlang=lng::en;
+            break;
           }
         }
       break;
-    case 5      :       //checking if logname is an admin
+    case 6      :       //checking if logname is an admin
       $stmt=$dbsql->Select("Select * from admins where email='$logname'");
       $id=$stmt->fetch(PDO::FETCH_ASSOC)['email'];
       if ($id!=NULL)
index 5c079dfef5d1694d9637c92acdb7f60f80a511fb..9936f8def534dc0ba25f98d83b382f0c2140cb08 100644 (file)
@@ -217,9 +217,11 @@ foreach($actions as $action) {
   $count++;
   }
 
-$rqst->close();
+//connection to database not needed anymore
+$rqst->release();
+unset($rqst);
 
-$start=starthtml(NULL);
+$start=starthtml("translations");
 $stop=endhtml();
 $top=topper($isadmin,$logname,"$myfilename");
 $footer=footer("$myfilename");