117 lines
3.0 KiB
PHP
117 lines
3.0 KiB
PHP
|
<?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 ses données
|
||
|
=========================================================*/
|
||
|
/* (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|varchar|text)/i', $type, $m) )
|
||
|
$type = ($m[1] == 'int') ? 'int' : 'text';
|
||
|
|
||
|
// 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 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');
|
||
|
?>
|