21 public static function getInstance($sConfig){
22 return self::_getInstance(__CLASS__,$sConfig);
25 public function findMany($tSql,$sClassRow){
26 $tRows=$this->query($this->
bind($tSql),$sClassRow);
34 public function findManySimple($tSql,$sClassRow){
35 return $this->findMany($tSql,$sClassRow);
37 public function findOne($tSql,$sClassRow){
38 $tRs=$this->query($this->
bind($tSql),$sClassRow);
46 public function findOneSimple($tSql,$sClassRow){
47 return $this->findOne($tSql,$sClassRow);
49 public function execute($tSql){
50 return $this->query($this->
bind($tSql));
53 public function update($sTable,$tProperty,$tWhere){
54 $iId=$this->getIdFromTab($tWhere);
56 $tProperty[
'id']=$iId;
58 $tFile=$this->getTabFromTable($sTable);
60 $tHeader=$this->getListColumn($sTable);
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]);
70 $tFile[$i]=implode(
';',$tUpdate).
';';
75 $this->save($tFile,$sTable);
77 public function delete($sTable,$tWhere){
78 $iId=$this->getIdFromTab($tWhere);
80 $tFile=$this->getTabFromTable($sTable);
82 foreach($tFile as $i => $sLigne){
83 if($i < 2){
continue; }
84 $tLigne=preg_split(
'/;/',$sLigne);
85 if($tLigne[0] == $iId){
91 $this->save($tFile,$sTable);
94 public function insert($sTable,$tProperty){
95 $iId=$this->getMaxId($sTable);
99 $tProperty[
'id']=$iId;
101 $tHeader=$this->getListColumn($sTable);
102 foreach($tHeader as $sHeader){
103 $tInsert[]=$this->encode($tProperty[ $sHeader]);
106 $tFile=$this->getTabFromTable($sTable);
107 $tFile[]=implode(
';',$tInsert).
';';
110 $this->save($tFile,$sTable);
115 public function getListColumn($sTable){
117 $tFile=$this->getTabFromTable($sTable);
119 $tHeader=preg_split(
'/;/',$tFile[1]);
121 if( trim($tHeader[ count($tHeader)-1 ])==
''){
122 unset($tHeader[ count($tHeader)-1 ]);
128 public function getListTable(){
129 $oDir=
new _dir( $this->_tConfig[$this->_sConfig.
'.database']);
130 $tDir=$oDir->getList();
132 foreach($tDir as $oDir){
133 $tSDir[]= preg_replace(
'/\.csv/',
'',$oDir->getName());
138 private function query($sReq,$sClassRow){
142 if(substr($sReq,0,6)==
'SELECT'){
144 $tReq=$this->explainSql($sReq);
149 if(isset($tReq[
'select']) and preg_match(
'/COUNT\(/i',$tReq[
'select'])){
153 $tCritere=$this->findListCritere($tReq);
155 $sTable=trim($tReq[
'from']);
157 $tObj=$this->findInTableWithCritere($sClassRow,$sTable,$tCritere);
161 $iCount=count($tObj);
162 return array($iCount);
163 }
else if(isset($tReq[
'order']) and $tObj!=null){
164 return $this->sortResult($tObj,$tReq);
173 private function explainSql($sReq){
175 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)WHERE(?<where>.*)ORDER BY(?<order>.*)/i'
176 ,$sReq,$tResult,PREG_SET_ORDER)
178 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)ORDER BY(?<order>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
180 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)WHERE(?<where>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
182 preg_match_all(
'/^SELECT(?<select>.*)FROM(?<from>.*)/i',$sReq,$tResult,PREG_SET_ORDER)
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);
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";
201 $this->erreur(
'Requete non supportee : '.$sReq.$msg);
204 private function findListCritere($tReq){
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);
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);
232 private function sortResult($tObj,$tReq){
234 list($sChamp,$sSens)=preg_split(
'/ /',trim($tReq[
'order']));
238 foreach($tObj as $i => $oObj){
240 $tTri[ $i ]=(string)$oObj->$sChamp;
249 $tOrderedObj=array();
250 $tId= array_keys($tTri);
251 foreach($tId as $id){
252 $tOrderedObj[]=$tIdObj[$id];
257 private function findInTableWithCritere($sClassRow,$sTable,$tCritere){
258 $tFile=$this->getTabFromFile($this->getTabFromTable($sTable));
261 foreach($tFile as $tRow){
263 foreach($tCritere as $sCritereField => $sCritereVal){
265 if(!isset($tRow[$sCritereField]) or
267 ($sCritereVal[0]==
'=' and (
string)$sCritereVal!=(
string)
'='.$tRow[$sCritereField])
271 ($sCritereVal[0]==
'!' and (
string)$sCritereVal==(
string)
'!'.$tRow[$sCritereField])
279 $oRow=
new $sClassRow($tRow);
289 public function quote($sVal){
292 public function getWhereAll(){
297 private function getIdFromTab($tId){
299 return current($tId);
304 private function save($tFile,$sTable){
305 $oFile=
new _file($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'.csv');
310 foreach($tFile as $sLigne){
311 $sFile.=trim($sLigne).$sRet;
314 $oFile->write($sFile);
316 private function getMaxId($sTable){
317 $tFile=$this->getTabFromTable($sTable);
319 $iMax=trim($tFile[0]);
325 public function getTabFromFile($tContent){
327 $tHeader=preg_split(
'/;/',$tContent[1]);
330 foreach($tContent as $i => $sLigne){
331 if($i < 2){
continue;}
332 $sLigne=html_entity_decode($sLigne,ENT_QUOTES);
333 $tLigne=preg_split(
'/;/',$sLigne);
336 foreach($tHeader as $i => $sHeader){
337 $tab2[ $sHeader ]=$this->decode($tLigne[$i]);
345 public function getTabFromTable($sTable){
346 $oFile=
new _file($this->_tConfig[$this->_sConfig.
'.database'].$sTable.
'.csv');
347 $tFile=$oFile->getTab();
351 public function encode($text){
352 return preg_replace(
'/\r/',
'',preg_replace(
'/\n/',
'##retour_chariot_fmk##',$text));
354 public function decode($text){
355 return preg_replace(
'/##retour_chariot_fmk##/',
"\n",$text);