371 lines
9.7 KiB
PHP
Raw Normal View History

2013-11-05 11:18:30 +00:00
<?php
2014-10-06 19:45:24 +00:00
class WDB
{
2015-03-11 12:52:58 +00:00
/**
* @var Zend_Db_Adapter_Abstract
*/
protected $db = null;
/**
*
* @var unknown
*/
protected $result = null;
protected $errorCode = 0;
protected $errorMsg = '';
public function __construct( $database = null, $host = null, $user = null, $password = null )
2014-06-10 10:09:51 +00:00
{
2015-03-11 12:52:58 +00:00
if ( $database === null ) {
$database = 'jo';
}
if ( $host === null ) {
$c = Zend_Registry::get('config');
$config = new Zend_Config(array(
'adapter' => $c->profil->db->metier->adapter,
'params' => array(
'host' => $c->profil->db->metier->params->host,
'username'=> $c->profil->db->metier->params->username,
'password'=> $c->profil->db->metier->params->password,
'dbname'=> $database,
'driver_options' => array(
MYSQLI_INIT_COMMAND => "SET NAMES utf8",
),
),
));
2014-03-14 15:54:41 +00:00
2015-03-11 12:52:58 +00:00
} else {
$config = new Zend_Config(array(
'adapter' => 'mysqli',
'params' => array(
'host' => $host,
'username'=> $user,
'password'=> $password,
'dbname'=> $database,
'driver_options' => array(
MYSQLI_INIT_COMMAND => "SET NAMES utf8",
),
),
));
}
try {
$this->db = Zend_Db::factory($config);
} catch ( Exception $e ) {
2015-03-17 19:35:45 +00:00
file_put_contents(LOG_PATH . '/mysql.log', date('Y-m-d H:i:s') . ' - Erreur : ' . $e->getMessage()."\n", FILE_APPEND);
2015-03-11 12:52:58 +00:00
}
2014-03-14 15:54:41 +00:00
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* INSERTION d'un tableau dans une table.
* Les index doivent avoir les mêmes noms que les champs.
* @param string Table
* @param array Valeurs insérer
* @return int Dernière valeur de l'auto-incrément, 1 si pas d'auto-incrément et 0 si erreur
*/
2014-06-10 10:09:51 +00:00
public function insert($table, $toAdd, $debug=false, $low_priority=false)
{
2015-03-11 12:52:58 +00:00
$this->errorCode = 0;
$this->errorMsg = '';
$fields = implode(array_keys($toAdd), ',');
2013-11-05 11:18:30 +00:00
foreach (array_values($toAdd) as $key=>$array_values)
$tmp[$key]=checkaddslashes($array_values);
$values = "'".implode(array_values($tmp), "','")."'"; # better
$values = str_replace("'NULL'", 'NULL', $values);
2014-06-10 10:09:51 +00:00
if ($low_priority)
2015-03-11 12:52:58 +00:00
$query = 'INSERT DELAYED INTO '.$table.' ('.$fields.') VALUES ('.$values.');';
2014-06-10 10:09:51 +00:00
else
2015-03-11 12:52:58 +00:00
$query = 'INSERT INTO '.$table.' ('.$fields.') VALUES ('.$values.');';
2014-03-14 15:54:41 +00:00
if ($debug) $tdeb=microtime_float();
2015-03-11 12:52:58 +00:00
try {
$stmt = $this->db->query($query);
$res = $this->db->lastInsertId();
if ($debug) $this->trace($query, $res, $tdeb);
if ( $res == 0 ) {
return true;
}
return $res;
} catch(Zend_Db_Exception $e) {
$this->errorCode = $e->getCode();
$this->errorMsg = $e->getMessage();
$this->trace($query, $res, $tdeb);
return false;
}
return false;
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Database Update
* @param unknown $table
* @param unknown $update
* @param unknown $where
* @param string $debug
* @param number $limit
* @param string $low_priority
* @return resource
*/
2014-06-10 10:09:51 +00:00
public function update($table, $update, $where, $debug=false, $limit=0, $low_priority=false)
{
2015-03-11 12:52:58 +00:00
$this->errorCode = 0;
$this->errorMsg = '';
$fields = array_keys($update);
2013-11-05 11:18:30 +00:00
$values = array_values($update);
$i=0;
2014-06-10 10:09:51 +00:00
if ($low_priority)
2015-03-11 12:52:58 +00:00
$query='UPDATE LOW_PRIORITY '.$table.' SET ';
2014-06-10 10:09:51 +00:00
else
2015-03-11 12:52:58 +00:00
$query='UPDATE '.$table.' SET ';
2013-11-05 11:18:30 +00:00
while(isset($fields[$i])){
if($i>0) { $query.=', '; }
2015-03-11 12:52:58 +00:00
$query.=' '.$fields[$i]."=".$this->db->quote($values[$i]);
2013-11-05 11:18:30 +00:00
$i++;
}
$query = str_replace("'NULL'", 'NULL', $query);
$query.=' WHERE '.$where;
if ($limit>0) $query.=" LIMIT $limit";
2015-03-11 12:52:58 +00:00
2013-11-05 11:18:30 +00:00
if ($debug) $tdeb=microtime_float();
2015-03-11 12:52:58 +00:00
try {
$stmt = $this->db->query($query);
$res = $this->db->lastInsertId();
if ($debug) $this->trace($query, $res, $tdeb);
if ( $res == 0 ) {
return true;
}
return $res;
} catch(Zend_Db_Exception $e) {
$this->errorCode = $e->getCode();
$this->errorMsg = $e->getMessage();
$this->trace($query, $res, $tdeb);
return false;
}
return false;
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Database delete
* @param unknown $table
* @param unknown $where
* @param string $debug
* @param string $low_priority
* @return resource
*/
2014-06-10 10:09:51 +00:00
public function delete($table, $where, $debug=false, $low_priority=false)
{
2015-03-11 12:52:58 +00:00
$this->errorCode = 0;
$this->errorMsg = '';
2014-06-10 10:09:51 +00:00
if ($low_priority)
$query='DELETE LOW_PRIORITY QUICK FROM `'.$table.'` WHERE '.$where.' LIMIT 1;';
else
$query='DELETE FROM `'.$table.'` WHERE '.$where.' LIMIT 1;';
2015-03-11 12:52:58 +00:00
if ($debug) $tdeb=microtime_float();
try {
$stmt = $this->db->query($query);
$res = $this->db->lastInsertId();
if ($debug) $this->trace($query, $res, $tdeb);
if ( $res == 0 ) {
$res = true;
}
return $res;
} catch(Zend_Db_Exception $e) {
$this->errorCode = $e->getCode();
$this->errorMsg = $e->getMessage();
$this->trace($query, $res, $tdeb);
return false;
}
return false;
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Database select
* @param string $table
* @param string $fields
* @param string $where
* @param string $debug
* @param string $assoc
* @param string $huge
* @return boolean|multitype:multitype: |number
*/
public function select($table, $fields, $where, $debug=false, $assoc=MYSQL_BOTH, $huge=false)
{
$this->errorCode = 0;
$this->errorMsg = '';
2013-11-05 11:18:30 +00:00
$query="SELECT $fields FROM $table WHERE $where;";
2015-03-11 12:52:58 +00:00
2013-11-05 11:18:30 +00:00
if ($debug) $tdeb=microtime_float();
2015-03-11 12:52:58 +00:00
try {
$stmt = $this->db->query($query);
} catch(Zend_Db_Exception $e) {
$this->errorCode = $e->getCode();
$this->errorMsg = $e->getMessage();
$this->trace($query);
return false;
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
if ( !$huge ) {
switch($assoc) {
case MYSQL_NUM:
$mode = Zend_Db::FETCH_NUM;
break;
case MYSQL_ASSOC:
$mode = Zend_Db::FETCH_ASSOC;
break;
case MYSQL_BOTH:
$mode = Zend_Db::FETCH_BOTH;
break;
}
$tab = $stmt->fetchAll($mode);
if ($debug) $this->trace($query, sizeof($tab), $tdeb);
return $tab;
2013-11-05 11:18:30 +00:00
} else {
2015-03-11 12:52:58 +00:00
$nbRows = $stmt->rowCount();
if ($debug) $this->trace($query, $nbRows, $tdeb);
return $nbRows;
2013-11-05 11:18:30 +00:00
}
}
2015-03-11 12:52:58 +00:00
/**
*
* @param string $assoc
* @return multitype:
*/
public function fetch($assoc=MYSQL_BOTH)
{
switch($assoc) {
case MYSQL_NUM:
$mode = Zend_Db::FETCH_NUM;
break;
case MYSQL_ASSOC:
$mode = Zend_Db::FETCH_ASSOC;
break;
case MYSQL_BOTH:
$mode = Zend_Db::FETCH_BOTH;
break;
}
return $this->result->fetch($mode);
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Trace
* @param string $query
* @param string $error
* @param int $tdeb
*/
public function trace($query, $res='', $tdeb = null)
{
if ( $tdeb === null) {
$duree = substr(''.microtime_float()-$tdeb, 0, 5);
} else {
$duree = 'N/D';
}
file_put_contents(LOG_PATH . '/mysql.log', date('Y-m-d H:i:s') ." - ".$query." - ".$this->errorCode.":".$this->errorMsg." - ".$duree."\n", FILE_APPEND);
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Exécute la requête passé en paramètre
*/
2014-06-10 10:09:51 +00:00
public function query($query, $debug=false)
{
2015-03-11 12:52:58 +00:00
$this->errorCode = 0;
$this->errorMsg = '';
try {
$stmt = $this->db->query($query);
2015-03-18 12:53:34 +00:00
$this->result = $stmt;
2015-03-11 12:52:58 +00:00
} catch(Zend_Db_Exception $e) {
$this->errorCode = $e->getCode();
$this->errorMsg = $e->getMessage();
$this->trace($query);
return false;
}
2015-03-18 12:53:34 +00:00
return $this->result->fetchAll(Zend_Db::FETCH_ASSOC);
2013-11-05 11:18:30 +00:00
}
/** Retourne le libellé de la dernière erreur **/
2015-03-11 12:52:58 +00:00
public function getLastErrorMsg()
{
return $this->errorMsg;
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Retourne le numéro de la dernière erreur
*/
2014-06-10 10:09:51 +00:00
public function getLastErrorNum()
{
2015-03-11 12:52:58 +00:00
return $this->errorCode;
2013-11-05 11:18:30 +00:00
}
/** Retourne le libellé et le numéro de la dernière erreur **/
2014-06-10 10:09:51 +00:00
public function getLastError()
{
2015-03-11 12:52:58 +00:00
return $this->errorMsg.' ('.$this->errorCode.')';
2013-11-05 11:18:30 +00:00
}
/** Retourne le nombre de lignes modifiées par la dernière requête **/
2014-06-10 10:09:51 +00:00
public function getAffectedRows()
{
2015-03-11 12:52:58 +00:00
return $this->result->rowCount();
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
/**
* Génère le fichier CSV pour la requete SQL
*
* @param string $query
* @param string $fileCsv
* @return bool
2013-11-05 11:18:30 +00:00
*/
2015-03-11 12:52:58 +00:00
public function exportCSV($query, $fileCsv, $sep=',', $eol=EOL)
{
$i = 0;
2013-11-05 11:18:30 +00:00
$fp = fopen($fileCsv, 'w');
if (!$fp) return false;
2015-03-11 12:52:58 +00:00
$res = $this->query($query);
$nbLignes = mysql_num_rows($res);
2013-11-05 11:18:30 +00:00
while ($ligne=$this->fetch(MYSQL_ASSOC)) {
2015-03-11 12:52:58 +00:00
//Header
if ($i==0) {
$nbCols = count($ligne);
$fields = array();
$header = array();
2013-11-05 11:18:30 +00:00
foreach ($ligne as $libCol=>$col) {
2015-03-11 12:52:58 +00:00
$header[] = $libCol;
$fields[] = $col;
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
fputcsv($fp, $header, $sep, '"');
fputcsv($fp, $fields, $sep, '"');
2013-11-05 11:18:30 +00:00
}
2015-03-11 12:52:58 +00:00
//Content
else {
$fields = array();
foreach ($ligne as $libCol=>$col) {
$fields[] = $col;
}
fputcsv($fp, $fields, $sep, '"');
2013-11-05 11:18:30 +00:00
}
$i++;
}
fclose($fp);
2015-03-11 12:52:58 +00:00
return $nbLignes;
}
2013-11-05 11:18:30 +00:00
}
?>