include_once('functions.php'); abstract class DB_Access implements DB_Manager { ## L'objet a été initialisé var $IsInit = false; ## retour par défaut des méthodes Search, GetJoinedObj, GetLinkDB_Obj, DistinctInTbl var $ReturnType = 'OBJECT'; static $DB_Server; function SetReturnType($v) { $this->ReturnType = $v; } function GetReturnType() { return $this->ReturnType; } function GetDBServer() { return self::$DB_Server; } function ReturnResult(&$r) { switch ($this->ReturnType) { case 'NO_CACHE': case 'COLLECTION': return new Collection($this, $r); break; case 'ASSOC': return $this->ReturnAssoc($r); break; case 'KEY': return $this->ReturnKeys($r); break; case 'COUNT': return $this->ReturnCount($r); break; case 'OBJECT': default: return $this->ReturnObj($r); break; } } function Init() { $this->IsInit = true; if ($this->Id == 0) return false; if (!($Nfo = $this->LoadInTbl())) { $this->Id = 0; } return $Nfo; } /* * Assigne une liste d'objet à une instance * args (object): passage de n objets en argument * La méthode "object->classname" est appelé dans l'instance */ function AssignObjects() { $numArgs = func_num_args(); if ($numArgs > 0) { for ($i = 0; $i < $numArgs; $i ++) { $o = func_get_arg($i); if (is_object($o)) { $c = get_class($o); if (method_exists($this, 'Set'.$c)) { call_user_method('Set'.$c, $this, $o); } } } } } /* * Renvoie le nom de la table + nom de la colonne primaire */ function GetColName() { return '`'.$this->TblName.'`.`'.$this->ColName.'`'; } /* * Renvoie le nom de la table + nom de la colonne primaire * dans la table définie par le paramètre $o * $o (Objet) => recherche de la table de jointure * $o (String) => concaténation simple */ function GetFK($o) { if (is_object($o)) { return '`'.$this->GetJoinTbl($o).'`.`'.$this->ColName.'`'; } else if (is_string($o)) { return '`'.$o.'`.`'.$this->ColName.'`'; } else { $Error = 'Erreur lors de l\'appel GetFK pour la classe '.get_class($this); $Error .= __FILE__.' , ligne '.__LINE__; die ($Error); } } function SetInitialisation($v) { $this->Initialisation = $v; } function __sleep() { $Save = array(); $Dont_Save = array('ColName', 'TblName', 'InDB', 'OrdCol', 'LabelCol', 'ColName'); foreach(get_object_vars($this) as $k => $v) { if (!(in_array($k, $Dont_Save))) { array_push($Save, $k); } } return $Save; } /* $S : Liste des colonnes à sélectionner dans la table $W : clause WHERE $T : Table(s) $L : LIMIT array(OFFSET, NOMBRE) */ function LoadInTbl($S = array(), $W = array(), $T = '', $L = array(), $O = '') { $L = $this->BuilLimit($L); $S = (sizeof($S) > 0)?join(", ", $S):"*"; $W = $this->BuildWhere((sizeof($W) > 0)?$W:$this); $T = $this->BuildTblList($T); if (strlen($O) > 0) $O = $this->BuildOrder($O); $q = ' SELECT '.$S.' FROM '.$T.' '.$W.' '.$O.' '.$L; $DB_Server = $this->GetDBServer(); if($DB_Server->query($q)) { if ($L != '') { $t = array(); while($tmp = $DB_Server->fetch()) { array_push($t, $tmp); } } else { $t = $DB_Server->fetch(); } } // db_free_result($r); return $t; } function BuildWhere($W, $Default = null) { if (is_array($W)) { if (sizeof($W) > 0) { $tmp = array(); foreach ($W as $v) { if (is_object($v)) { array_push($tmp, $v->ColName.' = '.(is_string($v->GetId())?"'".$v->GetId()."'":$v->GetId())); } else if (is_string($v) or is_int($v)) { array_push($tmp, $v); } } $W = 'WHERE '.join("\nAND ", $tmp); } else { $W = ''; } } else if (is_object($W)) { $W = 'WHERE '.$W->ColName.' = '.$W->GetId(); } else if (is_string($W)) { $W = 'WHERE '.$W; } if (strlen($W) == 0 && isset($Default)) { $W = $this->BuildWhere($Default); } return $W; } function BuilLimit($L) { return (sizeof($L) == 2 && $L[0] >= 0 && $L[1] > 0)?'LIMIT '.$L[0].', '.$L[1]:''; } function BuildOrder($O, $T = array()) { $AddPrefix = ($this->BuildTblList() == $this->BuildTblList($T)); /* if (is_array($O) && sizeof($O) > 0) { return 'ORDER BY '.join(', ', $O); } else if (is_string($O) && strlen($O) > 0) { return 'ORDER BY '.$O; } else if (!empty($this->OrdCol) && $AddPrefix) { return 'ORDER BY '.($AddPrefix?$this->BuildTblList().'.':'').$this->OrdCol; } else { return ''; } */ if (is_array($O) && sizeof($O) > 0) { return 'ORDER BY '.join(', ', $O); } else if (is_string($O) && strlen($O) > 0) { return 'ORDER BY '.$O; } else if (!empty($this->OrdCol)) { return $this->BuildOrder($this->OrdCol, $T); } else { return ''; } } function BuildTblList($T = '') { if (is_array($T) && sizeof($T) > 0) { return join(', ', $T); } else if (is_string($T) && strlen($T) > 0) { return $T; } else if (is_object($T) && isset($T->TblName)) { return $T->TblName; } else { return $this->TblName; } } function GetJoinTbl(&$Obj) { $PREFIX = defined('TBL_PREFIX')?TBL_PREFIX:''; $Tbl1 = str_replace($PREFIX, '', $this->TblName); $Tbl2 = str_replace($PREFIX, '', $Obj->TblName); if ($Tbl1 < $Tbl2) { return $PREFIX.$Tbl1.'_'.$Tbl2; } else { return $PREFIX.$Tbl2.'_'.$Tbl1; } } function BuildIn($a, $T = '') { foreach ($a as $v) { if (is_object($v) && get_class($v) == get_class($this)) { $a = array_keys($a); } break; } return ((is_string($T) && strlen($T) > 0)?$this->BuildTblList($T).'.':'').$this->ColName.' IN ('.join(', ', $a).')'; } function ReturnObj($r) { if (!$r || db_num_rows($r) <= 0) { return false; } $l = array(); while ($t = db_fetch_assoc($r)) { $l[intval($t[$this->ColName])] = Factory::Build(get_class($this), intval($t[$this->ColName])); } db_free_result($r); return $l; } function ReturnKeys($r) { if (!$r || db_num_rows($r) <= 0) { return false; } $l = array(); while ($t = db_fetch_assoc($r)) { array_push($l, intval($t[$this->ColName])); } db_free_result($r); return $l; } function ReturnAssoc($r) { if (!$r || db_num_rows($r) <= 0) { return false; } $l = array(); while ($t = db_fetch_assoc($r)) { $l[intval($t[$this->ColName])] = $t; } db_free_result($r); return $l; } function ReturnCount($r) { if (db_num_rows($r)) { $l = db_num_rows($r); } else { $l = 0; } db_free_result($r); return $l; } /* $Id : tableau de clefs renvoie une liste d'objet */ function InitList($Id) { if (!empty($Id) && is_array($Id) && sizeof($Id) > 0) { return $this->Search($this->BuildIn($Id)); } return false; } /* $W : clause WHERE $O : Order $L : LIMIT array(OFFSET, NOMBRE) $T : Table(s) */ function Search($W = array(), $O = array(), $L = array(), $T = array()) { $q = ' SELECT DISTINCT('.$this->ColName.') FROM '.$this->BuildTblList($T).' '. $this->BuildWhere($W).' '. $this->BuilLimit($L); /* ' $this->BuildOrder($O, $T).' */ if (isset($_SESSION['DEBUG_UTIL']) && $_SESSION['DEBUG_UTIL'] == true) echo '
'.$q.''; $DB_Server = $this->GetDBServer(); return $this->ReturnResult($DB_Server->query($q)); } function RecordQuery($q) {} /* $VAL = array(colonne sql => valeur) $T = Table(s) $Ignore : tenir compte des contraintes d'unicité $Delayed : insertion delayed */ function InsertInTbl($VAL = array(), $T = '', $IGNORE = false, $DELAYED = false) { ## Insertion d'une écriture ds la table de l'objet en cours ## $VAL[Col] = Val if (sizeof($VAL) < 1) return false; $Col = array_keys($VAL); $Val = array_values($VAL); if (sizeof($Col) != sizeof($Val)) return false; $T = $this->BuildTblList($T); foreach ($Val as $k => $v) { $Val[$k] = is_string($v)?"'".$v."'":$v; } $q = ' INSERT '.($DELAYED?'DELAYED':'').' '.($IGNORE?'IGNORE':'').' INTO '.$T.' ('.join(', ', $Col).') VALUES ('.join(', ', $Val).') '; $DB_Server = $this->GetDBServer(); if($DB_Server->query($q)) { return $DB_Server->DB_Insert_Id; } else { return 0; } } /* $VAL = array(colonne sql => valeur) $T = Table(s) $A = paramètre AND (en plus de la clause WHERE ColName = Id) */ function UpdateInTbl($VAL = array(), $T = '', $A = array()) { ## Modification d'une écriture ds la table de l'objet en cours ## $VAL[Col] = Val if (sizeof($VAL) < 1) return false; $T = $this->BuildTblList($T); $SET = array(); foreach ($VAL as $k => $v) { $SET[] = $k.' = '.(is_string($v)?"'".$v."'":$v); } if (is_string($A) && strlen($A) > 0) { $A = array($A); } else if (!is_array($A)) { $A = array($A); } array_push($A, $this); $W = $this->BuildWhere($A); $q = ' UPDATE '.$T.' SET '.join(", \n", $SET).' '.$W; $DB_Server = $this->GetDBServer(); $DB_Server->query($q); } function CountInTbl($T = array(), $W = array(), $Option = array()) { ## Récupère le nombre d'occurences de l'objet courant ## dans la table $Tbl $W = $this->BuildWhere($W, $this); $T = $this->BuildTblList($T); $O = (sizeof($Option) > 0)?join("\n", $Option):''; $q = ' SELECT COUNT(*) AS Total_Count FROM '.$T.' '. $W.' '. $O; $DB_Server = $this->GetDBServer(); $Return = 0; if ($DB_Server->query($q)) { if ($DB_Server->DB_Num_Rows == 1) { $t = $DB_Server->fetch(); $Return = intval($t['Total_Count']); } } return $Return; } function RemoveFromTbl($T, $A = array(), $Optimize = true) { ## Enlève une occurence d'un objet d'une table ($Tbl) if (is_string($A) && strlen($A) > 0) { $A = array($A); } else if (is_object($A) && $A->GetId() > 0) { $A = array($A); } array_push($A, $this); $W = $this->BuildWhere($A); $T = $this->BuildTblList($T); $q = ' DELETE FROM '.$T.' '.$W; $DB_Server = $this->GetDBServer(); $DB_Server->query($q); } function RemoveFromDB() { ## Enlève une occurence de toute la base ## Utilise la variable $InDB (propre à chaque classe) foreach($this->InDB as $k => $v) $this->RemoveFromTbl($v, array(), false); } function GetJoinedObj(&$Obj, $W = array(), $O = array(), $L = array()) { $Tbl = array($this->GetJoinTbl($Obj), $Obj->TblName); $L = $this->BuilLimit($L); $O = $this->BuildOrder($O, $Tbl); if (is_string($W) && strlen($W) > 0) { $W = array($W); } else if (is_object($W) && $W->GetId() > 0) { $W = array($W); } else if (!is_array($W)) { $W = array(); } if ($Obj->GetId() > 0) { array_push($W, $this->GetJoinTbl($Obj).'.'.$Obj->ColName.' = '.$Obj->GetId()); } array_push($W, $this->GetJoinTbl($Obj).'.'.$this->ColName.' = '.$this->GetId()); array_push($W, $this->GetJoinTbl($Obj).'.'.$Obj->ColName.' = '.$Obj->BuildTblList().'.'.$Obj->ColName); $W = $this->BuildWhere($W); $q = ' SELECT '.$this->GetJoinTbl($Obj).'.'.$Obj->ColName.' FROM '.$this->BuildTblList($Tbl).' '.$W.' '.$O.' '.$L; $DB_Server = $Obj->GetDBServer(); return $this->ReturnResult($DB_Server->query($q)); } function InsertThreeJoinedObj(&$Obj_1, &$Obj_2, $Additional_Cols = array()) { ## Insertion dans une table de jointure à 3 clés ## Seuls le 1er objet est pris en compte pour déterminer le nom de la table de jointure if ($Obj_1->GetId() == 0 || $Obj_2->GetId() == 0 || $this->GetId() == 0) return false; $a = array($Obj_2->ColName => $Obj_2->GetId()); if (is_array($Additional_Cols) && sizeof($Additional_Cols) > 0) { $a = array_merge($a, $Additional_Cols); } $this->InsertJoinedObj($Obj_1, $a); } function InsertJoinedObj(&$Obj, $Additional_Cols = array()) { if ($Obj->GetId() == 0 || $this->GetId() == 0) return false; $a = array( $this->ColName => $this->GetId(), $Obj->ColName => $Obj->GetId(), ); if (is_array($Additional_Cols) && sizeof($Additional_Cols) > 0) { $a = array_merge($a, $Additional_Cols); } $this->InsertInTbl($a, $this->GetJoinTbl($Obj), true); } function CountJoinedObj(&$Obj, $W = array()) { $Tbl = array($this->GetJoinTbl($Obj), $Obj->TblName); if (is_string($W) && strlen($W) > 0) { $W = array($W); } else if (is_object($W) && $W->GetId() > 0) { $W = array($W); } array_push($W, $this, $this->GetJoinTbl($Obj).'.'.$Obj->ColName.' = '.$Obj->BuildTblList().'.'.$Obj->ColName); $q = ' SELECT COUNT('.$this->GetJoinTbl($Obj).'.'.$Obj->ColName.') AS Total_Count FROM '.$this->BuildTblList($Tbl).' '.$this->BuildWhere($W); $DB_Server = $this->GetDBServer(); $Return = 0; if ($DB_Server->query($q)) { if ($DB_Server->DB_Num_Rows == 1) { $t = $DB_Server->fetch(); $Return = intval($t['Total_Count']); } } return $Return; } function RemoveJoinedObj() { $nObj = func_num_args(); if ($nObj == 0) return false; $Primary_Obj = func_get_arg(0); if ($Primary_Obj->GetId() == 0 || $this->GetId() == 0) return false; $Where = array($Primary_Obj); if ($nObj > 1) { for ($i = 1; $i < $nObj; $i ++) { $Tmp_Obj = func_get_arg($i); if (is_object($Tmp_Obj)) { array_push($Where, $Tmp_Obj); } } unset($Tmp_Obj); } $this->RemoveFromTbl($this->GetJoinTbl($Primary_Obj), $Where); } ## Obsolète : seulement présente dans CLIENT function GetLinkDB_Obj($Tbl, &$Obj, $L = array(), $A = array(), $O = array()) { if (is_string($A) && strlen($A) > 0) { $A = array($A); } array_push($A, $this); if (is_array($Tbl)) { $Col = $Tbl[0].".".$Obj->ColName; $Tbl = join("`, `", $Tbl); } else $Col = $Obj->ColName; $q = ' SELECT '.$Col.' FROM `'.$Tbl.'` '.$this->BuildWhere($A).' '.$this->BuildOrder($O, $Tbl).' '.$this->BuilLimit($L); $DB_Server = $Obj->GetDBServer(); if ($DB_Server->query($q)) { } else { return false; } return $Return; $r = db_query($q); return $Obj->ReturnResult($r); } function DistinctInTbl($Tbl, $O = array(), $L = array()) { $q = ' SELECT DISTINCT('.$this->GetColName().') FROM '.$Tbl.', '.$this->TblName.' WHERE '.$this->GetFK($Tbl).' = '.$this->GetColName().' '.$this->BuildOrder($O, $Tbl).' '.$this->BuilLimit($L).' '; $DB_Server = $Obj->GetDBServer(); return $this->ReturnResult($DB_Server->query($q)); } function ToOption($C = array(), $O = array() , $SELECTED = array(), $W = array(), $L = array()) { ## Renvoie une liste d'options pour un select if (sizeof($C) == 0) { $C = (is_array($this->LabelCol))?$this->LabelCol:array($this->LabelCol); } if (sizeof($SELECTED) == 0) { if ($this->Id > 0) { $SELECTED = array($this->Id); } else { $SELECTED = array(); } } else if (is_array($SELECTED)) { foreach ($SELECTED as $k => $v) { if (is_object($v)) { $SELECTED = array_keys($SELECTED); } break; } } else { $SELECTED = array(); } $q = ' SELECT '.$this->ColName.', CONCAT_WS(\' \', '.join(', ', $C).') AS Label FROM '.$this->TblName.' '.$this->BuildWhere($W).' '.$this->BuildOrder($O).' '.$this->BuilLimit($L); $O = ''; $DB_Server = $this->GetDBServer(); if ($DB_Server->query($q)) { while ($t = $DB_Server->fetch()) { $SEL = (in_array($t[$this->ColName], $SELECTED))?' selected':''; $O .= ''."\n"; } } return $O; } } ?>