123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- /*
- * Php-Gtk MySQL administrator - server controller
- *
- * @license CeCILL 2.0
- * @copyright 2008 Ouest Systemes Informatiques
- * @author Frederic G. MARAND
- * @version $Id: Server_Controller.php,v 1.1 2008-05-25 20:29:20 cvs Exp $
- */
- /**
- * Wrapper around the PDO object for MySQL
- */
- class Server_Controller
- {
- protected $host = 'localhost';
- protected $user = 'root';
- /**
- * TCP/IP port. Default is defined by MySQL
- *
- * @var int
- */
- protected $port = 3306;
-
- /**
- * cleartext password(!)
- * @FIXME find a way to avoid storing the clear text password
- * @var string
- */
- protected $pass = NULL;
- /**
- * @var PDO
- */
- public $conn;
-
- /**
- *
- * @param array $arCred
- * @return void
- */
- public function __construct(array $arCred = array())
- {
- foreach ($arCred as $key => $val)
- {
- $this->$key = $val;
- }
- }
-
- /**
- * Connect to the server
- *
- * @return bool
- * @throws PDOException
- */
- public function login()
- {
- $this->conn = new PDO('mysql:host=' . $this->host
- . ';port=' . $this->port
- . ';dbname=mysql',
- $this->user,
- $this->pass);
- return (is_object($this->conn));
- }
-
- /**
- * Obtain a list of the databases visible on this server
- * when connected with the current credentials
- *
- * @return array
- */
- public function getDatabases()
- {
- $q = $this->conn->query('SHOW DATABASES');
- $ret = $q->fetchAll(PDO::FETCH_COLUMN, 0);
- $q->closeCursor();
- return $ret;
- }
-
- /**
- * Return the status variables of the server, or a specific
- * variable if $key is not empty
- *
- * Note that if $key is not empty, there is no way to differentiate
- * between a NULL result and an undefined variable. This seems most
- * appropriate for "sloppy" code like PHP.
- *
- * @todo check whether it wouldn't be faster in the $key case to just fetch all like in the no key case, and use array indexing to extract the result
- *
- * @param string $key
- * @return mixed Return an array if $key is empty, or a single value if it is set
- */
- public function getStatus($key = NULL)
- {
- $q = $this->conn->query('SHOW STATUS');
- if (empty($key))
- {
- $ret = $q->fetchAll(PDO::FETCH_COLUMN, 0);
- $q->closeCursor();
- }
- else
- {
- $ret = NULL;
- while ($row = $q->fetch(PDO::FETCH_NUM))
- {
- if ($row[0] === $key)
- {
- $ret = $row[1];
- $q->closeCursor();
- break;
- }
- }
- }
- return $ret;
- }
-
- /**
- * Flush the query cache, optionally emptying it. Flushing without
- * emptying just defragments the cache, losing nothing
- *
- * @param bool $reset
- * @return void
- */
- public function flushQueryCache($reset = FALSE)
- {
- $sq = ($reset ? 'RESET' : 'FLUSH') . ' QUERY CACHE';
- $ret = $this->conn->query($sq);
- $ret->closeCursor();
- }
- }
|