20 public static function getInstance($sConfig){
21 return self::_getInstance(__CLASS__,$sConfig);
24 public function findMany($tSql,$sClassRow){
25 $tRows=$this->query($this->
bind($tSql),$sClassRow);
33 public function findManySimple($tSql,$sClassRow){
34 return $this->findMany($tSql,$sClassRow);
36 public function findOne($tSql,$sClassRow){
37 $tRs=$this->query($this->
bind($tSql),$sClassRow);
45 public function findOneSimple($tSql,$sClassRow){
46 return $this->findOne($tSql,$sClassRow);
48 public function execute($tSql){
49 return $this->query($this->
bind($tSql));
52 public function update($sTable,$tProperty,$tWhere){
53 $iId=$this->getIdFromTab($tWhere);
55 $sFile=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/'.$iId.
'.json';
56 $oJson=$this->json_decode($sFile);
57 $tJson=(array) $oJson;
60 $this->removeRowFromAllIndex($sTable,$tJson);
62 foreach($tProperty as $sVar => $sVal){
67 $this->addRowInAllIndex($sTable,$tJson);
69 $this->save($oJson,$sFile);
71 public function insert($sTable,$tProperty){
72 $iId=$this->getMaxId($sTable);
78 foreach($tProperty as $sVar => $sVal){
83 $sFile=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/'.$iId.
'.json';
84 $this->save($oJson,$sFile);
86 $sFileMax=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/max.txt';
87 file_put_contents($sFileMax,$iMax);
89 $this->addRowInAllIndex($sTable,$tProperty);
93 public function delete($sTable,$tWhere){
94 $iId=$this->getIdFromTab($tWhere);
96 $sFile=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/'.$iId.
'.json';
97 $oJson=$this->json_decode($sFile);
98 $tJson=(array) $oJson;
101 $this->removeRowFromAllIndex($sTable,$tJson);
103 unlink($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/'.$iId.
'.json');
106 public function getListColumn($sTable){
108 $sFile=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/structure.csv';
109 $sColumns=trim(file_get_contents($sFile));
111 $tColumn=explode(
';',$sColumns);
116 public function getListTable(){
117 $oDir=
new _dir( $this->_tConfig[$this->_sConfig.
'.database']);
118 $tDir=$oDir->getList();
120 foreach($tDir as $oDir){
121 $tSDir[]= $oDir->getName();
126 private function query($sReq,$sClassRow){
131 if(substr($sReq,0,6)==
'SELECT'){
133 $tReq=$this->explainSql($sReq);
138 if(isset($tReq[
'select']) and preg_match(
'/COUNT\(/i',$tReq[
'select'])){
142 $tCritere=$this->findListCritere($tReq);
144 $sTable=trim($tReq[
'from']);
147 $tSqlFieldEqual=array_keys($tCritere);
149 $sIndexToUse=$this->findIndexForTable($sTable,$tSqlFieldEqual);
153 if($sIndexToUse!=
''){
154 $tObj=$this->findWithTableIndex($sClassRow,$sTable,$sIndexToUse,$tCritere);
156 }elseif($tSqlFieldEqual==array(
'=id')){
157 $sFilename=$this->_tConfig[$this->_sConfig.
'.database'];
158 $sFilename.=$sTable.
'/'.(string)$tCritere[
'=id'].
'.json';
160 $tRow=(array)$this->json_decode($sFilename);
162 $oRow=
new $sClassRow($tRow);
166 $tObj=$this->findInTableWithCritere($sClassRow,$sTable,$tCritere);
172 $iCount=count($tObj);
173 return array($iCount);
174 }
else if(isset($tReq[
'order']) and $tObj!=null){
175 return $this->sortResult($tObj,$tReq);
184 private function explainSql($sReq){
186 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)WHERE(?<where>.*)ORDER BY(?<order>.*)/i'
187 ,$sReq,$tResult,PREG_SET_ORDER)
189 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)ORDER BY(?<order>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
191 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)WHERE(?<where>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
193 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
195 if(isset($tResult[0][
'where']) and preg_match(
'/ or /i',$tResult[0][
'where'])){
196 $this->erreur(
'Requete non supportee : '.$sReq.$msg);
197 }elseif(isset($tResult[0][
'order']) and !preg_match(
'/\s[ASC|DESC]/i',trim($tResult[0][
'order'])) ){
198 $this->erreur(
'Il faut definir un sens de tri: ASC ou DESC dans la requete'.$sReq.$msg);
204 $msg.=
"Le driver xml gere les requetes de type : \n";
205 $msg.=
"- SELECT liste_des_champs FROM ma_table WHERE champ=valeur ORDER BY champ DESC/ASC \n";
206 $msg.=
"- SELECT liste_des_champs FROM ma_table ORDER BY champ DESC/ASC \n";
207 $msg.=
"- SELECT liste_des_champs FROM ma_table WHERE champ=valeur \n";
208 $msg.=
"- SELECT liste_des_champs FROM ma_table \n";
209 $msg.=
" la clause where accepte uniquement champ=valeur, champ!=valeur et AND \n";
211 $this->erreur(
'Requete non supportee : '.$sReq.$msg);
214 private function findListCritere($tReq){
217 if(isset($tReq[
'where'])){
218 if(preg_match(
'/ and /i',$tReq[
'where'])){
219 $tWhere=preg_split(
'/ AND /i',$tReq[
'where']);
220 foreach($tWhere as $sWhereVal){
221 if(preg_match(
'/!=/',$sWhereVal)){
222 list($sVar,$sVal)=preg_split(
'/!=/',$sWhereVal);
223 $tCritere[trim($sVar)]=
'!'.trim($sVal);
224 }elseif(preg_match(
'/=/',$sWhereVal)){
225 list($sVar,$sVal)=preg_split(
'/=/',$sWhereVal);
226 $tCritere[trim($sVar)]=
'='.trim($sVal);
230 if(preg_match(
'/!=/',$tReq[
'where'])){
231 list($sVar,$sVal)=preg_split(
'/!=/',$tReq[
'where']);
232 $tCritere[trim($sVar)]=
'!'.trim($sVal);
233 }elseif(preg_match(
'/=/',$tReq[
'where'])){
234 list($sVar,$sVal)=preg_split(
'/=/',$tReq[
'where']);
235 $tCritere[trim($sVar)]=
'='.trim($sVal);
242 private function sortResult($tObj,$tReq){
244 list($sChamp,$sSens)=preg_split(
'/ /',trim($tReq[
'order']));
248 foreach($tObj as $i => $oObj){
250 $tTri[ $i ]=(string)$oObj->$sChamp;
259 $tOrderedObj=array();
260 $tId= array_keys($tTri);
261 foreach($tId as $id){
262 $tOrderedObj[]=$tIdObj[$id];
267 private function findIndexForTable($sTable,$tSqlFieldEqual){
268 $oDirIndex=
new _dir($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index');
269 if($oDirIndex->exist()){
270 $tFileIndex=$oDirIndex->getListDir();
271 foreach($tFileIndex as $oFileIndex){
272 $tFieldIndex=$this->getFieldsFromIndex($oFileIndex->getName());
274 foreach($tSqlFieldEqual as $sSqlFieldEqual){
276 $sSqlFieldEqual[0]==
'=' and !in_array(substr($sSqlFieldEqual,1),$tFieldIndex)
278 $sSqlFieldEqual[0]==
'!' and in_array(substr($sSqlFieldEqual,1),$tFieldIndex)
284 return $oFileIndex->getName();
289 private function findWithTableIndex($sClassRow,$sTable,$sIndexToUse,$tCritere){
290 $sDirIndex=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndexToUse;
292 $tFieldIndex=preg_split(
'/\./',$sIndexToUse);
294 $oDirIndex=
new _dir($sDirIndex);
295 $tFileIndex=$oDirIndex->getListFile();
298 foreach($tFileIndex as $oFileIndex){
299 $sFileIndex=trim($oFileIndex->getName());
301 $tRow=$this->getRowValueFromIndex($sFileIndex,$tFieldIndex);
303 foreach($tCritere as $sCritereField => $sCritereVal){
305 if(!isset($tRow[$sCritereField]) or
307 ($sCritereVal[0]==
'=' and (
string)$sCritereVal!=(
string)
'='.$tRow[$sCritereField])
311 ($sCritereVal[0]==
'!' and (
string)$sCritereVal==(
string)
'!'.$tRow[$sCritereField])
318 $tMatchedFile=file( $sDirIndex.
'/'.$sFileIndex );
319 foreach($tMatchedFile as $sMatchedFile){
320 $sMatchedFile=trim($sMatchedFile);
321 $sFilename=$this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/'.$sMatchedFile;
322 $tRow=(array)$this->json_decode($sFilename);
324 $oRow=
new $sClassRow($tRow);
331 private function findInTableWithCritere($sClassRow,$sTable,$tCritere){
332 $oDir=
new _dir($this->_tConfig[$this->_sConfig.
'.database'].$sTable);
333 $tFile=$oDir->getListFile();
337 foreach($tFile as $oFile){
338 if( in_array($oFile->getName(),array(
'structure.csv',
'max.txt'))){
continue; }
339 $tRow=(array)$this->json_decode($oFile->getAdresse());
342 foreach($tCritere as $sCritereField => $sCritereVal){
344 if(!isset($tRow[$sCritereField]) or
346 ($sCritereVal[0]==
'=' and (
string)$sCritereVal!=(
string)
'='.$tRow[$sCritereField])
350 ($sCritereVal[0]==
'!' and (
string)$sCritereVal==(
string)
'!'.$tRow[$sCritereField])
358 $oRow=
new $sClassRow($tRow);
368 public function quote($sVal){
371 public function getWhereAll(){
375 private function getFieldsFromIndex($sIndex){
376 $tFields=preg_split(
'/\./',substr($sIndex,0,-6));
379 private function getRowValueFromIndex($sFileIndex,$tFieldIndex){
380 $tValue=preg_split(
'/####/',substr($sFileIndex,0,-4) );
382 foreach($tFieldIndex as $i => $var){
383 $tRow[$var]=$tValue[$i];
387 private function getFileIndexFromTab($sIndex,$tRow){
388 $tFields=$this->getFieldsFromIndex($sIndex);
390 foreach($tFields as $sField){
391 $sFileIndex.=$tRow[$sField];
394 return $sFileIndex.
'.csv';
396 public function generateIndexForTable($sTable,$sIndex){
397 $tFields=$this->getFieldsFromIndex($sIndex);
399 $oDir=
new _dir($this->_tConfig[$this->_sConfig.
'.database'].$sTable);
400 $tFile=$oDir->getListFile();
402 $tIndexContent=array();
404 foreach($tFile as $oFile){
405 if($oFile->getName() ==
'structure.csv'){
continue;}
406 if($oFile->getName() ==
'max.txt'){
continue;}
408 $tRow=(array)$this->json_decode($oFile->getAdresse());
413 foreach($tFields as $sField){
414 $sKey.=$tRow[$sField];
417 $tIndexContent[$sKey][]=$tRow[
'id'].
'.json';
421 $oDir=
new _dir($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndex);
422 foreach($oDir->getListFile() as $oFile){
426 foreach($tIndexContent as $sKey => $tFile){
427 $oFile=
new _file($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndex.
'/'.$sKey.
'.csv');
428 $oFile->setContent(implode($tFile,
"\n"));
432 private function addRowInAllIndex($sTable,$tProperty){
434 $oDir=
new _dir($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index');
436 $tDirIndex=$oDir->getListDir();
437 foreach($tDirIndex as $oDirIndex){
438 $this->addRowInIndex($sTable,$tProperty,$oDirIndex->getName());
442 private function addRowInIndex($sTable,$tProperty,$sIndex){
444 $sFileIndex=$this->getFileIndexFromTab($sIndex,$tProperty);
446 $oFile=
new _file($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndex.
'/'.$sFileIndex);
447 $oFile->addContent($tProperty[
'id'].
'.json');
450 private function removeRowFromAllIndex($sTable,$tProperty){
452 $oDir=
new _dir($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index');
454 $tDirIndex=$oDir->getListDir();
455 foreach($tDirIndex as $oDirIndex){
456 $this->removeRowFromIndex($sTable,$tProperty,$oDirIndex->getName());
460 private function removeRowFromIndex($sTable,$tProperty,$sIndex){
462 $sFileIndex=$this->getFileIndexFromTab($sIndex,$tProperty);
464 if(!file_exists($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndex.
'/'.$sFileIndex)){
468 $tLine=file($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndex.
'/'.$sFileIndex);
470 foreach($tLine as $sLine){
472 if($sLine==$tProperty[
'id'].
'.json'){
478 $oFile=
new _file($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/index/'.$sIndex.
'/'.$sFileIndex);
479 $oFile->setContent(implode(
"\n",$tContent));
483 private function getIdFromTab($tId){
485 return current($tId);
490 private function save($oJson,$sFichier){
492 $sJson=json_encode($oJson);
494 file_put_contents($sFichier,$sJson);
496 private function getMaxId($sTable){
497 $iMax=trim(file_get_contents($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'/max.txt'));
503 private function json_decode($sFile){
504 return json_decode(file_get_contents($sFile));