MkFramework
 All Data Structures Functions
sgbd_mysql.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_mysql extends abstract_sgbd{
19 
20  public static function getInstance($sConfig){
21  return self::_getInstance(__CLASS__,$sConfig);
22  }
23 
24  public function findMany($tSql,$sClassRow){
25  $pRs=$this->query($this->bind($tSql));
26 
27  if(empty($pRs)){
28  return null;
29  }
30 
31  $tObj=array();
32  while($tRow=mysql_fetch_assoc($pRs)){
33  $oRow=new $sClassRow($tRow);
34  $tObj[]=$oRow;
35  }
36  return $tObj;
37  }
38  public function findManySimple($tSql,$sClassRow){
39  $pRs=$this->query($this->bind($tSql));
40 
41  if(empty($pRs)){
42  return null;
43  }
44 
45  $tObj=array();
46  while($oRow=mysql_fetch_object($pRs)){
47  $tObj[]=$oRow;
48  }
49  return $tObj;
50  }
51  public function findOne($tSql,$sClassRow){
52  $pRs=$this->query($this->bind($tSql));
53 
54  if(empty($pRs)){
55  return null;
56  }
57 
58  $tRow=mysql_fetch_assoc($pRs);
59  $oRow=new $sClassRow($tRow);
60 
61  return $oRow;
62  }
63  public function findOneSimple($tSql,$sClassRow){
64  $pRs=$this->query($this->bind($tSql));
65 
66  if(empty($pRs)){
67  return null;
68  }
69 
70  $oRow=mysql_fetch_object($pRs);
71 
72  return $oRow;
73  }
74  public function execute($tSql){
75  return $this->query($this->bind($tSql));
76  }
77 
78  public function update($sTable,$tProperty,$twhere){
79  $this->query('UPDATE '.$sTable.' SET '.$this->getUpdateFromTab($tProperty).' WHERE '.$this->getWhereFromTab($twhere));
80  }
81  public function insert($sTable,$tProperty){
82  $this->query('INSERT INTO '.$sTable.' '.$this->getInsertFromTab($tProperty));
83  }
84 
85  public function delete($sTable,$twhere){
86  $this->query('DELETE FROM '.$sTable.' WHERE '.$this->getWhereFromTab($twhere));
87  }
88 
89  public function getListColumn($sTable){
90  $pRs=$this->query(sgbd_syntax_mysql::getListColumn($sTable));
91  $tCol=array();
92 
93  if(empty($pRs)){
94  return $tCol;
95  }
96 
97  while($tRow=mysql_fetch_row($pRs)){
98  $tCol[]=$tRow[0];
99  }
100 
101  return $tCol;
102  }
103  public function getListTable(){
104  $pRs=$this->query(sgbd_syntax_mysql::getListTable());
105  $tCol=array();
106 
107  if(empty($pRs)){
108  return $tCol;
109  }
110 
111  while($tRow=mysql_fetch_row($pRs)){
112  $tCol[]=$tRow[0];
113  }
114  return $tCol;
115  }
116 
117  private function connect(){
118  if(empty($this->_pDb)){
119  if( ($this->_pDb=mysql_connect(
120  $this->_tConfig[$this->_sConfig.'.hostname'],
121  $this->_tConfig[$this->_sConfig.'.username'],
122  $this->_tConfig[$this->_sConfig.'.password']
123  ))==false ){
124  throw new Exception('Probleme connexion sql : '.mysql_error());
125  }
126  if( mysql_select_db($this->_tConfig[$this->_sConfig.'.database'],$this->_pDb) ==false){
127  $sMsg='Probleme selection de la base : '.$this->_tConfig[$this->_sConfig.'.database'].' '.mysql_error();
128  throw new Exception($sMsg);
129  }
130 
131  }
132  }
133  public function getLastInsertId(){
134  $pRs=$this->query(sgbd_syntax_mysql::getLastInsertId());
135 
136  if(empty($pRs)){
137  return null;
138  }
139  $tRow=$pRs->fetch(PDO::FETCH_NUM);
140  return (int)$tRow[0];
141  }
142 
143  private function query($sReq){
144  $this->connect();
145  $this->sReq=$sReq;
146  return mysql_query($sReq);
147  }
148  public function quote($sVal){
149  return str_replace("\\",'', str_replace("'",'\'',"'".$sVal."'"));
150  }
151  public function getWhereAll(){
152  return '1=1';
153  }
154 
155 
156 }