diff --git a/build/api/module/excelController.php b/build/api/module/excelController.php index e3c4a4d..6c786e5 100644 --- a/build/api/module/excelController.php +++ b/build/api/module/excelController.php @@ -348,6 +348,10 @@ class excelController /** @var tp $tpRepo */ $tpRepo = Repo::getRepo("tp"); + $CoursToLink = []; + $TDToLink = []; + $TPToLink = []; + foreach ($allUE as $codeUE => $UE){ if($UE["defaultFormation"]){ @@ -377,22 +381,25 @@ class excelController switch ($type){ case "Course": - $coursRepo->create( $codeUE, - $allProf[$group["professor"]]["dbId"], - $UE["disabled"] ? $UE["CourseVH"] : $group["VH"], - $formations); + $CoursToLink[] = ["id" => $coursRepo->create( $codeUE, + $allProf[$group["professor"]]["dbId"], + $UE["disabled"] ? $UE["CourseVH"] : $group["VH"], + []), + "form" => $formations]; break; case "TD": - $tdRepo->create($codeUE, - $allProf[$group["professor"]]["dbId"], - $UE["disabled"] ? $UE["TdVH"] : $group["VH"], - $formations); + $TDToLink[] = ["id" => $tdRepo->create($codeUE, + $allProf[$group["professor"]]["dbId"], + $UE["disabled"] ? $UE["TdVH"] : $group["VH"], + []), + "form" => $formations]; break; case "TP": - $tpRepo->create($codeUE, - $allProf[$group["professor"]]["dbId"], - $UE["disabled"] ? $UE["TpVH"] : $group["VH"], - $formations); + $TPToLink[] = ["id" => $tpRepo->create($codeUE, + $allProf[$group["professor"]]["dbId"], + $UE["disabled"] ? $UE["TpVH"] : $group["VH"], + []), + "form" => $formations]; break; } @@ -401,6 +408,26 @@ class excelController } } + Repo::enableStacking(); + + foreach ($CoursToLink as $cour){ + foreach ($cour["form"] as $formation){ + $coursRepo->linkFormation($formation,$cour["id"]); + } + } + foreach ($TDToLink as $cour){ + foreach ($cour["form"] as $formation){ + $tdRepo->linkFormation($formation,$cour["id"]); + } + } + foreach ($TPToLink as $cour){ + foreach ($cour["form"] as $formation){ + $tpRepo->linkFormation($formation,$cour["id"]); + } + } + + Repo::flushStack(); + return [ 'data' => ["professors" => $allProf, "formations" => $allFormations, "UEs" => $allUE ] ]; }catch (Exception $e){ return [ 'error' => new Error(Err::UnknownError) ]; diff --git a/build/database/core/DatabaseDriver.php b/build/database/core/DatabaseDriver.php index a7553fd..7ffe06d 100755 --- a/build/database/core/DatabaseDriver.php +++ b/build/database/core/DatabaseDriver.php @@ -11,6 +11,7 @@ **************************/ namespace database\core; + use database\core\PDOWrapper\PDOWrapper; use \error\core\Error; use \error\core\Err; @@ -69,9 +70,10 @@ try{ - $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', $this->username, $this->password, [ + $this->pdo = new PDOWrapper('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', $this->username, $this->password, [ \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, - \PDO::ATTR_TIMEOUT => 5 + \PDO::ATTR_TIMEOUT => 5, + \PDO::ERRMODE_EXCEPTION => true ]); $this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); @@ -194,6 +196,14 @@ ]; } + public function enableStacking(){ + $this->pdo->enableStacking(); + } + + public function flushStack(){ + $this->pdo->executeStack(); + } + } diff --git a/build/database/core/PDOWrapper/PDOStatementWrapper.php b/build/database/core/PDOWrapper/PDOStatementWrapper.php new file mode 100644 index 0000000..704d5bb --- /dev/null +++ b/build/database/core/PDOWrapper/PDOStatementWrapper.php @@ -0,0 +1,51 @@ +statement = $statement; + $this->connexion = $connexion; + + + } + + public function execute($input_parameters = []) + { + $this->parameters = $input_parameters; + $this->connexion->stackStatement($this); + return true; + } + + /** + * @return string + */ + public function getStatement() + { + return $this->statement; + } + + /** + * @return array + */ + public function getParameters() + { + return $this->parameters; + } + + + +} \ No newline at end of file diff --git a/build/database/core/PDOWrapper/PDOWrapper.php b/build/database/core/PDOWrapper/PDOWrapper.php new file mode 100644 index 0000000..d8526c2 --- /dev/null +++ b/build/database/core/PDOWrapper/PDOWrapper.php @@ -0,0 +1,91 @@ +stacking){ + return new PDOStatementWrapper($statement, $this); + }else{ + parent::setAttribute(\PDO::ATTR_EMULATE_PREPARES, true); + return parent::prepare($statement, $options); + + } + } + + public function enableStacking(){ + $this->stacking = true; + } + + public function stackStatement(PDOStatementWrapper $st){ + array_push($this->statements,$st); + } + + public function executeStack(){ + //init the statements and the generator of number + $finalStatement = ''; + $finalExecute = []; + $i = 0; + + //for each request stacked + foreach ($this->statements as $request){ + $statement = $request->getStatement(); + + // we have to modify the parameters index at the same time that we modify the request, so we use static class attribute + $tempParametes = $request->getParameters(); + + //find the given pattern in the request, then call our function and replace the matched string by the return value of our function + $finalStatement .= rtrim(preg_replace_callback("/(:[a-z_\-0-9]*)/is",function($matches) use (&$i,&$tempParametes){ + //get next number + $i++; + + //delete the ':' at the beginning of the string + $tempKey = ltrim($matches[0],':'); + + //copy the parameter with the modified index + $tempParametes[$tempKey.$i] = $tempParametes[$tempKey]; + + //delete the old index + unset($tempParametes[$tempKey]); + + //return the modified string for replacement + return $matches[0].$i; + },$statement),';').';'; + + $finalExecute = array_merge($finalExecute,$tempParametes); + } + + //disable stacking + $this->stacking = false; + + $this->beginTransaction(); + + $req = $this->prepare($finalStatement); + + $success = $req->execute($finalExecute); + //as we execute multiple query that we don't fetch, we have to close the cursor if we want to do other requests later + $req->closeCursor(); + $this->commit(); + //using beginTransaction/commit disable the autocommit, we re-activate it + $this->setAttribute(\PDO::ATTR_AUTOCOMMIT,1); + parent::setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + return $success; + } +} \ No newline at end of file diff --git a/build/database/core/Repo.php b/build/database/core/Repo.php index 6a09828..88b88f7 100644 --- a/build/database/core/Repo.php +++ b/build/database/core/Repo.php @@ -12,6 +12,7 @@ namespace database\core; + use database\core\PDOWrapper\PDOWrapper; use \error\core\Error; use \error\core\Err; @@ -20,6 +21,9 @@ /* (1) Driver ---------------------------------------------------------*/ + /** + * @var DatabaseDriver + */ private static $driver = null; public static function setDriver(DatabaseDriver $driver){ self::$driver = $driver; } @@ -57,6 +61,14 @@ return $instance; } + public static function enableStacking(){ + static::$driver->enableStacking(); + } + + public static function flushStack(){ + static::$driver->flushStack(); + } +