From 4ffde0c1b0ca3fa857170c734bad3395337d7d5a Mon Sep 17 00:00:00 2001 From: sparkyx Date: Wed, 5 Oct 2022 11:36:43 +0200 Subject: [PATCH] Code cleaning pg_fetch_array Postgresql fetch_array returns array with assoc only; except on specific occasions --- include/class/periode.class.php | 2 +- include/lib/database_core.class.php | 7 ++++--- include/lib/manage_table_sql.class.php | 26 +++++++++++++++++++------ scenario/test_manage_table_sql.php | 6 +++++- unit-test/include/lib/ac_commonTest.php | 9 ++++++++- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/class/periode.class.php b/include/class/periode.class.php index 44b4fc6f8..1e18583b9 100644 --- a/include/class/periode.class.php +++ b/include/class/periode.class.php @@ -346,7 +346,7 @@ class Periode $Res=$this->cn->exec_sql($sql, array($p_periode)); if (Database::num_row($Res)==0) return null; - return Database::fetch_array($Res, 0); + return Database::fetch_array($Res, 0,PGSQL_BOTH); } /*!\brief return the first day of periode diff --git a/include/lib/database_core.class.php b/include/lib/database_core.class.php index 19dbbc0cf..11b89ccb0 100644 --- a/include/lib/database_core.class.php +++ b/include/lib/database_core.class.php @@ -381,7 +381,7 @@ class DatabaseCore { if ($this->ret == false) throw new Exception('this->ret is empty'); - return pg_fetch_array($this->ret, $p_indice); + return pg_fetch_array($this->ret, $p_indice,PGSQL_ASSOC); } /** @@ -739,12 +739,13 @@ class DatabaseCore /**\brief wrapper for the function pg_fetch_array * \param $ret is the result of a pg_exec * \param $p_indice is the index + * \param $p_indice is the index * \return $array of column */ - static function fetch_array($ret, $p_indice = 0) + static function fetch_array($ret, $p_indice = 0,$p_mode=PGSQL_ASSOC) { - return pg_fetch_array($ret, $p_indice); + return pg_fetch_array($ret, $p_indice,$p_mode); } /**\brief wrapper for the function pg_fetch_all diff --git a/include/lib/manage_table_sql.class.php b/include/lib/manage_table_sql.class.php index 2dcb8899e..ccaec8118 100644 --- a/include/lib/manage_table_sql.class.php +++ b/include/lib/manage_table_sql.class.php @@ -85,7 +85,7 @@ class Manage_Table_SQL const UPDATABLE=1; const VISIBLE=2; - private $icon_mod; //!< place of right or left the icon update or mod, default right, accepted value=left,right,first column for mod + private $icon_mod; //!< place of right or left the icon update or mod, default right, accepted value=left,right,first,custom column for mod private $icon_del; //!< place of right or left the icon update or mod, default right, accepted value=left,right private $dialogbox_style; //!< style of the dialog box private $button_add_top; //!< place of the button add on the top, by default true @@ -573,13 +573,14 @@ function check() return $this->row_update; } /** - * @brief Set the icon to modify at the right ,the first col or left of the row - * - * @param type $pString - * @throws Exception + * @brief Set the icon to modify at the right ,the first col or left of the row, if the mod if custom , + * you have to override the function display_icon_custom + * @see Manage_Table_SQL::display_icon_custom($p_row) + * @param string $pString default right, accepted value=left,right,first,custom column for mod + * @throws Exception if invalide choice */ function set_icon_mod($pString) { - if ($pString != "right" && $pString != "left" && $pString!="first") + if (! in_array( $pString ,[ 'right','left','custom','first'] ) ) throw new Exception('set_icon_mod invalide '.$pString); $this->icon_mod=$pString; } @@ -987,6 +988,8 @@ function check() $this->display_icon_mod($p_row); if ($this->icon_del=="left") $this->display_icon_del($p_row); + if ( $this->icon_mod == "custom") + $this->display_icon_custom($p_row); $nb_order=count($this->a_order); for ($i=0; $i<$nb_order; $i++) @@ -1501,4 +1504,15 @@ function check() echo ""; } + /** + * @brief usually contain a link and calls another page, it must overriden + * @param array $p_row is the current database row + * @return void + */ + function display_icon_custom($p_row) + { + echo ''.''; + print_r($p_row); + echo ''; + } } diff --git a/scenario/test_manage_table_sql.php b/scenario/test_manage_table_sql.php index da6ea55cb..906260292 100644 --- a/scenario/test_manage_table_sql.php +++ b/scenario/test_manage_table_sql.php @@ -100,6 +100,10 @@ echo "

"." Delete right"."

"; $manage_table->set_icon_del("right"); $manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); +echo "

"." Custom , by default show the row"."

"; +$manage_table->set_icon_mod("custom"); +$manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); - ?> + +?> diff --git a/unit-test/include/lib/ac_commonTest.php b/unit-test/include/lib/ac_commonTest.php index 510395b17..6cdb60209 100644 --- a/unit-test/include/lib/ac_commonTest.php +++ b/unit-test/include/lib/ac_commonTest.php @@ -393,7 +393,14 @@ class Ac_CommonTest extends TestCase $expect=sprintf('%s',h("test@noalyss.be"),h("test@noalyss.be")); $expect=preg_replace('/\s+/','',$expect); $this->assertEquals(strtoupper($expect),strtoupper(preg_replace("/\s+/",'',mailTo('test@noalyss.be'))),); - $this->assertEquals('test@noalyss.@be',mailTo('test@noalyss.@be'),"Send email to invalidate email"); + $expect=<< +EOF; + $expect=preg_replace('/\s+/','',$expect); + $result=preg_replace('/\s+/','',mailTo("test@noalyss.@be")); + $this->assertEquals($expect, + $result, + "Send email to invalidate email"); $expect=sprintf('%s',h("test@noalyss.be"),h("test@noalyss.be")); $expect.=sprintf('%s',h("test2@noalyss.be"),h("test2@noalyss.be")); $expect=preg_replace('/\s+/','',$expect);