Code Improve : Single_Record

This commit is contained in:
sparkyx 2025-05-19 23:01:49 +02:00
parent 001677eb72
commit d9b1e4b2d2
2 changed files with 144 additions and 13 deletions

View file

@ -27,22 +27,37 @@ define ('CODE_EXCP_DUPLICATE',901);
/**
* @brief Objec to check a double insert into the database, this duplicate occurs after
* a refresh of the web page
* in
*/
class Single_Record
{
public $name;
public $id;
private $dbconx; //!< database connexion
/**
* Constructor $p_name will be set to $this->name, it is also the name
* @brief Constructor $p_name will be set to $this->name, it is also the name
* of the tag hidden in a form
* @remark $cn Db connexion
* @param $p_name
* @param $p_name (string) name of the HIDDEN INPUT
* @param $dbconx Database connx , if not given; use global $cn;
*/
function __construct($p_name)
function __construct($p_name,$dbconx=-1)
{
$this->name=$p_name;
$this->id=0;
if ( is_numeric($dbconx ) ) {
global $cn;
$this->dbconx=$cn;
}else {
$this->dbconx=$dbconx;
}
}
function __toString(): string {
return "Single_Record[name=" . $this->name
. ", id=" . $this->id
. ", dbconx=" . $this->dbconx
. "]";
}
/**
* @brief return a string with a tag hidden and a uniq value
@ -51,8 +66,7 @@ class Single_Record
*/
function hidden()
{
global $cn;
$this->id=$cn->get_next_seq('uos_pk_seq');
$this->id=$this->dbconx->get_next_seq('uos_pk_seq');
return HtmlInput::hidden($this->name,$this->id);
}
/**
@ -62,19 +76,18 @@ class Single_Record
*/
function save($p_array=null)
{
global $cn;
if ( $p_array == null ) $p_array=$_POST;
$this->id=$p_array[$this->name];
if ( $p_array == null ) $p_array=$_POST;
$this->id=$p_array[$this->name];
$sql="insert into tool_uos(uos_value) values ($1)";
try {
$cn->exec_sql($sql,array($this->id));
$this->dbconx->exec_sql($sql,array($this->id));
} catch (Exception $e)
{
throw new Exception('Duplicate value');
}
}
/**
* Count how many time we have this->id into the table tool_uos
* @brief Count how many time we have this->id into the table tool_uos
* @remark global $cn Database connexion
* @param $p_array is the array where to find the key name, usually it is
* $_POST. The default value is $_POST
@ -85,7 +98,7 @@ class Single_Record
global $cn;
if ( $p_array == null ) $p_array=$_POST;
$this->id=$p_array[$this->name];
$count=$cn->get_value('select count(*) from tool_uos where uos_value=$1',
$count=$this->dbconx->get_value('select count(*) from tool_uos where uos_value=$1',
array($this->id));
return $count;
}
@ -96,7 +109,7 @@ class Single_Record
$this->id=$p_array[$this->name];
try
{
$count=$cn->get_value('select count(*) from tool_uos where uos_value=$1',
$count=$this->dbconx->get_value('select count(*) from tool_uos where uos_value=$1',
array($this->id));
if ($count != 0 ) throw new Exception ('DUPLICATE',CODE_EXCP_DUPLICATE);
}catch (Exception $e)

View file

@ -0,0 +1,118 @@
<?php
/*
* This file is part of NOALYSS.
*
* NOALYSS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* NOALYSS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOALYSS; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Copyright Author Dany De Bontridder danydb@aevalys.eu 22/10/23
/**
* @file
* @brief test Single_Record
*/
use PHPUnit\Framework\TestCase;
class Single_RecordTest extends TestCase {
/**
* @covers Single_Record::hidden
* @covers Single_Record::__construct()
* @global $g_connection
*/
function testHiddenDatabase() {
global $g_connection;
$test_dup = new \Single_Record("test_dup", $g_connection);
$r = $test_dup->hidden();
$this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup);
}
/**
* @covers Single_Record::hidden
* @covers Single_Record::__construct()
* @global $g_connection
*/
function testHiddenDefaultDatabase() {
global $g_connection;
global $cn;
$cn = $g_connection;
$test_dup = new \Single_Record("test_dup");
$r = $test_dup->hidden();
$this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup);
}
/**
* @covers Single_Record::hidden
* @covers Single_Record::__construct()
* @global $g_connection
*/
function testSequence() {
global $g_connection;
global $cn;
$cn = $g_connection;
$test_dup = new \Single_Record("test_dup");
$r = $test_dup->hidden();
$a = $test_dup->id;
$this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup);
$r = $test_dup->hidden();
$this->assertTrue($test_dup->id > $a, " id not incremented" . $test_dup);
}
/**
* @covers Single_Record::save()
* @global $g_connection
*/
function testSave() {
global $g_connection;
global $cn;
$cn = $g_connection;
$test_dup = new \Single_Record("test_dup");
$r = $test_dup->hidden();
$this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup);
$test_dup->save(["test_dup"=>$test_dup->id]);
}
/**
* @covers Single_Record::check()
* @global $g_connection
*/
function testCheck() {
global $g_connection;
global $cn;
$this->expectException (\Exception::class);
$cn = $g_connection;
$test_dup = new \Single_Record("test_dup");
$r = $test_dup->hidden();
$test_dup->save(["test_dup"=>$test_dup->id]);
$this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup);
$test_dup->check(['test_dup'=>$test_dup->id]);
$test_dup->check(['test_dup'=>$test_dup->id]);
}
/**
* @covers Single_Record::get_count()
* @global $g_connection
*/
function testCount() {
global $g_connection;
global $cn;
$cn = $g_connection;
$test_dup = new \Single_Record("test_dup");
$r = $test_dup->hidden();
$this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup);
$test_dup->save(["test_dup"=>$test_dup->id]);
$cnt = $test_dup->get_count(['test_dup'=>$test_dup->id]);
$this->assertTrue($cnt > 0,"not count") ;
}
}