From: Jean-Marc Pigeon Date: Thu, 11 Dec 2025 20:46:22 +0000 (-0500) Subject: Improving SQL handling X-Git-Tag: tag-0.19~10 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=208db8f1f9ecc07ec5437a3201a819c83edf9272;p=jmp%2Fmailleur Improving SQL handling --- diff --git a/www/devsql.php b/www/devsql.php index 07e1f05..e28e60d 100644 --- a/www/devsql.php +++ b/www/devsql.php @@ -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; } //============================================================== diff --git a/www/gessql.php b/www/gessql.php index 7782ce1..272201e 100644 --- a/www/gessql.php +++ b/www/gessql.php @@ -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) diff --git a/www/lvlmai.php b/www/lvlmai.php index 5c079df..9936f8d 100644 --- a/www/lvlmai.php +++ b/www/lvlmai.php @@ -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");