/** * This method is called by 'loadData' and creates the necessary * SQL Statement for data loading. */ private function load($id, $class, $type, $order, $limit) { // create a NamedQuery, but don't set the query itself $namedQuery = new NamedQuery(); // use some booleans to handle the use of WHERE and AND clauses $hasConditions = $id != null ? true : false; $hasConditions = $class != null ? true : $hasConditions; $hasConditions = $type != null ? true : $hasConditions; $isAndNeccessary = false; // start with the default query $query = $this->QUERY_LOAD_PRODUCTS; // if there are conditions to add, handle them inside this block if ($hasConditions) { $query .= $this->CLAUSE_WHERE; // add the 'id' parameter if valued if ($id != null) { $query .= $this->COND_ID; $namedQuery->addParam(QueryParam::TYPE_INTEGER, $id); // WHERE clause is used, so mark that next statement has to add AND clause $isAndNeccessary = true; } // add the 'classification' parameter if valued if ($class != null) { if ($isAndNeccessary) { $query .= $this->CLAUSE_AND; } $query .= $this->COND_CLASSIFICATION; $namedQuery->addParam(QueryParam::TYPE_STRING, $class); // previously added AND is used, so mark that next statement has to add AND again $isAndNeccessary = true; } // add the 'type' parameter if valued if ($type != null) { if ($isAndNeccessary) { $query .= $this->CLAUSE_AND; } $query .= $this->COND_TYPE; $namedQuery->addParam(QueryParam::TYPE_STRING, $type); } } // add the order type to the query $query .= $this->COND_ORDER_BY . $order; // if a limit is given, add the limit suffix and the query param if ($limit > 0) { $query .= $this->SUFFIX_LIMIT; $namedQuery->addParam(QueryParam::TYPE_INTEGER, $limit); } // at this point, the query is finished and can be set into the NamedQuery $namedQuery->setNamedQuery($query); // now, we can call the CRUDService with the finished NamedQuery return CRUDService::getInstance()->fetchNamedQuery($namedQuery, "Product"); }