/** * Renders the One_Query into an xpath that can fetch results from the XML-datastore * * @param One_Query $query */ public function render(One_Query $query) { $this->query = $query; $this->scheme = $this->query; $resources = $this->scheme->getResources(); if (trim($query->getRaw()) != '') { return $query->getRaw(); } $xpath = $resources['item']; // add possible filters to the query if (isset($resources['filter'])) { $filters = explode(';', $resources['filter']); if (count($filters) > 0) { foreach ($filters as $filterName) { if ($filterName != '') { $filter = One_Repository::getFilter($filterName, $query->name()); $filter->affect($query); } } } } $whereClauses = $query->getWhereClauses(); if (!is_null($whereClauses)) { $clauses = $this->whereClauses($whereClauses); if (!is_null($clauses)) { $xpath .= '[' . $clauses . ']'; } } return $xpath; }
/** * Render the query * * @param One_Query $query * @return string */ public function render(One_Query $query, $overrideFilters = false) { $this->query = $query; $this->scheme = $this->query->getScheme(); // if the person wants to perform a raw query, return the raw query if (!is_null($query->getRaw())) { if (One_Config::get('debug.query')) { echo '<pre>'; var_dump($query->getRaw()); echo '</pre>'; } return $query->getRaw(); } $this->query = $query; $this->scheme = $this->query->getScheme(); $resources = $this->scheme->getResources(); // fetch collection to fetch data from $this->_collection = $resources['collection']; // add possible filters to the query if (!$overrideFilters && isset($resources['filter'])) { $filters = explode(';', $resources['filter']); if (count($filters) > 0) { foreach ($filters as $filterName) { if ($filterName != '') { $filter = One_Repository::getFilter($filterName, $query->getScheme()->getName()); $filter->affect($query); } } } } $findQuery = array('fields' => array(), 'query' => array()); if (count($query->getSelect()) > 0) { $findQuery['fields'] = $this->createSelects($query->getSelect()); } // get where clauses $whereClauses = $query->getWhereClauses(); $where = NULL; if (!is_null($whereClauses)) { $where = $this->whereClauses($whereClauses); } if (!is_null($where)) { $findQuery['query'] = $where; } if (One_Config::get('debug.query')) { echo '<pre>'; var_dump($findQuery); echo '</pre>'; } $findQuery = json_encode($findQuery); return $findQuery; }
/** * Render the query * * @param One_Query $query * @return string */ public function render(One_Query $query, $overrideFilters = false) { $this->query = $query; $this->scheme = $this->query->getScheme(); // if the person wants to perform a raw query, return the raw query if (!is_null($query->getRaw())) { if (One_Config::get('debug.query')) { echo '<pre>'; var_dump($query->getRaw()); echo '</pre>'; } return $query->getRaw(); } $this->query = $query; $this->scheme = $this->query->getScheme(); $resources = $this->scheme->getResources(); // fetch main table to fetch data from $this->mainTable = $resources['table']; $this->defineRole('$self$'); // add possible filters to the query if (!$overrideFilters && isset($resources['filter'])) { $filters = explode(';', $resources['filter']); if (count($filters) > 0) { foreach ($filters as $filterName) { if ($filterName != '') { $filter = One_Repository::getFilter($filterName, $query->getScheme()->getName()); $filter->affect($query); } } } } // TR20100531 No longer needs to be run after the rest since joins are now checked while adding // sselects, order, ... $joins = NULL; $qJoins = $query->getJoins(); if (count($qJoins) > 0) { foreach ($qJoins as $join => $type) { $this->defineRole($join); $query->setRoleAlias($join, $this->aliases[$join]); $joins .= $this->createJoin($query->getRole($join), $type); } } $selects = $this->aliases['$self$'] . '.*'; if (count($query->getSelect()) > 0) { $selects = $this->createSelects($query->getSelect()); } // get where clauses $whereClauses = $query->getWhereClauses(); $where = NULL; if (!is_null($whereClauses)) { $where = $this->whereClauses($whereClauses); } // get having clauses $havingClauses = $query->getHavingClauses(); $having = NULL; if (!is_null($havingClauses)) { $having = $this->whereClauses($havingClauses); } // get order $order = $this->createOrder(); //get grouping $group = $this->createGroup(); // get limit $limit = $this->createLimit(); $sql = 'SELECT ' . $selects . ' FROM ' . $this->mainTable . ' ' . $this->aliases['$self$']; if (!is_null($joins)) { $sql .= $joins; } if (!is_null($where)) { $sql .= ' WHERE ' . $where; } if (!is_null($group)) { $sql .= ' GROUP BY ' . $group; } if (!is_null($having)) { $sql .= ' HAVING ' . $having; } if (!is_null($order)) { $sql .= ' ORDER BY ' . $order; } if (!is_null($limit)) { /* Use the following format to replace MySQL LIMIT for PL/SQL : SELECT * FROM ( SELECT rownum rnum, a.* FROM( SELECT fieldA,fieldB FROM table ORDER BY fieldA ) a WHERE rownum <= START + LIMIT ) WHERE rnum >= START ** or ** SELECT rownum rnum, a.* FROM( SELECT fieldA,fieldB FROM table ORDER BY fieldA ) a WHERE rownum <= LIMIT */ $qLimit = $this->query->getLimit(); if (isset($qLimit['start']) && intval($qLimit['start']) > -1) { $start = intval($qLimit['start']); } else { $start = 0; } if (isset($qLimit['limit']) && intval($qLimit['limit']) > 0) { $limit = intval($qLimit['limit']); } else { $limit = 50; } // @TODO: clean this up // create alias for rownum field $rnfield = $this->createAlias(); $subsel = $this->createAlias(); $sql = "SELECT rownum {$rnfield}, {$subsel}.* FROM ( {$sql} ) {$subsel} WHERE rownum <= {$limit}"; if ($start) { $sql = "SELECT * FROM ( {$sql} ) WHERE {$rnfield} > {$start}"; } } if (One_Config::get('debug.query')) { echo '<pre>'; var_dump($sql); echo '</pre>'; } return $sql; }
/** * Render the query * * @param One_Query $query * @return string */ public function render(One_Query $query, $overrideFilters = false) { $this->query = $query; $this->scheme = $this->query->getScheme(); // if the person wants to perform a raw query, return the raw query if (!is_null($query->getRaw())) { if (One_Config::get('debug.query')) { echo '<pre>'; var_dump($query->getRaw()); echo '</pre>'; } return $query->getRaw(); } $this->query = $query; $this->scheme = $this->query->getScheme(); $resources = $this->scheme->getResources(); // fetch main table to fetch data from $this->mainTable = $resources['table']; $this->defineRole('$self$'); // add possible filters to the query if (!$overrideFilters && isset($resources['filter'])) { $filters = explode(';', $resources['filter']); if (count($filters) > 0) { foreach ($filters as $filterName) { if ($filterName != '') { $filter = One_Repository::getFilter($filterName, $query->getScheme()->getName()); $filter->affect($query); } } } } // TR20100531 No longer needs to be run after the rest since joins are now checked while adding // sselects, order, ... $joins = NULL; $qJoins = $query->getJoins(); if (count($qJoins) > 0) { foreach ($qJoins as $join => $type) { $this->defineRole($join); $query->setRoleAlias($join, $this->aliases[$join]); $joins .= $this->createJoin($query->getRole($join), $type); } } // *** TODO: change the '*' to only the relavant fields defined in the scheme $selects = $this->aliases['$self$'] . '.*'; if (count($query->getSelect()) > 0) { $selects = $this->createSelects($query->getSelect()); } // get where clauses $whereClauses = $query->getWhereClauses(); $where = NULL; if (!is_null($whereClauses)) { $where = $this->whereClauses($whereClauses); } // get having clauses $havingClauses = $query->getHavingClauses(); $having = NULL; if (!is_null($havingClauses)) { $having = $this->whereClauses($havingClauses); } // get order $order = $this->createOrder(); //get grouping $group = $this->createGroup(); // get limit $limit = $this->createLimit(); $sql = 'SELECT ' . $selects . ' FROM `' . $this->mainTable . '` ' . $this->aliases['$self$']; if (!is_null($joins)) { $sql .= $joins; } if (!is_null($where)) { $sql .= ' WHERE ' . $where; } if (!is_null($group)) { $sql .= ' GROUP BY ' . $group; } if (!is_null($having)) { $sql .= ' HAVING ' . $having; } if (!is_null($order)) { $sql .= ' ORDER BY ' . $order; } if (!is_null($limit)) { $sql .= ' LIMIT ' . $limit; } if (One_Config::get('debug.query')) { echo '<pre>'; var_dump($sql); echo '</pre>'; } return $sql; }