Implemented Wrapper and Stacker + optimized Excel import
This commit is contained in:
parent
78e66a0a26
commit
1c513acaa4
|
@ -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,
|
||||
$CoursToLink[] = ["id" => $coursRepo->create( $codeUE,
|
||||
$allProf[$group["professor"]]["dbId"],
|
||||
$UE["disabled"] ? $UE["CourseVH"] : $group["VH"],
|
||||
$formations);
|
||||
[]),
|
||||
"form" => $formations];
|
||||
break;
|
||||
case "TD":
|
||||
$tdRepo->create($codeUE,
|
||||
$TDToLink[] = ["id" => $tdRepo->create($codeUE,
|
||||
$allProf[$group["professor"]]["dbId"],
|
||||
$UE["disabled"] ? $UE["TdVH"] : $group["VH"],
|
||||
$formations);
|
||||
[]),
|
||||
"form" => $formations];
|
||||
break;
|
||||
case "TP":
|
||||
$tpRepo->create($codeUE,
|
||||
$TPToLink[] = ["id" => $tpRepo->create($codeUE,
|
||||
$allProf[$group["professor"]]["dbId"],
|
||||
$UE["disabled"] ? $UE["TpVH"] : $group["VH"],
|
||||
$formations);
|
||||
[]),
|
||||
"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) ];
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: lucas
|
||||
* Date: 30/08/16
|
||||
* Time: 17:42
|
||||
*/
|
||||
|
||||
namespace database\core\PDOWrapper;
|
||||
|
||||
|
||||
class PDOStatementWrapper extends \PDOStatement
|
||||
{
|
||||
private $statement;
|
||||
private $parameters;
|
||||
private $connexion;
|
||||
|
||||
public function __construct($statement, PDOWrapper $connexion)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: lucas
|
||||
* Date: 30/08/16
|
||||
* Time: 17:26
|
||||
*/
|
||||
|
||||
namespace database\core\PDOWrapper;
|
||||
|
||||
|
||||
class PDOWrapper extends \PDO
|
||||
{
|
||||
private $statements = [];
|
||||
private $stacking = false;
|
||||
|
||||
public function __construct(String $dsn, String $username, String $passwd, array $options = [])
|
||||
{
|
||||
parent::__construct($dsn, $username, $passwd, $options);
|
||||
}
|
||||
|
||||
public function prepare($statement, $options = [])
|
||||
{
|
||||
if($this->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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue