MkFramework
 All Data Structures Functions
abstract_sgbd_pdo.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
23 abstract class abstract_sgbd_pdo{
24 
25  protected $_tConfig;
26  protected $_sConfig;
27  protected $_sClassRow;
28  protected $_pDb;
29  protected $_sReq;
30  protected $_tId;
31 
32  private static $_tInstance=array();
33 
38  protected static function _getInstance($class,$sConfig) {
39  if ( !isset(self::$_tInstance[$class][$sConfig]) ){
40  $oSgbd = new $class();
41  $oSgbd->chooseConfig($sConfig);
42  self::$_tInstance[$class][$sConfig]=$oSgbd;
43 
44  }
45  return self::$_tInstance[$class][$sConfig];
46  }
47 
53  public function setClassRow($sClassRow){
54  $this->_sClassRow=$sClassRow;
55  }
60  public function chooseConfig($sConfig){
61  $this->_sConfig=$sConfig;
62  }
67  public function setConfig($tConfig){
68  $this->_tConfig=$tConfig;
69  }
73  public function getRequete(){
74  return $this->_sReq;
75  }
76 
77  public function getInsertFromTab($tProperty){
78 
79  $sCols='';
80  $sVals='';
81 
82  if($tProperty){
83  $tKey=array_keys($tProperty);
84  foreach($tKey as $sVar){
85  $sCols.=$sVar.',';
86  $sVals.='?,';
87  }
88  }
89  return '('.substr($sCols,0,-1).') VALUES ('.substr($sVals,0,-1).') ';
90  }
91  public function getUpdateFromTab($tProperty){
92  $sReq='';
93  if($tProperty){
94  $tKey=array_keys($tProperty);
95  foreach($tKey as $sVar){
96  $sReq.=$sVar.'=?,';
97  }
98  }
99  return substr($sReq,0,-1);
100  }
101  public function setId($tId){
102  $this->_tId=$tId;
103  }
104  public function getWhereFromTab($tId){
105  $sWhere='';
106  if(is_array($tId)){
107  $tKeyId=array_keys($tId);
108  foreach($tKeyId as $sVar){
109  if($sWhere!=''){ $sWhere.=' AND '; }
110  $sWhere.=$sVar.'=?';
111  }
112  }
113 
114  return $sWhere;
115  }
116 
117 
118  private function getRequestAndParam($tReq){
119  $sReq=null;
120  $tParam=null;
121  if(is_array($tReq)){
122  $sReq=$tReq[0];
123  if(isset($tReq[1]) and is_array($tReq[1])){
124  $tParam=$tReq[1];
125  }else{
126  unset($tReq[0]);
127  $tParam=array_values($tReq);
128  }
129  }else{
130  $sReq=$tReq;
131  }
132 
133  return array($sReq,$tParam);
134  }
135 
136  public function findManySimple($tSql,$sClassRow){
137  list($sReq,$tParam)=$this->getRequestAndParam($tSql);
138 
139  $pRs=$this->query($sReq,$tParam);
140 
141  if(!$pRs){
142  return null;
143  }
144 
145  return $pRs->fetchAll(PDO::FETCH_OBJ);
146  }
147  public function findMany($tSql,$sClassRow){
148  list($sReq,$tParam)=$this->getRequestAndParam($tSql);
149 
150  $pRs=$this->query($sReq,$tParam);
151 
152  if(!$pRs){
153  return null;
154  }
155 
156  $tObj=array();
157  while($tRow=$pRs->fetch(PDO::FETCH_ASSOC)){
158  $oRow=new $sClassRow($tRow);
159  $tObj[]=$oRow;
160  }
161  return $tObj;
162  }
163  public function findOne($tSql,$sClassRow){
164  list($sReq,$tParam)=$this->getRequestAndParam($tSql);
165 
166  $pRs=$this->query($sReq,$tParam);
167 
168  $tRow=$pRs->fetch(PDO::FETCH_ASSOC);
169 
170  if(empty($tRow) ){
171  return null;
172  }
173 
174  $oRow=new $sClassRow($tRow);
175 
176  return $oRow;
177  }
178  public function findOneSimple($tSql,$sClassRow){
179  list($sReq,$tParam)=$this->getRequestAndParam($tSql);
180 
181  $pRs=$this->query($sReq,$tParam);
182 
183  $oRow=$pRs->fetch(PDO::FETCH_OBJ);
184 
185  if(empty($oRow) ){
186  return null;
187  }
188 
189  return $oRow;
190  }
191  public function execute($tSql){
192  list($sReq,$tParam)=$this->getRequestAndParam($tSql);
193 
194  return $this->query($sReq,$tParam);
195  }
196 
197  public function update($sTable,$tProperty,$tWhere){
198 
199  $sReq='UPDATE '.$sTable.' SET '.$this->getUpdateFromTab($tProperty).' WHERE '.$this->getWhereFromTab($tWhere);
200 
201  $tPropertyAndWhere=array_merge($tProperty,$tWhere);
202  $tParam=array_values($tPropertyAndWhere);
203 
204  $this->query($sReq,$tParam);
205  }
206  public function insert($sTable,$tProperty){
207  $sReq='INSERT INTO '.$sTable.' '.$this->getInsertFromTab($tProperty);
208  $tParam=array_values($tProperty);
209 
210  $this->query($sReq,$tParam);
211 
212  return $this->getLastInsertId();
213  }
214 
215  public function delete($sTable,$tWhere){
216 
217  $sReq='DELETE FROM '.$sTable.' WHERE '.$this->getWhereFromTab($tWhere);
218  $tParam=array_values($tWhere);
219 
220  $this->query($sReq,$tParam);
221  }
222 
223  public function getPdo(){
224  $this->connect();
225  return $this->_pDb;
226  }
227 
228  protected function query($sReq,$tParam=null){
229  if($tParam==null){
230  $tParam=array();
231  }
232 
233  $tATTRERRMODE=array(
234  'SILENT' => PDO::ERRMODE_SILENT,
235  'WARNING' => PDO::ERRMODE_WARNING,
236  'EXCEPTION' => PDO::ERRMODE_EXCEPTION,
237  );
238  $tATTRCASE=array(
239  'LOWER' => PDO::CASE_LOWER,
240  'NATURAL' => PDO::CASE_NATURAL,
241  'UPPER' => PDO::CASE_UPPER,
242  );
243 
244 
245  $this->connect();
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);
252  }else{
253  $sth->execute();
254  }
255  return $sth;
256  }
257 
258  public function erreur($sErreur){
259  throw new Exception($sErreur);
260  }
261 
262 }
static getConfigVar($sCatAndVar, $uDefaut=null)
Definition: class_root.php:654
static _getInstance($class, $sConfig)