noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 01/16: Code Improve : Data_SQL


From: dwm
Subject: [Noalyss-commit] [noalyss] 01/16: Code Improve : Data_SQL
Date: Tue, 27 May 2025 09:26:06 -0400 (EDT)

sparkyx pushed a commit to branch unstable
in repository noalyss.

commit 69acd7f67d759db62a954e7e547d2390e711a7b5
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Sun May 11 21:34:13 2025 +0200

    Code Improve : Data_SQL
---
 include/constant.php                       |   1 +
 include/lib/data_sql.class.php             |  70 +++++++----
 unit-test/include/lib/data_newsqlTest.php  | 189 ++++++++++++++++++++++++++++
 unit-test/include/lib/data_sqlTest.php     | 193 +++++++++++++++++++++++++++++
 unit-test/include/lib/databaseCoreTest.php |   1 -
 5 files changed, 431 insertions(+), 23 deletions(-)

diff --git a/include/constant.php b/include/constant.php
index b0042047a..15df1481c 100644
--- a/include/constant.php
+++ b/include/constant.php
@@ -347,6 +347,7 @@ define('EXC_PARAM_TYPE', 1006);
 define('EXC_DUPLICATE', 1200);
 define('EXC_INVALID', 1400);
 define('EXC_FORBIDDEN', 1500);
+define('EXC_DATA_SQL', 2001);
 // exception when balance is incorrect when saving an operation
 define('EXC_BALANCE', 1501);
 define("UNPINDG", "&#xf047;");
diff --git a/include/lib/data_sql.class.php b/include/lib/data_sql.class.php
index 72c8ca646..54fffbdc3 100644
--- a/include/lib/data_sql.class.php
+++ b/include/lib/data_sql.class.php
@@ -34,7 +34,8 @@
  * 
  *   - table = name of the view or empty
  *   - sql = sql statement
- *   - name = array of column name, match between logic and actual name
+ *   - name = array of column name, match between logic and actual name, or 
+ *            only an array of columns
  *   - type = array , match between column and type of data
  *   - default = array of column with a default value
  *   - date_format = format of the date
@@ -92,7 +93,7 @@
 @endcode
  * 
  */
-#[AllowDynamicProperties]
+
 abstract class Data_SQL
 {
    var $cn;         //! Database connection
@@ -133,7 +134,7 @@ abstract class Data_SQL
         foreach ($this->name as $key)
         {
             if ( 
in_array($key,['name','type','format_date','cn','date_format','default'] ) ) {
-                throw new Exception ('DATASQL-94 invalid column name'.$key);
+                throw new Exception ('DATASQL-94 invalid column name'.$key,94);
             }
             $this->$key=null;
         }
@@ -158,56 +159,81 @@ abstract class Data_SQL
      *@brief get the value thanks the colum name and not the alias (name). 
      *@see getp
      */
-    public function get($p_string)
+    public function get($cols)
     {
-        if (array_key_exists($p_string, $this->type)) {
-            return $this->$p_string;
+        if (array_key_exists($cols, $this->type)) {
+            return $this->$cols;
         }
         else
-            throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur 
attribut inexistant '.$p_string);
+             throw new \Exception (" unknow cols [$cols] 
=".$this,EXC_DATA_SQL);
     }
 
     /**
      *@brief set the value thanks the colum name and not the alias (name)
      *@see setp
      */
-    public function set($p_string, $p_value)
+    public function set($cols, $p_value)
     {
-        if (array_key_exists($p_string, $this->type))    {
-            $this->$p_string=$p_value;
+        if (array_key_exists($cols, $this->type))    {
+            $this->$cols=$p_value;
             return $this;
         }        else
-            throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur 
attribut inexistant '.$p_string);
+           throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
+            
     }
 
     /**
      *@brief set the value thanks the alias name instead of the colum name 
+     * if not       found try the column name 
      *@see get
      */
-    public function getp($p_string)
+    public function getp($cols)
     {
-        if (array_key_exists($p_string, $this->name)) {
-            $idx=$this->name[$p_string];
+        if (array_key_exists($cols, $this->name)) {
+            $idx=$this->name[$cols];
             return $this->$idx;
         }
-        else
-            throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur 
attribut inexistant '.$p_string);
+        if (array_key_exists($cols, $this->type)) {
+            return $this->$cols;
+        }
+        
+        throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
     }
 
     /**
-     *@brief set the value thanks the alias name instead of the colum name 
+     *@brief set the value thanks the alias name instead of the colum name, 
+     * if not       found try the column name 
      *@see set
      */
-    public function setp($p_string, $p_value)
+    public function setp($cols, $p_value)
     {
-        if (array_key_exists($p_string, $this->name))    {
-            $idx=$this->name[$p_string];
+        if (array_key_exists($cols, $this->name))    {
+            $idx=$this->name[$cols];
             $this->$idx=$p_value;
             return $this;
-        }        else
-            throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur 
attribut inexistant '.$p_string);
+        }       
+        if (array_key_exists($cols, $this->type))    {
+            $this->$cols=$p_value;
+            return $this;
+        }
+        
+        throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
     }
 
+    public function __set($cols,$p_value) {
+        if (array_key_exists($cols, $this->type))    {
+            $this->$cols=$p_value;
+            return $this;
+        }        else
+           throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
+    }
+    public function __get($cols) {
+         if (array_key_exists($cols, $this->type)) {
+            return $this->$cols;
+        }
+        else
+           throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
+    }
     abstract function insert();
 
     abstract function delete();
diff --git a/unit-test/include/lib/data_newsqlTest.php 
b/unit-test/include/lib/data_newsqlTest.php
new file mode 100644
index 000000000..2662f6475
--- /dev/null
+++ b/unit-test/include/lib/data_newsqlTest.php
@@ -0,0 +1,189 @@
+<?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
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @file
+ * @brief New syntax for Table_Data_SQL
+ */
+
+class Currency_SQL extends \Table_Data_SQL
+{
+     function __construct(Database $p_cn, $p_id=-1)
+    {
+        $this->table="public.currency";
+        $this->primary_key="id";
+        /*
+         * List of columns
+         */
+        $this->name=array(
+             "id"
+            , "cr_code_iso"
+            ,"cr_name"
+        );
+        /*
+         * Type of columns
+         */
+        $this->type=array(
+            "id"=>"numeric"
+            , "cr_code_iso"=>"text"
+            , "cr_name"=>"text"
+        );
+
+
+        $this->default=array(
+            "id"=>"auto"
+        );
+
+        $this->date_format="DD.MM.YYYY";
+        parent::__construct($p_cn, $p_id);
+    }
+}
+class Data_NewSQLTest extends TestCase 
+{
+    /**
+     * @testdox create object currency_sql
+     */
+    function testBuildObject()
+    {
+        $cn=new \Database(DOSSIER);
+        $currency_sql = new Currency_SQL($cn,0);
+        $this->assertEquals($currency_sql->cr_code_iso,'EUR','invalid ISO 
Code');
+    }
+    /**
+     * @testdox Insert , update and delete row
+     */
+    function testDMLObject()
+    {
+        /* insert */
+        $cn=new \Database(DOSSIER);
+        $currency_sql = new Currency_SQL($cn);
+        $currency_sql->cr_code_iso = 'XIU';
+        $currency_sql->cr_name = 'Currency for UNIT TEST';
+        $currency_sql->insert();
+        $this->assertTrue($currency_sql->id > 0,' row not created');
+         /* update */
+        $update_sql = new Currency_SQL($cn,$currency_sql->id);
+        $this->assertTrue($update_sql->cr_code_iso=='XIU'," update can not 
find row");
+        $update_sql->cr_name='xxxx';
+        $update_sql->save();
+        $check_sql=new Currency_SQL($cn,$currency_sql->id);
+        $this->assertEquals($check_sql->get('cr_name'),'xxxx','get row not 
updated');
+        $this->assertEquals($check_sql->cr_name,'xxxx',' __get row not 
updated');
+        $this->assertEquals($check_sql->getp('cr_name'),'xxxx',' getp row not 
updated');
+        /**
+         * delete
+         */
+        $check_sql->delete();
+        $update_sql->load();
+        $this->assertTrue($update_sql->id < 0,' row not deleted');
+    }
+    /**
+     * @testdox  __get an unknow col 
+     */
+    function testGetUnknowCol1()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $a=$currency_sql->dummy;
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox get an unknow col 
+     */
+    function testGetUnknowCol2()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->get("dummy");
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox getp an unknow col 
+     */
+    function testGetUnknowCol3()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->getp("dummy");
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+     /**
+     * @testdox  __set an unknow col 
+     */
+    function testSetUnknowCol1()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->dummy=1;
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox set an unknow col 
+     */
+    function testSetUnknowCol2()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->set("dummy",1);
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox setp an unknow col 
+     */
+    function testSetUnknowCol3()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->setp("dummy",1);
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+}
\ No newline at end of file
diff --git a/unit-test/include/lib/data_sqlTest.php 
b/unit-test/include/lib/data_sqlTest.php
new file mode 100644
index 000000000..72bec5e9d
--- /dev/null
+++ b/unit-test/include/lib/data_sqlTest.php
@@ -0,0 +1,193 @@
+<?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
+
+use PHPUnit\Framework\TestCase;
+/**
+ * @file
+ * @brief Test Data_SQL
+ */
+require DIRTEST.'/global.php';
+
+class Currency_SQL extends \Table_Data_SQL
+{
+
+    function __construct(Database $p_cn, $p_id=-1)
+    {
+        $this->table="public.currency";
+        $this->primary_key="id";
+        /*
+         * List of columns
+         */
+        $this->name=array(
+            "id"=>"id"
+            , "cr_code_iso"=>"cr_code_iso"
+            ,"cr_name"=>"cr_name"
+        );
+        /*
+         * Type of columns
+         */
+        $this->type=array(
+            "id"=>"numeric"
+            , "cr_code_iso"=>"text"
+            , "cr_name"=>"text"
+        );
+
+
+        $this->default=array(
+            "id"=>"auto"
+        );
+
+        $this->date_format="DD.MM.YYYY";
+        parent::__construct($p_cn, $p_id);
+    }
+
+}
+
+class Data_SQLTest extends TestCase 
+{
+    /**
+     * @testdox create object currency_sql
+     */
+    function testBuildObject()
+    {
+        $cn=new \Database(DOSSIER);
+        $currency_sql = new Currency_SQL($cn,0);
+        $this->assertEquals($currency_sql->cr_code_iso,'EUR','invalid ISO 
Code');
+    }
+    /**
+     * @testdox Insert , update and delete row
+     */
+    function testDMLObject()
+    {
+        /* insert */
+        $cn=new \Database(DOSSIER);
+        $currency_sql = new Currency_SQL($cn);
+        $currency_sql->cr_code_iso = 'XIU';
+        $currency_sql->cr_name = 'Currency for UNIT TEST';
+        $currency_sql->insert();
+        $this->assertTrue($currency_sql->id > 0,' row not created');
+         /* update */
+        $update_sql = new Currency_SQL($cn,$currency_sql->id);
+        $this->assertTrue($update_sql->cr_code_iso=='XIU'," update can not 
find row");
+        $update_sql->cr_name='xxxx';
+        $update_sql->save();
+        $check_sql=new Currency_SQL($cn,$currency_sql->id);
+        $this->assertEquals($check_sql->get('cr_name'),'xxxx','get row not 
updated');
+        $this->assertEquals($check_sql->cr_name,'xxxx',' __get row not 
updated');
+        $this->assertEquals($check_sql->getp('cr_name'),'xxxx',' getp row not 
updated');
+        /**
+         * delete
+         */
+        $check_sql->delete();
+        $update_sql->load();
+        $this->assertTrue($update_sql->id < 0,' row not deleted');
+    }
+    /**
+     * @testdox  __get an unknow col 
+     */
+    function testGetUnknowCol1()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $a=$currency_sql->dummy;
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox get an unknow col 
+     */
+    function testGetUnknowCol2()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->get("dummy");
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox getp an unknow col 
+     */
+    function testGetUnknowCol3()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->getp("dummy");
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+     /**
+     * @testdox  __set an unknow col 
+     */
+    function testSetUnknowCol1()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->dummy=1;
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox set an unknow col 
+     */
+    function testSetUnknowCol2()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->set("dummy",1);
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+      /**
+     * @testdox setp an unknow col 
+     */
+    function testSetUnknowCol3()
+    {
+        try {
+            $cn=new \Database(DOSSIER);
+            $currency_sql = new Currency_SQL($cn,0);
+            $currency_sql->setp("dummy",1);
+            $currency_sql->save();
+            
+        } catch (Exception $ex) {
+                $this->assertEquals($ex->getCode(), EXC_DATA_SQL,' get unknow 
column fails');
+        }
+    }
+}
\ No newline at end of file
diff --git a/unit-test/include/lib/databaseCoreTest.php 
b/unit-test/include/lib/databaseCoreTest.php
index 246097350..5280d19f9 100644
--- a/unit-test/include/lib/databaseCoreTest.php
+++ b/unit-test/include/lib/databaseCoreTest.php
@@ -29,7 +29,6 @@ use PHPUnit\Framework\TestCase;
  * Generated by PHPUnit_SkeletonGenerator on 2016-02-11 at 15:41:40.
  */
 class DatabaseCoreTest extends TestCase
-
 {
 
     /**



reply via email to

[Prev in Thread] Current Thread [Next in Thread]