From 4b9d9e713a3891e5e785ee6dcea4e59fac448c23 Mon Sep 17 00:00:00 2001 From: Franzz Date: Sat, 7 Sep 2019 12:59:44 +0200 Subject: [PATCH] Adding batch loads --- inc/db.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/inc/db.php b/inc/db.php index 5fbd1ce..7e42368 100644 --- a/inc/db.php +++ b/inc/db.php @@ -121,6 +121,44 @@ class Db extends PhpObject //Create tables @array_walk($this->getInstallQueries(), array($this, 'setQuery')); } + + public function loadFile($sFilePath) { + set_time_limit(0); + $bResult = false; + + if(file_exists($sFilePath)) + { + $sContent = file_get_contents($sFilePath); + $sContent = ToolBox::fixEOL($sContent); + if(str_replace(ToolBox::FILE_EOL, '', $sContent)!='') + { + $asLines = explode(ToolBox::FILE_EOL, $sContent); + $sSql = ''; + foreach ($asLines as $sLine) + { + $sSql .= trim($sLine); + if(substr($sSql, -1)==';') //Multi line SQL + { + $asResult = $this->setQuery($sSql); + if($asResult === false) + { + $this->addError('SQL failed with error: '.$this->db->error, $sSql); + $bResult = false; + break; + } + + $bResult = true; + $sSql = ''; + } + else $sSql .= " "; + } + } + else $this->addError('File is empty: '.basename($sFilePath)); + } + else $this->addError('File not found: '.$sFilePath); + + return $bResult; + } //For debug purposes public function getFullInstallQuery()