[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 51/73: Code rewriting : doc and remove duplic
From: |
Dany De Bontridder |
Subject: |
[Noalyss-commit] [noalyss] 51/73: Code rewriting : doc and remove duplicated function |
Date: |
Fri, 28 May 2021 05:26:41 -0400 (EDT) |
sparkyx pushed a commit to branch master
in repository noalyss.
commit 49117a5ee06df060d9c2440cc760f0f030d019d3
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Fri May 7 15:23:30 2021 +0200
Code rewriting : doc and remove duplicated function
---
include/lib/data_sql.class.php | 16 +++-
include/lib/noalyss_sql.class.php | 195 ++------------------------------------
2 files changed, 22 insertions(+), 189 deletions(-)
diff --git a/include/lib/data_sql.class.php b/include/lib/data_sql.class.php
index 2fe0698..ce6990e 100644
--- a/include/lib/data_sql.class.php
+++ b/include/lib/data_sql.class.php
@@ -75,6 +75,7 @@ abstract class Data_SQL
var $primary_key; //! Column name of the primary key
var $type; //! Type of the data
var $date_format; //! defaullt date format
+ var $default;
function __construct(DatabaseCore $p_cn, $p_id=-1)
{
@@ -85,9 +86,14 @@ abstract class Data_SQL
if (count($this->name) != count($this->type) ){
throw new Exception (__FILE__." $this->table Cannot
instantiate");
}
+ // forbid the use of a column named type , date_format, name or
primary_key to avoid conflict
+
/* Initialize an empty object */
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);
+ }
$this->$key=null;
}
$this->$pk=$p_id;
@@ -127,7 +133,7 @@ abstract class Data_SQL
public function set($p_string, $p_value)
{
if (array_key_exists($p_string, $this->type)) {
- $this->$idx=$p_value;
+ $this->$p_string=$p_value;
} else
throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur
attribut inexistant '.$p_string);
}
@@ -217,15 +223,17 @@ abstract class Data_SQL
return $this;
}
/**
- * Turn an object (row) into an array
+ *
+ * Turn an object (row) into an array, and the key could be prefixed with
$prefix
+ * @param string $prefix before the key
* @return array
*/
- public function to_array()
+ public function to_array($prefix="")
{
$array=array();
foreach ($this->name as $key=> $value)
{
-
+ $nkey=$prefix.$key;
$array[$key]=$this->$key;
}
return $array;
diff --git a/include/lib/noalyss_sql.class.php
b/include/lib/noalyss_sql.class.php
index 25edb2f..3c79b41 100644
--- a/include/lib/noalyss_sql.class.php
+++ b/include/lib/noalyss_sql.class.php
@@ -87,89 +87,13 @@ require NOALYSS_INCLUDE."/lib/data_sql.class.php";
abstract class Noalyss_SQL extends Data_SQL
{
- var $default;
- function __construct(&$p_cn, $p_id=-1)
- {
- $this->cn=$p_cn;
- $pk=$this->primary_key;
- $this->$pk=$p_id;
- // check that the definition is correct
- if (count($this->name) != count($this->type) ){
- throw new Exception (__FILE__." $this->table Cannot
instantiate");
- }
- /* Initialize an empty object */
- foreach ($this->name as $key)
- {
- $this->$key=null;
- }
- $this->$pk=$p_id;
- /* load it */
- if ($p_id != -1 )$this->load();
-
- }
-/**
- * Insert or update : if the row already exists, update otherwise insert
- */
- public function save()
+
+ function __construct($p_cn, $p_id=-1)
{
- $count = $this->exist();
+ parent::__construct($p_cn, $p_id);
- if ($count == 0)
- $this->insert();
- else
- $this->update();
- }
- /**
- *@brief get the value thanks the colum name and not the alias (name).
- *@see getp
- */
- public function get($p_string)
- {
- if (array_key_exists($p_string, $this->type)) {
- return $this->$p_string;
- }
- else
- throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur
attribut inexistant '.$p_string);
- }
-
- /**
- *@brief set the value thanks the colum name and not the alias (name)
- *@see setp
- */
- public function set($p_string, $p_value)
- {
- if (array_key_exists($p_string, $this->type)) {
- $this->$p_string=$p_value;
- } else
- throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur
attribut inexistant '.$p_string);
}
- /**
- *@brief set the value thanks the alias name instead of the colum name
- *@see get
- */
- public function getp($p_string)
- {
- if (array_key_exists($p_string, $this->name)) {
- $idx=$this->name[$p_string];
- return $this->$idx;
- }
- else
- throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur
attribut inexistant '.$p_string);
- }
-
- /**
- *@brief set the value thanks the alias name instead of the colum name
- *@see set
- */
- public function setp($p_string, $p_value)
- {
- if (array_key_exists($p_string, $this->name)) {
- $idx=$this->name[$p_string];
- $this->$idx=$p_value;
- } else
- throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur
attribut inexistant '.$p_string);
- }
public function insert()
{
@@ -244,17 +168,9 @@ abstract class Noalyss_SQL extends Data_SQL
$sql.=" where ".$this->primary_key." = $".$idx;
$this->cn->exec_sql($sql, $array);
}
- public function set_pk_value($p_value)
- {
- $pk=$this->primary_key;
- $this->$pk=$p_value;
- }
- public function get_pk_value()
- {
- $pk=$this->primary_key;
- return $this->$pk;
- }
-
+ /***
+ * @brief load a row , corresponding to the primary key
+ */
public function load()
{
$sql=$this->build_query();
@@ -273,59 +189,6 @@ abstract class Noalyss_SQL extends Data_SQL
}
}
- public function get_info()
- {
- return var_export($this, true);
- }
-/**
- * @todo ajout vérification type (date, text ou numeric)
- * @return int
- */
- public function verify()
- {
- foreach ($this->name as $key)
- {
- if (trim($this->$key)=='')
- $this->$key=null;
- }
- return 0;
- }
-
- /**
- * Transform an array into object
- * @param type $p_array
- * @return object
- */
- public function from_array($p_array)
- {
- foreach ($this->name as $key=> $value)
- {
- if (isset($p_array[$value]))
- {
- $this->$value=$p_array[$value];
- }
- else
- {
- $this->$value=null;
- }
- }
- return $this;
- }
- /**
- * Turn an object (row) into an array, and the key could be prefixed with
$prefix
- * @param string $prefix before the key
- * @return array
- */
- public function to_array($prefix="")
- {
- $array=array();
- foreach ($this->name as $key=> $value)
- {
- $nkey=$prefix.$key;
- $array[$nkey]=$this->$key;
- }
- return $array;
- }
/**
* @brief retrieve array of object thanks a condition
@@ -342,51 +205,13 @@ abstract class Noalyss_SQL extends Data_SQL
return $ret;
}
- /**
- * get_seek return the next object, the return of the query must have all
the column
- * of the object
- * @param $p_ret is the return value of an exec_sql
- * @param $idx is the index
- * @see seek
- * @return object
- */
- public function next($ret, $i)
- {
- $array=$this->cn->fetch_array($ret, $i);
- return $this->from_array($array);
- }
/**
- * @see next
+ * return the number of count in the table corresponding to the where
condition
+ * @param string $p_where the condition appended to the SQL select query ,
where must be given
+ * @param array $p_array variable from the $p_where condition
+ * @return type
*/
- public function get_object($p_ret, $idx)
- {
- return $this->next($p_ret, $idx);
- }
-
- /**
- * @brief return an array of objects.
- * Do not use this function if they are too many objects, it takes a lot
of memory,
- * and could slow down your application.
- * @param $cond condition, order...
- * @param $p_array array to use for a condition
- * @note this function could slow down your application.
- */
- function collect_objects($cond='', $p_array=null)
- {
- if ($p_array != null && ! is_array($p_array) )
- {
- throw new Exception(_("Erreur : exec_sql attend un array"));
- }
- $ret=$this->seek($cond, $p_array);
- $max=Database::num_row($ret);
- $a_return=array();
- for ($i=0; $i<$max; $i++)
- {
- $a_return[$i]=clone $this->next($ret, $i);
- }
- return $a_return;
- }
public function count($p_where="",$p_array=null) {
$count=$this->cn->get_value("select count(*) from $this->table
".$p_where,$p_array);
return $count;
- [Noalyss-commit] [noalyss] 52/73: Code Rewriting : rename Noalyss_SQL class by Table_Data_SQL, (continued)
- [Noalyss-commit] [noalyss] 52/73: Code Rewriting : rename Noalyss_SQL class by Table_Data_SQL, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 53/73: Code Rewriting : rename Noalyss_SQL class by Table_Data_SQL, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 45/73: Cosmetic : button with HREF, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 60/73: Cosmetic : navigator on small device, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 62/73: Remove dead code, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 63/73: Unit test : update, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 65/73: Task #0001992: Prévision, calcul avec comptabilité analytique, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 54/73: Security : Check the folder, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 05/73: comptability version 7.4 warning null is not a countable, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 48/73: PLpgSQL function , format properly the accounting before inserting fiche_detail and tmp_pcmn tables, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 51/73: Code rewriting : doc and remove duplicated function,
Dany De Bontridder <=
- [Noalyss-commit] [noalyss] 58/73: Improve Manage_Table_SQL : missing param for execute-query, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 59/73: Correct menu PARAM, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 61/73: Bootstrap 4.6, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 64/73: cosmetic : menu on small screen, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 66/73: Devise correct autoliquidation not taken into account for currency, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 67/73: remove debug info, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 68/73: Payment by bank : currency info was missing, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 70/73: Bootstrap 4.6, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 69/73: Javascript format date, transform space into dot, Dany De Bontridder, 2021/05/28
- [Noalyss-commit] [noalyss] 71/73: Cosmetic : adapt for small screen , add border to cells, Dany De Bontridder, 2021/05/28