?
avatar waader
waader
13 Dec 2015

getContacts() in administrator/components/com_languages/helpers/multilangstatus, which is used to get information displayed in the Multilingual Status in the backend, contains a query that is not very sql standard compliant.

$query = $db->getQuery(true)
->select('u.name, count(cd.language) as counted, MAX(cd.language=' . $db->quote('') . ') as all_languages')
->from('#__users AS u')
->join('LEFT', '#__contact_details AS cd ON cd.user_id=u.id')
->join('LEFT', '#__languages as l on cd.language=l.lang_code')
->where('EXISTS (SELECT * from #__content as c where c.created_by=u.id)')
->where('(l.published=1 or cd.language=' . $db->quote('
') . ')')
->where('cd.published=1')
->group('u.id')
->having('(counted !=' . count(JLanguageHelper::getLanguages()) . ' OR all_languages=1)')
->having('(counted !=1 OR all_languages=0)');

There are serveral problems. The group and having clause can be easily changed but the argument of the MAX function is a boolean which is a problem eg. for postgres. So a CAST is the obvious remedy but as it appears mysql needs a un/signed Integer cast in contrast to postgres that only needs Integer.

$query = $db->getQuery(true)
    ->select('u.name, count(cd.language) as counted, MAX(CAST(cd.language=' . $db->quote('*') . ' AS UNSIGNED INTEGER)) as all_languages')
    ->from('#__users AS u')
    ->join('LEFT', '#__contact_details AS cd ON cd.user_id=u.id')
    ->join('LEFT', '#__languages as l on cd.language=l.lang_code')
    ->where('EXISTS (SELECT * from #__content as c where  c.created_by=u.id)')
    ->where('(l.published=1 or cd.language=' . $db->quote('*') . ')')
    ->where('cd.published=1')
    ->group('u.name')
    ->having('(count(cd.language) !=' . count(JLanguageHelper::getLanguages()) . ' OR MAX(CAST(cd.language=' . $db->quote('*') . ' AS UNSIGNED INTEGER))=1)')
    ->having('(count(cd.language) !=1 OR MAX(CAST(cd.language=' . $db->quote('*') . ' AS UNSIGNED INTEGER))=0)');

Does anyone know an "elegant" solution to this problem?

avatar waader waader - open - 13 Dec 2015
avatar brianteeman brianteeman - change - 14 Dec 2015
Labels Added: ?
avatar brianteeman brianteeman - change - 28 Feb 2016
Category SQL
avatar brianteeman
brianteeman - comment - 20 Mar 2016

Closed as we have a PR #9492

avatar brianteeman brianteeman - change - 20 Mar 2016
Status New Closed
Closed_Date 0000-00-00 00:00:00 2016-03-20 10:33:04
Closed_By brianteeman
avatar brianteeman brianteeman - close - 20 Mar 2016

Add a Comment

Login with GitHub to post a comment