= &$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"); } }
The server returned a "500 - Whoops, looks like something went wrong."