2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-29 18:33:30 +01:00
|
|
|
namespace descartes;
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
|
|
|
* Cette classe sert de mère à tous les modèles, elle permet de gérer l'ensemble des fonction necessaires aux requetes en base de données
|
2019-11-08 19:17:07 +01:00
|
|
|
* @param $pdo : Une instance de \PDO
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
|
|
|
class Model
|
|
|
|
{
|
|
|
|
//Les variables internes au Model
|
|
|
|
var $pdo;
|
|
|
|
|
|
|
|
//Les constantes des différents types de retours possibles
|
|
|
|
const NO = 0; //Pas de retour
|
|
|
|
const FETCH = 1; //Retour de type fetch
|
|
|
|
const FETCHALL = 2; //Retour de type fetchall
|
|
|
|
const ROWCOUNT = 3; //Retour de type rowCount()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model constructor
|
2019-11-08 19:17:07 +01:00
|
|
|
* @param \PDO $pdo : \PDO connect to use
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-11-08 19:17:07 +01:00
|
|
|
public function __construct(\PDO $pdo)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$this->pdo = $pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-08 19:17:07 +01:00
|
|
|
* Cette fonction permet créer une connexion à une base SQL via \PDO
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param string $host : L'host à contacter
|
|
|
|
* @param string $dbname : Le nom de la base à contacter
|
|
|
|
* @param string $user : L'utilisateur à utiliser
|
|
|
|
* @param string $password : Le mot de passe à employer
|
2019-11-08 19:17:07 +01:00
|
|
|
* @return mixed : Un objet \PDO ou false en cas d'erreur
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2021-04-26 02:41:41 +02:00
|
|
|
public static function _connect ($host, $dbname, $user, $password, ?string $charset = 'utf8mb4', ?array $options = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$options = $options ?? [
|
2019-11-08 19:17:07 +01:00
|
|
|
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
|
|
|
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
|
2019-10-29 14:57:13 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
// On se connecte à MySQL
|
2019-11-08 19:17:07 +01:00
|
|
|
$pdo = new \PDO('mysql:host=' . $host . ';dbname=' . $dbname . ';charset=' . $charset , $user, $password, $options);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
if ($pdo === false)
|
|
|
|
{
|
|
|
|
throw new DescartesExceptionDatabaseConnection('Cannot connect to database ' . $dbname . '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a query and return result
|
|
|
|
* @param string $query : Query to run
|
2021-01-17 03:16:57 +01:00
|
|
|
* @param array $data : Data to pass to query
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param const $return_type : Type of return, by default all results, see Model constants
|
|
|
|
* @param const $fetch_mode : Format of result from db, by default array, FETCH_ASSOC
|
|
|
|
* @param boolean $debug : If we must return debug info instead of data, by default false
|
|
|
|
* @return mixed : Result of query, depend of $return_type | null | array | object | int
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
protected function _run_query (string $query, array $data = array(), int $return_type = self::FETCHALL, int $fetch_mode = \PDO::FETCH_ASSOC, bool $debug = false)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
try
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-04-09 03:40:37 +02:00
|
|
|
//Must convert bool to 1 or 0 because of some strange inconsistent behavior between PHP versions
|
2021-01-17 03:16:57 +01:00
|
|
|
foreach ($data as $key => $value)
|
2020-04-09 03:40:37 +02:00
|
|
|
{
|
|
|
|
if (is_bool($value))
|
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
$data[$key] = (int) $value;
|
2020-04-09 03:40:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
$query = $this->pdo->prepare($query);
|
|
|
|
$query->setFetchMode($return_type);
|
2021-01-17 03:16:57 +01:00
|
|
|
$query->execute($data);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
if ($debug)
|
|
|
|
{
|
|
|
|
return $query->errorInfo();
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
switch ($return_type)
|
|
|
|
{
|
|
|
|
case self::NO :
|
|
|
|
$return = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::FETCH :
|
|
|
|
$return = $query->fetch();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::FETCHALL :
|
|
|
|
$return = $query->fetchAll();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::ROWCOUNT :
|
|
|
|
$return = $query->rowCount();
|
|
|
|
break;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
default :
|
|
|
|
$return = $query->fetchAll();
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
catch (\PDOException $e)
|
|
|
|
{
|
|
|
|
$error = $query->errorInfo();
|
2019-11-09 03:26:48 +01:00
|
|
|
|
|
|
|
//Get query string and params
|
|
|
|
ob_start();
|
|
|
|
$query->debugDumpParams();
|
|
|
|
$debug_string = ob_get_clean();
|
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
throw new \descartes\exceptions\DescartesExceptionSqlError(
|
|
|
|
'SQL Error : ' . "\n" .
|
|
|
|
'SQLSTATE : ' . $error[0] . "\n" .
|
|
|
|
'Driver Error Code : ' . $error[1] . "\n" .
|
2019-11-09 03:26:48 +01:00
|
|
|
'Driver Error Message : ' . $error[2] . "\n" .
|
|
|
|
'SQL QUERY DEBUG :' . "\n" .
|
|
|
|
'-----------------' . "\n" .
|
|
|
|
$debug_string . "\n" .
|
|
|
|
'-----------------' . "\n"
|
2019-11-08 19:17:07 +01:00
|
|
|
);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return last inserted id
|
|
|
|
* return int : Last inserted id
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _last_id() : int
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
return $this->pdo->lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fonctions d'execution des requetes ou de génération
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate IN query params and values
|
2023-02-24 16:29:10 +01:00
|
|
|
* @param array $values : Values to generate in array from
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : Array ['QUERY' => string 'IN(...)', 'PARAMS' => [parameters to pass to execute]]
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _generate_in_from_array ($values)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$return = array(
|
|
|
|
'QUERY' => '',
|
|
|
|
'PARAMS' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$flags = array();
|
|
|
|
|
|
|
|
$values = count($values) ? $values : array();
|
|
|
|
|
2019-11-04 17:45:16 +01:00
|
|
|
foreach ($values as $key => $value)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-04 17:45:16 +01:00
|
|
|
$key = preg_replace('#[^a-zA-Z0-9_]#', '', $key);
|
|
|
|
$return['PARAMS']['in_value_' . $key] = $value;
|
|
|
|
$flags[] = ':in_value_' . $key;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$return['QUERY'] .= ' IN(' . implode(', ', $flags) . ')';
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate a condition to generate query string and params array for
|
|
|
|
* @param string $fieldname : fieldname possibly preceed by '<, >, <=, >=, ! or ='
|
|
|
|
* @param $value : value of field
|
|
|
|
* @return array : array with QUERY and PARAMS
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _evaluate_condition (string $fieldname, $value) : array
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$first_char = mb_substr($fieldname, 0, 1);
|
|
|
|
$second_char = mb_substr($fieldname, 1, 1);
|
|
|
|
|
|
|
|
switch(true)
|
|
|
|
{
|
|
|
|
//Important de traiter <= & >= avant < & >
|
|
|
|
case ('<=' == $first_char . $second_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 2);
|
|
|
|
$operator = '<=';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('>=' == $first_char . $second_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 2);
|
|
|
|
$operator = '>=';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('!=' == $first_char . $second_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 2);
|
|
|
|
$operator = '!=';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('!' == $first_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 1);
|
|
|
|
$operator = '!=';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('<' == $first_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 1);
|
|
|
|
$operator = '<';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('>' == $first_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 1);
|
|
|
|
$operator = '>';
|
|
|
|
break;
|
|
|
|
|
2023-02-24 16:29:10 +01:00
|
|
|
case ('%' == $first_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 1);
|
|
|
|
$operator = 'LIKE';
|
|
|
|
break;
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
case ('=' == $first_char) :
|
|
|
|
$true_fieldname = mb_substr($fieldname, 1);
|
|
|
|
$operator = '=';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
$true_fieldname = $fieldname;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
2019-11-04 17:45:16 +01:00
|
|
|
//Protect against injection in fieldname
|
|
|
|
$true_fieldname = preg_replace('#[^a-zA-Z0-9_]#', '', $true_fieldname);
|
|
|
|
|
2023-02-24 16:29:10 +01:00
|
|
|
// Add a uid to fieldname so we can combine multiple rules on same field
|
|
|
|
$uid = uniqid();
|
|
|
|
|
|
|
|
$query = '`' . $true_fieldname . '` ' . $operator . ' :where_' . $true_fieldname . '_' . $uid;
|
|
|
|
$param = ['where_' . $true_fieldname . '_' . $uid => $value];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
return ['QUERY' => $query, 'PARAM' => $param];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get from table, posssibly with some conditions
|
|
|
|
* @param string $table : table name
|
|
|
|
* @param array $conditions : Where conditions to use, format 'fieldname' => 'value', fieldname can be preceed by operator '<, >, <=, >=, ! or = (by default)' to adapt comparaison operator
|
|
|
|
* @param ?string $order_by : name of column to order result by, null by default
|
|
|
|
* @param string $desc : L'ordre de tri (asc ou desc). Si non défini, ordre par défaut (ASC)
|
|
|
|
* @param string $limit : Le nombre maximum de résultats à récupérer (par défaut pas le limite)
|
|
|
|
* @param string $offset : Le nombre de résultats à ignorer (par défaut pas de résultats ignorés)
|
|
|
|
* @return mixed : False en cas d'erreur, sinon les lignes retournées
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _select (string $table, array $conditions = [], ?string $order_by = null, bool $desc = false, ?int $limit = null, ?int $offset = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
try
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
$wheres = array();
|
|
|
|
$params = array();
|
|
|
|
foreach ($conditions as $label => $value)
|
|
|
|
{
|
|
|
|
$condition = $this->_evaluate_condition($label, $value);
|
|
|
|
$wheres[] = $condition['QUERY'];
|
|
|
|
$params = array_merge($params, $condition['PARAM']);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-09 03:29:40 +01:00
|
|
|
$query = "SELECT * FROM `" . $table . "` WHERE 1 " . (count($wheres) ? 'AND ' : '') . implode(' AND ', $wheres);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
if ($order_by !== null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
$query .= ' ORDER BY ' . $order_by;
|
|
|
|
|
|
|
|
if ($desc)
|
|
|
|
{
|
|
|
|
$query .= ' DESC';
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
if ($limit !== null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
$query .= ' LIMIT :limit';
|
|
|
|
if ($offset !== null)
|
|
|
|
{
|
|
|
|
$query .= ' OFFSET :offset';
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
$query = $this->pdo->prepare($query);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
if ($limit !== null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
$query->bindParam(':limit', $limit, \PDO::PARAM_INT);
|
|
|
|
|
|
|
|
if ($offset !== null)
|
|
|
|
{
|
|
|
|
$query->bindParam(':offset', $offset, \PDO::PARAM_INT);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
foreach ($params as $label => &$param)
|
|
|
|
{
|
|
|
|
$query->bindParam(':' . $label, $param);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
$query->setFetchMode(\PDO::FETCH_ASSOC);
|
|
|
|
$query->execute();
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
return $query->fetchAll();
|
|
|
|
}
|
|
|
|
catch (\PDOException $e)
|
|
|
|
{
|
|
|
|
$error = $query->errorInfo();
|
|
|
|
|
|
|
|
//Get query string and params
|
|
|
|
ob_start();
|
|
|
|
$query->debugDumpParams();
|
|
|
|
$debug_string = ob_get_clean();
|
|
|
|
|
|
|
|
throw new \descartes\exceptions\DescartesExceptionSqlError(
|
|
|
|
'SQL Error : ' . "\n" .
|
|
|
|
'SQLSTATE : ' . $error[0] . "\n" .
|
|
|
|
'Driver Error Code : ' . $error[1] . "\n" .
|
|
|
|
'Driver Error Message : ' . $error[2] . "\n" .
|
|
|
|
'SQL QUERY DEBUG :' . "\n" .
|
|
|
|
'-----------------' . "\n" .
|
|
|
|
$debug_string . "\n" .
|
|
|
|
'-----------------' . "\n"
|
|
|
|
);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get one line from table, posssibly with some conditions
|
|
|
|
* see get
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _select_one (string $table, array $conditions = [], ?string $order_by = null, bool $desc = false, ?int $limit = null, ?int $offset = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:30 +01:00
|
|
|
$result = $this->_select($table, $conditions, $order_by, $desc, $limit, $offset);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
if (empty($result[0]))
|
|
|
|
{
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count line from table, posssibly with some conditions
|
|
|
|
* @param array $conditions : conditions of query Les conditions pour la mise à jour sous la forme "label" => "valeur". Un operateur '<, >, <=, >=, !' peux précder le label pour modifier l'opérateur par défaut (=)
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _count (string $table, array $conditions = []) : int
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
try
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-08 19:17:07 +01:00
|
|
|
$wheres = array();
|
|
|
|
$params = array();
|
|
|
|
foreach ($conditions as $label => $value)
|
|
|
|
{
|
|
|
|
$condition = $this->_evaluate_condition($label, $value);
|
|
|
|
$wheres[] = $condition['QUERY'];
|
|
|
|
$params = array_merge($params, $condition['PARAM']);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-09 03:29:40 +01:00
|
|
|
$query = "SELECT COUNT(*) as `count` FROM `" . $table . "` WHERE 1 " . (count($wheres) ? 'AND ' : '') . implode(' AND ', $wheres);
|
2019-11-08 19:17:07 +01:00
|
|
|
$query = $this->pdo->prepare($query);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
foreach ($params as $label => &$param)
|
|
|
|
{
|
|
|
|
$query->bindParam(':' . $label, $param);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
$query->setFetchMode(\PDO::FETCH_ASSOC);
|
|
|
|
$query->execute();
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
return $query->fetch()['count'];
|
|
|
|
}
|
|
|
|
catch (\PDOException $e)
|
|
|
|
{
|
|
|
|
$error = $query->errorInfo();
|
2019-11-09 03:26:48 +01:00
|
|
|
|
|
|
|
//Get query string and params
|
|
|
|
ob_start();
|
|
|
|
$query->debugDumpParams();
|
|
|
|
$debug_string = ob_get_clean();
|
|
|
|
|
2019-11-08 19:17:07 +01:00
|
|
|
throw new \descartes\exceptions\DescartesExceptionSqlError(
|
|
|
|
'SQL Error : ' . "\n" .
|
|
|
|
'SQLSTATE : ' . $error[0] . "\n" .
|
|
|
|
'Driver Error Code : ' . $error[1] . "\n" .
|
2019-11-09 03:26:48 +01:00
|
|
|
'Driver Error Message : ' . $error[2] . "\n" .
|
|
|
|
'SQL QUERY DEBUG :' . "\n" .
|
|
|
|
'-----------------' . "\n" .
|
|
|
|
$debug_string . "\n" .
|
|
|
|
'-----------------' . "\n"
|
2019-11-08 19:17:07 +01:00
|
|
|
);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update data from table with some conditions
|
|
|
|
* @param string $table : table name
|
2021-01-17 03:16:57 +01:00
|
|
|
* @param array $data : new data to set
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $conditions : conditions of update, Les conditions pour la mise à jour sous la forme "label" => "valeur". Un operateur '<, >, <=, >=, !' peux précder le label pour modifier l'opérateur par défaut (=)
|
|
|
|
* @param array $conditions : conditions to use, format 'fieldname' => 'value', fieldname can be preceed by operator '<, >, <=, >=, ! or = (by default)' to adapt comparaison operator
|
|
|
|
* @return mixed : Number of line modified
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
protected function _update (string $table, array $data, array $conditions = array()) : int
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$params = array();
|
|
|
|
$sets = array();
|
|
|
|
|
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
foreach ($data as $label => $value)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-04 17:45:16 +01:00
|
|
|
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
|
2019-10-29 14:57:13 +01:00
|
|
|
$params['set_' . $label] = $value;
|
|
|
|
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$wheres = array();
|
|
|
|
foreach ($conditions as $label => $value)
|
|
|
|
{
|
2019-10-29 18:33:30 +01:00
|
|
|
$condition = $this->_evaluate_condition($label, $value);
|
2019-10-29 14:57:13 +01:00
|
|
|
$wheres[] = $condition['QUERY'];
|
|
|
|
$params = array_merge($params, $condition['PARAM']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
$query = "UPDATE `" . $table . "` SET " . implode(', ', $sets) . " WHERE 1 " . (count($wheres) ? " AND " : "") . implode(' AND ', $wheres);
|
2019-10-29 18:33:30 +01:00
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete from table according to certain conditions
|
|
|
|
* @param string $table : Table name
|
|
|
|
* @param array $conditions : conditions to use, format 'fieldname' => 'value', fieldname can be preceed by operator '<, >, <=, >=, ! or = (by default)' to adapt comparaison operator
|
|
|
|
* @return mixed : Number of line deleted
|
|
|
|
*/
|
2019-10-29 18:33:30 +01:00
|
|
|
protected function _delete (string $table, array $conditions = []) : int
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
//On gère les conditions
|
|
|
|
$wheres = array();
|
|
|
|
$params = array();
|
|
|
|
foreach ($conditions as $label => $value)
|
|
|
|
{
|
2019-10-29 18:33:30 +01:00
|
|
|
$condition = $this->_evaluate_condition($label, $value);
|
2019-10-29 14:57:13 +01:00
|
|
|
$wheres[] = $condition['QUERY'];
|
|
|
|
$params = array_merge($params, $condition['PARAM']);
|
|
|
|
}
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
$query = "DELETE FROM `" . $table . "` WHERE 1 " . (count($wheres) ? " AND " : "") . implode(' AND ', $wheres);
|
2019-10-29 18:33:30 +01:00
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert new line into table
|
|
|
|
* @param string $table : table name
|
2021-01-17 03:16:57 +01:00
|
|
|
* @param array $data : new data
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return mixed : null on error, number of line inserted else
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
protected function _insert (string $table, array $data) : ?int
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$params = array();
|
|
|
|
$field_names = array();
|
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
foreach ($data as $field_name => $value)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-04 17:45:16 +01:00
|
|
|
//Protect against injection in fieldname
|
|
|
|
$field_name = preg_replace('#[^a-zA-Z0-9_]#', '', $field_name);
|
2019-10-29 14:57:13 +01:00
|
|
|
$params[$field_name] = $value;
|
|
|
|
$field_names[] = $field_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = "INSERT INTO `" . $table . "` (`" . implode('`, `', $field_names) . "`) VALUES(:" . implode(', :', $field_names) . ")";
|
|
|
|
|
|
|
|
//On retourne le nombre de lignes insérées
|
2019-10-29 18:33:30 +01:00
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|