MkFramework
 All Data Structures Functions
sgbd_csv.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 */
18 class sgbd_csv extends abstract_sgbd{
19 
20 
21  public static function getInstance($sConfig){
22  return self::_getInstance(__CLASS__,$sConfig);
23  }
24 
25  public function findMany($tSql,$sClassRow){
26  $tRows=$this->query($this->bind($tSql),$sClassRow);
27 
28  if(!$tRows){
29  return null;
30  }
31 
32  return $tRows;
33  }
34  public function findManySimple($tSql,$sClassRow){
35  return $this->findMany($tSql,$sClassRow);
36  }
37  public function findOne($tSql,$sClassRow){
38  $tRs=$this->query($this->bind($tSql),$sClassRow);
39 
40  if(empty($tRs)){
41  return null;
42  }
43 
44  return $tRs[0];
45  }
46  public function findOneSimple($tSql,$sClassRow){
47  return $this->findOne($tSql,$sClassRow);
48  }
49  public function execute($tSql){
50  return $this->query($this->bind($tSql));
51  }
52 
53  public function update($sTable,$tProperty,$tWhere){
54  $iId=$this->getIdFromTab($tWhere);
55 
56  $tProperty['id']=$iId;
57 
58  $tFile=$this->getTabFromTable($sTable);
59 
60  $tHeader=$this->getListColumn($sTable);
61 
62  foreach($tFile as $i => $sLigne){
63  if($i < 2){ continue; }
64  $tLigne=preg_split('/;/',$sLigne);
65  if($tLigne[0] == $iId){
66  foreach($tHeader as $sHeader){
67  $tUpdate[]=$this->encode($tProperty[ $sHeader]);
68  }
69 
70  $tFile[$i]=implode(';',$tUpdate).';';
71  }
72  }
73 
74 
75  $this->save($tFile,$sTable);
76  }
77  public function delete($sTable,$tWhere){
78  $iId=$this->getIdFromTab($tWhere);
79 
80  $tFile=$this->getTabFromTable($sTable);
81 
82  foreach($tFile as $i => $sLigne){
83  if($i < 2){ continue; }
84  $tLigne=preg_split('/;/',$sLigne);
85  if($tLigne[0] == $iId){
86 
87  unset( $tFile[$i] );
88  }
89  }
90 
91  $this->save($tFile,$sTable);
92 
93  }
94  public function insert($sTable,$tProperty){
95  $iId=$this->getMaxId($sTable);
96 
97  $iMax=($iId+1);
98 
99  $tProperty['id']=$iId;
100 
101  $tHeader=$this->getListColumn($sTable);
102  foreach($tHeader as $sHeader){
103  $tInsert[]=$this->encode($tProperty[ $sHeader]);
104  }
105 
106  $tFile=$this->getTabFromTable($sTable);
107  $tFile[]=implode(';',$tInsert).';';
108  $tFile[0]=$iMax;
109 
110  $this->save($tFile,$sTable);
111 
112  return $iId;
113  }
114 
115  public function getListColumn($sTable){
116 
117  $tFile=$this->getTabFromTable($sTable);
118 
119  $tHeader=preg_split('/;/',$tFile[1]);
120 
121  if( trim($tHeader[ count($tHeader)-1 ])==''){
122  unset($tHeader[ count($tHeader)-1 ]);
123  }
124 
125  return $tHeader;
126 
127  }
128  public function getListTable(){
129  $oDir=new _dir( $this->_tConfig[$this->_sConfig.'.database']);
130  $tDir=$oDir->getList();
131  $tSDir=array();
132  foreach($tDir as $oDir){
133  $tSDir[]= preg_replace('/\.csv/','',$oDir->getName());
134  }
135  return $tSDir;
136  }
137 
138  private function query($sReq,$sClassRow){
139  //traitement de la requete $sReq
140  $sReq=trim($sReq);
141  $this->_sReq=$sReq;
142  if(substr($sReq,0,6)== 'SELECT'){
143 
144  $tReq=$this->explainSql($sReq);
145 
146  //count
147  $bCount=false;
148  $iCount=0;
149  if(isset($tReq['select']) and preg_match('/COUNT\(/i',$tReq['select'])){
150  $bCount=true;
151  }
152 
153  $tCritere=$this->findListCritere($tReq);
154 
155  $sTable=trim($tReq['from']);
156 
157  $tObj=$this->findInTableWithCritere($sClassRow,$sTable,$tCritere);
158 
159  //count
160  if($bCount){
161  $iCount=count($tObj);
162  return array($iCount);
163  }else if(isset($tReq['order']) and $tObj!=null){
164  return $this->sortResult($tObj,$tReq);
165  }else{
166  return $tObj;
167  }
168  }
169 
170 
171 
172  }
173  private function explainSql($sReq){
174  if(
175  preg_match_all('/^SELECT(?<select>.*)FROM(?<from>.*)WHERE(?<where>.*)ORDER BY(?<order>.*)/i'
176  ,$sReq,$tResult,PREG_SET_ORDER)
177  or
178  preg_match_all('/^SELECT(?<select>.*)FROM(?<from>.*)ORDER BY(?<order>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
179  or
180  preg_match_all('/^SELECT(?<select>.*)FROM(?<from>.*)WHERE(?<where>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
181  or
182  preg_match_all('/^SELECT(?<select>.*)FROM(?<from>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
183  ){
184  if(isset($tResult[0]['where']) and preg_match('/ or /i',$tResult[0]['where'])){
185  $this->erreur('Requete non supportee : '.$sReq.$msg);
186  }elseif(isset($tResult[0]['order']) and !preg_match('/\s[ASC|DESC]/i',trim($tResult[0]['order'])) ){
187  $this->erreur('Il faut definir un sens de tri: ASC ou DESC dans la requete'.$sReq.$msg);
188  }else{
189  return $tResult[0];
190  }
191 
192  }else{
193  $msg="\n\n";
194  $msg.="Le driver xml gere les requetes de type : \n";
195  $msg.="- SELECT liste_des_champs FROM ma_table WHERE champ=valeur ORDER BY champ DESC/ASC \n";
196  $msg.="- SELECT liste_des_champs FROM ma_table ORDER BY champ DESC/ASC \n";
197  $msg.="- SELECT liste_des_champs FROM ma_table WHERE champ=valeur \n";
198  $msg.="- SELECT liste_des_champs FROM ma_table \n";
199  $msg.=" la clause where accepte uniquement champ=valeur, champ!=valeur et AND \n";
200 
201  $this->erreur('Requete non supportee : '.$sReq.$msg);
202  }
203  }
204  private function findListCritere($tReq){
205  $tCritere=array();
206 
207  if(isset($tReq['where'])){
208  if(preg_match('/ and /i',$tReq['where'])){
209  $tWhere=preg_split('/ AND /i',$tReq['where']);
210  foreach($tWhere as $sWhereVal){
211  if(preg_match('/!=/',$sWhereVal)){
212  list($sVar,$sVal)=preg_split('/!=/',$sWhereVal);
213  $tCritere[trim($sVar)]='!'.trim($sVal);
214  }elseif(preg_match('/=/',$sWhereVal)){
215  list($sVar,$sVal)=preg_split('/=/',$sWhereVal);
216  $tCritere[trim($sVar)]='='.trim($sVal);
217  }
218  }
219  }else{
220  if(preg_match('/!=/',$tReq['where'])){
221  list($sVar,$sVal)=preg_split('/!=/',$tReq['where']);
222  $tCritere[trim($sVar)]='!'.trim($sVal);
223  }elseif(preg_match('/=/',$tReq['where'])){
224  list($sVar,$sVal)=preg_split('/=/',$tReq['where']);
225  $tCritere[trim($sVar)]='='.trim($sVal);
226  }
227  }
228 
229  }
230  return $tCritere;
231  }
232  private function sortResult($tObj,$tReq){
233 
234  list($sChamp,$sSens)=preg_split('/ /',trim($tReq['order']));
235 
236  $tTri=array();
237  $tIdObj=array();
238  foreach($tObj as $i => $oObj){
239  $tIdObj[ $i ]=$oObj;
240  $tTri[ $i ]=(string)$oObj->$sChamp;
241  }
242 
243  if($sSens=='DESC'){
244  arsort($tTri);
245  }else{
246  asort($tTri);
247  }
248 
249  $tOrderedObj=array();
250  $tId= array_keys($tTri);
251  foreach($tId as $id){
252  $tOrderedObj[]=$tIdObj[$id];
253  }
254 
255  return $tOrderedObj;
256  }
257  private function findInTableWithCritere($sClassRow,$sTable,$tCritere){
258  $tFile=$this->getTabFromFile($this->getTabFromTable($sTable));
259 
260  $tObj=array();
261  foreach($tFile as $tRow){
262 
263  foreach($tCritere as $sCritereField => $sCritereVal){
264 
265  if(!isset($tRow[$sCritereField]) or
266  (
267  ($sCritereVal[0]=='=' and (string)$sCritereVal!=(string)'='.$tRow[$sCritereField])
268 
269  or
270 
271  ($sCritereVal[0]=='!' and (string)$sCritereVal==(string)'!'.$tRow[$sCritereField])
272  )
273  ){
274  continue 2;
275  }
276  }
277 
278 
279  $oRow=new $sClassRow($tRow);
280  $tObj[]=$oRow;
281 
282  }
283 
284  return $tObj;
285 
286 
287  }
288 
289  public function quote($sVal){
290  return $sVal;
291  }
292  public function getWhereAll(){
293  return '1=1';
294  }
295 
296 
297  private function getIdFromTab($tId){
298  if(is_array($tId)){
299  return current($tId);
300  }else{
301  return $tId;
302  }
303  }
304  private function save($tFile,$sTable){
305  $oFile=new _file($this->_tConfig[$this->_sConfig.'.database'].$sTable.'.csv');
306  $sRet="\n";
307 
308  $sFile='';
309  if($tFile){
310  foreach($tFile as $sLigne){
311  $sFile.=trim($sLigne).$sRet;
312  }
313  }
314  $oFile->write($sFile);
315  }
316  private function getMaxId($sTable){
317  $tFile=$this->getTabFromTable($sTable);
318 
319  $iMax=trim($tFile[0]);
320 
321  return (int)$iMax;
322  }
323 
324 
325  public function getTabFromFile($tContent){
326 
327  $tHeader=preg_split('/;/',$tContent[1]);
328 
329  $tab=array();
330  foreach($tContent as $i => $sLigne){
331  if($i < 2){ continue;}
332  $sLigne=html_entity_decode($sLigne,ENT_QUOTES);
333  $tLigne=preg_split('/;/',$sLigne);
334 
335  $tab2=array();
336  foreach($tHeader as $i => $sHeader){
337  $tab2[ $sHeader ]=$this->decode($tLigne[$i]);
338  }
339  $tab[]=$tab2;
340 
341  }
342  return $tab;
343 
344  }
345  public function getTabFromTable($sTable){
346  $oFile=new _file($this->_tConfig[$this->_sConfig.'.database'].$sTable.'.csv');
347  $tFile=$oFile->getTab();
348  return $tFile;
349  }
350 
351  public function encode($text){
352  return preg_replace('/\r/','',preg_replace('/\n/','##retour_chariot_fmk##',$text));
353  }
354  public function decode($text){
355  return preg_replace('/##retour_chariot_fmk##/',"\n",$text);
356  }
357 
358 
359 }