27 protected $_sClassRow;
32 private static $_tInstance=array();
39 if ( !isset(self::$_tInstance[$class][$sConfig]) ){
40 $oSgbd =
new $class();
41 $oSgbd->chooseConfig($sConfig);
42 self::$_tInstance[$class][$sConfig]=$oSgbd;
45 return self::$_tInstance[$class][$sConfig];
54 $this->_sClassRow=$sClassRow;
61 $this->_sConfig=$sConfig;
68 $this->_tConfig=$tConfig;
77 public function getInsertFromTab($tProperty){
83 $tKey=array_keys($tProperty);
84 foreach($tKey as $sVar){
89 return '('.substr($sCols,0,-1).
') VALUES ('.substr($sVals,0,-1).
') ';
91 public function getUpdateFromTab($tProperty){
94 $tKey=array_keys($tProperty);
95 foreach($tKey as $sVar){
99 return substr($sReq,0,-1);
101 public function setId($tId){
104 public function getWhereFromTab($tId){
107 $tKeyId=array_keys($tId);
108 foreach($tKeyId as $sVar){
109 if($sWhere!=
''){ $sWhere.=
' AND '; }
118 private function getRequestAndParam($tReq){
123 if(isset($tReq[1]) and is_array($tReq[1])){
127 $tParam=array_values($tReq);
133 return array($sReq,$tParam);
136 public function findManySimple($tSql,$sClassRow){
137 list($sReq,$tParam)=$this->getRequestAndParam($tSql);
139 $pRs=$this->query($sReq,$tParam);
145 return $pRs->fetchAll(PDO::FETCH_OBJ);
147 public function findMany($tSql,$sClassRow){
148 list($sReq,$tParam)=$this->getRequestAndParam($tSql);
150 $pRs=$this->query($sReq,$tParam);
157 while($tRow=$pRs->fetch(PDO::FETCH_ASSOC)){
158 $oRow=
new $sClassRow($tRow);
163 public function findOne($tSql,$sClassRow){
164 list($sReq,$tParam)=$this->getRequestAndParam($tSql);
166 $pRs=$this->query($sReq,$tParam);
168 $tRow=$pRs->fetch(PDO::FETCH_ASSOC);
174 $oRow=
new $sClassRow($tRow);
178 public function findOneSimple($tSql,$sClassRow){
179 list($sReq,$tParam)=$this->getRequestAndParam($tSql);
181 $pRs=$this->query($sReq,$tParam);
183 $oRow=$pRs->fetch(PDO::FETCH_OBJ);
191 public function execute($tSql){
192 list($sReq,$tParam)=$this->getRequestAndParam($tSql);
194 return $this->query($sReq,$tParam);
197 public function update($sTable,$tProperty,$tWhere){
199 $sReq=
'UPDATE '.$sTable.
' SET '.$this->getUpdateFromTab($tProperty).
' WHERE '.$this->getWhereFromTab($tWhere);
201 $tPropertyAndWhere=array_merge($tProperty,$tWhere);
202 $tParam=array_values($tPropertyAndWhere);
204 $this->query($sReq,$tParam);
206 public function insert($sTable,$tProperty){
207 $sReq=
'INSERT INTO '.$sTable.
' '.$this->getInsertFromTab($tProperty);
208 $tParam=array_values($tProperty);
210 $this->query($sReq,$tParam);
212 return $this->getLastInsertId();
215 public function delete($sTable,$tWhere){
217 $sReq=
'DELETE FROM '.$sTable.
' WHERE '.$this->getWhereFromTab($tWhere);
218 $tParam=array_values($tWhere);
220 $this->query($sReq,$tParam);
223 public function getPdo(){
228 protected function query($sReq,$tParam=null){
234 'SILENT' => PDO::ERRMODE_SILENT,
235 'WARNING' => PDO::ERRMODE_WARNING,
236 'EXCEPTION' => PDO::ERRMODE_EXCEPTION,
239 'LOWER' => PDO::CASE_LOWER,
240 'NATURAL' => PDO::CASE_NATURAL,
241 'UPPER' => PDO::CASE_UPPER,
246 $this->_sReq=$sReq.
' [ '.implode(
' | ',$tParam).
' ]';
247 $this->_pDb->setAttribute(PDO::ATTR_ERRMODE, $tATTRERRMODE[ trim(
_root::getConfigVar(
'pdo.ATTR_ERRMODE',
'WARNING')) ] );
248 $this->_pDb->setAttribute(PDO::ATTR_CASE, $tATTRCASE[ trim(
_root::getConfigVar(
'pdo.ATTR_CASE',
'NATURAL')) ] );
249 $sth = $this->_pDb->prepare($sReq);
250 if(is_array($tParam)){
251 $sth->execute($tParam);
258 public function erreur($sErreur){
259 throw new Exception($sErreur);
static getConfigVar($sCatAndVar, $uDefaut=null)
static _getInstance($class, $sConfig)