Noalyss_SQL Create a function build query

This commit is contained in:
Dany De Bontridder 2019-09-04 21:11:37 +02:00
parent 72342dbec2
commit d0b2543669

View file

@ -87,6 +87,7 @@ 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;
@ -104,6 +105,7 @@ abstract class Noalyss_SQL extends Data_SQL
$this->$pk=$p_id;
/* load it */
if ($p_id != -1 )$this->load();
}
/**
* Insert or update : if the row already exists, update otherwise insert
@ -255,23 +257,8 @@ abstract class Noalyss_SQL extends Data_SQL
public function load()
{
$sql=" select ";
$sep="";
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=",";
}
$sql=$this->build_query();
$pk=$this->primary_key;
$sql.=" from ".$this->table;
$sql.=" where ".$this->primary_key." = $1";
$result=$this->cn->get_array($sql,array ($this->$pk));
if ($this->cn->count()==0)
@ -412,6 +399,33 @@ abstract class Noalyss_SQL extends Data_SQL
$count=$this->cn->get_value("select count(*) from ".$this->table." where ".$this->primary_key."=$1",array($this->$pk));
return $count;
}
/**
* Build the SQL select statement for querying the object and returns it
* @return string Query of the object
*/
public function build_query()
{
$sql=" select ";
$sep="";
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." = $1";
return $sql;
}
}
?>