/**
  * Method to get the next ordering value for a group of rows defined by an SQL WHERE clause.
  * This is useful for placing a new item last in a group of items in the table.
  *
  * @param   string  $where  WHERE clause to use for selecting the MAX(ordering) for the table.
  *
  * @return  mixed  Boolean false an failure or the next ordering value as an integer.
  */
 public function getNextOrder($where = '')
 {
     // If there is no ordering field set an error and return false.
     $ordering = $this->getColumnAlias('ordering');
     if (!in_array($ordering, $this->getKnownFields())) {
         throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this)));
     }
     // Get the largest ordering value for a given where clause.
     $query = $this->_db->getQuery(true);
     $query->select('MAX(' . $this->_db->qn($ordering) . ')');
     $query->from($this->_tbl);
     if ($where) {
         $query->where($where);
     }
     $this->_db->setQuery($query);
     $max = (int) $this->_db->loadResult();
     // Return the largest ordering value + 1.
     return $max + 1;
 }
 /**
  * Sets the SQL statement string for later execution.
  *
  * @param   mixed    $query          The SQL statement to set either as a FOFDatabaseQuery object or a string.
  * @param   integer  $offset         The affected row offset to set.
  * @param   integer  $limit          The maximum affected rows to set.
  * @param   array    $driverOptions  The optional PDO driver options.
  *
  * @return  FOFDatabaseDriver  This object to support method chaining.
  *
  * @since   12.1
  */
 public function setQuery($query, $offset = null, $limit = null, $driverOptions = array())
 {
     $this->connect();
     $this->freeResult();
     if (is_string($query)) {
         // Allows taking advantage of bound variables in a direct query:
         $query = $this->getQuery(true)->setQuery($query);
     }
     if ($query instanceof FOFDatabaseQueryLimitable && !is_null($offset) && !is_null($limit)) {
         $query = $query->processLimit($query, $limit, $offset);
     }
     // Create a stringified version of the query (with prefixes replaced):
     $sql = $this->replacePrefix((string) $query);
     // Use the stringified version in the prepare call:
     $this->prepared = $this->connection->prepare($sql, $driverOptions);
     // Store reference to the original FOFDatabaseQuery instance within the class.
     // This is important since binding variables depends on it within execute():
     parent::setQuery($query, $offset, $limit);
     return $this;
 }