From 69acd7f67d759db62a954e7e547d2390e711a7b5 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Sun, 11 May 2025 21:34:13 +0200 Subject: [PATCH] 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(-) create mode 100644 unit-test/include/lib/data_newsqlTest.php create mode 100644 unit-test/include/lib/data_sqlTest.php 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", ""); 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 @@ +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 @@ +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 - { /**