MySQL.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Sveta A. Smirnova                          *
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 /* $Id: MySQL.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00021     final class MySQL extends Sequenceless
00022     {
00026         public static function getDialect()
00027         {
00028             return MyDialect::me();
00029         }
00030         
00034         public function setDbEncoding()
00035         {
00036             mysql_query("SET NAMES '{$this->encoding}'", $this->link);
00037             
00038             return $this;
00039         }
00040 
00044         public function connect()
00045         {
00046             $this->link =
00047                 $this->persistent
00048                     ?
00049                         mysql_pconnect(
00050                             $this->hostname,
00051                             $this->username,
00052                             $this->password,
00053                             // 2 == CLIENT_FOUND_ROWS
00054                             2
00055                         )
00056                     :
00057                         mysql_connect(
00058                             $this->hostname,
00059                             $this->username,
00060                             $this->password,
00061                             true,
00062                             // 2 == CLIENT_FOUND_ROWS
00063                             2
00064                         );
00065                             
00066             if (
00067                 !$this->link
00068                 || (
00069                     $this->basename
00070                     && !mysql_select_db($this->basename, $this->link)
00071                 )
00072             )
00073                 throw new DatabaseException(
00074                     'can not connect to MySQL server: '.mysql_error($this->link),
00075                     mysql_errno($this->link)
00076                 );
00077             
00078             if ($this->encoding)
00079                 $this->setDbEncoding();
00080             
00081             return $this;
00082         }
00083         
00087         public function disconnect()
00088         {
00089             if ($this->isConnected())
00090                 mysql_close($this->link);
00091 
00092             return $this;
00093         }
00094         
00099         public function queryCount(Query $query)
00100         {
00101             $this->queryNull($query);
00102             
00103             return mysql_affected_rows($this->link);
00104         }
00105         
00106         public function queryRow(Query $query)
00107         {
00108             $res = $this->query($query);
00109             
00110             if ($this->checkSingle($res))
00111                 return mysql_fetch_assoc($res);
00112             else
00113                 return null;
00114         }
00115         
00116         public function queryColumn(Query $query)
00117         {
00118             $res = $this->query($query);
00119             
00120             if ($res) {
00121                 $array = array();
00122 
00123                 while ($row = mysql_fetch_row($res))
00124                     $array[] = $row[0];
00125 
00126                 return $array;
00127             } else
00128                 return null;
00129         }
00130         
00131         public function querySet(Query $query)
00132         {
00133             $res = $this->query($query);
00134             
00135             if ($res) {
00136                 $array = array();
00137 
00138                 while ($row = mysql_fetch_assoc($res))
00139                     $array[] = $row;
00140 
00141                 return $array;
00142             } else
00143                 return null;
00144         }
00145         
00146         public function queryRaw($queryString)
00147         {
00148             if (!$result = mysql_query($queryString, $this->link)) {
00149                 
00150                 $code = mysql_errno($this->link);
00151                 
00152                 if ($code == 1062)
00153                     $e = 'DuplicateObjectException';
00154                 else
00155                     $e = 'DatabaseException';
00156 
00157                 throw new $e(
00158                     mysql_error($this->link).' - '.$queryString,
00159                     $code
00160                 );
00161             }
00162 
00163             return $result;
00164         }
00165         
00166         public function getTableInfo($table)
00167         {
00168             throw new UnimplementedFeatureException();
00169         }
00170         
00171         public function hasQueue()
00172         {
00173             return false;
00174         }
00175         
00176         protected function getInsertId()
00177         {
00178             return mysql_insert_id($this->link);
00179         }
00180         
00181         private function checkSingle($result)
00182         {
00183             if (mysql_num_rows($result) > 1)
00184                 throw new TooManyRowsException(
00185                     'query returned too many rows (we need only one)'
00186                 );
00187             
00188             return $result;
00189         }
00190     }
00191 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4