php__pgma/Server_Controller.php

129 lines
No EOL
2.9 KiB
PHP

<?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();
}
}