255 lines
12 KiB
PHP
255 lines
12 KiB
PHP
<?php
|
|
namespace Module\Agefoddx\Classe\GestionBD;
|
|
|
|
class ligneGestion{
|
|
/**
|
|
* @param string $nomTable nom de la table d'agefodd
|
|
* @param int $tid id de la table
|
|
* @return array|false renvoie la liste des colonnes non liée présente dans la table ciblée d'agefodd, sans id_source et table_source car celle-ci sont généré par le code
|
|
*/
|
|
public function getListeLigneAgefodd(string $nomTable, int $tid) {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$nomTable = $db->escape($nomTable); // pour protéger contre l'injection sql
|
|
$list = [];
|
|
$sql = "SELECT column_name as 'NomLigne' FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_NAME = '$nomTable' AND column_name != 'id_source'
|
|
AND column_name != 'table_source' AND EXTRA != 'auto_increment';";
|
|
$resql1=$db->query($sql);
|
|
$listeFk = array_column(self::getListeFK($nomTable, $tid), "NomLigne") ?? [];
|
|
$listeLigneECorreler = array_column(self::getListeLigneCorreler($tid), "nomLigneA");
|
|
if ($resql1) {
|
|
while ($row = $db->fetch_array($resql1)) {
|
|
$rowid = htmlspecialchars($row['NomLigne']); // Par alias (nom de la colonne)
|
|
if(!in_array($rowid, $listeFk) && !in_array($rowid, $listeLigneECorreler)) $list[] = $rowid;
|
|
}
|
|
} else { return false; }
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* @param int $tid identifient de la table
|
|
* @return array renvoie la liste des lignes liées pour la table passée en paramètre
|
|
*/
|
|
public function getListeLigneCorreler(int $tid) : array{
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$tid = $db->escape($tid); // pour protéger contre l'injection sql
|
|
$sql = "SELECT rid, bdid, tid, nomligneexterne, nomligneagfodd FROM llx_desal_external_row WHERE tid = '$tid' ";
|
|
$resql=$db->query($sql);
|
|
if ($resql) { // Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql)) {
|
|
$list[] = ["idBD"=>$row["bdid"], "idTable"=>$row["tid"],"idLigne"=>$row["rid"],"nomLigneE"=>$row["nomligneexterne"],"nomLigneA"=>$row["nomligneagfodd"]];
|
|
}
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* @param int $rid id de la colonne
|
|
* @return array renvoie la ligne à partir de son id
|
|
*/
|
|
public function getLigneByPk(int $rid){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$rid = $db->escape($rid); // pour protéger contre l'injection sql
|
|
$sql = "SELECT rid, bdid, tid, nomligneexterne, nomligneagfodd FROM llx_desal_external_row WHERE rid = '$rid';";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
$row = $db->fetch_array($resql);
|
|
$list = ["idBD"=>$row["bdid"], "idTable"=>$row["tid"], "idLigne"=>$row["rid"],
|
|
"nomLigneE"=>$row["nomligneexterne"],"nomLigneA"=>$row["nomligneagfodd"]];
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* @param int $idTable id de la table
|
|
* @param string $ligneA nom de la colonne d'agefodd
|
|
* @return bool renvoie-la si la ligne est déjà liée dans cette table
|
|
*/
|
|
public function correlationEstDansTable(int $idTable, string $ligneA): bool{
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$ligneA = $db->escape($ligneA); $idTable = $db->escape($idTable); // pour protéger contre l'injection sql
|
|
$sql = "SELECT COUNT(*) as nb FROM llx_desal_external_row WHERE tid = $idTable AND nomLigneAgfodd = '$ligneA'";
|
|
$resql=$db->query($sql); $row = $db->fetch_array($resql); $nb = $row["nb"];
|
|
if ($nb == 0) return false; return true;
|
|
}
|
|
|
|
/**
|
|
* @param int $tid id de la table
|
|
* @param int $bdid id de la base de donnée
|
|
* @param string $nomLigneA nom de la ligne d'agefodd
|
|
* @param string $nomLigneE nom de la ligne externe
|
|
* @return bool renvoie si l'insertion à fonctionner
|
|
*/
|
|
public function insertion(int $tid, int $bdid, string $nomLigneA, string $nomLigneE) : bool{
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$tid = $db->escape($tid); $bdid = $db->escape($bdid); // pour protéger contre l'injection sql
|
|
$nomLigneA = $db->escape($nomLigneA); $nomLigneE = $db->escape($nomLigneE); // pour protéger contre l'injection sql
|
|
$sql = "INSERT INTO llx_desal_external_row( bdid, tid, nomLigneExterne, nomLigneAgfodd )
|
|
VALUES ('$bdid','$tid','$nomLigneE','$nomLigneA')";
|
|
$resql=$db->query($sql);
|
|
if ($resql) return true;
|
|
else return false;
|
|
}
|
|
|
|
/**
|
|
* @param int $tid id de la table
|
|
* @param int $bdid id de la bd
|
|
* @param string $nomLigneA nom de la ligne d'agefodd
|
|
* @param mixed $valueFixe la valeur fixe à lier avec la ligne d'agefodd
|
|
* @return bool renvoie si l'insertion a bien été éffectuer
|
|
*/
|
|
public function insertionValue(int $tid, int $bdid, string $nomLigneA, $valueFixe){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$tid = $db->escape($tid); $bdid = $db->escape($bdid); // pour protéger contre l'injection sql
|
|
$nomLigneA = $db->escape($nomLigneA); $valueFixe = $db->escape($valueFixe); // pour protéger contre l'injection sql
|
|
$sql = "INSERT INTO llx_desal_external_row( bdid, tid, valeurFixe, nomLigneAgfodd)
|
|
VALUES ('$bdid','$tid','$valueFixe','$nomLigneA')";
|
|
$resql=$db->query($sql);
|
|
if ($resql) return true; else return false;
|
|
}
|
|
|
|
/**
|
|
* @param int $rid id de la colonne
|
|
* @return bool renvoie la valeur de la suppression
|
|
*/
|
|
public function suppression(int $rid){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$rid = $db->escape($rid); // pour protéger contre l'injection sql
|
|
$sql = "DELETE FROM llx_desal_external_row WHERE rid='$rid';";
|
|
$resql=$db->query($sql);
|
|
if ($resql) { return true; } return false;
|
|
}
|
|
|
|
/**
|
|
* @param string $typeA type de la ligne d'agefodd
|
|
* @param string $typeE type de la ligne en correlation
|
|
* @return bool renvoie si les deux lignes sont corrélées
|
|
*/
|
|
public function sontCompatible(string $typeA, string $typeE) : bool {
|
|
$compatible_types = [
|
|
['int', 'bigint', 'smallint', 'mediumint', 'tinyint'],
|
|
['varchar', 'text', 'char'],
|
|
['decimal', 'float', 'double'],
|
|
['datetime', 'timestamp', 'date']
|
|
];
|
|
$super_type = ['varchar', 'text'];
|
|
$lien_plus_peux_moins = [['decimal', 'float', 'double'], ['int', 'bigint', 'smallint', 'mediumint', 'tinyint']];
|
|
if (in_array($typeA, $super_type)) return true;
|
|
if (in_array($typeA, $lien_plus_peux_moins[0]) && in_array($typeE, $lien_plus_peux_moins[1])) return true;
|
|
foreach ($compatible_types as $group) {
|
|
if (in_array($typeA, $group) && in_array($typeE, $group)) return true;
|
|
}return false;
|
|
}
|
|
|
|
/**
|
|
* @param string $nomTable nom de la table
|
|
* @param string $nomLigne nom de la ligne
|
|
* @return string|null renvoie le type de la ligne
|
|
*/
|
|
public function getTypeLigne(string $nomTable, string $nomLigne) : ?string {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$nomTable = $db->escape($nomTable); $nomLigne = $db->escape($nomLigne);
|
|
$sql = "SELECT COLUMN_TYPE as 'colType'FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_NAME = '$nomTable' AND COLUMN_NAME = '$nomLigne';";
|
|
$resql=$db->query($sql);
|
|
if ($resql) { while ( $row = $db->fetch_array($resql) ) { $liste = $row['colType']; } return $liste; }
|
|
else return null;
|
|
}
|
|
|
|
/**
|
|
* @param string $tableA nom de la table d'agefodd
|
|
* @param int $tid id de la table
|
|
* @return array|false renvoie la liste des clefs primaires
|
|
*/
|
|
public function getListeFK(string $tableA, int $tid){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$tid = $db->escape($tid);
|
|
$sql = "SELECT COLUMN_NAME as 'NomLigne', REFERENCED_TABLE_NAME AS 'TableReference'
|
|
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
WHERE TABLE_NAME = '$tableA' AND REFERENCED_TABLE_NAME IS NOT NULL;";
|
|
$listeLigneECorreler = array_column(self::getListeLigneCorreler($tid), "nomLigneA");
|
|
$resql=$db->query($sql);
|
|
if ($resql) { // Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql)) {
|
|
if(!in_array($row['NomLigne'], $listeLigneECorreler))
|
|
$list[] = ["NomLigne"=>$row['NomLigne'], "TableReference"=>$row['TableReference']];
|
|
}
|
|
} else { return false; }return $list;
|
|
}
|
|
|
|
/**
|
|
* @param string $tableA nom table agefodd
|
|
* @param int $tid id de la table
|
|
* @return array|false renvoie la liste des clefs primaires corréler
|
|
*/
|
|
public function getListeFKCorreler(string $tableA, int $tid){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$tid = $db->escape($tid);
|
|
$sql = "SELECT COLUMN_NAME as 'NomLigne', REFERENCED_TABLE_NAME AS 'TableReference' FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '$tableA' AND REFERENCED_TABLE_NAME IS NOT NULL;";
|
|
$listeLigneECorreler = array_column(self::getListeLigneCorreler($tid), "nomLigneA");
|
|
$resql=$db->query($sql);
|
|
if ($resql) {// Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql)) {
|
|
if(in_array($row['NomLigne'], $listeLigneECorreler))$list[] = ["NomLigne"=>$row['NomLigne'], "TableReference"=>$row['TableReference']];
|
|
}
|
|
} else {return false;
|
|
}return $list;
|
|
}
|
|
|
|
/**
|
|
* @param string $tableA nom de la table d'agefodd
|
|
* @param int $tid id de la table
|
|
* @return bool renvoie si la table possède une clef primaire non corréler
|
|
*/
|
|
public function possedeFk(string $tableA, int $tid){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$tid = $db->escape($tid);
|
|
$sql = "SELECT COLUMN_NAME as 'NomLigne', REFERENCED_TABLE_NAME AS 'TableReference' FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '$tableA' AND REFERENCED_TABLE_NAME IS NOT NULL;";
|
|
$listeLigneECorreler = array_column(self::getListeLigneCorreler($tid), "nomLigneA");
|
|
$resql=$db->query($sql);
|
|
if ($resql) {// Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql)) {
|
|
$list[] = ["NomLigne"=>$row['NomLigne'], "TableReference"=>$row['TableReference']];
|
|
}
|
|
} else { return false; }
|
|
return sizeof($list) > 0;
|
|
}
|
|
|
|
/**
|
|
* @param string $table nom de la table
|
|
* @param string $clef nom de la clef
|
|
* @return false|string revoit la colonne de référence
|
|
*/
|
|
public function getClefReferente(string $table, string $clef) : string{
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$table = $db->escape($table); $clef = $db->escape($clef);
|
|
$sql = "SELECT REFERENCED_COLUMN_NAME AS 'fk'
|
|
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
WHERE TABLE_NAME = '$table'
|
|
AND COLUMN_NAME = '$clef';";
|
|
$resql=$db->query($sql);
|
|
if ($resql) { $clef = $db->fetch_array($resql)['fk'];
|
|
} else { return false; } return $clef;
|
|
}
|
|
|
|
} |