function explain()
 {
     //Check required Fields
     $missing = false;
     if (!isset($_POST["sql"]) || strlen($_POST["sql"]) == 0) {
         $missing = true;
     }
     if ($missing) {
         $this->res->success = false;
         $this->res->message = "missing required field!";
         return null;
     }
     //get vars
     $sql = $_POST["sql"];
     $schema_id = $this->params['schema_id'];
     $schema = Doo::db()->getOne('Schemata', array('where' => 'id = ' . $schema_id));
     //init
     $shard_query = new ShardQuery($schema->schema_name);
     //execute query
     $stmt = $shard_query->query($sql, true);
     //build data
     $this->res->message = $stmt;
     //return
     $this->res->success = true;
 }
 public function run($arg)
 {
     require 'shard-query-config.php';
     if (isset($cache_servers) && !empty($cache_servers)) {
         $this->cache = new SaltedCache($cache_servers);
     }
     if (isset($cache_rules) && !empty($cache_rules)) {
         $this->cache_rules = $cache_rules;
     }
     if (!$arg) {
         return false;
     }
     $arg = (object) $arg;
     $fields = false;
     $errors = false;
     $rows = false;
     $sql = "";
     $has_rows = false;
     $resultset = null;
     if (!isset($arg->schema_name)) {
         $arg->schema_name = null;
     }
     $SQ = new ShardQuery($arg->schema_name);
     print_r($arg);
     if (empty($arg->sql) || !trim($arg->sql)) {
         return false;
     }
     if (stripos($arg->sql, 'select database()') !== false) {
         $fields = array(array('type' => 250, 'name' => 'DATABASE()'));
         $rows = array(array($SQ->state->schema_name));
         $resultset = array('fields' => &$fields, 'rows' => &$rows);
     } elseif (preg_match('/^SHOW/i', $arg->sql)) {
         $shard_id = array_rand($SQ->state->shards);
         $DAL = SimpleDAL::factory($SQ->state->shards[$shard_id]);
         $DAL->my_select_db($SQ->state->shards[$shard_id]['db']);
         if (preg_match('/show\\s+databases/i', $arg->sql)) {
             $databases = $SQ->state->mapper->get_schemata();
             $fields = array(array('type' => 250, 'name' => 'Database'));
             /*	$rows = array();
             				foreach($databases as $schema_info) {
             					$rows[] = array($schema_info['schema_name']);
             				}		
             			*/
             $rows = array(array($SQ->state->schema_name));
             $resultset = array('fields' => &$fields, 'rows' => &$rows);
         } elseif ($stmt = $DAL->my_query($arg->sql)) {
             $row = $DAL->my_fetch_assoc();
             foreach ($row as $field => $val) {
                 $rows[0][] = $val;
                 $fields[] = array('type' => 250, 'name' => $field);
             }
             while ($row = $DAL->my_fetch_array($stmt, MYSQL_NUM)) {
                 $rows[] = $row;
             }
             print_r($rows);
             $resultset = array('fields' => &$fields, 'rows' => &$rows);
         }
         $DAL->my_close();
         unset($DAL);
     } elseif (preg_match('/select\\s+.*\\sfrom\\s.*/i', $arg->sql) || preg_match('/(create|drop|alter)\\s+.*/i', $arg->sql)) {
         $cache_ttl = null;
         if (isset($this->cache)) {
             $patterns = $this->cache_rules;
             foreach ($patterns as $pattern => $ttl) {
                 if (preg_match($pattern, $arg->sql)) {
                     $cache_ttl = $ttl;
                     $resultset = $this->cache->get($arg->sql, $arg->schema_name);
                     break;
                 }
             }
         }
         if (!$resultset) {
             $stmt = $SQ->query($arg->sql);
             if (!empty($SQ->errors)) {
                 $errors = trim(str_replace(array("\n", "Array", "(", ")", "  "), "", print_r($SQ->errors, true)));
             }
             if ($stmt) {
                 $has_rows = true;
                 $rows = array(array());
                 # get the first row and use it to construct the list of fields + collect row data
                 # in this first fetch we process data one output column at a time
                 $row = $SQ->DAL->my_fetch_assoc($stmt);
                 foreach ($row as $field => $val) {
                     $rows[0][] = $val;
                     $fields[] = array('type' => 250, 'name' => $field);
                 }
                 # fetch the rest of the rows numerically and stuff into $rows, a row at a time
                 while ($row = $SQ->DAL->my_fetch_array($stmt, MYSQL_NUM)) {
                     $rows[] = $row;
                 }
                 $resultset = array('fields' => &$fields, 'rows' => &$rows);
             }
             if (isset($cache_ttl)) {
                 $this->cache->set($arg->sql, $resultset, $cache_ttl, $arg->schema_name);
             }
             # return the actual object so that the proxy can finish aggregation and drop the table
         }
     } else {
         $sql = $arg->sql;
     }
     return json_encode(array('resultset' => $resultset, 'errors' => $errors, 'sql' => $sql, 'has_rows' => $has_rows));
 }