Server_Controller.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * Php-Gtk MySQL administrator - server controller
  4. *
  5. * @license CeCILL 2.0
  6. * @copyright 2008 Ouest Systemes Informatiques
  7. * @author Frederic G. MARAND
  8. * @version $Id: Server_Controller.php,v 1.1 2008-05-25 20:29:20 cvs Exp $
  9. */
  10. /**
  11. * Wrapper around the PDO object for MySQL
  12. */
  13. class Server_Controller
  14. {
  15. protected $host = 'localhost';
  16. protected $user = 'root';
  17. /**
  18. * TCP/IP port. Default is defined by MySQL
  19. *
  20. * @var int
  21. */
  22. protected $port = 3306;
  23. /**
  24. * cleartext password(!)
  25. * @FIXME find a way to avoid storing the clear text password
  26. * @var string
  27. */
  28. protected $pass = NULL;
  29. /**
  30. * @var PDO
  31. */
  32. public $conn;
  33. /**
  34. *
  35. * @param array $arCred
  36. * @return void
  37. */
  38. public function __construct(array $arCred = array())
  39. {
  40. foreach ($arCred as $key => $val)
  41. {
  42. $this->$key = $val;
  43. }
  44. }
  45. /**
  46. * Connect to the server
  47. *
  48. * @return bool
  49. * @throws PDOException
  50. */
  51. public function login()
  52. {
  53. $this->conn = new PDO('mysql:host=' . $this->host
  54. . ';port=' . $this->port
  55. . ';dbname=mysql',
  56. $this->user,
  57. $this->pass);
  58. return (is_object($this->conn));
  59. }
  60. /**
  61. * Obtain a list of the databases visible on this server
  62. * when connected with the current credentials
  63. *
  64. * @return array
  65. */
  66. public function getDatabases()
  67. {
  68. $q = $this->conn->query('SHOW DATABASES');
  69. $ret = $q->fetchAll(PDO::FETCH_COLUMN, 0);
  70. $q->closeCursor();
  71. return $ret;
  72. }
  73. /**
  74. * Return the status variables of the server, or a specific
  75. * variable if $key is not empty
  76. *
  77. * Note that if $key is not empty, there is no way to differentiate
  78. * between a NULL result and an undefined variable. This seems most
  79. * appropriate for "sloppy" code like PHP.
  80. *
  81. * @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
  82. *
  83. * @param string $key
  84. * @return mixed Return an array if $key is empty, or a single value if it is set
  85. */
  86. public function getStatus($key = NULL)
  87. {
  88. $q = $this->conn->query('SHOW STATUS');
  89. if (empty($key))
  90. {
  91. $ret = $q->fetchAll(PDO::FETCH_COLUMN, 0);
  92. $q->closeCursor();
  93. }
  94. else
  95. {
  96. $ret = NULL;
  97. while ($row = $q->fetch(PDO::FETCH_NUM))
  98. {
  99. if ($row[0] === $key)
  100. {
  101. $ret = $row[1];
  102. $q->closeCursor();
  103. break;
  104. }
  105. }
  106. }
  107. return $ret;
  108. }
  109. /**
  110. * Flush the query cache, optionally emptying it. Flushing without
  111. * emptying just defragments the cache, losing nothing
  112. *
  113. * @param bool $reset
  114. * @return void
  115. */
  116. public function flushQueryCache($reset = FALSE)
  117. {
  118. $sq = ($reset ? 'RESET' : 'FLUSH') . ' QUERY CACHE';
  119. $ret = $this->conn->query($sq);
  120. $ret->closeCursor();
  121. }
  122. }