enable to get results from table with joigned tables

This commit is contained in:
Romain Guerrero 2016-02-05 22:20:58 +01:00
parent 8c05efaa7c
commit 0072a8b2f8
1 changed files with 76 additions and 55 deletions

View File

@ -113,35 +113,42 @@
/**
* Cette fonction décrit une table et retourne un tableau sur cette description
* @param string $table : Le nom de la table a analyser
* @param string $tables : Le nom de la ou des tables a analyser séparées par une virgule
* @return mixed : Si la table existe un tableau la décrivant, sinon false
*/
public function describeTable($table)
public function describeTable($tables)
{
if (!$this->tableExist($table))
{
return false;
}
//On recupere tous les champs pour pouvoir apres les analyser
$query = 'DESCRIBE ' . $table;
$fields = $this->runQuery($query);
$tableArray = explode(',', $tables);
$return = array();
foreach ($fields as $field)
{
$fieldInfo = array();
$fieldInfo['NAME'] = $field['Field'];
$fieldInfo['NULL'] = $field['Null'] == 'NO' ? false : true;
$fieldInfo['AUTO_INCREMENT'] = $field['Extra'] == 'auto_increment' ? true : false;
$fieldInfo['PRIMARY'] = $field['Key'] == 'PRI' ? true : false;
$fieldInfo['FOREIGN'] = $field['Key'] == 'MUL' ? true : false;
$fieldInfo['UNIQUE'] = $field['Key'] == 'UNI' ? true : false;
$fieldInfo['TYPE'] = mb_convert_case(preg_replace('#[^a-z]#ui', '', $field['Type']), MB_CASE_UPPER);
$fieldInfo['SIZE'] = filter_var($field['Type'], FILTER_SANITIZE_NUMBER_INT);
$fieldInfo['HAS_DEFAULT'] = $field['Default'] !== NULL ? true : false;
$fieldInfo['DEFAULT'] = $field['Default'];
$return[$field['Field']] = $fieldInfo;
foreach ($tableArray as $key => $table) {
if (!$this->tableExist($table))
{
return false;
}
//On recupere tous les champs pour pouvoir apres les analyser
$query = 'DESCRIBE ' . $table;
$fields = $this->runQuery($query);
foreach ($fields as $field)
{
$fieldInfo = array();
$fieldInfo['NAME'] = $field['Field'];
$fieldInfo['NULL'] = $field['Null'] == 'NO' ? false : true;
$fieldInfo['AUTO_INCREMENT'] = $field['Extra'] == 'auto_increment' ? true : false;
$fieldInfo['PRIMARY'] = $field['Key'] == 'PRI' ? true : false;
$fieldInfo['FOREIGN'] = $field['Key'] == 'MUL' ? true : false;
$fieldInfo['UNIQUE'] = $field['Key'] == 'UNI' ? true : false;
$fieldInfo['TYPE'] = mb_convert_case(preg_replace('#[^a-z]#ui', '', $field['Type']), MB_CASE_UPPER);
$fieldInfo['SIZE'] = filter_var($field['Type'], FILTER_SANITIZE_NUMBER_INT);
$fieldInfo['HAS_DEFAULT'] = $field['Default'] !== NULL ? true : false;
$fieldInfo['DEFAULT'] = $field['Default'];
if (sizeof($tableArray) == 1) {
$return[$field['Field']] = $fieldInfo;
} else {
$return[$table.'.'.$field['Field']] = $fieldInfo;
}
}
}
return $return;
@ -300,13 +307,27 @@
* @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)
* @param string $joinConditions : Les tables à joindre avec leurs conditions (par défaut pas de jointure)
* @return mixed : False en cas d'erreur, sinon les lignes retournées
*/
public function getFromTableWhere($table, $restrictions = array(), $order_by = '', $desc = false, $limit = false, $offset = false)
public function getFromTableWhere($table, $restrictions = array(), $order_by = '', $desc = false, $limit = false, $offset = false, $joinConditions = array())
{
$restrictions = !is_array($restrictions) ? array() : $restrictions;
$tableToDescribe = $table;
$fields = $this->describeTable($table);
// on gère les jointures et récupère toutes les tables
$join = ' ';
foreach ($joinConditions as $joinCondition) {
if (array_key_exists('table', $joinCondition)) {
$tableJointure = $joinCondition['table'];
$tableToDescribe .= ',' . $tableJointure;
$onCondition = array_key_exists('on', $joinCondition) ? " ON " . $joinCondition['on'] : '';
$type = array_key_exists('type', $joinCondition) ? $joinCondition['type'] : '';
$join .= $type . " JOIN " . $tableJointure . $onCondition . " ";
}
}
$fields = $this->describeTable($tableToDescribe);
if (!$fields)
{
return false;
@ -367,7 +388,7 @@
$i++;
}
$query = "SELECT * FROM " . $table . " WHERE 1 " . (count($wheres) ? 'AND ' : '') . implode('AND ', $wheres);
$query = "SELECT * FROM " . $table . $join . " WHERE 1 " . (count($wheres) ? 'AND ' : '') . implode('AND ', $wheres);
if ($order_by)
{