noalyss Version-9
Public Member Functions
Table_Data_SQL Class Reference

Interface : this wrapper is used to created easily a wrapper to a table (ORM) You must create a class extending this one, in the constructor these variables have to be defined. More...

Inherits Data_SQL.

Inherited by Acc_Other_Tax_SQL, Action_Gestion_Comment_SQL, Action_Gestion_SQL, Anc_Key_Activity_SQL, Anc_Key_Detail_SQL, Anc_Key_Ledger_SQL, Anc_Key_SQL, Attr_Def_SQL, Contact_option_ref_SQL, Currency_SQL, Currency_history_SQL, Default_Menu_SQL, Document_State_SQL, Document_type_SQL, Fiche_def_ref_SQL, Forecast_Category_SQL, Forecast_Item_SQL, Forecast_SQL, Form_Definition_SQL, Form_Detail_SQL, Jrn_Note_SQL, Jrn_Tax_SQL, Jrn_def_SQL, Jrn_periode_SQL, Menu_Ref_SQL, Noalyss_SQL, Op_Predef_SQL, Operation_Exercice_Detail_SQL, Operation_Exercice_SQL, Operation_currency_SQL, Parameter_Extra_SQL, Parm_periode_SQL, Payment_method_SQL, Poste_analytique_SQL, Profile_Menu_sql, Profile_Mobile_SQL, Profile_sql, Quant_Fin_SQL, Quant_Purchase_SQL, Quant_Sold_SQL, Stock_Change_Sql, Stock_Goods_Sql, Stock_Sql, Tag_SQL, Tag_group_SQL, Tmp_Pcmn_SQL, Tva_Rate_SQL, User_filter_SQL, and V_Tva_Rate_SQL.

+ Collaboration diagram for Table_Data_SQL:

Public Member Functions

 __construct ($p_cn, $p_id=-1)
 
 build_query ()
 Build the SQL select statement for querying the object and returns it. More...
 
 column_update ($column_expr, $value)
 update the value of a column with an expression for $value for the current record More...
 
 count ($p_where="", $p_array=null)
 return the number of count in the table corresponding to the where condition More...
 
 delete ()
 
 exist ()
 Count the number of record with the id ,. More...
 
 get_all_to_array ($p_key_col, $p_cond="", $p_array=NULL)
 Get all the row and use the p_key_code are the key value of array. More...
 
 insert ()
 
 load ()
 Load the current row return false if not found. More...
 
 seek ($cond='', $p_array=null)
 retrieve array of object thanks a condition More...
 
 update ()
 
- Public Member Functions inherited from Data_SQL
 __construct (DatabaseCore $p_cn, $p_id=-1)
 
 __toString ()
 
 collect_objects ($cond='', $p_array=null)
 return an array of objects. More...
 
 count ($p_where="", $p_array=null)
 
 delete ()
 
 exist ()
 Count the number of record with the id ,. More...
 
 from_array ($p_array)
 Transform an array into object. More...
 
 get ($p_string)
 get the value thanks the colum name and not the alias (name). More...
 
 get_cn ()
 
 get_info ()
 
 get_name ()
 
 get_object ($p_ret, $idx)
 
 get_pk_value ()
 
 get_primary_key ()
 
 get_type ()
 
 getp ($p_string)
 set the value thanks the alias name instead of the colum name More...
 
 insert ()
 
 load ()
 Load the current row return false if not found. More...
 
 next ($ret, $i)
 get_seek return the next object, the return of the query must have all the column of the object More...
 
 save ()
 Insert or update : if the row already exists, update otherwise insert. More...
 
 seek ($cond='', $p_array=null)
 retrieve array of object thanks a condition More...
 
 set ($p_string, $p_value)
 set the value thanks the colum name and not the alias (name) More...
 
 set_cn ($cn)
 
 set_name ($name)
 
 set_pk_value ($p_value)
 
 set_primary_key ($primary_key)
 
 set_type ($type)
 
 setp ($p_string, $p_value)
 set the value thanks the alias name instead of the colum name More...
 
 to_array ($prefix="")
 Turn an object (row) into an array, and the key could be prefixed with $prefix. More...
 
 to_row ($p_array)
 turns a row fetched from the DB into a SQL object in updating all his attribute More...
 
 update ()
 
 verify ()
 

Additional Inherited Members

- Data Fields inherited from Data_SQL
 $cn
 
 $date_format
 Type of the data. More...
 
 $default
 defaullt date format More...
 
 $name
 Database connection. More...
 
 $primary_key
 Array of logical and real name. More...
 
 $table
 
 $type
 Column name of the primary key. More...
 

Detailed Description

Interface : this wrapper is used to created easily a wrapper to a table (ORM) You must create a class extending this one, in the constructor these variables have to be defined.

Match a table or a view into an object, you need to add the code for each table.

After you call the parent constructor

Note
the view or the table must include an unique key, otherwise the load doesn't work.
: the primary key must be an integer
class table_name_sql extends Table_Data_SQL
{
function __construct($p_id=-1)
{
$this->table = "schema.table";
$this->primary_key = "o_id";
$this->name=array(
"id"=>"o_id",
"program"=>"o_prog",
"date"=>"o_date",
"qcode"=>"o_qcode",
"fiche"=>"f_id",
);
$this->type = array(
"o_id"=>"numeric",
"o_prog"=>"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);
}
}
$from_poste name
$input_from type
Definition: balance.inc.php:65
Interface : this wrapper is used to created easily a wrapper to a table (ORM) You must create a class...
__construct($p_cn, $p_id=-1)
$all table

Definition at line 89 of file table_data_sql.class.php.

Constructor & Destructor Documentation

◆ __construct()

Table_Data_SQL::__construct (   $p_cn,
  $p_id = -1 
)

Reimplemented in Stock_Goods_Sql, Stock_Change_Sql, Stock_Sql, Profile_Menu, Anc_Key_SQL, Anc_Key_Activity_SQL, and Tmp_Pcmn_SQL.

Definition at line 93 of file table_data_sql.class.php.

94 {
95 parent::__construct($p_cn, $p_id);
96
97 }

References $p_id.

Member Function Documentation

◆ build_query()

Table_Data_SQL::build_query ( )

Build the SQL select statement for querying the object and returns it.

Returns
string Query of the object

Definition at line 257 of file table_data_sql.class.php.

258 {
259 $sql=" select ";
260 $sep="";
261 foreach ($this->name as $key) {
262 switch ($this->type[$key])
263 {
264 case "date":
265 $sql .= $sep.'to_char('.$key.",'".$this->date_format."') as ".$key;
266 break;
267 default:
268 $sql.=$sep.$key;
269 }
270 $sep=",";
271 }
273 $sql.=" from ".$this->table;
274
275 $sql.=" where ".$this->primary_key." = $1";
276
277 return $sql;
278 }
$primary_key
Array of logical and real name.

References Data_SQL\$primary_key, $sep, $sql, name, and type.

Referenced by load().

◆ column_update()

Table_Data_SQL::column_update (   $column_expr,
  $value 
)

update the value of a column with an expression for $value for the current record

Parameters
$column_expstring like column = $1 or column=function($1)
$valuevalue replacing $1
Note
you can use it for entering a date with the hour and minute like this $this->column_update("field_date = to_date($1,'DD.MM.YY HH24:MI'),date('d.m.Y H:i'))
Returns
Table_Data_SQL

Definition at line 152 of file table_data_sql.class.php.

152 {
154 $sql="update ".$this->table." set {$column_expr} where {$this->primary_key} = $2";
155 $this->cn->exec_sql($sql,[$value,$this->$pk]);
156 return $this;
157
158 }
$input_from cn
Definition: balance.inc.php:66

References Data_SQL\$primary_key, $sql, $value, and cn.

◆ count()

Table_Data_SQL::count (   $p_where = "",
  $p_array = null 
)

return the number of count in the table corresponding to the where condition

Parameters
string$p_wherethe condition appended to the SQL select query , where must be given
array$p_arrayvariable from the $p_where condition
Returns
type

Reimplemented from Data_SQL.

Definition at line 239 of file table_data_sql.class.php.

239 {
240 $count=$this->cn->get_value("select count(*) from $this->table ".$p_where,$p_array);
241 return $count;
242 }
$count

References $count, $p_array, and cn.

Referenced by get_all_to_array(), and Stock_Goods\take_last_inventory().

◆ delete()

Table_Data_SQL::delete ( )

Reimplemented from Data_SQL.

Definition at line 137 of file table_data_sql.class.php.

138 {
140 $sql=" delete from ".$this->table." where ".$this->primary_key."= $1";
141 $this->cn->exec_sql($sql,array($this->$pk));
142 }

References Data_SQL\$primary_key, $sql, and cn.

◆ exist()

Table_Data_SQL::exist ( )

Count the number of record with the id ,.

Returns
integer 0 doesn't exist , 1 exists

Reimplemented from Data_SQL.

Definition at line 247 of file table_data_sql.class.php.

247 {
249 $count=$this->cn->get_value("select count(*) from ".$this->table." where ".$this->primary_key."=$1",array($this->$pk));
250 return $count;
251 }

References $count, Data_SQL\$primary_key, cn, and table.

◆ get_all_to_array()

Table_Data_SQL::get_all_to_array (   $p_key_col,
  $p_cond = "",
  $p_array = NULL 
)

Get all the row and use the p_key_code are the key value of array.

The key column is usually the primary key or any unique key. the returns looks like

[ID1]=>array( ["PRIMARYKEY"=>"ID1"
, "VALUE" => 2]);
[ID2]=>array( ["PRIMARYKEY"=>"ID2"
, "VALUE" => 2]);
Note
It should be used only for small tables: the array is build row by row
Parameters
string$p_key_colexisting and unique key
string$p_condsql cond
array$p_arrayarray of value for the SQL condition

Definition at line 294 of file table_data_sql.class.php.

295 {
296 $ret=$this->seek($p_cond, $p_array);
297 if ($ret==FALSE)
298 return array();
299 $a_array=Database::fetch_all($ret);
300 $nb_array=count($a_array);
301 $a_result=array();
302 try
303 {
304 for ($i=0; $i<$nb_array; $i++)
305 {
306 if (!isset($a_array[$i][$p_key_col]))
307 {
308 throw new Exception("col not found ".$p_key_col);
309 }
310 $key=$a_array[$i][$p_key_col];
311 if ( isset ($a_result[$key]) ){
312 throw new Exception ("duplicate found col : $key");
313 }
314 $a_result[$key]=$a_array[$i];
315 }
316 }
317 catch (Exception $exc)
318 {
319 echo $exc->getMessage();
320 record_log($exc->getMessage());
321 record_log($exc->getTraceAsString());
322 throw $exc;
323 }
324 return $a_result;
325 }
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
static fetch_all($ret)
wrapper for the function pg_fetch_all
count($p_where="", $p_array=null)
return the number of count in the table corresponding to the where condition
seek($cond='', $p_array=null)
retrieve array of object thanks a condition

References $i, $nb_array, $p_array, $ret, count(), DatabaseCore\fetch_all(), record_log(), and seek().

+ Here is the call graph for this function:

◆ insert()

Table_Data_SQL::insert ( )

Reimplemented from Data_SQL.

Definition at line 100 of file table_data_sql.class.php.

101 {
102 $this->verify();
103 $sql="insert into ".$this->table." ( ";
104 $sep="";
105 $par="";
106 $idx=1;
107 $array=array();
108 foreach ($this->name as $key=> $value)
109 {
110 if (isset($this->default[$value])&&$this->default[$value]=="auto"&&$this->$value==null)
111 continue;
112 // prob. with table when the pk is not auto.
113 if ($value==$this->primary_key && $this->$value==-1 && isset($this->default[$value]) )
114 continue;
115 $sql.=$sep.$value;
116 switch ($this->type[$value])
117 {
118 case "date":
119 if ($this->date_format=="")
120 throw new Exception('Format Date invalide');
121 $par .=$sep.'to_timestamp($'.$idx.",'".$this->date_format."')";
122 break;
123 default:
124 $par .= $sep."$".$idx;
125 }
126
127 $array[]=$this->$value;
128 $sep=",";
129 $idx++;
130 }
131 $sql.=") values (".$par.") returning ".$this->primary_key;
133 $returning=$this->cn->get_value($sql, $array);
134 $this->$pk=$returning;
135 }
$idx

References $array, $idx, Data_SQL\$primary_key, $sep, $sql, $value, cn, name, type, and Data_SQL\verify().

+ Here is the call graph for this function:

◆ load()

Table_Data_SQL::load ( )

Load the current row return false if not found.

if ( $this->get_limit_fiche_qcode() != 0 )
{
$sql=sprintf($this->sql," limit ".$this->get_limit_fiche_qcode());
} else
{
$sql=sprintf($this->sql," ");
}
$result=$this->cn->get_array($sql. " where id=$1",array ($this->$pk));
if ($this->cn->count()==0)
{
$this->$pk=-1;
return false;
}
foreach ($result[0] as $key=> $value)
{
$this->$key=$value;
}
return true;
Returns
bool

Reimplemented from Data_SQL.

Definition at line 192 of file table_data_sql.class.php.

192 :bool
193 {
194 $sql=$this->build_query();
196 // primary cannot be null or empty
197 if (trim($this->$pk??"")==="" || $this->$pk===null) {
198 $this->pk=-1;
199 return false;
200 }
201
202 $result=$this->cn->get_array($sql,array ($this->$pk));
203 if ($this->cn->count()==0)
204 {
205 $this->$pk=-1;
206 return false;
207 }
208
209 foreach ($result[0] as $key=> $value)
210 {
211 $this->$key=$value;
212 }
213 return true;
214 }
build_query()
Build the SQL select statement for querying the object and returns it.

References Data_SQL\$primary_key, $result, $sql, $value, build_query(), and cn.

+ Here is the call graph for this function:

◆ seek()

Table_Data_SQL::seek (   $cond = '',
  $p_array = null 
)

retrieve array of object thanks a condition

Parameters
$condcondition (where clause) (optional by default all the rows are fetched) you can use this parameter for the order or subselect
$p_arrayarray for the SQL stmt
See also
Database::exec_sql get_object Database::num_row
Returns
the return value of exec_sql

Reimplemented from Data_SQL.

Definition at line 225 of file table_data_sql.class.php.

226 {
227 $sql="select * from ".$this->table." $cond";
228 $ret=$this->cn->exec_sql($sql, $p_array);
229 return $ret;
230 }

References $p_array, $ret, $sql, and cn.

Referenced by get_all_to_array().

◆ update()

Table_Data_SQL::update ( )

Reimplemented from Data_SQL.

Definition at line 159 of file table_data_sql.class.php.

160 {
161 $this->verify();
163 $sql="update ".$this->table." ";
164 $sep="";
165 $idx=1;
166 $array=array();
167 $set=" set ";
168 foreach ($this->name as $key=> $value) {
169 if (isset($this->default[$value])&&$this->default[$value]=="auto")
170 continue;
171 switch ($this->type[$value])
172 {
173 case "date":
174 $par=$value.'=to_timestamp($'.$idx.",'".$this->date_format."')";
175 break;
176 default:
177 $par=$value."= $".$idx;
178 }
179 $sql.=$sep." $set ".$par;
180 $array[]=$this->$value;
181 $sep=",";
182 $set="";
183 $idx++;
184 }
185 $array[]=$this->$pk;
186 $sql.=" where ".$this->primary_key." = $".$idx;
187 $this->cn->exec_sql($sql, $array);
188 }

References $array, $idx, Data_SQL\$primary_key, $sep, $sql, $value, cn, name, type, and Data_SQL\verify().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: