From f1c0bf9f1db3a54ed19a45a12d51330c51219428 Mon Sep 17 00:00:00 2001 From: lode2k Date: Wed, 31 Mar 2021 15:01:17 +0200 Subject: [PATCH 1/4] adding unset for Property to Update too --- abstract/abstract_row.php | 1 + 1 file changed, 1 insertion(+) diff --git a/abstract/abstract_row.php b/abstract/abstract_row.php index 8c4ed2d..93b7995 100644 --- a/abstract/abstract_row.php +++ b/abstract/abstract_row.php @@ -180,6 +180,7 @@ public function __isset($sVar){ */ public function __unset($sVar){ unset($this->_tProperty[$sVar]); + unset($this->_tPropertyToUpdate[$sVar]); } /** * force l'id de l'enregistrement From 8ea204297c5dde866f6b140c1c6aa181eb71fd1e Mon Sep 17 00:00:00 2001 From: lode2k Date: Wed, 31 Mar 2021 15:01:33 +0200 Subject: [PATCH 2/4] add .gitignore for PHPStorm (.idea) --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c0c12a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ + From e6c272a0f24977bb7b7dc0a56eb68c869190c261 Mon Sep 17 00:00:00 2001 From: lode2k Date: Wed, 31 Mar 2021 15:02:14 +0200 Subject: [PATCH 3/4] add possibility of use transaction with a global variable (useTransaction) and the related fonction --- abstract/abstract_model.php | 38 ++++++++++++++++++++++++++ sgbd/sgbd_oracle.php | 53 +++++++++++++++++++++++++++++++------ 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/abstract/abstract_model.php b/abstract/abstract_model.php index e41f0e4..77567df 100644 --- a/abstract/abstract_model.php +++ b/abstract/abstract_model.php @@ -240,5 +240,43 @@ protected static function _getInstance($class) { } return self::$_tInstance[$class]; } + + + /** + * @param $request + * @return false|resource + * @throws Exception + */ + public function ociBindByName($request) + { + return $this->getSgbd()->ociBindByName($request); + } + + /** + * @return bool + * @throws Exception + */ + public function commit() + { + return $this->getSgbd()->commit(); + } + + /** + * @return bool + * @throws Exception + */ + public function rollback() + { + return $this->getSgbd()->rollback(); + } + + /** + * @return array|false + * @throws Exception + */ + public function getError() + { + return $this->getSgbd()->getError(); + } } diff --git a/sgbd/sgbd_oracle.php b/sgbd/sgbd_oracle.php index dd52638..4221694 100644 --- a/sgbd/sgbd_oracle.php +++ b/sgbd/sgbd_oracle.php @@ -157,16 +157,21 @@ public function getLastInsertId(){ return null; } - private function query($sReq){ - $this->connect(); - $this->sReq=$sReq; - // return oci_parse($this->_pDb,$sReq); + private function query($sReq) + { + $this->connect(); + $this->_sReq = $sReq; + $query = oci_parse($this->_pDb, $sReq); - $query = oci_parse($this->_pDb,$sReq); - oci_execute($query); - return $query; + if (isset($this->_tConfig[$this->_sConfig . '.useTransaction']) && $this->_tConfig[$this->_sConfig . '.useTransaction'] == 1) { + oci_execute($query, OCI_NO_AUTO_COMMIT); + } else { + oci_execute($query); + } + + return $query; + } - } public function quote($sVal){ if ( isset($this->_tConfig[$this->_sConfig.'.escapeDateField']) && $this->_tConfig[$this->_sConfig.'.escapeDateField']==1 && (preg_match('!^TO_DATE\(.*\)!', $sVal) || preg_match('!^TO_TIMESTAMP\(.*\)!', $sVal)) ) { return $sVal; @@ -177,5 +182,37 @@ public function getWhereAll(){ return '1=1'; } + /** + * @param $request + * @return false|resource + */ + public function ociBindByName($request) + { + return oci_parse($this->_pDb, $request); + } + + /** + * @return bool + */ + public function commit() + { + return oci_commit($this->_pDb); + } + + /** + * @return bool + */ + public function rollback() + { + return oci_rollback($this->_pDb); + } + + /** + * @return array|false + */ + public function getError() + { + return oci_error($this->_pDb); + } } From 56a03e1b1966d73f42eed012272b8d6726921573 Mon Sep 17 00:00:00 2001 From: lode2k Date: Thu, 2 Nov 2023 16:41:49 +0100 Subject: [PATCH 4/4] Add new oci_connect for Cloud without hostname parameter --- sgbd/sgbd_oracle.php | 409 +++++++++++++++++++++++++++---------------- 1 file changed, 258 insertions(+), 151 deletions(-) diff --git a/sgbd/sgbd_oracle.php b/sgbd/sgbd_oracle.php index 4221694..350c556 100644 --- a/sgbd/sgbd_oracle.php +++ b/sgbd/sgbd_oracle.php @@ -1,4 +1,5 @@ . */ -class sgbd_oracle extends abstract_sgbd{ - - public static function getInstance($sConfig){ - return self::_getInstance(__CLASS__,$sConfig); - } - - public function findMany($tSql,$sClassRow){ - $pRs=$this->query($this->bind($tSql)); - - if(empty($pRs)){ - return null; - } - - $tObj=array(); - while($tRow=oci_fetch_assoc($pRs)){ - $oRow=new $sClassRow($tRow); - if( (int)_root::getConfigVar('security.xss.model.enabled',0)==1 ){ - $oRow->enableCleaning(); - } - $tObj[]=$oRow; - } - return $tObj; - } - public function findManySimple($tSql,$sClassRow){ - $pRs=$this->query($this->bind($tSql)); - - if(empty($pRs)){ - return null; - } - - $tObj=array(); - while($oRow=oci_fetch_object($pRs)){ - $tObj[]=$oRow; - } - return $tObj; - } - public function findOne($tSql,$sClassRow){ - $pRs=$this->query($this->bind($tSql)); - - if(empty($pRs)){ - return null; - } - - $tRow=oci_fetch_assoc($pRs); - $oRow=new $sClassRow($tRow); - if( (int)_root::getConfigVar('security.xss.model.enabled',0)==1 ){ - $oRow->enableCleaning(); - } - - return $oRow; - } - public function findOneSimple($tSql,$sClassRow){ - $pRs=$this->query($this->bind($tSql)); - - if(empty($pRs)){ - return null; - } - - $oRow=oci_fetch_object($pRs); - - return $oRow; - } - public function execute($tSql){ - return $this->query($this->bind($tSql)); - } - - public function update($sTable,$tProperty,$twhere){ - $this->query('UPDATE '.$sTable.' SET '.$this->getUpdateFromTab($tProperty).' WHERE '.$this->getWhereFromTab($twhere)); - } - public function insert($sTable,$tProperty){ - $this->query('INSERT INTO '.$sTable.' '.$this->getInsertFromTab($tProperty)); - } - - public function delete($sTable,$twhere){ - $this->query('DELETE FROM '.$sTable.' WHERE '.$this->getWhereFromTab($twhere)); - } - - public function getListColumn($sTable){ - $pRs=$this->query(sgbd_syntax_oracle::getListColumn($sTable)); - $tCol=array(); - - if(empty($pRs)){ - return $tCol; - } - - while($tRow=oci_fetch_row($pRs)){ - $tCol[]=$tRow[0]; - } - - return $tCol; - } - public function getListTable(){ - $pRs=$this->query(sgbd_syntax_oracle::getListTable()); - $tCol=array(); - - if(empty($pRs)){ - return $tCol; - } - - while($tRow=oci_fetch_row($pRs)){ - $tCol[]=$tRow[0]; - } - return $tCol; - } - - private function connect(){ - if(empty($this->_pDb)){ - - if(isset($this->_tConfig[$this->_sConfig.'.character_set'])){ - if( ($this->_pDb=oci_connect( - - $this->_tConfig[$this->_sConfig.'.username'], - $this->_tConfig[$this->_sConfig.'.password'], - $this->_tConfig[$this->_sConfig.'.hostname'].'/'.$this->_tConfig[$this->_sConfig.'.database'], - $this->_tConfig[$this->_sConfig.'.character_set'] - - ))==false ){ - $e = oci_error(); - throw new Exception('Probleme connexion sql : '.$e['message']); - } - }else{ - if( ($this->_pDb=oci_connect( - - $this->_tConfig[$this->_sConfig.'.username'], - $this->_tConfig[$this->_sConfig.'.password'], - $this->_tConfig[$this->_sConfig.'.hostname'].'/'.$this->_tConfig[$this->_sConfig.'.database'] - - ))==false ){ - $e = oci_error(); - throw new Exception('Probleme connexion sql : '.$e['message']); - } - } - - - - - } - } - public function getLastInsertId(){ - return null; - } + +class sgbd_oracle extends abstract_sgbd +{ + + public static function getInstance($sConfig) + { + return self::_getInstance(__CLASS__, $sConfig); + } + + public function findMany($tSql, $sClassRow) + { + $query_start_time = microtime(true); # ADDED FOR BENCHMARK + + $pRs = $this->query($this->bind($tSql)); + + $query_end_time = microtime(true); # ADDED FOR BENCHMARK + $query_time = ($query_end_time - $query_start_time) * 1000; # ADDED FOR BENCHMARK + + if (empty($pRs)) { + return null; + } + + $network_start_time = microtime(true); # ADDED FOR BENCHMARK + + $tObj = array(); + while ($tRow = oci_fetch_assoc($pRs)) { + $oRow = new $sClassRow($tRow); + if ((int)_root::getConfigVar('security.xss.model.enabled', 0) == 1) { + $oRow->enableCleaning(); + } + $tObj[] = $oRow; + } + + $network_end_time = microtime(true); # ADDED FOR BENCHMARK + $network_time = ($network_end_time - $network_start_time) * 1000; # ADDED FOR BENCHMARK + + $total_time = round(($network_end_time - $query_start_time) * 1000, 4); # ADDED FOR BENCHMARK + self::printBench('findMany', $total_time, $query_time, $network_time); # ADDED FOR BENCHMARK + + return $tObj; + } + + public function findManySimple($tSql, $sClassRow) + { + + $query_start_time = microtime(true); # ADDED FOR BENCHMARK + $pRs = $this->query($this->bind($tSql)); + $query_end_time = microtime(true); # ADDED FOR BENCHMARK + $query_time = ($query_end_time - $query_start_time) * 1000; # ADDED FOR BENCHMARK + + if (empty($pRs)) { + return null; + } + + $network_start_time = microtime(true); # ADDED FOR BENCHMARK + $tObj = array(); + while ($oRow = oci_fetch_object($pRs)) { + $tObj[] = $oRow; + } + $network_end_time = microtime(true); # ADDED FOR BENCHMARK + $network_time = ($network_end_time - $network_start_time) * 1000; # ADDED FOR BENCHMARK + $total_time = round(($network_end_time - $query_start_time) * 1000, 4); # ADDED FOR BENCHMARK + self::printBench('findManySimple', $total_time, $query_time, $network_time); # ADDED FOR BENCHMARK + return $tObj; + } + + public function findOne($tSql, $sClassRow) + { + $query_start_time = microtime(true); # ADDED FOR BENCHMARK + $pRs = $this->query($this->bind($tSql)); + $query_end_time = microtime(true); # ADDED FOR BENCHMARK + $query_time = ($query_end_time - $query_start_time) * 1000; # ADDED FOR BENCHMARK + + if (empty($pRs)) { + return null; + } + + $network_start_time = microtime(true); # ADDED FOR BENCHMARK + $tRow = oci_fetch_assoc($pRs); + $oRow = new $sClassRow($tRow); + if ((int)_root::getConfigVar('security.xss.model.enabled', 0) == 1) { + $oRow->enableCleaning(); + } + $network_end_time = microtime(true); # ADDED FOR BENCHMARK + $network_time = ($network_end_time - $network_start_time) * 1000; # ADDED FOR BENCHMARK + $total_time = round(($network_end_time - $query_start_time) * 1000, 4); # ADDED FOR BENCHMARK + self::printBench('findOne', $total_time, $query_time, $network_time); # ADDED FOR BENCHMARK + + + return $oRow; + } + + public function findOneSimple($tSql, $sClassRow) + { + $query_start_time = microtime(true); # ADDED FOR BENCHMARK + $pRs = $this->query($this->bind($tSql)); + $query_end_time = microtime(true); # ADDED FOR BENCHMARK + $query_time = ($query_end_time - $query_start_time) * 1000; # ADDED FOR BENCHMARK + + if (empty($pRs)) { + return null; + } + + $network_start_time = microtime(true); # ADDED FOR BENCHMARK + $oRow = oci_fetch_object($pRs); + $network_end_time = microtime(true); # ADDED FOR BENCHMARK + $network_time = ($network_end_time - $network_start_time) * 1000; # ADDED FOR BENCHMARK + $total_time = round(($network_end_time - $query_start_time) * 1000, 4); # ADDED FOR BENCHMARK + self::printBench('findOneSimple', $total_time, $query_time, $network_time); # ADDED FOR BENCHMARK + + return $oRow; + } + + public function execute($tSql) + { + return $this->query($this->bind($tSql)); + } + + public function update($sTable, $tProperty, $twhere) + { + + $this->query('UPDATE ' . $sTable . ' SET ' . $this->getUpdateFromTab($tProperty) . ' WHERE ' . $this->getWhereFromTab($twhere)); + } + + public function insert($sTable, $tProperty) + { + $this->query('INSERT INTO ' . $sTable . ' ' . $this->getInsertFromTab($tProperty)); + } + + public function delete($sTable, $twhere) + { + $this->query('DELETE FROM ' . $sTable . ' WHERE ' . $this->getWhereFromTab($twhere)); + } + + public function getListColumn($sTable) + { + $pRs = $this->query(sgbd_syntax_oracle::getListColumn($sTable)); + $tCol = array(); + + if (empty($pRs)) { + return $tCol; + } + + while ($tRow = oci_fetch_row($pRs)) { + $tCol[] = $tRow[0]; + } + + return $tCol; + } + + public function getListTable() + { + $pRs = $this->query(sgbd_syntax_oracle::getListTable()); + $tCol = array(); + + if (empty($pRs)) { + return $tCol; + } + + while ($tRow = oci_fetch_row($pRs)) { + $tCol[] = $tRow[0]; + } + return $tCol; + } + + private function connect() + { + if (empty($this->_pDb)) { + + if (isset($this->_tConfig[$this->_sConfig . '.oci'])) { + if (($this->_pDb = oci_connect( + $this->_tConfig[$this->_sConfig . '.username'], + $this->_tConfig[$this->_sConfig . '.password'], + $this->_tConfig[$this->_sConfig . '.database'], + 'AL32UTF8' + )) == false) { + $e = oci_error(); + throw new Exception('Probleme connexion sql : ' . $e['message']); + } + } elseif (isset($this->_tConfig[$this->_sConfig . '.character_set'])) { + if (($this->_pDb = oci_connect( + $this->_tConfig[$this->_sConfig . '.username'], + $this->_tConfig[$this->_sConfig . '.password'], + $this->_tConfig[$this->_sConfig . '.hostname'] . '/' . $this->_tConfig[$this->_sConfig . '.database'], + $this->_tConfig[$this->_sConfig . '.character_set'] + )) == false) { + $e = oci_error(); + throw new Exception('Probleme connexion sql : ' . $e['message']); + } + } else { + if (($this->_pDb = oci_connect( + $this->_tConfig[$this->_sConfig . '.username'], + $this->_tConfig[$this->_sConfig . '.password'], + $this->_tConfig[$this->_sConfig . '.hostname'] . '/' . $this->_tConfig[$this->_sConfig . '.database'] + )) == false) { + $e = oci_error(); + throw new Exception('Probleme connexion sql : ' . $e['message']); + } + } + + } + } + + public function getLastInsertId() + { + return null; + } private function query($sReq) { @@ -163,7 +230,8 @@ private function query($sReq) $this->_sReq = $sReq; $query = oci_parse($this->_pDb, $sReq); - if (isset($this->_tConfig[$this->_sConfig . '.useTransaction']) && $this->_tConfig[$this->_sConfig . '.useTransaction'] == 1) { + $useTransaction = _root::getConfigVar('useTransaction', false); + if ($useTransaction) { oci_execute($query, OCI_NO_AUTO_COMMIT); } else { oci_execute($query); @@ -172,15 +240,18 @@ private function query($sReq) return $query; } - public function quote($sVal){ - if ( isset($this->_tConfig[$this->_sConfig.'.escapeDateField']) && $this->_tConfig[$this->_sConfig.'.escapeDateField']==1 && (preg_match('!^TO_DATE\(.*\)!', $sVal) || preg_match('!^TO_TIMESTAMP\(.*\)!', $sVal)) ) { - return $sVal; - } - return str_replace("\\",'', str_replace("'",'\'',"'".$sVal."'")); - } - public function getWhereAll(){ - return '1=1'; - } + public function quote($sVal) + { + if (isset($this->_tConfig[$this->_sConfig . '.escapeDateField']) && $this->_tConfig[$this->_sConfig . '.escapeDateField'] == 1 && (preg_match('!^TO_DATE\(.*\)!', $sVal) || preg_match('!^TO_TIMESTAMP\(.*\)!', $sVal))) { + return $sVal; + } + return str_replace("\\", '', str_replace("'", '\'', "'" . $sVal . "'")); + } + + public function getWhereAll() + { + return '1=1'; + } /** * @param $request @@ -215,4 +286,40 @@ public function getError() return oci_error($this->_pDb); } + public static function formatMilliseconds($milliseconds) + { + $seconds = floor($milliseconds / 1000); + $minutes = floor($seconds / 60); + $hours = floor($minutes / 60); + $milliseconds = $milliseconds % 1000; + $seconds = $seconds % 60; + $minutes = $minutes % 60; + + if ($hours > 0) { + $format = '%dh %dm'; + $time = sprintf($format, $hours, $minutes); + } elseif ($minutes > 0) { + $format = '%dm %ds'; + $time = sprintf($format, $minutes, $seconds); + } elseif ($seconds > 0) { + $format = '%ds %dms'; + $time = sprintf($format, $seconds, $milliseconds); + } else { + $format = '%dms'; + $time = sprintf($format, $milliseconds); + } + + return $time; + } + + public static function printBench($function, $total_time, $query_time, $network_time) + { + $percentage_query = round(((round($query_time, 4) / $total_time) * 100), 1); + $percentage_network = round(((round($network_time, 4) / $total_time) * 100), 1); + $percentage_query_str = number_format($percentage_query, 1, '.', ' ') . ' %'; + $percentage_network_str = number_format($percentage_network, 1, '.', ' ') . ' %'; + + echo "

$function | " . self::formatMilliseconds($total_time) . " - query : $percentage_query_str - network : $percentage_network_str

"; + + } }