Subversion Repositories PHPX

Rev

Rev 73 | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
<?php

namespace PointedEars\PHPX\Db;

class MySQLDB extends Database
{
  /**
   * Database host
   * @var string
   */

  protected $_host;

  /**
   * Database port on the host
   * @var int
   */

  protected $_port;

  /**
   * MySQL Unix socket (shouldn't be used with host or port).
   * @var string
   */

  protected $_unix_socket;

  /**
  * Database name
  * @var string
  */

  protected $_dbname;

  /**
   * Username to access the database
   * @var string
   */

  protected $_username;

  /**
   * Password to access the database
   * @var string
   */

  protected $_password;

  /**
   * Optional charset parameter value
   * @var string
   */

  protected $_charset = null;

  /**
   * (non-PHPdoc)
   * @see Database::_leftQuote
   */

    protected $_leftQuote = '`';

  /**
   * (non-PHPdoc)
   * @see Database::_rightQuote
   */

  protected $_rightQuote = '`';

  public function __construct()
  {
        $dbconfig = $this->readConfig();
        if ($dbconfig === false)
        {
          return;
        }

        if (isset($dbconfig['unix_socket']))
        {
          $this->_unix_socket = $dbconfig['unix_socket'];
        }

    $this->_dsn = "mysql:host={$this->_host}"
      . (!is_null($this->_port) ? ";port={$this->_port}" : '')
      . (!is_null($this->_unix_socket) ? ";unix_socket={$this->_unix_socket}" : '')
      . (!is_null($this->_dbname) ? ";dbname={$this->_dbname}" : '')
      . (!is_null($this->_charset) ? ";charset={$this->_charset}" : '');

    if (!is_null($this->_charset))
    {
      $this->_options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . $this->_charset;
    }

    parent::__construct();
  }

  /**
   * (non-PHPdoc)
   * @see \PointedEars\PHPX\Db\Database::_columnDef()
   */

  protected function _columnDef (array $data, $columnName)
  {
        $standard_Def = parent::_columnDef($data, $columnName);
        $mySQL_def = $standardDef
        . (isset($data['format'])  && $data['format']  ? " COLUMN_FORMAT {$data['format']}" : '')
                  . (isset($data['storage']) && $data['storage'] ? " STORAGE {$data['storage']}"      : '');

        return $mySQL_def;
  }

  /**
   * Creates this MySQL database
   *
   * @param string $dsn
   *   Ignored.  Required for strict compatibility only.
   * @param string $username = null
   * @param string $password = null
   * @param array? $options = null
   *   Ignored.  Required for strict compatibility only.
   * @param string $dbspec = null
   *   Currently ignored.
   * @param boolean $force = false
   * @see Database::create()
   */

  public function create ($dsn, $username = null, $password = null,
    array $options = null, $dbspec = null, $force = false)
  {
    return parent::create(
      "mysql:host={$this->_host}"
      . (!is_null($this->_charset) ? ";charset={$this->_charset}" : ''),
      $username, $password, null, $force);
  }

  /**
   * (non-PHPdoc)
   * @see \PointedEars\PHPX\Db\Database::getLastModified()
   */

  public function getLastModified ($table = null)
  {
    $filter = array('TABLE_SCHEMA' => $this->_dbname);

    if ($table !== null)
    {
      $filter['TABLE_NAME'] = $table;
    }

    $times = $this->select(
      '`information_schema`.`tables`',
      array('CREATE_TIME', 'UPDATE_TIME'),
      $filter,
      array('UPDATE_TIME DESC', 'CREATE_TIME DESC'),
      1
    );

    $modi = ($times ? $times[0] : null);

    if ($modi)
    {
      $modi = $modi['UPDATE_TIME'] ?: $modi['CREATE_TIME'];

      if ($modi)
      {
        $modi = strtotime($modi . ' GMT');
      }
    }

    return $modi;
  }
}