/** * Tests the JDatabaseQuery::format method. * * @return void * * @since 12.3 */ public function testFormat() { $result = $this->_instance->format('SELECT %n FROM %n WHERE %n = %a', 'foo', '#__bar', 'id', 10); $expected = 'SELECT ' . $this->_instance->qn('foo') . ' FROM ' . $this->_instance->qn('#__bar') . ' WHERE ' . $this->_instance->qn('id') . ' = 10'; $this->assertThat($result, $this->equalTo($expected), 'Line: ' . __LINE__ . '.'); $result = $this->_instance->format('SELECT %n FROM %n WHERE %n = %t OR %3$n = %Z', 'id', '#__foo', 'date'); $expected = 'SELECT ' . $this->_instance->qn('id') . ' FROM ' . $this->_instance->qn('#__foo') . ' WHERE ' . $this->_instance->qn('date') . ' = ' . $this->_instance->currentTimestamp() . ' OR ' . $this->_instance->qn('date') . ' = ' . $this->_instance->nullDate(true); $this->assertThat($result, $this->equalTo($expected), 'Line: ' . __LINE__ . '.'); }
/** * Process the query filters. * * @param JDatabaseQuery $query The query object. * @param array $filters The filters values. * * @return JDatabaseQuery The db query object. */ protected function processFilters(\JDatabaseQuery $query, $filters = array()) { $user = $this->container->get('user'); $db = $this->container->get('db'); $date = $this->container->get('date'); // If no state filter, set published >= 0 if (!isset($filters['user.state']) && property_exists($this->getTable(), 'state')) { $query->where($query->quoteName('user.state') . ' >= 0'); } // Category // ===================================================================================== $category = $this->getCategory(); if ($category->id != 1 && in_array('category.lft', $this->filterFields) && in_array('category.rgt', $this->filterFields)) { $query->where($query->format('(%n >= %a AND %n <= %a)', 'category.lft', $category->lft, 'category.rgt', $category->rgt)); } // Max Level // ===================================================================================== $maxLevel = $this->state->get('filter.max_category_levels', -1); if ($maxLevel > 0) { $query->where($query->quoteName('category.level') . " <= " . $maxLevel); } // Edit Access // ===================================================================================== if ($this->state->get('filter.unpublished')) { $query->where('user.state >= 0'); } else { $query->where('user.state > 0'); $nullDate = $query->Quote($db->getNullDate()); $nowDate = $query->Quote($date->toSQL(true)); if (in_array('user.publish_up', $this->filterFields) && in_array('user.publish_down', $this->filterFields)) { $query->where('(user.publish_up = ' . $nullDate . ' OR user.publish_up <= ' . $nowDate . ')'); $query->where('(user.publish_down = ' . $nullDate . ' OR user.publish_down >= ' . $nowDate . ')'); } } // View Level // ===================================================================================== if ($access = $this->state->get('filter.access') && in_array('user.access', $this->filterFields)) { $query->where(new InCompare('user.access', $user->getAuthorisedViewLevels())); } // Language // ===================================================================================== if ($this->state->get('filter.language') && in_array('a.language', $this->filterFields)) { $lang_code = $db->quote(JFactory::getLanguage()->getTag()); $query->where("a.language IN ('{$lang_code}', '*')"); } return parent::processFilters($query, $filters); }
/** * Build conditions into query object. * * @param Query $query The query object to add where conditions. * @param array $conditions The where conditions array to add to query object. * * @return Query Return the query object. */ public static function buildWheres(Query $query, array $conditions) { foreach ($conditions as $key => $value) { // NULL if ($value === null) { $query->where($query->format('%n = NULL', $key)); } elseif ($value instanceof Compare) { $query->where((string) static::buildCompare($key, $value, $query)); } elseif (is_numeric($key)) { $query->where($value); } elseif (is_array($value) || is_object($value)) { $value = array_map(array($query, 'quote'), (array) $value); $query->where($query->quoteName($key) . new QueryElement('IN ()', $value, ',')); } else { $query->where($query->format('%n = %q', $key, $value)); } } return $query; }
/** * The post getQuery object. * * @param JDatabaseQuery $query The db query object. * * @return void */ protected function postGetQuery(\JDatabaseQuery $query) { $keys = $this->state->get('profileKeys'); // Build SQL Pivot // ======================================================================== foreach ($keys as $key) { if ($key) { /* * Use MySQL Pivot query: * MAX(IF(profile.key = 'foo', profile.value, NULL)) AS foo */ $query->select($query->format("MAX(IF(profile.key = %q, profile.value, NULL)) AS %e", $key, $key)); } } $query->group('user.id'); }
/** * The post getQuery object. * * @param JDatabaseQuery $query The db query object. * * @return void */ protected function postGetQuery(\JDatabaseQuery $query) { $query->where($query->format('%n = %q', 'type', 'plugin')); }