]> SAFE projects GIT repository - jmp/mailleur/commitdiff
the generic database scan function )named probe) seems to be working
authorJean-Marc Pigeon <jmp@safe.c>
Wed, 19 Nov 2025 17:57:58 +0000 (12:57 -0500)
committerJean-Marc Pigeon <jmp@safe.c>
Wed, 19 Nov 2025 17:57:58 +0000 (12:57 -0500)
www/gessql.php
www/lvlusr.php

index 1abc672e60ca6a27f3f317764682ac6bcb30c59a..ca52def31f521f313bacd0203073fcf87d0ed5d1 100644 (file)
@@ -8,6 +8,168 @@
 include_once "subrou.php";
 include_once "devsql.php";
 
+class probe       {
+    private devsql $db;         // devsql database instance
+    private string $table;      // table name
+
+    private array $where = [];  // WHERE conditions
+    private array $params = []; // parameters for prepared statements
+
+    private ?string $order = null; // ORDER BY clause
+    private ?int $limit = null;    // LIMIT for pagination
+    private int $offset = 0;       // OFFSET for pagination, internal only
+
+    /**
+     * Constructor
+     */
+    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 a WHERE condition
+     */
+    public function where(string $column, string $operator, $value): self
+    {
+        $param = ':' . $column . count($this->where);
+        $this->where[] = "$column $operator $param";
+        $this->params[$param] = $value;
+        return $this;
+    }
+
+    /**
+     * Set ORDER BY clause
+     */
+    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;
+    }
+
+    /**
+     * Execute the query and return results
+     */
+    public function get(): array
+    {
+        $sql = "SELECT * FROM {$this->table}";
+
+        if ($this->where) {
+            $sql .= " WHERE " . implode(' AND ', $this->where);
+        }
+
+        if ($this->order) {
+            $sql .= " ORDER BY {$this->order}";
+        }
+
+        if ($this->limit !== null) {
+            $sql .= " LIMIT {$this->limit}";
+        }
+
+        if ($this->offset > 0) {
+            $sql .= " OFFSET {$this->offset}";
+        }
+
+        $stmt = $this->db->Select($sql, $this->params);
+        return $stmt->fetchAll();
+    }
+
+    /**
+     * Count total number of items matching the current WHERE conditions
+     */
+    public function count(): int
+    {
+        $sql = "SELECT COUNT(*) AS cnt FROM {$this->table}";
+
+        if ($this->where) {
+            $sql .= " WHERE " . implode(' AND ', $this->where);
+        }
+
+        $stmt = $this->db->Select($sql, $this->params);
+        $row = $stmt->fetch();
+        return (int) $row['cnt'];
+    }
+
+    /** ----------------------------- **/
+    /** Pagination / navigation      **/
+    /** ----------------------------- **/
+
+    /**
+     * Move offset to next page
+     * Does NOT fetch data
+     */
+    public function goNext(): int
+    {
+        if ($this->limit !== null) {
+            $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;
+    }
+
+    /**
+     * Move offset to first page
+     * Does NOT fetch data
+     */
+    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->count();
+            $this->offset = max(0, $total - $this->limit);
+        }
+        return $this->offset;
+    }
+
+    /**
+     * Get current offset value
+     */
+    public function getOffset(): int
+    {
+        return $this->offset;
+    }
+}
+
 //==============================================================
 //
 //      Function to check if password is the right one
index 799378acc740af2c1a9971c58f638be78d74e79d..230b58e8e21d354c776ddb143ebdb2b07199ab50 100644 (file)
@@ -89,39 +89,38 @@ global $logname;
 $limit=20;
 $offset=0;
 $username="";
-$dbsql=sql_connect();
-if (isset($_POST['username']))
-  $username=trim(($_POST['username']));
 if (isset($_POST['limit']))
   $limit=intval($_POST['limit']);
 if (isset($_POST['offset']))
   $offset=intval($_POST['offset']);
-$numrec=sql_getnumemail($dbsql,$username);
+$rqst=NEW probe("emails",$limit,$offset);
+if (isset($_POST['username'])) {
+  $username=trim(($_POST['username']));
+  $rqst->where("email","like","%$username%");
+  }
+$numrec=$rqst->count();
 if (isset($_POST['scanner'])) {
   $scanner=$_POST['scanner'];
   switch ($scanner) {
     case "golast"       :
-      $offset=$numrec-$limit;
+      $offset=$rqst->goLast();
       break;
     case "gonext"       :
-
-      $offset+=$limit;
-      if ($offset>($numrec-$limit))
-        $offset=$numrec-$limit;
+      $offset=$rqst->goNext();
       break;
     case "goprevious"   :
-      $offset-=$limit;
-      if ($offset<0)
-        $offset=0;
+      $offset=$rqst->goPrevious();
       break;
     case "gofirst"       :
-      $offset=0;
+      $offset=$rqst->goFirst();
       break;
     default             :
       break;
     }
   }
-$emails=sql_getemaillist($dbsql,$username,$limit,$offset);
+$emails=$rqst->order("email asc")
+             ->limit($limit)
+             ->get();
 $line="";
 $count=$offset;
 foreach($emails as $email) {
@@ -144,7 +143,7 @@ foreach($emails as $email) {
   $line=$line."<TD align=center>$lock</TD>\r\n";
   $line=$line."</TR>";
   }
-sql_close($dbsql);
+$rqst->close();
 
 $start=starthtml("directives");
 $stop=endhtml();