00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00021 final class MySQLim extends Sequenceless
00022 {
00026 public static function getDialect()
00027 {
00028 return MyImprovedDialect::me();
00029 }
00030
00034 public function setDbEncoding()
00035 {
00036 mysqli_set_charset($this->link, $this->encoding);
00037
00038 return $this;
00039 }
00040
00044 public function connect()
00045 {
00046 if ($this->persistent)
00047 throw new UnsupportedMethodException();
00048
00049 $this->link = mysqli_init();
00050
00051 try {
00052 mysqli_real_connect(
00053 $this->link,
00054 $this->hostname,
00055 $this->username,
00056 $this->password,
00057 $this->basename,
00058 $this->port,
00059 null,
00060 MYSQLI_CLIENT_FOUND_ROWS
00061 );
00062 } catch (BaseException $e) {
00063 throw new DatabaseException(
00064 'can not connect to MySQL server: '.$e->getMessage()
00065 );
00066 }
00067
00068 if ($this->encoding)
00069 $this->setDbEncoding();
00070
00071 return $this;
00072 }
00073
00077 public function disconnect()
00078 {
00079 if ($this->isConnected())
00080 mysqli_close($this->link);
00081
00082 return $this;
00083 }
00084
00089 public function queryCount(Query $query)
00090 {
00091 $this->queryNull($query);
00092
00093 return mysqli_affected_rows($this->link);
00094 }
00095
00096 public function queryRow(Query $query)
00097 {
00098 $res = $this->query($query);
00099
00100 if ($this->checkSingle($res))
00101 return mysqli_fetch_assoc($res);
00102 else
00103 return null;
00104 }
00105
00106 public function queryColumn(Query $query)
00107 {
00108 $res = $this->query($query);
00109
00110 if ($res) {
00111 $array = array();
00112
00113 while ($row = mysqli_fetch_row($res))
00114 $array[] = $row[0];
00115
00116 return $array;
00117 } else
00118 return null;
00119 }
00120
00121 public function querySet(Query $query)
00122 {
00123 $res = $this->query($query);
00124
00125 if ($res) {
00126 $array = array();
00127
00128 while ($row = mysqli_fetch_assoc($res))
00129 $array[] = $row;
00130
00131 return $array;
00132 } else
00133 return null;
00134 }
00135
00136 public function queryRaw($queryString)
00137 {
00138 return $this->realQueryRaw($queryString, false);
00139 }
00140
00144 public function queueFlush()
00145 {
00146 if ($this->queue)
00147 $this->realQueryRaw(
00148 implode(";\n", $this->queue),
00149 true
00150 );
00151
00152 $this->toQueue = false;
00153
00154 return $this->queueDrop();
00155 }
00156
00157 public function getTableInfo($table)
00158 {
00159 throw new UnimplementedFeatureException();
00160 }
00161
00162 protected function getInsertId()
00163 {
00164 return mysqli_insert_id($this->link);
00165 }
00166
00167 private function realQueryRaw($queryString, $multi = false)
00168 {
00169 $function = $multi ? 'mysqli_multi_query' : 'mysqli_query';
00170
00171 if (!$result = $function($this->link, $queryString)) {
00172
00173 $code = mysqli_errno($this->link);
00174
00175 if ($code == 1062)
00176 $e = 'DuplicateObjectException';
00177 else
00178 $e = 'DatabaseException';
00179
00180 throw new $e(
00181 mysqli_error($this->link).' - '.$queryString,
00182 $code
00183 );
00184 }
00185
00186 return $result;
00187 }
00188
00189 private function checkSingle($result)
00190 {
00191 if (mysqli_num_rows($result) > 1)
00192 throw new TooManyRowsException(
00193 'query returned too many rows (we need only one)'
00194 );
00195
00196 return $result;
00197 }
00198 }
00199 ?>