Class QueryExplain

Description

class QueryExplain rough utility class to get query explain plan and extract table names from abitrary sql so we can run show create table on them.

This class needs a user defined method to find the database connection info from the query and a bit of other data stored with the query_review_history table

The class is used as follows:

$result = $mysqli->query("SELECT sample, hostname_max, database_max FROM query_review_history WHERE checksum=1"); $row = $result->fetch_assoc();

$explainer = new QueryExplain($callback, $row); print $explainer->explain();

where $callback might look like: $callback = function(array $sample) { return array( 'host' => $sample['hostname_max'], 'db' => $sample['database_max'], 'user' => 'username', 'password' => 'password', ); }

The callback funtion will always take one array argument, and it needs to return and array with the following keys defined: host,db,user,password; and optionally: port

Because the object takes data associated with one query to return the db connection info, that means a QueryExplain object will always only be valid for one query, and will maintain its own connection

Sharing connection objects would be advantageous if you want to explain many queries which could be from the same database; but that's not needed for it's current use case.

Located in /QueryExplain.php (line 48)


	
			
Method Summary
QueryExplain __construct (callback $get_connection_func, array $sample)
null|string explain ()
string get_create ()
{string} result_as_table ({MySQLi_Result} $result)
Methods
Constructor __construct (line 65)

Constructor. See class documentation for explaination of the parameters

  • throws: Exception if a database connection cannot be made
QueryExplain __construct (callback $get_connection_func, array $sample)
  • callback $get_connection_func: The callback function
  • array $sample: array of information about the query
explain (line 144)

If the given query is a SELECT statement, return the explain plan

  • return: The explain plan, or an error message
  • access: public
null|string explain ()
get_create (line 91)

Extract the table names from a query, and return the result of SHOW CREATE TABLE tablename;

  • return: the create table statements, or an error message
  • access: public
string get_create ()
get_tables_from_query (line 80)

Try to parse the real table names out of a sql query

  • return: the list of tables in the query
  • access: public
array get_tables_from_query ()
get_table_status (line 118)

Extract the table names and the return the result of SHOW TABLE STATUS LIKE 'tablename' for each table;

  • access: public
null get_table_status ()
result_as_table (line 216)

given a mysqli result handle, format a string to look like the mysql cli

type tables

  • return: The formatted result set string
{string} result_as_table ({MySQLi_Result} $result)
  • {MySQLi_Result} $result: The result set handle

Documentation generated on Mon, 12 Mar 2012 11:50:14 -0700 by phpDocumentor 1.4.4