[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 05/06: Replace PhpCompta_SQL by NOALYSS_SQL A
From: |
Dany De Bontridder |
Subject: |
[Noalyss-commit] [noalyss] 05/06: Replace PhpCompta_SQL by NOALYSS_SQL Add the method in NOALYSS_SQL collect_objects |
Date: |
Thu, 11 Sep 2014 17:07:12 +0000 |
sparkyx pushed a commit to branch master
in repository noalyss.
commit bc630ba4e7a67db6aa083aca3d90db74ef4ef19a
Author: Dany De Bontridder <address@hidden>
Date: Fri Sep 5 20:06:22 2014 +0200
Replace PhpCompta_SQL by NOALYSS_SQL
Add the method in NOALYSS_SQL collect_objects
---
include/class_default_menu_sql.php | 4 +-
include/class_noalyss_sql.php | 329 ++++++++++++++++++++++++++++++++++++
include/class_phpcompta_sql.php | 290 -------------------------------
include/class_profile_sql.php | 4 +-
include/class_stock_goods_sql.php | 6 +-
include/class_stock_sql.php | 4 +-
include/class_tag_sql.php | 4 +-
7 files changed, 340 insertions(+), 301 deletions(-)
diff --git a/include/class_default_menu_sql.php
b/include/class_default_menu_sql.php
index 76f5758..2b3f058 100644
--- a/include/class_default_menu_sql.php
+++ b/include/class_default_menu_sql.php
@@ -23,9 +23,9 @@
*
* @author dany
*/
-require_once 'class_phpcompta_sql.php';
+require_once 'class_noalyss_sql.php';
-class Default_Menu_SQL extends Phpcompta_SQL
+class Default_Menu_SQL extends Noalyss_SQL
{
var $md_id;
var $md_code;
diff --git a/include/class_noalyss_sql.php b/include/class_noalyss_sql.php
new file mode 100644
index 0000000..b73f67b
--- /dev/null
+++ b/include/class_noalyss_sql.php
@@ -0,0 +1,329 @@
+<?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 address@hidden
+
+/**
+ * @file
+ * @brief
+ * this wrapper is used to created easily a wrapper to a table
+ *
+ * @class
+ * Match a table into an object, you need to add the code for each table
+ * @note : the primary key must be an integer
+ *
+ * @code
+ class table_name_sql extends Noalyss_SQL
+ {
+
+ function __construct($p_id=-1)
+ {
+ $this->table = "schema.table";
+ $this->primary_key = "o_id";
+
+ $this->name=array(
+ "id"=>"o_id",
+ "dolibarr"=>"o_doli",
+ "date"=>"o_date",
+ "qcode"=>"o_qcode",
+ "fiche"=>"f_id",
+
+
+ );
+
+ $this->type = array(
+ "o_id"=>"numeric",
+ "o_doli"=>"numeric",
+ "o_date"=>"date",
+ "o_qcode"=>"text",
+ "f_id"=>"numeric",
+
+ );
+
+ $this->default = array(
+ "o_id" => "auto",
+ );
+ $this->date_format = "DD.MM.YYYY";
+ global $cn;
+
+ parent::__construct($cn,$p_id);
+ }
+
+ }
+ * @endcode
+ *
+ */
+class Noalyss_SQL
+{
+
+ function __construct(&$p_cn, $p_id=-1)
+ {
+ $this->cn=$p_cn;
+ $pk=$this->primary_key;
+ $this->$pk=$p_id;
+
+ /* Initialize an empty object */
+ foreach ($this->name as $key)
+ {
+ $this->$key=null;
+ }
+ $this->$pk=$p_id;
+ /* load it */
+ $this->load();
+ }
+
+ public function save()
+ {
+ $pk=$this->primary_key;
+ if ($this->$pk==-1)
+ $this->insert();
+ else
+ $this->update();
+ }
+
+ 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);
+ }
+
+ 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()
+ {
+ $this->verify();
+ $sql="insert into ".$this->table." ( ";
+ $sep="";
+ $par="";
+ $idx=1;
+ $array=array();
+ foreach ($this->name as $key=> $value)
+ {
+ if
(isset($this->default[$value])&&$this->default[$value]=="auto"&&$this->$value==null)
+ continue;
+ if ($value==$this->primary_key&&$this->$value==-1)
+ continue;
+ $sql.=$sep.$value;
+ switch ($this->type[$value])
+ {
+ case "date":
+ if ($this->date_format=="")
+ throw new Exception('Format Date invalide');
+ $par .=$sep.'to_date($'.$idx.",'".$this->date_format."')";
+ break;
+ default:
+ $par .= $sep."$".$idx;
+ }
+
+ $array[]=$this->$value;
+ $sep=",";
+ $idx++;
+ }
+ $sql.=") values (".$par.") returning ".$this->primary_key;
+ $pk=$this->primary_key;
+ $this->$pk=$this->cn->get_value($sql, $array);
+ }
+
+ public function delete()
+ {
+ $pk=$this->primary_key;
+ $sql=" delete from ".$this->table." where
".$this->primary_key."=".sql_string($this->$pk);
+ $this->cn->exec_sql($sql);
+ }
+
+ public function update()
+ {
+ $this->verify();
+ $pk=$this->primary_key;
+ $sql="update ".$this->table." ";
+ $sep="";
+ $idx=1;
+ $array=array();
+ $set=" set ";
+ foreach ($this->name as $key=> $value)
+ {
+ if (isset($this->default[$value])&&$this->default[$value]=="auto")
+ continue;
+ switch ($this->type[$value])
+ {
+ case "date":
+ $par=$value.'=to_date($'.$idx.",'".$this->date_format."')";
+ break;
+ default:
+ $par=$value."= $".$idx;
+ }
+ $sql.=$sep." $set ".$par;
+ $array[]=$this->$value;
+ $sep=",";
+ $set="";
+ $idx++;
+ }
+ $sql.=" where ".$this->primary_key." =".$this->$pk;
+ $this->cn->exec_sql($sql, $array);
+ }
+
+ public function load()
+ {
+ $sql=" select ";
+ $sep="";
+ $par="";
+
+ foreach ($this->name as $key)
+ {
+
+ switch ($this->type[$key])
+ {
+ case "date":
+ $sql .= $sep.'to_char('.$key.",'".$this->date_format."')
as ".$key;
+ break;
+ default:
+ $sql.=$sep.$key;
+ }
+ $sep=",";
+ }
+ $pk=$this->primary_key;
+ $sql.=" from ".$this->table;
+ $sql.=" where ".$this->primary_key." = ".$this->$pk;
+ $result=$this->cn->get_array($sql);
+ if ($this->cn->count()==0)
+ {
+ $this->$pk=-1;
+ return;
+ }
+
+ foreach ($result[0] as $key=> $value)
+ {
+ $this->$key=$value;
+ }
+ }
+
+ public function get_info()
+ {
+ return var_export($this, true);
+ }
+
+ 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;
+ }
+
+ /**
+ * @brief retrieve array of object thanks a condition
+ * @param $cond condition (where clause) (optional by default all the rows
are fetched)
+ * you can use this parameter for the order or subselect
+ * @param $p_array array for the SQL stmt
+ * @see Database::exec_sql get_object Database::num_row
+ * @return the return value of exec_sql
+ */
+ function seek($cond='', $p_array=null)
+ {
+ $sql="select * from ".$this->table." $cond";
+ $ret=$this->cn->exec_sql($sql, $p_array);
+ 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
+ */
+ 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]=$this->next($ret, $i);
+ }
+ return $a_return;
+ }
+
+}
+
+?>
diff --git a/include/class_phpcompta_sql.php b/include/class_phpcompta_sql.php
deleted file mode 100644
index 6ee93d7..0000000
--- a/include/class_phpcompta_sql.php
+++ /dev/null
@@ -1,290 +0,0 @@
-<?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 address@hidden
-
-/**
- * @file
- * @brief
- * this wrapper is used to created easily a wrapper to a table
- *
- address@hidden
- * Match a table into an object, you need to add the code for each table
- address@hidden : the primary key must be an integer
- *
- * @code
-class table_name_sql extends phpcompta_sql
-{
-
- function __construct($p_id=-1)
- {
- $this->table = "schema.table";
- $this->primary_key = "o_id";
-
- $this->name=array(
- "id"=>"o_id",
- "dolibarr"=>"o_doli",
- "date"=>"o_date",
- "qcode"=>"o_qcode",
- "fiche"=>"f_id",
-
-
- );
-
- $this->type = array(
- "o_id"=>"numeric",
- "o_doli"=>"numeric",
- "o_date"=>"date",
- "o_qcode"=>"text",
- "f_id"=>"numeric",
-
- );
-
- $this->default = array(
- "o_id" => "auto",
- );
- $this->date_format = "DD.MM.YYYY";
- global $cn;
-
- parent::__construct($cn,$p_id);
- }
-
-}
- * @endcode
- *
- */
-class Phpcompta_SQL
-{
-
- function __construct(&$p_cn, $p_id = -1)
- {
- $this->cn = $p_cn;
- $pk=$this->primary_key;
- $this->$pk= $p_id;
-
- /* Initialize an empty object */
- foreach ($this->name as $key )
- {
- $this->$key= null;
- }
- $this->$pk= $p_id;
- /* load it */
- $this->load();
- }
- public function save() {
- $pk=$this->primary_key;
- if ( $this->$pk== -1 )
- $this->insert();
- else
- $this->update();
- }
- 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);
- }
-
- 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()
- {
- $this->verify();
- $sql = "insert into " . $this->table . " ( ";
- $sep = "";
- $par = "";
- $idx = 1;
- $array = array();
- foreach ($this->name as $key=>$value)
- {
- if (isset($this->default[$value]) &&
$this->default[$value] == "auto" && $this->$value ==null )
- continue;
- if ( $value==$this->primary_key && $this->$value == -1
) continue;
- $sql.=$sep.$value;
- switch ($this->type[$value])
- {
- case "date":
- if ($this->date_format=="")
throw new Exception('Format Date invalide');
- $par .=$sep. 'to_date($' . $idx . ",'"
. $this->date_format . "')" ;
- break;
- default:
- $par .= $sep."$" . $idx ;
- }
-
- $array[] = $this->$value;
- $sep = ",";
- $idx++;
- }
- $sql.=") values (" . $par . ") returning " . $this->primary_key;
- $pk=$this->primary_key;
- $this->$pk = $this->cn->get_value($sql, $array);
- }
-
- public function delete()
- {
- $pk=$this->primary_key;
- $sql = " delete from " . $this->table . " where " .
$this->primary_key . "=" . sql_string($this->$pk);
- $this->cn->exec_sql($sql);
- }
-
- public function update()
- {
- $this->verify();
- $pk=$this->primary_key;
- $sql = "update " . $this->table . " ";
- $sep = "";
- $idx = 1;
- $array = array();
- $set=" set ";
- foreach ($this->name as $key=>$value)
- {
- if (isset($this->default[$value]) &&
$this->default[$value] == "auto" )
- continue;
- switch ($this->type[$value])
- {
- case "date":
- $par =$value. '=to_date($' . $idx .
",'" . $this->date_format . "')" ;
- break;
- default:
- $par = $value."= $" . $idx ;
- }
- $sql.=$sep." $set " . $par ;
- $array[] = $this->$value;
- $sep = ",";$set="";$idx++;
- }
- $sql.=" where " . $this->primary_key . " =" . $this->$pk;
- $this->cn->exec_sql($sql, $array);
-
- }
-
- public function load()
- {
- $sql = " select ";
- $sep="";$par="";
-
- foreach ($this->name as $key)
- {
-
- switch ($this->type[$key])
- {
- case "date":
- $sql .= $sep.'to_char(' . $key . ",'" .
$this->date_format . "') as ".$key ;
- break;
- default:
- $sql.=$sep.$key ;
- }
- $sep = ",";
- }
- $pk=$this->primary_key;
- $sql.=" from ".$this->table;
- $sql.=" where " . $this->primary_key . " = " . $this->$pk;
- $result = $this->cn->get_array($sql);
- if ($this->cn->count() == 0 ) {
- $this->$pk=-1;
- return ;
- }
-
- foreach ($result[0] as $key=>$value) {
- $this->$key=$value;
- }
- }
-
- public function get_info()
- {
- return var_export($this, true);
- }
-
- 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;
- }
- /**
- address@hidden retrieve array of object thanks a condition
- address@hidden $cond condition (where clause) (optional by default all the
rows are fetched)
- * you can use this parameter for the order or subselect
- address@hidden $p_array array for the SQL stmt
- address@hidden Database::exec_sql get_object Database::num_row
- address@hidden the return value of exec_sql
- */
- function seek($cond='', $p_array=null)
- {
- $sql = "select * from ".$this->table." $cond";
- $ret = $this->cn->exec_sql($sql, $p_array);
- return $ret;
- }
- /**
- *get_seek return the next object, the return of the query must have all
the column
- * of the object
- address@hidden $p_ret is the return value of an exec_sql
- address@hidden $idx is the index
- address@hidden seek
- address@hidden object
- */
- public function next($ret,$i) {
- $array=$this->cn->fetch_array($ret,$i);
- return $this->from_array($array);
- }
- /**
- address@hidden next
- */
- public function get_object($p_ret,$idx)
- {
- return $this->next($p_ret, $idx);
- }
-}
-
-
-?>
diff --git a/include/class_profile_sql.php b/include/class_profile_sql.php
index 7e8c3c3..50a57e7 100644
--- a/include/class_profile_sql.php
+++ b/include/class_profile_sql.php
@@ -30,12 +30,12 @@
*/
require_once('class_database.php');
require_once('ac_common.php');
-require_once 'class_phpcompta_sql.php';
+require_once 'class_noalyss_sql.php';
/**
* @brief Manage the table public.profile
*/
-class Profile_sql extends Phpcompta_SQL
+class Profile_sql extends Noalyss_SQL
{
/* example private
$variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0);
*/
diff --git a/include/class_stock_goods_sql.php
b/include/class_stock_goods_sql.php
index 13c90cd..56ffb83 100644
--- a/include/class_stock_goods_sql.php
+++ b/include/class_stock_goods_sql.php
@@ -25,9 +25,9 @@
* @brief
*
*/
-require_once 'class_phpcompta_sql.php';
+require_once 'class_noalyss_sql.php';
-class Stock_Goods_Sql extends Phpcompta_SQL
+class Stock_Goods_Sql extends Noalyss_SQL
{
function __construct($cn,$p_id = -1)
@@ -81,7 +81,7 @@ class Stock_Goods_Sql extends Phpcompta_SQL
}
-class Stock_Change_Sql extends Phpcompta_SQL
+class Stock_Change_Sql extends Noalyss_SQL
{
function __construct($cn,$p_id = -1)
diff --git a/include/class_stock_sql.php b/include/class_stock_sql.php
index 9853ab7..e6b1cf1 100644
--- a/include/class_stock_sql.php
+++ b/include/class_stock_sql.php
@@ -25,9 +25,9 @@
* @brief
*
*/
-require_once 'class_phpcompta_sql.php';
+require_once 'class_noalyss_sql.php';
-class Stock_Sql extends Phpcompta_SQL {
+class Stock_Sql extends Noalyss_SQL {
function __construct($cn,$p_id=-1)
{
$this->table = "public.stock_repository";
diff --git a/include/class_tag_sql.php b/include/class_tag_sql.php
index 82a1263..ba4caa0 100644
--- a/include/class_tag_sql.php
+++ b/include/class_tag_sql.php
@@ -1,7 +1,7 @@
<?php
require_once('class_database.php');
require_once('ac_common.php');
-require_once 'class_phpcompta_sql.php';
+require_once 'class_noalyss_sql.php';
/*
* This file is part of NOALYSS.
*
@@ -23,7 +23,7 @@ require_once 'class_phpcompta_sql.php';
/**
* @brief Manage the table public.tag
*/
-class Tag_SQL extends Phpcompta_SQL
+class Tag_SQL extends Noalyss_SQL
{
/* example private
$variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0);
*/
- [Noalyss-commit] [noalyss] branch master updated (12d6119 -> 59e76cb), Dany De Bontridder, 2014/09/11
- [Noalyss-commit] [noalyss] 06/06: Database::make_array ajoute un paramètre dans le cas où le SQL admet une condition, Dany De Bontridder, 2014/09/11
- [Noalyss-commit] [noalyss] 05/06: Replace PhpCompta_SQL by NOALYSS_SQL Add the method in NOALYSS_SQL collect_objects,
Dany De Bontridder <=
- [Noalyss-commit] [noalyss] 03/06: Replace message error_box, Dany De Bontridder, 2014/09/11
- [Noalyss-commit] [noalyss] 01/06: Formatage code, Dany De Bontridder, 2014/09/11
- [Noalyss-commit] [noalyss] 02/06: Securité : amélioration pour les backups, Dany De Bontridder, 2014/09/11
- [Noalyss-commit] [noalyss] 04/06: Reformat code, Dany De Bontridder, 2014/09/11