noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
Data_SQL Class Reference

this an abstract class , all the SQL class, like noalyss_sql (table), Acc_Plan_SQL (based on a SQL not a table) or a view. More...

+ Inheritance diagram for Data_SQL:
+ Collaboration diagram for Data_SQL:

Public Member Functions

 __construct (DatabaseCore $p_cn, $p_id=-1)
 
 __get ($cols)
 
 __set ($cols, $p_value)
 
 __toString ()
 
 collect_objects ($cond='', $p_array=null)
 return an array of objects.
 
 count ($p_where="", $p_array=null)
 
 delete ()
 
 exist ()
 Count the number of record with the id ,.
 
 from_array ($p_array)
 Transform an array into object.
 
 get ($cols)
 get the value thanks the colum name and not the alias (name).
 
 get_a_virtual_col ()
 return array of virtual cols (alias calculated, formatted cols)
 
 get_cn ()
 
 get_info ()
 
 get_name ()
 
 get_object ($p_ret, $idx)
 
 get_pk_value ()
 
 get_primary_key ()
 
 get_type ()
 
 getp ($cols)
 set the value thanks the alias name instead of the colum name if not found try the column name
 
 insert ()
 
 load ()
 Load the current row return false if not found.
 
 next ($ret, $i)
 get_seek return the next object, the return of the query must have all the column of the object including the virtual columns
 
 save ()
 Insert or update : if the row already exists, update otherwise insert.
 
 seek ($cond='', $p_array=null)
 retrieve array of object thanks a condition
 
 set ($cols, $p_value)
 set the value thanks the colum name and not the alias (name)
 
 set_cn ($cn)
 
 set_name ($name)
 
 set_pk_value ($p_value)
 
 set_primary_key ($primary_key)
 
 set_type ($type)
 
 set_virtual_col ($col_name, $sql_expression)
 add a virtual column (formatted column, sum of 2 col, ...)
 
 setp ($cols, $p_value)
 set the value thanks the alias name instead of the colum name, if not found try the column name
 
 to_array ($prefix="")
 Turn an object (row) into an array, and the key could be prefixed with $prefix.
 
 to_row ($p_array)
 update the data member of current object with the value from the array.
 
 update ()
 
 verify ()
 

Data Fields

 $cn
 
 $date_format
 Type of the data.
 
 $default
 defaullt date format
 
 $name
 Database connection.
 
 $primary_key
 Array of logical and real name.
 
 $table
 
 $type
 Column name of the primary key.
 

Protected Attributes

 $a_virtual_col
 

Detailed Description

this an abstract class , all the SQL class, like noalyss_sql (table), Acc_Plan_SQL (based on a SQL not a table) or a view.

You must create a class extending this one, in the constructor these variables have to be defined

  • table = name of the view or empty
  • sql = sql statement
  • 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

if you give a SQL or a View you have to give a primary key, usually , the best is to use a key composed of different PK of the tables Example : in this SQL (or view) the PK is id and it is composed with the PK f_id and sg_id , remember that the pk cannot be null and must be unique ! For SQL , the value is computed , so you need a subselect like this

select
ssw.sg_id::text||'-'||vfp.f_id::text id,
vfp.f_id,vfp.f_enable,vfp.person_name ,vfp.person_fname ,vfp.person_qcode ,ssw.sg_id
from rash.vw_fiche_person vfp
join rash.security_social_worker ssw using(f_id) ;
$anc_grandlivre from
$input_from id

For SQL , the value is computed , so you need a subselect like this

select * from (
select
ssw.sg_id::text||'-'||vfp.f_id::text id,
vfp.f_id,vfp.f_enable,vfp.person_name ,vfp.person_fname ,vfp.person_qcode ,ssw.sg_id
from rash.vw_fiche_person vfp
join rash.security_social_worker ssw using(f_id))sub1
@encode
After you call the parent constructor
@note the view or the table must include an unique key, otherwise the load
doesn't work.
@pnote
Name is an array the key is the logical name and the value is the name of the column
@code
$this->name=array(
"id"=>"o_id",
"program"=>"o_prog",
"date"=>"o_date",
"qcode"=>"o_qcode",
"fiche"=>"f_id",
);
load()
Load the current row return false if not found.
$all table

the type is an array , key = column name , value = type

$this->type = array(
"o_id"=>"numeric",
"o_prog"=>"numeric",
"o_date"=>"date",
"o_qcode"=>"text",
"f_id"=>"numeric",
);
$input_from type

Definition at line 98 of file data_sql.class.php.

Constructor & Destructor Documentation

◆ __construct()

Data_SQL::__construct ( DatabaseCore $p_cn,
$p_id = -1 )

Reimplemented in Acc_Other_Tax_SQL, Action_Gestion_Comment_SQL, Action_Gestion_SQL, Attr_Def_SQL, Contact_option_ref_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_def_SQL, Jrn_Note_SQL, Jrn_periode_SQL, Jrn_Tax_SQL, Op_Predef_SQL, Operation_Exercice_Detail_SQL, Operation_Exercice_SQL, Parameter_Extra_SQL, Parm_periode_SQL, Payment_method_SQL, Poste_analytique_SQL, Profile_Mobile_SQL, Quant_Fin_SQL, Quant_Purchase_SQL, Quant_Sold_SQL, Tag_group_SQL, Tva_Rate_SQL, User_filter_SQL, and V_Tva_Rate_SQL.

Definition at line 124 of file data_sql.class.php.

125 {
126 $this->cn=$p_cn;
128 $this->$pk=$p_id;
129 $this->a_virtual_col=array();
130 // check that the definition is correct
131 if (count($this->name) != count($this->type) ){
132 throw new Exception (__FILE__." $this->table Cannot instantiate");
133 }
134 // forbid the use of a column named type , date_format, name or primary_key to avoid conflict
135
136 /* Initialize an empty object */
137 foreach ($this->name as $key)
138 {
139 if ( in_array($key,['name','type','format_date','cn','date_format','default'] ) ) {
140 throw new Exception ('DATASQL-94 invalid column name'.$key,94);
141 }
142 $this->$key=null;
143 }
144 $this->$pk=$p_id;
145 /* load it , if the pk id doesn't exist, it will be turned into -1 */
146 if ($p_id != -1 )$this->load();
147 if ( empty($this->date_format) ) $this->date_format="DD.MM.YYYY";
148 }
$input_from cn
$from_poste name
$primary_key
Array of logical and real name.
count($p_where="", $p_array=null)

References $p_id, $primary_key, cn, count(), load(), name, and type.

+ Here is the call graph for this function:

Member Function Documentation

◆ __get()

Data_SQL::__get ( $cols)

Definition at line 265 of file data_sql.class.php.

265 {
266 if (array_key_exists($cols, $this->type)) {
267 return $this->$cols;
268 }
269 if (! empty ($this->a_virtual_col) && array_key_exists($cols, $this->a_virtual_col))
270 {
271 return $this->$cols;
272 }
273 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
274 }
const EXC_DATA_SQL
Definition constant.php:360

References EXC_DATA_SQL, and type.

◆ __set()

Data_SQL::__set ( $cols,
$p_value )

Definition at line 252 of file data_sql.class.php.

252 {
253 if (array_key_exists($cols, $this->type)) {
254 $this->$cols=$p_value;
255 return $this;
256 }
257 if ( ! empty ($this->a_virtual_col) && array_key_exists($cols, $this->a_virtual_col))
258 {
259 $this->$cols=$p_value;
260 return $this;
261 }
262
263 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
264 }

References EXC_DATA_SQL, and type.

◆ __toString()

Data_SQL::__toString ( )

Reimplemented in Table_Data_SQL.

Definition at line 108 of file data_sql.class.php.

108 : string
109 {
110 $ret=" members: ";
111 foreach ($this->name as $name) {
112 $ret.="[ $name => {$this->$name} ]";
113 }
114
115 $ret.="| type ".print_r($this->type,true);
116 $ret.="| default ".print_r($this->default,true);
117 $ret.="| primary key ".$this->primary_key;
118 $ret.="| date_format ".$this->date_format;
119 $ret.="| a_virtual_col".var_export($this->a_virtual_col,true);
120 return $ret;
121 }
$name
Database connection.

References $name, $ret, name, and type.

◆ collect_objects()

Data_SQL::collect_objects ( $cond = '',
$p_array = null )

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.

Parameters
$condcondition, order...
$p_arrayarray to use for a condition
Note
this function could slow down your application.

Definition at line 454 of file data_sql.class.php.

455 {
456 if ($p_array != null && ! is_array($p_array) )
457 {
458 throw new Exception(_("Erreur : exec_sql attend un array"));
459 }
460 $ret=$this->seek($cond, $p_array);
462 $a_return=array();
463 for ($i=0; $i<$max; $i++)
464 {
465 $x=clone $this->next($ret, $i);
466
467 $a_return[$i]=$x;
468 }
469 return $a_return;
470 }
_("actif, passif,charge,...")
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
next($ret, $i)
get_seek return the next object, the return of the query must have all the column of the object inclu...
static num_row($ret)
wrapper for the function pg_num_rows

References $i, $max, $p_array, $ret, $x, _, next(), DatabaseCore\num_row(), and seek().

+ Here is the call graph for this function:

◆ count()

Data_SQL::count ( $p_where = "",
$p_array = null )
abstract

Reimplemented in Acc_Plan_SQL, Table_Data_SQL, V_Contact_SQL, and V_Currency_Last_Value_SQL.

References $p_array.

Referenced by __construct().

◆ delete()

Data_SQL::delete ( )
abstract

◆ exist()

Data_SQL::exist ( )
abstract

Count the number of record with the id ,.

Returns
integer 0 doesn't exist , 1 exists

Reimplemented in Acc_Plan_SQL, Table_Data_SQL, V_Contact_SQL, and V_Currency_Last_Value_SQL.

Referenced by save().

◆ from_array()

Data_SQL::from_array ( $p_array)

Transform an array into object.

Parameters
type$p_array
Returns
object

Definition at line 344 of file data_sql.class.php.

345 {
346 foreach ($this->name as $key=> $value)
347 {
348 if (isset($p_array[$value]))
349 {
350 $this->$value=$p_array[$value];
351 }
352 else
353 {
354 $this->$value=null;
355 }
356 }
357 return $this;
358 }

References $p_array, $value, and name.

Referenced by next().

◆ get()

Data_SQL::get ( $cols)

get the value thanks the colum name and not the alias (name).

See also
getp

Definition at line 184 of file data_sql.class.php.

185 {
186 if (array_key_exists($cols, $this->type)) {
187 return $this->$cols;
188 }
189 if (array_key_exists($cols, $this->a_virtual_col))
190 {
191 return $this->$cols;
192 }
193 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
194 }

References EXC_DATA_SQL, and type.

◆ get_a_virtual_col()

Data_SQL::get_a_virtual_col ( )

return array of virtual cols (alias calculated, formatted cols)

Returns
array

Definition at line 165 of file data_sql.class.php.

165 {
167 }

References $a_virtual_col.

◆ get_cn()

Data_SQL::get_cn ( )

Definition at line 478 of file data_sql.class.php.

479 {
480 return $this->cn;
481 }

References $cn.

Referenced by Operation_Exercice\input_row().

◆ get_info()

Data_SQL::get_info ( )

Definition at line 321 of file data_sql.class.php.

322 {
323 return var_export($this, true);
324 }

◆ get_name()

Data_SQL::get_name ( )

Definition at line 483 of file data_sql.class.php.

484 {
485 return $this->name;
486 }

References $name.

◆ get_object()

Data_SQL::get_object ( $p_ret,
$idx )
See also
next

Definition at line 441 of file data_sql.class.php.

442 {
443 return $this->next($p_ret, $idx);
444 }

References $idx, and next().

+ Here is the call graph for this function:

◆ get_pk_value()

Data_SQL::get_pk_value ( )

Reimplemented in Acc_Plan_SQL.

Definition at line 286 of file data_sql.class.php.

287 {
289 return $this->$pk;
290 }

References $primary_key.

◆ get_primary_key()

Data_SQL::get_primary_key ( )

Definition at line 488 of file data_sql.class.php.

489 {
490 return $this->primary_key;
491 }

References $primary_key.

◆ get_type()

Data_SQL::get_type ( )

Definition at line 493 of file data_sql.class.php.

494 {
495 return $this->type;
496 }
$type
Column name of the primary key.

References $type.

◆ getp()

Data_SQL::getp ( $cols)

set the value thanks the alias name instead of the colum name if not found try the column name

See also
get

Definition at line 215 of file data_sql.class.php.

216 {
217 if (array_key_exists($cols, $this->name)) {
218 $idx=$this->name[$cols];
219 return $this->$idx;
220 }
221 if (array_key_exists($cols, $this->type)) {
222 return $this->$cols;
223 }
224 if (array_key_exists($cols, $this->a_virtual_col))
225 {
226 return $this->$cols;
227 }
228
229 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
230 }

References $idx, EXC_DATA_SQL, name, and type.

Referenced by Operation_Exercice\input_row().

◆ insert()

Data_SQL::insert ( )
abstract

◆ load()

Data_SQL::load ( )
abstract

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 in Acc_Plan_SQL, Table_Data_SQL, V_Contact_SQL, and V_Currency_Last_Value_SQL.

Referenced by __construct().

◆ next()

Data_SQL::next ( $ret,
$i )

get_seek return the next object, the return of the query must have all the column of the object including the virtual columns

Parameters
$p_retis the return value of an exec_sql
$idxis the index
See also
seek
Returns
object

Definition at line 421 of file data_sql.class.php.

422 {
423 $array=$this->cn->fetch_array($ret, $i);
424 $this->from_array($array);
425 if ( ! empty ($this->a_virtual_col )) {
426 $a_column= array_keys($this->a_virtual_col);
427 foreach( $a_column as $column){
428 if ( isset ($array[$column] )) {
429 $this->$column = $array[$column];
430 } else {
431 $this->$column = null;
432 }
433 }
434 }
435 return $this;
436 }
from_array($p_array)
Transform an array into object.

References $array, $i, $ret, cn, and from_array().

Referenced by collect_objects(), and get_object().

+ Here is the call graph for this function:

◆ save()

Data_SQL::save ( )

Insert or update : if the row already exists, update otherwise insert.

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

153 {
154 $count = $this->exist();
155
156 if ($count == 0)
157 $this->insert();
158 else
159 $this->update();
160 }
exist()
Count the number of record with the id ,.
$count

References $count, exist(), insert(), and update().

+ Here is the call graph for this function:

◆ seek()

Data_SQL::seek ( $cond = '',
$p_array = null )
abstract

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 in Acc_Plan_SQL, Table_Data_SQL, V_Contact_SQL, and V_Currency_Last_Value_SQL.

References $p_array.

Referenced by collect_objects().

◆ set()

Data_SQL::set ( $cols,
$p_value )

set the value thanks the colum name and not the alias (name)

See also
setp

Definition at line 200 of file data_sql.class.php.

201 {
202 if (array_key_exists($cols, $this->type)) {
203 $this->$cols=$p_value;
204 return $this;
205 } else
206 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
207
208 }

References EXC_DATA_SQL, and type.

◆ set_cn()

Data_SQL::set_cn ( $cn)

Definition at line 498 of file data_sql.class.php.

499 {
500 $this->cn=$cn;
501 return $this;
502 }

References $cn, and cn.

◆ set_name()

Data_SQL::set_name ( $name)
Parameters
string$name
Returns
$this

Definition at line 508 of file data_sql.class.php.

509 {
510 $this->name=$name;
511 return $this;
512 }

References $name, and name.

◆ set_pk_value()

Data_SQL::set_pk_value ( $p_value)

Definition at line 281 of file data_sql.class.php.

282 {
284 $this->$pk=$p_value;
285 }

References $primary_key.

◆ set_primary_key()

Data_SQL::set_primary_key ( $primary_key)
Parameters
string$primary_key
Returns
$this

Definition at line 518 of file data_sql.class.php.

519 {
520 $this->primary_key=$primary_key;
521 return $this;
522 }

References $primary_key.

◆ set_type()

Data_SQL::set_type ( $type)
Parameters
array$type
Returns
$this

Definition at line 528 of file data_sql.class.php.

529 {
530 $this->type=$type;
531 return $this;
532 }

References $type, and type.

◆ set_virtual_col()

Data_SQL::set_virtual_col ( $col_name,
$sql_expression )

add a virtual column (formatted column, sum of 2 col, ...)

Parameters
$col_name(string) name of the column , will be use in get , getp
$sql_expression(string) SQL Expression for the column like "to_char(col1,'DD.MM.YY HH24:MI:SS')", col1+col2, ...
Note
sql expression and col_name must be valid , there is no futher check
Returns
void

Definition at line 177 of file data_sql.class.php.

177 {
178 $this->a_virtual_col[$col_name]=$sql_expression;
179 }

◆ setp()

Data_SQL::setp ( $cols,
$p_value )

set the value thanks the alias name instead of the colum name, if not found try the column name

See also
set

Definition at line 237 of file data_sql.class.php.

238 {
239 if (array_key_exists($cols, $this->name)) {
240 $idx=$this->name[$cols];
241 $this->$idx=$p_value;
242 return $this;
243 }
244 if (array_key_exists($cols, $this->type)) {
245 $this->$cols=$p_value;
246 return $this;
247 }
248
249 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
250 }

References $idx, EXC_DATA_SQL, name, and type.

◆ to_array()

Data_SQL::to_array ( $prefix = "")

Turn an object (row) into an array, and the key could be prefixed with $prefix.

Parameters
string$prefixbefore the key
Returns
array

Definition at line 365 of file data_sql.class.php.

366 {
367 $array=array();
368 foreach ($this->name as $key=> $value)
369 {
370 $nkey=$prefix.$key;
371 $array[$key]=$this->$key;
372 }
373 if ( ! empty ($this->a_virtual_col )) {
374 $a_column= array_keys($this->a_virtual_col);
375 foreach( $a_column as $column){
376 $array[$column]=$this->a_virtual_col[$column];
377 }
378 }
379 return $array;
380 }

References $array, $prefix, $value, and name.

◆ to_row()

Data_SQL::to_row ( $p_array)

update the data member of current object with the value from the array.

includes the virtual column, usefull if need to update several columns in once

Parameters
$p_array(array) associative key = column_vale, value new value for this col.
Returns
void

Definition at line 388 of file data_sql.class.php.

388 {
389 foreach ($this->name as $name) {
390 if (isset ($p_array[$name])) {
391 $this->$name=$p_array[$name];
392 }
393 }
394 if ( ! empty ($this->a_virtual_col )) {
395 $a_column= array_keys($this->a_virtual_col);
396 foreach( $a_column as $column){
397 if ( isset ($p_array[$column])) {
398 $this->$column = $p_array[$column];
399 }
400 }
401 }
402 }

References $name, $p_array, and name.

Referenced by V_Contact_SQL\load().

◆ update()

Data_SQL::update ( )
abstract

◆ verify()

Data_SQL::verify ( )
Todo
ajout vérification type (date, text ou numeric)
Returns
int

Reimplemented in Menu_Ref.

Definition at line 329 of file data_sql.class.php.

330 {
331 foreach ($this->name as $key)
332 {
333 if (noalyss_trim($this->$key)=='')
334 $this->$key=null;
335 }
336 return 0;
337 }
noalyss_trim($p_string)

References name, and noalyss_trim().

Referenced by Table_Data_SQL\insert(), and Table_Data_SQL\update().

+ Here is the call graph for this function:

Field Documentation

◆ $a_virtual_col

Data_SQL::$a_virtual_col
protected

Definition at line 107 of file data_sql.class.php.

Referenced by get_a_virtual_col().

◆ $cn

◆ $date_format

Data_SQL::$date_format

Type of the data.

Definition at line 104 of file data_sql.class.php.

◆ $default

Data_SQL::$default

defaullt date format

Definition at line 105 of file data_sql.class.php.

◆ $name

Data_SQL::$name

Database connection.

Definition at line 101 of file data_sql.class.php.

Referenced by __toString(), Table_Data_SQL\__toString(), get_name(), set_name(), and to_row().

◆ $primary_key

◆ $table

Data_SQL::$table

Definition at line 106 of file data_sql.class.php.

◆ $type

Data_SQL::$type

Column name of the primary key.

Definition at line 103 of file data_sql.class.php.

Referenced by get_type(), and set_type().


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