* @return $this * * @since 1.0 * @throws QueryTypeAlreadyDefinedException if the query type has already been defined */ public function select($columns) { if ($this->type !== null && $this->type !== '' && $this->type !== 'select') { throw new QueryTypeAlreadyDefinedException( \sprintf( 'Cannot set the query type to "select" as the query type is already set to "%s".' . ' You should either call the `clear()` method to reset the type or create a new query object.', $this->type ) ); } $this->type = 'select'; if ($this->select === null) { $this->select = new Query\QueryElement('SELECT', $columns); } else { $this->select->append($columns); } return $this; } /** * Add a single condition string, or an array of strings to the SET clause of the query. * * Usage: * $query->set('a = 1')->set('b = 2'); * $query->set(array('a = 1', 'b = 2'); * * @param array|string $conditions A string or array of string conditions. * @param string $glue The glue by which to join the condition strings. Defaults to ,. * Note that the glue is set on first use and cannot be changed. * * @return $this * * @since 1.0 */ public function set($conditions, $glue = ',') { if ($this->set === null) { $glue = strtoupper($glue); $this->set = new Query\QueryElement('SET', $conditions, \PHP_EOL . "\t$glue "); } else { $this->set->append($conditions); } return $this; } /** * Sets the offset and limit for the result set, if the database driver supports it. * * Usage: * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record) * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record) * * @param integer $limit The limit for the result set * @param integer $offset The offset for the result set * * @return $this * * @since 2.0.0 */ public function setLimit($limit = 0, $offset = 0) { $this->limit = (int) $limit; $this->offset = (int) $offset; return $this; } /** * Allows a direct query to be provided to the database driver's setQuery() method, but still allow queries * to have bounded variables. * * Usage: * $query->setQuery('select * from #__users'); * * @param DatabaseQuery|string $sql A SQL query string or DatabaseQuery object * * @return $this * * @since 1.0 */ public function setQuery($sql) { $this->sql = $sql; return $this; } /** * Add a table name to the UPDATE clause of the query. * * Usage: * $query->update('#__foo')->set(...); * * @param string $table A table to update. * * @return $this * * @since 1.0 * @throws QueryTypeAlreadyDefinedException if the query type has already been defined */ public function update($table) { if ($this->type !== null && $this->type !== '' && $this->type !== 'update') { throw new QueryTypeAlreadyDefinedException( \sprintf( 'Cannot set the query type to "update" as the query type is already set to "%s".' . ' You should either call the `clear()` method to reset the type or create a new query object.', $this->type ) ); } $this->type = 'update'; $this->update = new Query\QueryElement('UPDATE', $table); return $this; } /** * Adds a tuple, or array of tuples that would be used as values for an INSERT INTO statement. * * Usage: * $query->values('1,2,3')->values('4,5,6'); * $query->values(array('1,2,3', '4,5,6')); * * @param array|string $values A single tuple, or array of tuples. * * @return $this * * @since 1.0 */ public function values($values) { if ($this->values === null) { $this->values = new Query\QueryElement('()', $values, '),('); } else { $this->values->append($values); } return $this; } /** * Add a single condition, or an array of conditions to the WHERE clause of the query. * * Usage: * $query->where('a = 1')->where('b = 2'); * $query->where(array('a = 1', 'b = 2')); * * @param array|string $conditions A string or array of where conditions. * @param string $glue The glue by which to join the conditions. Defaults to AND. * Note that the glue is set on first use and cannot be changed. * * @return $this * * @since 1.0 */ public function where($conditions, $glue = 'AND') { if ($this->where === null) { $glue = strtoupper($glue); $this->where = new Query\QueryElement('WHERE', $conditions, " $glue "); } else { $this->where->append($conditions); } return $this; } /** * Add a WHERE IN statement to the query. * * Note that all values must be the same data type. * * Usage * $query->whereIn('id', [1, 2, 3]); * * @param string $keyName Key name for the where clause * @param array $keyValues Array of values to be matched * @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it * has to be same length of $keyValues * * @return $this * * @since 2.0.0 */ public function whereIn(string $keyName, array $keyValues, $dataType = ParameterType::INTEGER) { return $this->where( $keyName . ' IN (' . implode(',', $this->bindArray($keyValues, $dataType)) . ')' ); } /** * Add a WHERE NOT IN statement to the query. * * Note that all values must be the same data type. * * Usage * $query->whereNotIn('id', [1, 2, 3]); * * @param string $keyName Key name for the where clause * @param array $keyValues Array of values to be matched * @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it * has to be same length of $keyValues * * @return $this * * @since 2.0.0 */ public function whereNotIn(string $keyName, array $keyValues, $dataType = ParameterType::INTEGER) { return $this->where( $keyName . ' NOT IN (' . implode(',', $this->bindArray($keyValues, $dataType)) . ')' ); } /** * Extend the WHERE clause with a single condition or an array of conditions, with a potentially * different logical operator from the one in the current WHERE clause. * * Usage: * $query->where(array('a = 1', 'b = 2'))->extendWhere('XOR', array('c = 3', 'd = 4')); * will produce: WHERE ((a = 1 AND b = 2) XOR (c = 3 AND d = 4) * * @param string $outerGlue The glue by which to join the conditions to the current WHERE conditions. * @param mixed $conditions A string or array of WHERE conditions. * @param string $innerGlue The glue by which to join the conditions. Defaults to AND. * * @return $this * * @since 1.3.0 */ public function extendWhere($outerGlue, $conditions, $innerGlue = 'AND') { // Replace the current WHERE with a new one which has the old one as an unnamed child. $this->where = new Query\QueryElement('WHERE', $this->where->setName('()'), " $outerGlue "); // Append the new conditions as a new unnamed child. $this->where->append(new Query\QueryElement('()', $conditions, " $innerGlue ")); return $this; } /** * Extend the WHERE clause with an OR and a single condition or an array of conditions. * * Usage: * $query->where(array('a = 1', 'b = 2'))->orWhere(array('c = 3', 'd = 4')); * will produce: WHERE ((a = 1 AND b = 2) OR (c = 3 AND d = 4) * * @param mixed $conditions A string or array of WHERE conditions. * @param string $glue The glue by which to join the conditions. Defaults to AND. * * @return $this * * @since 1.3.0 */ public function orWhere($conditions, $glue = 'AND') { return $this->extendWhere('OR', $conditions, $glue); } /** * Extend the WHERE clause with an AND and a single condition or an array of conditions. * * Usage: * $query->where(array('a = 1', 'b = 2'))->andWhere(array('c = 3', 'd = 4')); * will produce: WHERE ((a = 1 AND b = 2) AND (c = 3 OR d = 4) * * @param mixed $conditions A string or array of WHERE conditions. * @param string $glue The glue by which to join the conditions. Defaults to OR. * * @return $this * * @since 1.3.0 */ public function andWhere($conditions, $glue = 'OR') { return $this->extendWhere('AND', $conditions, $glue); } /** * Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. * * @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of * the form ':key', but can also be an integer. * @param mixed $value The value that will be bound. It can be an array, in this case it has to be * same length of $key; The value is passed by reference to support output * parameters such as those possible with stored procedures. * @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it * has to be same length of $key * @param integer $length The length of the variable. Usually required for OUTPUT parameters. * @param array $driverOptions Optional driver options to be used. * * @return $this * * @since 1.5.0 * @throws \InvalidArgumentException */ public function bind($key, &$value, $dataType = ParameterType::STRING, $length = 0, $driverOptions = []) { if (!$key) { throw new \InvalidArgumentException('A key is required'); } $key = (array) $key; $count = \count($key); if (\is_array($value)) { if ($count != \count($value)) { throw new \InvalidArgumentException('Array length of $key and $value are not equal'); } reset($value); } if (\is_array($dataType) && $count != \count($dataType)) { throw new \InvalidArgumentException('Array length of $key and $dataType are not equal'); } foreach ($key as $index) { if (\is_array($value)) { $localValue = &$value[key($value)]; next($value); } else { $localValue = &$value; } if (\is_array($dataType)) { $localDataType = array_shift($dataType); } else { $localDataType = $dataType; } // Validate parameter type if (!isset($this->parameterMapping[$localDataType])) { throw new \InvalidArgumentException(sprintf('Unsupported parameter type `%s`', $localDataType)); } $obj = new \stdClass(); $obj->value = &$localValue; $obj->dataType = $this->parameterMapping[$localDataType]; $obj->length = $length; $obj->driverOptions = $driverOptions; // Add the Key/Value into the bounded array $this->bounded[$index] = $obj; unset($localValue); } return $this; } /** * Method to unbind a bound variable. * * @param array|string|integer $key The key or array of keys to unbind. * * @return $this * * @since 2.0.0 */ public function unbind($key) { if (\is_array($key)) { foreach ($key as $k) { unset($this->bounded[$k]); } } else { unset($this->bounded[$key]); } return $this; } /** * Binds an array of values and returns an array of prepared parameter names. * * Note that all values must be the same data type. * * Usage: * $query->where('column in (' . implode(',', $query->bindArray($keyValues, $dataType)) . ')'); * * @param array $values Values to bind * @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it * has to be same length of $key * * @return array An array with parameter names * * @since 2.0.0 */ public function bindArray(array $values, $dataType = ParameterType::INTEGER) { $parameterNames = []; for ($i = 0; $i < count($values); $i++) { $parameterNames[] = ':preparedArray' . (++$this->preparedIndex); } $this->bind($parameterNames, $values, $dataType); return $parameterNames; } /** * Method to provide basic copy support. * * Any object pushed into the data of this class should have its own __clone() implementation. * This method does not support copying objects in a multidimensional array. * * @return void * * @since 1.0 */ public function __clone() { foreach ($this as $k => $v) { if ($k === 'db') { continue; } if (\is_object($v)) { $this->{$k} = clone $v; } elseif (\is_array($v)) { foreach ($v as $i => $element) { if (\is_object($element)) { $this->{$k}[$i] = clone $element; } } } } } /** * Retrieves the bound parameters array when key is null and returns it by reference. If a key is provided then that item is * returned. * * @param mixed $key The bounded variable key to retrieve. * * @return mixed * * @since 1.5.0 */ public function &getBounded($key = null) { if (empty($key)) { return $this->bounded; } if (isset($this->bounded[$key])) { return $this->bounded[$key]; } } /** * Combine a select statement to the current query by one of the set operators. * Operators: UNION, UNION ALL, EXCEPT or INTERSECT. * * @param string $name The name of the set operator with parentheses. * @param DatabaseQuery|string $query The DatabaseQuery object or string. * * @return $this * * @since 2.0.0 */ protected function merge($name, $query) { $this->type = $this->type ?: 'select'; $this->merge[] = new Query\QueryElement($name, $query); return $this; } /** * Add a query to UNION with the current query. * * Usage: * $query->union('SELECT name FROM #__foo') * $query->union('SELECT name FROM #__foo', true) * * @param DatabaseQuery|string $query The DatabaseQuery object or string to union. * @param boolean $distinct True to only return distinct rows from the union. * * @return $this * * @since 1.0 */ public function union($query, $distinct = true) { // Set up the name with parentheses, the DISTINCT flag is redundant return $this->merge($distinct ? 'UNION ()' : 'UNION ALL ()', $query); } /** * Add a query to UNION ALL with the current query. * * Usage: * $query->unionAll('SELECT name FROM #__foo') * * @param DatabaseQuery|string $query The DatabaseQuery object or string to union. * * @return $this * * @see union * @since 1.5.0 */ public function unionAll($query) { return $this->union($query, false); } /** * Set a single query to the query set. * On this type of DatabaseQuery you can use union(), unionAll(), order() and setLimit() * * Usage: * $query->querySet($query2->select('name')->from('#__foo')->order('id DESC')->setLimit(1)) * ->unionAll($query3->select('name')->from('#__foo')->order('id')->setLimit(1)) * ->order('name') * ->setLimit(1) * * @param DatabaseQuery $query The DatabaseQuery object or string. * * @return $this * * @since 2.0.0 */ public function querySet($query) { $this->type = 'querySet'; $this->querySet = $query; return $this; } /** * Create a DatabaseQuery object of type querySet from current query. * * Usage: * $query->select('name')->from('#__foo')->order('id DESC')->setLimit(1) * ->toQuerySet() * ->unionAll($query2->select('name')->from('#__foo')->order('id')->setLimit(1)) * ->order('name') * ->setLimit(1) * * @return DatabaseQuery A new object of the DatabaseQuery. * * @since 2.0.0 */ public function toQuerySet() { return (new static($this->db))->querySet($this); } /** * Find and replace sprintf-like tokens in a format string. * Each token takes one of the following forms: * %% - A literal percent character. * %[t] - Where [t] is a type specifier. * %[n]$[x] - Where [n] is an argument specifier and [t] is a type specifier. * * Types: * a - Numeric: Replacement text is coerced to a numeric type but not quoted or escaped. * e - Escape: Replacement text is passed to $this->escape(). * E - Escape (extra): Replacement text is passed to $this->escape() with true as the second argument. * n - Name Quote: Replacement text is passed to $this->quoteName(). * q - Quote: Replacement text is passed to $this->quote(). * Q - Quote (no escape): Replacement text is passed to $this->quote() with false as the second argument. * r - Raw: Replacement text is used as-is. (Be careful) * * Date Types: * - Replacement text automatically quoted (use uppercase for Name Quote). * - Replacement text should be a string in date format or name of a date column. * y/Y - Year * m/M - Month * d/D - Day * h/H - Hour * i/I - Minute * s/S - Second * * Invariable Types: * - Takes no argument. * - Argument index not incremented. * t - Replacement text is the result of $this->currentTimestamp(). * z - Replacement text is the result of $this->nullDate(false). * Z - Replacement text is the result of $this->nullDate(true). * * Usage: * $query->format('SELECT %1$n FROM %2$n WHERE %3$n = %4$a', 'foo', '#__foo', 'bar', 1); * Returns: SELECT `foo` FROM `#__foo` WHERE `bar` = 1 * * Notes: * The argument specifier is optional but recommended for clarity. * The argument index used for unspecified tokens is incremented only when used. * * @param string $format The formatting string. * * @return string Returns a string produced according to the formatting string. * * @since 1.0 */ public function format($format) { $query = $this; $args = \array_slice(\func_get_args(), 1); array_unshift($args, null); $i = 1; $func = function ($match) use ($query, $args, &$i) { if (isset($match[6]) && $match[6] === '%') { return '%'; } // No argument required, do not increment the argument index. switch ($match[5]) { case 't': return $query->currentTimestamp(); case 'z': return $query->nullDate(false); case 'Z': return $query->nullDate(true); } // Increment the argument index only if argument specifier not provided. $index = is_numeric($match[4]) ? (int) $match[4] : $i++; if (!$index || !isset($args[$index])) { // TODO - What to do? sprintf() throws a Warning in these cases. $replacement = ''; } else { $replacement = $args[$index]; } switch ($match[5]) { case 'a': return 0 + $replacement; case 'e': return $query->escape($replacement); case 'E': return $query->escape($replacement, true); case 'n': return $query->quoteName($replacement); case 'q': return $query->quote($replacement); case 'Q': return $query->quote($replacement, false); case 'r': return $replacement; // Dates case 'y': return $query->year($query->quote($replacement)); case 'Y': return $query->year($query->quoteName($replacement)); case 'm': return $query->month($query->quote($replacement)); case 'M': return $query->month($query->quoteName($replacement)); case 'd': return $query->day($query->quote($replacement)); case 'D': return $query->day($query->quoteName($replacement)); case 'h': return $query->hour($query->quote($replacement)); case 'H': return $query->hour($query->quoteName($replacement)); case 'i': return $query->minute($query->quote($replacement)); case 'I': return $query->minute($query->quoteName($replacement)); case 's': return $query->second($query->quote($replacement)); case 'S': return $query->second($query->quoteName($replacement)); } return ''; }; /** * Regexp to find an replace all tokens. * Matched fields: * 0: Full token * 1: Everything following '%' * 2: Everything following '%' unless '%' * 3: Argument specifier and '$' * 4: Argument specifier * 5: Type specifier * 6: '%' if full token is '%%' */ return preg_replace_callback('#%(((([\d]+)\$)?([aeEnqQryYmMdDhHiIsStzZ]))|(%))#', $func, $format); } /** * Validate arguments which are passed to selectRowNumber method and set up common variables. * * @param string $orderBy An expression of ordering for window function. * @param string $orderColumnAlias An alias for new ordering column. * * @return void * * @since 2.0.0 * @throws \RuntimeException */ protected function validateRowNumber($orderBy, $orderColumnAlias) { if ($this->selectRowNumber) { throw new \RuntimeException("Method 'selectRowNumber' can be called only once per instance."); } $this->type = 'select'; $this->selectRowNumber = [ 'orderBy' => $orderBy, 'orderColumnAlias' => $orderColumnAlias, ]; } /** * Return the number of the current row. * * Usage: * $query->select('id'); * $query->selectRowNumber('ordering,publish_up DESC', 'new_ordering'); * $query->from('#__content'); * * @param string $orderBy An expression of ordering for window function. * @param string $orderColumnAlias An alias for new ordering column. * * @return $this * * @since 2.0.0 * @throws \RuntimeException */ public function selectRowNumber($orderBy, $orderColumnAlias) { $this->validateRowNumber($orderBy, $orderColumnAlias); return $this->select("ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias"); } } An Error Occurred: Whoops, looks like something went wrong.

Sorry, there was a problem we could not recover from.

The server returned a "500 - Whoops, looks like something went wrong."

Help me resolve this