/** * 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; }