Data_SQL and Table_Data_SQL add virtual column, to allow transformation

of columns in SQL, faster than transforming in PHP afterward
This commit is contained in:
sparkyx 2025-05-21 11:15:13 +02:00
parent d9b1e4b2d2
commit bfdae33f16
5 changed files with 481 additions and 30 deletions

View file

@ -88,15 +88,29 @@
#[AllowDynamicProperties]
abstract class Table_Data_SQL extends Data_SQL
{
function __construct($p_cn, $p_id=-1)
{
parent::__construct($p_cn, $p_id);
}
public function __toString(): string
{
$ret=" members : ";
foreach ($this->name as $name) {
$ret.="[ $name => {$this->$name} ]";
}
$ret.="| type ".var_export($this->type,true);
$ret.="| default ".var_export($this->default,true);
$ret.="| primary key ".$this->primary_key;
$ret.="| date_format ".$this->date_format;
$ret.="| a_virtual_col".var_export($this->a_virtual_col,true);
return $ret;
}
public function insert()
{
$this->verify();
@ -156,6 +170,9 @@ abstract class Table_Data_SQL extends Data_SQL
return $this;
}
/**
* @brief update the row but not the column with a default value
*/
public function update()
{
$this->verify();
@ -191,22 +208,23 @@ abstract class Table_Data_SQL extends Data_SQL
*/
public function load():bool
{
$sql=$this->build_query();
$pk=$this->primary_key;
// primary cannot be null or empty
if (trim($this->$pk??"")==="" || $this->$pk===null) {
$this->$pk=-1;
return false;
}
$result=$this->cn->get_array($sql,array ($this->$pk));
$sql=$this->build_query();
$sql.=" where ".$this->primary_key." = $1";
$result=$this->cn->get_row($sql,array ($this->$pk));
if ($this->cn->count()==0)
{
$this->$pk=-1;
return false;
}
foreach ($result[0] as $key=> $value)
foreach ($result as $key=> $value)
{
$this->$key=$value;
}
@ -224,7 +242,8 @@ abstract class Table_Data_SQL extends Data_SQL
*/
function seek($cond='', $p_array=null)
{
$sql="select * from ".$this->table." $cond";
$sql=$this->build_query();
$sql.=" $cond ";
$ret=$this->cn->exec_sql($sql, $p_array);
return $ret;
}
@ -270,12 +289,22 @@ abstract class Table_Data_SQL extends Data_SQL
$sep=",";
}
$pk=$this->primary_key;
/**
* add virtual column
*/
if ( ! empty( $this->a_virtual_col)){
$nb_virtual_col=count($this->a_virtual_col);
$a_col= array_keys($this->a_virtual_col);
for ($x=0;$x<$nb_virtual_col ;$x++) {
$col=$a_col[$x];
$expr=sprintf("$sep %s as %s ",$this->a_virtual_col[$col], $col);
$sql.=" $expr ";
}
}
$sql.=" from ".$this->table;
$sql.=" where ".$this->primary_key." = $1";
return $sql;
}
/**
* @brief 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.
@ -316,9 +345,7 @@ abstract class Table_Data_SQL extends Data_SQL
}
catch (Exception $exc)
{
echo $exc->getMessage();
record_log($exc->getMessage());
record_log($exc->getTraceAsString());
record_log($exc);
throw $exc;
}
return $a_result;