Skip to content
Snippets Groups Projects
Commit 18db8553 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

rel #17179 : Cosmogramme class_sql refactoring

parent 54d5bacf
Branches
Tags
4 merge requests!529Hotline 6.56,!519Master,!518Hotline 6.55,!517Hotline#17179 mysql gone away
......@@ -22,23 +22,19 @@
require_once("classe_log.php");
class sql {
private $hpdo; // Handle de connexion
private $ignore_erreurs=false; // Pour l'intégration les erreurs sont ignorées
private $adapter; // Handle de connexion
private $ignore_erreurs = false; // Pour l'intégration les erreurs sont ignorées
private $log; // Instance classe de log
private $statements = []; // liste des prepare statements pour les insertions
public function __construct($server, $user, $pwd, $base) {
$this->hpdo = Zend_Db_Table::getDefaultAdapter();
$this->adapter = Zend_Db_Table::getDefaultAdapter();
}
public function fetchOne($req) {
protected function run($closure) {
try {
$result = $this->hpdo->prepare($req);
$result->execute();
$data = $result->fetch(Zend_Db::FETCH_NUM);
return stripslashes($data[0]);
return $closure();
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
......@@ -46,45 +42,47 @@ class sql {
}
public function fetchEnreg($req, $num=false) {
try {
$result = $this->hpdo->prepare($req);
$result->execute();
return $result->fetch(($num == true) ?
Zend_Db::FETCH_NUM :
Zend_Db::FETCH_ASSOC);
public function fetchOne($req) {
return $this->run(
function() use ($req){
$result = $this->adapter->prepare($req);
$result->execute();
$data = $result->fetch(Zend_Db::FETCH_NUM);
return stripslashes($data[0]);
});
}
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
public function fetchEnreg($req, $num=false) {
return $this->run(
function() use($req, $num) {
$result = $this->adapter->prepare($req);
$result->execute();
return $result->fetch(($num == true) ?
Zend_Db::FETCH_NUM :
Zend_Db::FETCH_ASSOC);
});
}
public function fetchAll($req, $num=false) {
try {
$result = $this->hpdo->prepare($req);
$result->execute();
return $result->fetchAll(($num == true) ?
Zend_Db::FETCH_NUM :
Zend_Db::FETCH_ASSOC);
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
return $this->run(
function() use ($req, $num) {
$result = $this->adapter->prepare($req);
$result->execute();
return $result->fetchAll(($num == true) ?
Zend_Db::FETCH_NUM :
Zend_Db::FETCH_ASSOC);
});
}
public function execute($req) {
try {
$this->hpdo->getConnection()->query($req);
return $this->hpdo->getConnection()->affected_rows;
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
return $this->run(
function() use ($req) {
$this->adapter->query($req);
return $this->adapter->getConnection()->affected_rows;
});
}
......@@ -96,51 +94,42 @@ class sql {
$req = str_replace('@SET@', $set, $req);
try {
$this->hpdo->getConnection()->query($req);
return $this->hpdo->getConnection()->affected_rows;
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
return $this->run(
function() use ($req) {
$this->adapter->query($req);
return $this->adapter->getConnection()->affected_rows;
});
}
public function insert($table, $data) {
try {
$this->hpdo->insert($table, $data);
return $this->hpdo->lastInsertId();
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
return $this->run(
function() use ($table, $data) {
$this->adapter->insert($table, $data);
return $this->adapter->lastInsertId();
});
}
public function prepareListe($req) {
try {
return $this->hpdo->query($req);
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
return $this->run(
function () use ($req) {
return $this->adapter->query($req);
});
}
public function fetchNext($result, $onlyOne=0) {
try {
$data = $result->fetch($onlyOne==1 ? Zend_Db::FETCH_NUM : Zend_Db::FETCH_ASSOC);
return ($onlyOne==1) ? $data[0] : $data;
} catch(Exception $e) {
$this->traiteErreur($req, $e);
return false;
}
return $this->run(
function() use ($result, $onlyOne) {
$data = $result->fetch($onlyOne==1 ?
Zend_Db::FETCH_NUM :
Zend_Db::FETCH_ASSOC);
return ($onlyOne==1) ? $data[0] : $data;
});
}
public function ignoreErreurs($mode) {
$this->ignore_erreurs = $mode;
return $this;
......@@ -155,21 +144,22 @@ class sql {
// Sinon on affiche l'erreur
print '<h3 class="erreur">Erreur SQL</h3>';
$msg = '<div class="erreur_sql">';
$msg .= "<b>Code : </b>".$erreur[1] . BR;
$msg .= '<b>Requete : </b>'.$requete . BR;
$msg .= '<b>Erreur : </b>'.$erreur[2] . BR . BR;
$msg = '<div class="erreur_sql">'
. '<b>Code : </b>' . $erreur[1] . BR
. '<b>Requete : </b>' . $requete . BR
. '<b>Erreur : </b>' . $erreur[2] . BR . BR;
$stack = debug_backtrace();
for ($i = count($stack)-1; $i>0; $i--) {
$lig = $stack[$i];
$msg.= "<b>Script : </b>". $lig["file"];
$msg.= " - <b>Ligne : </b>". $lig["line"];
$msg.= " - <b>Fonction : </b>". $lig["function"].BR;
$msg .= '<b>Script : </b>' . $lig['file']
. ' - <b>Ligne : </b>' . $lig['line']
. ' - <b>Fonction : </b>' . $lig['function'] . BR;
}
$msg .= '</div>';
// Afficher l'erreur
print('</center>' . $msg);
print '</center>' . $msg;
// On log l'erreur
if (!$this->log)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment