NxTIC/manager/ORM/Table.php

132 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace manager\ORM;
use \manager\Database;
use \manager\ManagerError;
use \manager\ORM\Rows;
// CLASSE MAITRE
class Table{
private static $database = 'stefproject';
/* 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
// ORM::Table = ORM::Table('user');
//
// // USE CASE :: getBy{ATTRIBUTE}
// ORM::Row = ORM::Table->getByUsername('someUsername'); // ORM_FETCH by default
// ORM::Row = ORM::Table->getByUsername('someUsername', ORM_FETCH);
// ORM::Column = ORM::Table->getByUsername('someUsername', ORM_FETCHALL);
//
// // USE CASE :: getById -> primary key(s)
// ORM::Row = ORM::Table->getById(5, 7); // because PRIMARY KEY is composed by '5' and '7'
//
// // USE CASE :: getAll
// ORM::Column = ORM::Table->getAll();
//
// // USE CASE :: select(FIELD)
// mixed = ORM::Row->select('username');
//
// // USE CASE :: select(FIELD1, FIELD2, ...)
// mixed = ORM::Row->select('id_user', 'username');