<?php
// DbSQL.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Database;use Docuform\Lib\ApiUuid;class DbSQL{public const cst_auto=0;public const cst_date=3;public const cst_text=4;public const cst_intornull=5;public const cst_numeric=6;public const cst_bool=7;public const cst_uuid=8;private $m_database;private $m_fields;private $m_params;public function __construct(DbPDO $pdo){$this->m_database=$pdo;$this->clear();}public function clear():void{$this->m_fields=[];$this->m_params=[];}public static function addCondition(string &$condition,string $add,string $operator='AND'):void{$condition=empty($condition)?'':$condition.' '.$operator.' ';$condition.=$add;}public function getInsertValues():string{$Result='';if(count($this->m_fields)){$Cols=implode(',',$this->m_fields);$Vals=implode(',',array_fill(0,count($this->m_fields),'?'));$Result='('.$Cols.') VALUES ('.$Vals.')';}return $Result;}public function getUpdateSet():string{$Result='';if(count($this->m_fields)){$Result=implode('=?,',$this->m_fields).'=?';}return $Result;}public function getParams($params=[]):array{$this->m_params=array_merge($this->m_params,$params);return $this->m_params;}public function append($field,$type=DbSQL::cst_text,$value=''):void{if($field!=''){$key=array_search($field,$this->m_fields,false);if($key!==false){unset($this->m_fields[$key],$this->m_params[$key]);}$this->m_fields[]=$field;switch($type){case self::cst_date:$this->m_params[]=$value;break;default:case self::cst_text:$this->m_params[]=trim((string)$value);break;case self::cst_intornull:$this->m_params[]=$value===null?$value:(int)$value;break;case self::cst_numeric:$this->m_params[]=$this->m_database->localizeFloatValue($value);break;case self::cst_bool:if(isset($value)){$this->m_params[]=($value==false?0:1);}else{$this->m_params[]=0;}break;case self::cst_uuid:$this->m_params[]=ApiUuid::toHex(trim((string)$value));break;}}}}