196 lines
5.2 KiB
PHP
196 lines
5.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace orm\core;
|
|
|
|
use \database\core\Database;
|
|
use \manager\ManagerError;
|
|
use \orm\core\Rows;
|
|
|
|
|
|
// CLASSE MAITRE
|
|
class Table{
|
|
|
|
private static $database = 'logauth';
|
|
|
|
|
|
/* RENVOIE LES DONNEES D'UNE TABLE
|
|
*
|
|
* @table<String> Nom de la table à selectionner
|
|
*
|
|
* @return this<ORM> Retourne une instance de l'ORM
|
|
*
|
|
*/
|
|
public static function get($table_name){
|
|
/* [0] Initialisation des attributs
|
|
=========================================================*/
|
|
$schema = [
|
|
'database' => self::$database,
|
|
'table' => null,
|
|
'columns' => null
|
|
];
|
|
|
|
|
|
/* [1] On vérifie que la table existe
|
|
=========================================================*/
|
|
/* (1) Requête */
|
|
$checkTable = Database::getPDO()->query("SHOW tables FROM ".self::$database);
|
|
$checkTableResult = Database::delNumeric( $checkTable->fetchAll() );
|
|
|
|
/* (2) On met en forme les données */
|
|
$tables = [];
|
|
foreach($checkTableResult as $table)
|
|
$tables[] = $table['Tables_in_'.self::$database];
|
|
|
|
/* (3) Si n'existe pas, on renvoie une erreur */
|
|
if( !in_array($table_name, $tables) )
|
|
return null;
|
|
|
|
/* (4) On enregistre les données */
|
|
$schema['table'] = $table_name;
|
|
|
|
|
|
|
|
/* [2] Si la table existe, on récupère les colonnes
|
|
=========================================================*/
|
|
/* (1) On récupère les colonnes */
|
|
$getColumns = Database::getPDO()->query("SHOW columns FROM ".self::$database.'.'.$table_name);
|
|
$columnsResult = Database::delNumeric( $getColumns->fetchAll() );
|
|
|
|
/* (2) On met en forme les données */
|
|
$columns = [];
|
|
foreach($columnsResult as $col){
|
|
// On formatte le type //
|
|
$type = $col['Type'];
|
|
if( preg_match('/^(int|float|varchar|text)/i', $type, $m) )
|
|
$type = strtolower($m[1]);
|
|
|
|
// On ajoute la colonne //
|
|
$columns[$col['Field']] = [
|
|
'type' => $type,
|
|
'primary' => $col['Key'] == 'PRI'
|
|
];
|
|
}
|
|
|
|
|
|
/* (3) Si on trouve rien, on envoie une erreur */
|
|
if( !is_array($columns) || count($columns) == 0 )
|
|
return null;
|
|
|
|
/* (4) On enregistre les colonnes */
|
|
$schema['columns'] = $columns;
|
|
|
|
|
|
|
|
/* [3] On récupère les clés étrangères
|
|
=========================================================*/
|
|
/* (1) On récupère le texte du 'CREATE TABLE' */
|
|
$getCreateTable = Database::getPDO()->query("show create table ".$table_name);
|
|
$create_table = $getCreateTable->fetch()['Create Table'];
|
|
|
|
/* (2) On découpte en lignes */
|
|
$create_table_lines = explode("\n", $create_table);
|
|
|
|
/* (3) Pour chaque ligne, si c'est une contrainte, on l'enregistre dans la colonne associée */
|
|
foreach($create_table_lines as $i=>$line)
|
|
if( preg_match('/CONSTRAINT `.+` FOREIGN KEY \(`(.+)`\) REFERENCES `(.+)` \(`(.+)`\)+/i', $line, $m) )
|
|
$schema['columns'][$m[1]]['references'] = [$m[2], $m[3]];
|
|
|
|
|
|
|
|
/* [3] On renvoie une instance de 'Rows'
|
|
=========================================================*/
|
|
return new Rows($schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/*** USE CASE :: ACCESS TABLE `user` ***/
|
|
// ORM::Table('user');
|
|
|
|
|
|
/**** USE CASE :: WHERE ****/
|
|
// WHERE `username` = 'someUsername'
|
|
// ORM::Table('user')->whereUsername('someUsername');
|
|
// EQUIVALENT TO
|
|
// ORM::Table('user')->whereUsername('someUsername', Rows::COND_EQUAL);
|
|
|
|
// WHERE `id_user` < 100
|
|
// ORM::Table('user')->whereIdUser(100, Rows::COND_INF);
|
|
|
|
// WHERE `id_user` <= 100
|
|
// ORM::Table('user')->whereIdUser(100, Rows::COND_INFEQ);
|
|
|
|
// WHERE `id_user` > 10
|
|
// ORM::Table('user')->whereIdUser(10, Rows::COND_SUP);
|
|
|
|
// WHERE `id_user` >= 10
|
|
// ORM::Table('user')->whereIdUser(10, Rows::COND_SUPEQ);
|
|
|
|
// WHERE `id_user` in (1, 2, 3, 8)
|
|
// ORM::Table('user')->whereIdUser([1, 2, 3, 8], Rows::COND_IN);
|
|
|
|
// WHERE `id_user` LIKE 'John %'
|
|
// ORM::Table('user')->whereIdUser('John %', Rows::COND_LIKE);
|
|
|
|
|
|
/*** USE CASE :: ORDER BY ****/
|
|
// ORDER BY `a` ASC, `b` DESC
|
|
// Table::get('someTable')
|
|
// ->orderby('a', Rows::ORDER_ASC)
|
|
// ->orderby('b', Rows::ORDER_DESC);
|
|
//
|
|
// Note: `Rows::ORDER_ASC` is set by default if the given FLAG is invalid
|
|
|
|
|
|
/**** USE CASE :: SELECT ****/
|
|
// SELECT id_user, username
|
|
// Table::get('user')
|
|
// ->select('id_user')
|
|
// ->select('username');
|
|
|
|
|
|
/**** USE CASE :: AGGREGATION FUNCTIONS ****/
|
|
// SELECT COUNT(`count`)
|
|
// Table::get('user')->select('count', Rows::SEL_COUNT)
|
|
|
|
// SELECT SUM(distinct `count`)
|
|
// Table::get('user')->select('count', Rows::SEL_SUM, Rows::SEL_DISTINCT);
|
|
|
|
// SELECT AVG(`count`)
|
|
// Table::get('user')->select('count', Rows::SEL_AVG);
|
|
|
|
// SELECT MAX(`id_user`)
|
|
// Table::get('user')->select('id_user', Rows::SEL_MAX);
|
|
|
|
// SELECT MIN(`id_user`)
|
|
// Table::get('user')->select('id_user', Rows::SEL_MIN);
|
|
|
|
// SELECT GROUP_CONCAT(`count`)
|
|
// Table::get('user')->select('count', Rows::SEL_CONCAT);
|
|
|
|
|
|
|
|
/**** USE CASE :: FETCH ****/
|
|
// SELECT ... FROM ... WHERE ... ORDERBY ... LIMIT ...
|
|
// Table::get('user')
|
|
// ->select('id_user')
|
|
// ->fetch();
|
|
|
|
// SELECT UNIQUE ... FROM ... WHERE ... ORDERBY ... LIMIT ...
|
|
// Table::get('user')
|
|
// ->select('id_user')
|
|
// ->unique->fetch();
|
|
|
|
|
|
/**** USE CASE :: TABLE JOIN ****/
|
|
// WHERE `user`.`id_user` = `user_merge`.`id_user`
|
|
// Table::get('user_merge')->join(
|
|
// Table::get('user')->whereIdUser(1, Rows::COND_SUP)
|
|
// );
|