.

DBDriverMysql Class Reference
[Model]

Driver for MySQL. More...

Inheritance diagram for DBDriverMysql:
IDBDriver

List of all members.

Public Member Functions

  escape ($value)
  Escape given value.
  escape_database_entity ($obj, $type=self::FIELD)
  Quote given database object, liek table, field etc.
  execute ($sql)
  Execute an SQL command (Insert, Update.
  explain ($sql)
  Explain the given query.
  get_db_name ()
  Returns name of DB.
  get_driver_name ()
  Return name of driver, e.g "mysql".
  get_host ()
  Returns host name of database.
  get_status ()
  Return current status.
  has_feature ($feature)
  Returns true, if a given feature is supported.
  initialize ($dbname, $user= '', $password= '', $host= 'localhost', $params=false)
  Connect to DB.
  last_insert_id ()
  Get last insert ID.
  make_default ()
  Make this driver the default driver.
  query ($sql)
  Execute a Select statement.
  quote ($value)
  Quote given value.
  trans_commit ()
  Commit transaction.
  trans_rollback ()
  Rollback transaction.
  trans_start ()
  Start transaction.

Public Attributes

const  PRIMARY = 0
  Primary connection.
const  SECONDARY = 1
  A connectionj other than default one.

Protected Member Functions

  connect ()
  Connect if not already connceted.

Protected Attributes

  $connect_params
  $db_handle = false
  $type

Static Protected Attributes

static  $transaction_count = 0

Detailed Description

Driver for MySQL.

Author:
Gerd Riesselmann

Definition at line 8 of file dbdriver.mysql.php.


Member Function Documentation

DBDriverMysql::connect (  )  [protected]

Connect if not already connceted.

Returns:
void

Definition at line 79 of file dbdriver.mysql.php.

00079                                      {
00080                 if ($this->db_handle === false) {
00081                         $err = new Status();
00082                         $this->db_handle = mysql_connect(
00083                                 $this->connect_params['host'], 
00084                                 $this->connect_params['user'],
00085                                 $this->connect_params['pwd']
00086                         );
00087                         if ($this->db_handle) {
00088                                 if ($this->type == self::PRIMARY) {
00089                                         $err->merge($this->make_default());
00090                                 } 
00091                                 if ($err->is_ok()) {
00092                                         // We are connected
00093                                         if (GyroLocale::get_charset() == 'UTF-8') {
00094                                                 $this->execute("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
00095                                         }
00096                                         //$this->execute("SET sql_mode=STRICT_ALL_TABLES");
00097                                 }
00098                         }
00099                         else {
00100                                 $err->append(tr(
00101                                         'Could not connect to server %host', 
00102                                         'core', 
00103                                         array('%host' => $this->connect_params['host'])
00104                                 ));
00105                         }
00106                         if ($err->is_error()) {
00107                                 throw new Exception($err->to_string(Status::OUTPUT_PLAIN));
00108                         }
00109                 }
00110         }
DBDriverMysql::escape ( value  ) 

Escape given value.

Parameters:
mixed  $value
Returns:
string

Implements IDBDriver.

Definition at line 141 of file dbdriver.mysql.php.

00141                                        {
00142                 $this->connect();
00143                 return mysql_real_escape_string(Cast::string($value), $this->db_handle);
00144         }
DBDriverMysql::escape_database_entity ( obj,
type = self::FIELD  
)

Quote given database object, liek table, field etc.

Parameters:
string  $obj

Implements IDBDriver.

Definition at line 126 of file dbdriver.mysql.php.

00126                                                                         {
00127                 $ret = '';
00128                 if ($type === self::TABLE) {
00129                         $ret .= '`' . $this->get_db_name() . '`.';
00130                 }
00131                 $ret .= '`' . $obj . '`';
00132                 return $ret;
00133         }
DBDriverMysql::execute ( sql  ) 

Execute an SQL command (Insert, Update.

..)

Parameters:
string  $sql
Returns:
Status

Implements IDBDriver.

Definition at line 166 of file dbdriver.mysql.php.

00166                                       {
00167                 $this->connect();
00168                 mysql_query($sql, $this->db_handle);
00169                 return $this->get_status();             
00170         }
DBDriverMysql::explain ( sql  ) 

Explain the given query.

Parameters:
string  $sql
Returns:
IDBResultSet

Implements IDBDriver.

Definition at line 191 of file dbdriver.mysql.php.

00191                                       {
00192                 $ret = false;
00193                 if (strtolower(substr($sql, 0, 6)) === 'select') {
00194                         $sql = 'EXPLAIN ' . $sql;
00195                         $ret = $this->query($sql);
00196                 }
00197                 return $ret;
00198         }       
DBDriverMysql::get_db_name (  ) 

Returns name of DB.

Returns:
string

Implements IDBDriver.

Definition at line 49 of file dbdriver.mysql.php.

00049                                       {
00050                 return Arr::get_item($this->connect_params, 'db', '');
00051         }
DBDriverMysql::get_driver_name (  ) 

Return name of driver, e.g "mysql".

Lowercase!

Returns:
string

Implements IDBDriver.

Definition at line 31 of file dbdriver.mysql.php.

00031                                           {
00032                 return 'mysql';
00033         }
DBDriverMysql::get_host (  ) 

Returns host name of database.

Returns:
string

Implements IDBDriver.

Definition at line 40 of file dbdriver.mysql.php.

00040                                    {
00041                 return Arr::get_item($this->connect_params, 'host', '');
00042         }
DBDriverMysql::get_status (  ) 

Return current status.

Returns:
Status

Implements IDBDriver.

Definition at line 151 of file dbdriver.mysql.php.

00151                                      {
00152                 $this->connect();
00153                 $ret = new Status();
00154                 if (mysql_errno($this->db_handle)) {
00155                         $ret->append(mysql_error($this->db_handle));
00156                 }
00157                 return $ret;
00158         }
DBDriverMysql::has_feature ( feature  ) 

Returns true, if a given feature is supported.

Parameters:
string  feature
Returns:
bool

Implements IDBDriver.

Definition at line 274 of file dbdriver.mysql.php.

00274                                               {
00275                 switch ($feature) {
00276                         case self::FEATURE_REPLACE:
00277                                 return true;
00278                         default:
00279                                 return false;
00280                 }
00281         }       
DBDriverMysql::initialize ( dbname,
user = '',
password = '',
host = 'localhost',
params = false  
)

Connect to DB.

Parameters:
string  $dbname Name of DB
string  $user Username
string  $password Password
string  $host Host
array  $params Associative array allowing the following keys:
  • type: Connection type

Implements IDBDriver.

Definition at line 64 of file dbdriver.mysql.php.

00064                                                                                                               {
00065                 $this->connect_params = array(
00066                         'host' => $host,
00067                         'user' => $user,
00068                         'pwd' => $password,
00069                         'db' => $dbname,                        
00070                 );
00071                 $this->type = Arr::get_item($params, 'type', self::PRIMARY);
00072         }
DBDriverMysql::last_insert_id (  ) 

Get last insert ID.

Implements IDBDriver.

Definition at line 264 of file dbdriver.mysql.php.

00264                                          {
00265                 return mysql_insert_id($this->db_handle);
00266         }
DBDriverMysql::make_default (  ) 

Make this driver the default driver.

Returns:
Status

Implements IDBDriver.

Definition at line 205 of file dbdriver.mysql.php.

00205                                        {
00206                 $ret = new Status();
00207                 $this->connect();
00208                 if (!mysql_select_db($this->connect_params['db'], $this->db_handle)) {
00209                         $ret->append(tr(
00210                                 'Could not connect to database %db on server %host', 
00211                                 'core', 
00212                                 array('%db' => $this->connect_params['db'], '%host' => $this->connect_params['host'])
00213                         ));
00214                 }       
00215                 return $ret;    
00216         }       
DBDriverMysql::query ( sql  ) 

Execute a Select statement.

Parameters:
string  $sql
Returns:
IDBResultSet

Implements IDBDriver.

Definition at line 178 of file dbdriver.mysql.php.

00178                                     {
00179                 $this->connect();
00180                 $handle = mysql_query($sql, $this->db_handle);
00181                 $status = $this->get_status();
00182                 return new DBResultSetMysql($handle, $status);
00183         }
DBDriverMysql::quote ( value  ) 

Quote given value.

Parameters:
string  $value

Implements IDBDriver.

Definition at line 117 of file dbdriver.mysql.php.

00117                                       {
00118                 return "'" . $this->escape($value) . "'";
00119         }
DBDriverMysql::trans_commit (  ) 

Commit transaction.

Implements IDBDriver.

Definition at line 236 of file dbdriver.mysql.php.

00236                                        {
00237                 if (self::$transaction_count > 0) {
00238                         self::$transaction_count--;
00239                         if (self::$transaction_count == 0) {
00240                                 mysql_query('COMMIT', $this->db_handle);
00241                         }
00242                 }
00243         }
DBDriverMysql::trans_rollback (  ) 

Rollback transaction.

Implements IDBDriver.

Definition at line 248 of file dbdriver.mysql.php.

00248                                          {
00249                 if (self::$transaction_count > 0) {
00250                         self::$transaction_count--;
00251                         if (self::$transaction_count == 0) {
00252                                 // Rollback anything up to now
00253                                 mysql_query('ROLLBACK', $this->db_handle);
00254                         }
00255                 }
00256                 // Start a transaction that won't get committed
00257                 //mysql_query('START TRANSACTION', $this->db_handle);   
00258                 //self::$transaction_count = -1; // No commits will be issued
00259         }
DBDriverMysql::trans_start (  ) 

Start transaction.

Implements IDBDriver.

Definition at line 221 of file dbdriver.mysql.php.

00221                                       {
00222                 if (self::$transaction_count >= 0) {
00223                         if (self::$transaction_count == 0) {
00224                                 // We support nested transaction, while MySQL doesn't
00225                                 // "Beginning a transaction causes any pending transaction to be committed.", http://dev.mysql.com/doc/refman/5.0/en/commit.html
00226                                 $this->connect();
00227                                 mysql_query('START TRANSACTION', $this->db_handle);     
00228                         }
00229                         self::$transaction_count++;
00230                 }               
00231         }

Member Data Documentation

DBDriverMysql::$connect_params [protected]

Definition at line 25 of file dbdriver.mysql.php.

DBDriverMysql::$db_handle = false [protected]

Definition at line 23 of file dbdriver.mysql.php.

DBDriverMysql::$transaction_count = 0 [static, protected]

Definition at line 24 of file dbdriver.mysql.php.

DBDriverMysql::$type [protected]

Definition at line 22 of file dbdriver.mysql.php.

Primary connection.

Definition at line 12 of file dbdriver.mysql.php.

A connectionj other than default one.

Definition at line 16 of file dbdriver.mysql.php.


The documentation for this class was generated from the following file: