338 lines
8.4 KiB
PHP
338 lines
8.4 KiB
PHP
<?php
|
|
class WDB
|
|
{
|
|
/**
|
|
* @var Zend_Db_Adapter_Abstract
|
|
*/
|
|
protected $db = null;
|
|
|
|
/**
|
|
*
|
|
* @var unknown
|
|
*/
|
|
protected $result = null;
|
|
|
|
protected $errorCode = 0;
|
|
|
|
protected $errorMsg = '';
|
|
|
|
public function __construct( $db = null )
|
|
{
|
|
if ($db === null) {
|
|
$this->db = Zend_Db_Table::getDefaultAdapter();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function insert($table, $toAdd, $debug=false, $low_priority=false)
|
|
{
|
|
$this->errorCode = 0;
|
|
$this->errorMsg = '';
|
|
|
|
$fields = implode(array_keys($toAdd), ',');
|
|
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);
|
|
|
|
if ($low_priority) {
|
|
$query = 'INSERT DELAYED INTO '.$table.' ('.$fields.') VALUES ('.$values.');';
|
|
} else {
|
|
$query = 'INSERT INTO '.$table.' ('.$fields.') VALUES ('.$values.');';
|
|
}
|
|
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 ) {
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Database Update
|
|
* @param unknown $table
|
|
* @param unknown $update
|
|
* @param unknown $where
|
|
* @param string $debug
|
|
* @param number $limit
|
|
* @param string $low_priority
|
|
* @return resource
|
|
*/
|
|
public function update($table, $update, $where, $debug=false, $limit=0, $low_priority=false)
|
|
{
|
|
$this->errorCode = 0;
|
|
$this->errorMsg = '';
|
|
|
|
if ($low_priority) {
|
|
$query='UPDATE LOW_PRIORITY '.$table.' SET ';
|
|
} else {
|
|
$query='UPDATE '.$table.' SET ';
|
|
}
|
|
$i=0;
|
|
foreach ($update as $field => $value) {
|
|
$query.= ' '.$field."=".$this->db->quote($value);
|
|
$i++;
|
|
if ($i<count($update)) {
|
|
$query.= ',';
|
|
}
|
|
}
|
|
$query = str_replace("'NULL'", 'NULL', $query);
|
|
$query.=' WHERE '.$where;
|
|
if ($limit>0) $query.=" LIMIT $limit";
|
|
|
|
if ($debug) $tdeb=microtime_float();
|
|
|
|
try {
|
|
$stmt = $this->db->query($query);
|
|
$res = $stmt->rowCount();
|
|
if ($debug) $this->trace($query, $res, $tdeb);
|
|
if ( $res == 0 ) {
|
|
return false;
|
|
}
|
|
return $res;
|
|
} catch(Zend_Db_Exception $e) {
|
|
$this->errorCode = $e->getCode();
|
|
$this->errorMsg = $e->getMessage();
|
|
$this->trace($query, $res, $tdeb);
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Database delete
|
|
* @param string $table
|
|
* @param string $where
|
|
* @param string $debug
|
|
* @param string $low_priority
|
|
* @return resource
|
|
*/
|
|
public function delete($table, $where, $debug=false, $low_priority=false)
|
|
{
|
|
$this->errorCode = 0;
|
|
$this->errorMsg = '';
|
|
|
|
if ($low_priority)
|
|
$query='DELETE LOW_PRIORITY QUICK FROM '.$table.' WHERE '.$where.';';
|
|
else
|
|
$query='DELETE FROM '.$table.' WHERE '.$where.';';
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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 = '';
|
|
|
|
$query="SELECT $fields FROM $table WHERE $where;";
|
|
|
|
if ($debug) $tdeb=microtime_float();
|
|
|
|
try {
|
|
$stmt = $this->db->query($query);
|
|
} catch(Zend_Db_Exception $e) {
|
|
$this->errorCode = $e->getCode();
|
|
$this->errorMsg = $e->getMessage();
|
|
$this->trace($query);
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
} else {
|
|
$nbRows = $stmt->rowCount();
|
|
if ($debug) $this->trace($query, $nbRows, $tdeb);
|
|
return $nbRows;
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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);
|
|
}
|
|
|
|
/**
|
|
* 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';
|
|
}
|
|
if ($this->errorCode == 0) {
|
|
$msg = date('Y-m-d H:i:s') ." - DEBUG - ".$this->errorCode.":".$this->errorMsg." - ".$query." - ".$duree;
|
|
} else {
|
|
$msg = date('Y-m-d H:i:s') ." - ERROR - ".$this->errorCode.":".$this->errorMsg." - ".$query." - ".$duree;
|
|
}
|
|
file_put_contents(LOG_PATH . '/mysql.log', $msg . "\n", FILE_APPEND);
|
|
}
|
|
|
|
/**
|
|
* Exécute la requête passé en paramètre
|
|
*/
|
|
public function query($query, $debug=false)
|
|
{
|
|
$this->errorCode = 0;
|
|
$this->errorMsg = '';
|
|
|
|
try {
|
|
$stmt = $this->db->query($query);
|
|
$this->result = $stmt;
|
|
} catch(Zend_Db_Exception $e) {
|
|
$this->errorCode = $e->getCode();
|
|
$this->errorMsg = $e->getMessage();
|
|
$this->trace($query);
|
|
return false;
|
|
}
|
|
|
|
return $this->result->fetchAll(Zend_Db::FETCH_ASSOC);
|
|
}
|
|
|
|
/** Retourne le libellé de la dernière erreur **/
|
|
public function getLastErrorMsg()
|
|
{
|
|
return $this->errorMsg;
|
|
}
|
|
/**
|
|
* Retourne le numéro de la dernière erreur
|
|
*/
|
|
public function getLastErrorNum()
|
|
{
|
|
return $this->errorCode;
|
|
}
|
|
|
|
/** Retourne le libellé et le numéro de la dernière erreur **/
|
|
public function getLastError()
|
|
{
|
|
return $this->errorMsg.' ('.$this->errorCode.')';
|
|
}
|
|
|
|
/** Retourne le nombre de lignes modifiées par la dernière requête **/
|
|
public function getAffectedRows()
|
|
{
|
|
return $this->result->rowCount();
|
|
}
|
|
|
|
/**
|
|
* Génère le fichier CSV pour la requete SQL
|
|
*
|
|
* @param string $query
|
|
* @param string $fileCsv
|
|
* @return bool
|
|
*/
|
|
public function exportCSV($query, $fileCsv, $sep=',', $eol=EOL)
|
|
{
|
|
$i = 0;
|
|
$fp = fopen($fileCsv, 'w');
|
|
if (!$fp) return false;
|
|
|
|
$res = $this->query($query);
|
|
$nbLignes = count($res);
|
|
for($i=0; $i<$nbLignes; $i++) {
|
|
$ligne = $res[$i];
|
|
|
|
//Header
|
|
if ($i==0) {
|
|
$nbCols = count($ligne);
|
|
$header = array();
|
|
foreach ($ligne as $libCol => $col) {
|
|
$header[] = $libCol;
|
|
}
|
|
fputcsv($fp, $header, $sep, '"');
|
|
}
|
|
|
|
//Content
|
|
$fields = array();
|
|
foreach ($ligne as $libCol => $col) {
|
|
$fields[] = $col;
|
|
}
|
|
fputcsv($fp, $fields, $sep, '"');
|
|
}
|
|
|
|
fclose($fp);
|
|
return $nbLignes;
|
|
}
|
|
}
|
|
?>
|