Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 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
 
15
App::uses('ConnectionManager', 'Model');
16
 
17
/**
18
 * Class ToolbarAccess
19
 *
20
 * Contains logic for accessing DebugKit specific information.
21
 */
22
class ToolbarAccess extends Object {
23
 
24
/**
25
 * Runs an explain on a query if the connection supports EXPLAIN.
26
 * currently only PostgreSQL and MySQL are supported.
27
 *
28
 * @param string $connection Connection name
29
 * @param string $query SQL query to explain / find query plan for.
30
 * @return array Array of explain information or empty array if connection is unsupported.
31
 */
32
	public function explainQuery($connection, $query) {
33
		$db = ConnectionManager::getDataSource($connection);
34
		$datasource = $db->config['datasource'];
35
 
36
		$return = array();
37
		if (preg_match('/(Mysql|Postgres)$/', $datasource)) {
38
			$explained = $db->query('EXPLAIN ' . $query);
39
			if (preg_match('/Postgres$/', $datasource)) {
40
				$queryPlan = array();
41
				foreach ($explained as $postgreValue) {
42
					$queryPlan[] = array($postgreValue[0]['QUERY PLAN']);
43
				}
44
				$return = array_merge(array(array('')), $queryPlan);
45
			} else {
46
				$keys = array_keys($explained[0][0]);
47
				foreach ($explained as $mysqlValue) {
48
					$queryPlan[] = array_values($mysqlValue[0]);
49
				}
50
				$return = array_merge(array($keys), $queryPlan);
51
			}
52
		}
53
		return $return;
54
	}
55
 
56
}