mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-05-01 22:06:28 +02:00
update descartes
This commit is contained in:
parent
1ae58220b8
commit
65ab7a4b1b
7 changed files with 152 additions and 98 deletions
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* 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
|
||||||
* @param $pdo : Une instance de PDO
|
* @param $pdo : Une instance de \PDO
|
||||||
*/
|
*/
|
||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
|
@ -18,30 +18,30 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model constructor
|
* Model constructor
|
||||||
* @param PDO $pdo : PDO connect to use
|
* @param \PDO $pdo : \PDO connect to use
|
||||||
*/
|
*/
|
||||||
public function __construct(PDO $pdo)
|
public function __construct(\PDO $pdo)
|
||||||
{
|
{
|
||||||
$this->pdo = $pdo;
|
$this->pdo = $pdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cette fonction permet créer une connexion à une base SQL via PDO
|
* Cette fonction permet créer une connexion à une base SQL via \PDO
|
||||||
* @param string $host : L'host à contacter
|
* @param string $host : L'host à contacter
|
||||||
* @param string $dbname : Le nom de la base à contacter
|
* @param string $dbname : Le nom de la base à contacter
|
||||||
* @param string $user : L'utilisateur à utiliser
|
* @param string $user : L'utilisateur à utiliser
|
||||||
* @param string $password : Le mot de passe à employer
|
* @param string $password : Le mot de passe à employer
|
||||||
* @return mixed : Un objet PDO ou false en cas d'erreur
|
* @return mixed : Un objet \PDO ou false en cas d'erreur
|
||||||
*/
|
*/
|
||||||
public static function _connect ($host, $dbname, $user, $password, ?string $charset = 'UTF8', ?array $options = null)
|
public static function _connect ($host, $dbname, $user, $password, ?string $charset = 'UTF8', ?array $options = null)
|
||||||
{
|
{
|
||||||
$options = $options ?? [
|
$options = $options ?? [
|
||||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
|
||||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
|
||||||
];
|
];
|
||||||
|
|
||||||
// On se connecte à MySQL
|
// On se connecte à MySQL
|
||||||
$pdo = new PDO('mysql:host=' . $host . ';dbname=' . $dbname . ';charset=' . $charset , $user, $password, $options);
|
$pdo = new \PDO('mysql:host=' . $host . ';dbname=' . $dbname . ';charset=' . $charset , $user, $password, $options);
|
||||||
|
|
||||||
if ($pdo === false)
|
if ($pdo === false)
|
||||||
{
|
{
|
||||||
|
@ -60,7 +60,9 @@
|
||||||
* @param boolean $debug : If we must return debug info instead of data, by default false
|
* @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
|
* @return mixed : Result of query, depend of $return_type | null | array | object | int
|
||||||
*/
|
*/
|
||||||
protected function _run_query (string $query, array $datas = array(), int $return_type = self::FETCHALL, int $fetch_mode = PDO::FETCH_ASSOC, bool $debug = false)
|
protected function _run_query (string $query, array $datas = array(), int $return_type = self::FETCHALL, int $fetch_mode = \PDO::FETCH_ASSOC, bool $debug = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
$query = $this->pdo->prepare($query);
|
$query = $this->pdo->prepare($query);
|
||||||
$query->setFetchMode($return_type);
|
$query->setFetchMode($return_type);
|
||||||
|
@ -95,6 +97,17 @@
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
catch (\PDOException $e)
|
||||||
|
{
|
||||||
|
$error = $query->errorInfo();
|
||||||
|
throw new \descartes\exceptions\DescartesExceptionSqlError(
|
||||||
|
'SQL Error : ' . "\n" .
|
||||||
|
'SQLSTATE : ' . $error[0] . "\n" .
|
||||||
|
'Driver Error Code : ' . $error[1] . "\n" .
|
||||||
|
'Driver Error Message : ' . $error[2] . "\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return last inserted id
|
* Return last inserted id
|
||||||
|
@ -213,6 +226,8 @@
|
||||||
* @return mixed : False en cas d'erreur, sinon les lignes retournées
|
* @return mixed : False en cas d'erreur, sinon les lignes retournées
|
||||||
*/
|
*/
|
||||||
protected function _select (string $table, array $conditions = [], ?string $order_by = null, bool $desc = false, ?int $limit = null, ?int $offset = null)
|
protected function _select (string $table, array $conditions = [], ?string $order_by = null, bool $desc = false, ?int $limit = null, ?int $offset = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
$wheres = array();
|
$wheres = array();
|
||||||
$params = array();
|
$params = array();
|
||||||
|
@ -249,11 +264,11 @@
|
||||||
|
|
||||||
if ($limit !== null)
|
if ($limit !== null)
|
||||||
{
|
{
|
||||||
$query->bindParam(':limit', $limit, PDO::PARAM_INT);
|
$query->bindParam(':limit', $limit, \PDO::PARAM_INT);
|
||||||
|
|
||||||
if ($offset !== null)
|
if ($offset !== null)
|
||||||
{
|
{
|
||||||
$query->bindParam(':offset', $offset, PDO::PARAM_INT);
|
$query->bindParam(':offset', $offset, \PDO::PARAM_INT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,11 +277,32 @@
|
||||||
$query->bindParam(':' . $label, $param);
|
$query->bindParam(':' . $label, $param);
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->setFetchMode(PDO::FETCH_ASSOC);
|
$query->setFetchMode(\PDO::FETCH_ASSOC);
|
||||||
$query->execute();
|
$query->execute();
|
||||||
|
|
||||||
return $query->fetchAll();
|
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"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -290,6 +326,8 @@
|
||||||
* @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 (=)
|
* @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 (=)
|
||||||
*/
|
*/
|
||||||
protected function _count (string $table, array $conditions = []) : int
|
protected function _count (string $table, array $conditions = []) : int
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
$wheres = array();
|
$wheres = array();
|
||||||
$params = array();
|
$params = array();
|
||||||
|
@ -309,11 +347,22 @@
|
||||||
$query->bindParam(':' . $label, $param);
|
$query->bindParam(':' . $label, $param);
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->setFetchMode(PDO::FETCH_ASSOC);
|
$query->setFetchMode(\PDO::FETCH_ASSOC);
|
||||||
$query->execute();
|
$query->execute();
|
||||||
|
|
||||||
return $query->fetch()['count'];
|
return $query->fetch()['count'];
|
||||||
}
|
}
|
||||||
|
catch (\PDOException $e)
|
||||||
|
{
|
||||||
|
$error = $query->errorInfo();
|
||||||
|
throw new \descartes\exceptions\DescartesExceptionSqlError(
|
||||||
|
'SQL Error : ' . "\n" .
|
||||||
|
'SQLSTATE : ' . $error[0] . "\n" .
|
||||||
|
'Driver Error Code : ' . $error[1] . "\n" .
|
||||||
|
'Driver Error Message : ' . $error[2] . "\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace descartes\exceptions;
|
namespace descartes\exceptions;
|
||||||
|
|
||||||
class DescartesException404 extends Exception {};
|
class DescartesException404 extends \Exception {};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace descartes\exceptions;
|
namespace descartes\exceptions;
|
||||||
|
|
||||||
class DescartesExceptionConsoleInvocationError extends Exception {};
|
class DescartesExceptionConsoleInvocationError extends \Exception {};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace descartes\exceptions;
|
namespace descartes\exceptions;
|
||||||
|
|
||||||
class DescartesExceptionRouterInvocationError extends Exception {};
|
class DescartesExceptionRouterInvocationError extends \Exception {};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace descartes\exceptions;
|
namespace descartes\exceptions;
|
||||||
|
|
||||||
class DescartesExceptionRouterUrlGenerationError extends Exception {};
|
class DescartesExceptionRouterUrlGenerationError extends \Exception {};
|
||||||
|
|
||||||
|
|
5
descartes/exceptions/DescartesExceptionSqlError.php
Normal file
5
descartes/exceptions/DescartesExceptionSqlError.php
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
namespace descartes\exceptions;
|
||||||
|
|
||||||
|
class DescartesExceptionSqlError extends \Exception {};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace descartes\exceptions;
|
namespace descartes\exceptions;
|
||||||
|
|
||||||
class DescartesExceptionTemplateNotReadable extends Exception {};
|
class DescartesExceptionTemplateNotReadable extends \Exception {};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue