MySQLim.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00020     final class MySQLim extends Sequenceless
00021     {
00025         public static function getDialect()
00026         {
00027             return MyImprovedDialect::me();
00028         }
00029         
00033         public function setDbEncoding()
00034         {
00035             mysqli_set_charset($this->link, $this->encoding);
00036             
00037             return $this;
00038         }
00039 
00043         public function connect()
00044         {
00045             if ($this->persistent)
00046                 throw new UnsupportedMethodException();
00047             
00048             $this->link = mysqli_init();
00049             
00050             try {
00051                 mysqli_real_connect(
00052                     $this->link,
00053                     $this->hostname,
00054                     $this->username,
00055                     $this->password,
00056                     $this->basename,
00057                     $this->port,
00058                     null,
00059                     MYSQLI_CLIENT_FOUND_ROWS
00060                 );
00061             } catch (BaseException $e) {
00062                 throw new DatabaseException(
00063                     'can not connect to MySQL server: '.$e->getMessage()
00064                 );
00065             }
00066             
00067             if ($this->encoding)
00068                 $this->setDbEncoding();
00069             
00070             return $this;
00071         }
00072         
00076         public function disconnect()
00077         {
00078             if ($this->isConnected())
00079                 mysqli_close($this->link);
00080 
00081             return $this;
00082         }
00083         
00088         public function queryCount(Query $query)
00089         {
00090             $this->queryNull($query);
00091             
00092             return mysqli_affected_rows($this->link);
00093         }
00094         
00095         public function queryRow(Query $query)
00096         {
00097             $res = $this->query($query);
00098             
00099             if ($this->checkSingle($res))
00100                 return mysqli_fetch_assoc($res);
00101             else
00102                 return null;
00103         }
00104         
00105         public function queryColumn(Query $query)
00106         {
00107             $res = $this->query($query);
00108             
00109             if ($res) {
00110                 $array = array();
00111 
00112                 while ($row = mysqli_fetch_row($res))
00113                     $array[] = $row[0];
00114 
00115                 return $array;
00116             } else
00117                 return null;
00118         }
00119         
00120         public function querySet(Query $query)
00121         {
00122             $res = $this->query($query);
00123             
00124             if ($res) {
00125                 $array = array();
00126 
00127                 while ($row = mysqli_fetch_assoc($res))
00128                     $array[] = $row;
00129 
00130                 return $array;
00131             } else
00132                 return null;
00133         }
00134         
00135         public function queryRaw($queryString)
00136         {
00137             if (!$result = mysqli_query($this->link, $queryString)) {
00138                 
00139                 $code = mysqli_errno($this->link);
00140                 
00141                 if ($code == 1062)
00142                     $e = 'DuplicateObjectException';
00143                 else
00144                     $e = 'DatabaseException';
00145                 
00146                 throw new $e(
00147                     mysqli_error($this->link).' - '.$queryString,
00148                     $code
00149                 );
00150             }
00151             
00152             return $result;
00153         }
00154         
00155         public function getTableInfo($table)
00156         {
00157             throw new UnimplementedFeatureException();
00158         }
00159         
00160         public function hasQueue()
00161         {
00162             return false;
00163         }
00164         
00165         protected function getInsertId()
00166         {
00167             return mysqli_insert_id($this->link);
00168         }
00169         
00170         private function checkSingle($result)
00171         {
00172             if (mysqli_num_rows($result) > 1)
00173                 throw new TooManyRowsException(
00174                     'query returned too many rows (we need only one)'
00175                 );
00176             
00177             return $result;
00178         }
00179     }
00180 ?>