/** * Test builder {@link epQueryBuilder} * @param string $q the query string * @param array $args the array of arguments * @param string $expect the expected resultant SQL statement * @param boolean $debug whether to print out debugging info * @return void */ function _testBuilder($q, $args = array(), $expect, $debug = false) { static $tests = 0; $tests++; echo " testBuilder{$tests}..."; // setup manager $this->_setUp('adodb', 'mysql'); $this->assertTrue($this->m); // parse query string $this->assertNotNull($p = new epQueryParser($q)); $t0 = microtime(true); $root = $p->parse(); $t = microtime(true) - $t0; $this->assertTrue($root); // debug if ($debug) { echo 'Parser : ' . $t . ' s' . "\n"; } // build sql statement $b = new epQueryBuilder($root, $q, $args); $this->assertNotNull($b); $t0 = microtime(true); $sql = $b->build(); $t = microtime(true) - $t0; // debug if ($debug) { echo 'Builder: ' . $t . ' s' . "\n"; echo "EZOQL : {$q}\n"; } $r = true; //print_r($sql); if (is_string($expect)) { // debug if ($debug) { echo "SQL Expected : {$expect}\n"; echo "SQL Actual : " . $sql[0] . "\n"; } $this->assertTrue($r = $sql[0] === $expect); } else { for ($i = 0; $i < count($expect); $i++) { // debug if ($debug) { echo "SQL Expected [{$i}] : " . $expect[$i] . "\n"; echo "SQL Actual [{$i}] : " . $sql[$i] . "\n"; } $this->assertTrue($r_ = $sql[$i] === $expect[$i]); if ($r_ == false) { $r = false; } } } echo "done\n"; // return whether result matches expected return $r; }
/** * Parse the EZOQL statement and translate it into equivalent * SQL statement * * @param string $q (the EZOQL query string) * @param array $args arguments for the query * @return false|string * @throws epExceptionQuery, epQueryExceptionBuilder */ public function parse($oql_stmt, $args = array()) { // reset aggregation function $this->aggr_func = false; // get oql statement (query) if ($oql_stmt) { $this->oq_stmt = $oql_stmt; } // check if query empty if (!$this->oq_stmt) { throw new epExceptionQuery('Empty EZOQL query'); return false; } // the root to the parsed syntax tree for oql stmt $root = null; // check if any cached parsed syntax tree if (!isset($this->parsed[$oql_stmt])) { // instantiate a query parser if not already if (!$this->p) { if (!($this->p = new epQueryParser())) { return false; } } // parse query and get syntax tree $this->parsed[$oql_stmt] = $this->p->parse($oql_stmt); } // get syntax tree from cache $root = $this->parsed[$oql_stmt]; // check if there is any errors if (!$root || ($errors = $this->p->errors())) { $emsg = 'EZOQL parsing error'; if (isset($errors) && $errors) { $emsg .= ":\n"; foreach ($errors as $error) { $emsg .= $error->__toString() . "\n"; } } else { $emsg .= " (unknown)"; } throw new epExceptionQuery($emsg); return false; } // build the SQL query if (!$this->b) { // instantiate builder if not yet if (!($this->b = new epQueryBuilder($root, $this->oq_stmt, $args))) { // should not happen return false; } } else { // initialize builder $this->b->initialize($root, $this->oq_stmt, $args); } // build the SQL query from syntax tree return $this->b->build(); }
/** * Parse the EZOQL statement and translate it into equivalent * SQL statement * * @param string $q (the EZOQL query string) * @param array $args arguments for the query * @return false|string * @throws epExceptionQuery, epQueryExceptionBuilder */ public function parse($oql_stmt, $args = array()) { // reset aggregation function $this->aggr_func = false; // get oql statement (query) if ($oql_stmt) { $this->oq_stmt = $oql_stmt; } // check if query empty if (!$this->oq_stmt) { throw new epExceptionQuery('Empty EZOQL query'); return false; } // get oql statement (query) if ($args) { $this->args = $args; } // instantiate a query parser if (!($p = new epQueryParser($oql_stmt))) { return false; } // parse query and get syntax tree $root = $p->parse(); // check if there is any errors if (!$root || ($errors = $p->errors())) { $emsg = 'EZOQL parsing error'; if ($errors) { $emsg .= ":\n"; foreach ($errors as $error) { $emsg .= $error->__toString() . "\n"; } } else { $emsg .= " (unknown)"; } throw new epExceptionQuery($emsg); return false; } // instantiate builder to build the SQL query if (!($b = new epQueryBuilder($root, $this->oq_stmt, $this->args))) { return false; } // build the SQL query from syntax tree $this->sql_stmt = $b->build(); // get the aggregate function in the query $this->aggr_func = $b->getAggregateFunction(); // get the limit $this->limit = $b->getLimit(); // get the order $this->orderby = $b->getOrderBy(); // get the root classes of this query $this->root_cms =& $b->getRootClassMaps(); return $this->sql_stmt; }