Base for a static organization website

ToolbarAccess.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since DebugKit 1.3
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('ConnectionManager', 'Model');
  15. /**
  16. * Class ToolbarAccess
  17. *
  18. * Contains logic for accessing DebugKit specific information.
  19. */
  20. class ToolbarAccess extends Object {
  21. /**
  22. * Runs an explain on a query if the connection supports EXPLAIN.
  23. * currently only PostgreSQL and MySQL are supported.
  24. *
  25. * @param string $connection Connection name
  26. * @param string $query SQL query to explain / find query plan for.
  27. * @return array Array of explain information or empty array if connection is unsupported.
  28. */
  29. public function explainQuery($connection, $query) {
  30. $db = ConnectionManager::getDataSource($connection);
  31. $datasource = $db->config['datasource'];
  32. $return = array();
  33. if (preg_match('/(Mysql|Postgres)$/', $datasource)) {
  34. $explained = $db->query('EXPLAIN ' . $query);
  35. if (preg_match('/Postgres$/', $datasource)) {
  36. $queryPlan = array();
  37. foreach ($explained as $postgreValue) {
  38. $queryPlan[] = array($postgreValue[0]['QUERY PLAN']);
  39. }
  40. $return = array_merge(array(array('')), $queryPlan);
  41. } else {
  42. $keys = array_keys($explained[0][0]);
  43. foreach ($explained as $mysqlValue) {
  44. $queryPlan[] = array_values($mysqlValue[0]);
  45. }
  46. $return = array_merge(array($keys), $queryPlan);
  47. }
  48. }
  49. return $return;
  50. }
  51. }