diff --git a/mod/glossary/sql.php b/mod/glossary/sql.php index cfa9dc38faf..75279803fdb 100644 --- a/mod/glossary/sql.php +++ b/mod/glossary/sql.php @@ -14,41 +14,39 @@ $sqlsortkey = NULL; $textlib = textlib_get_instance(); +/// Calculate the SQL sortkey to be used by the SQL statements later + switch ( $sortkey ) { + case "CREATION": + $sqlsortkey = "timecreated"; + break; + case "UPDATE": + $sqlsortkey = "timemodified"; + break; + case "FIRSTNAME": + $sqlsortkey = "firstname"; + break; + case "LASTNAME": + $sqlsortkey = "lastname"; + break; + } + $sqlsortorder = $sortorder; + /// Pivot is the field that set the break by groups (category, initial, author name, etc) /// fullpivot indicate if the whole pivot should be compared agasint the db or just the first letter /// printpivot indicate if the pivot should be printed or not - switch ($CFG->dbtype) { - case 'postgres7': - $as = 'as'; - break; - case 'mysql': - $as = ''; - break; - } - - switch ( $sortkey ) { - case "CREATION": - $sqlsortkey = "timecreated"; - break; - - case "UPDATE": - $sqlsortkey = "timemodified"; - break; - } - $sqlsortorder = $sortorder; $fullpivot = 1; $userid = ''; - if ( isset($USER->id) ) { + if ( !empty($USER->id) ) { $userid = "OR ge.userid = $USER->id"; } switch ($tab) { case GLOSSARY_CATEGORY_VIEW: if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES ) { - $sqlselect = "SELECT gec.id, ge.*, gec.entryid, gc.name $as pivot"; + $sqlselect = "SELECT gec.id AS cid, ge.*, gec.entryid, gc.name AS glossarypivot"; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories gec, {$CFG->prefix}glossary_categories gc"; @@ -61,7 +59,7 @@ } elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED ) { $printpivot = 0; - $sqlselect = "SELECT ge.*, concept $as pivot"; + $sqlselect = "SELECT ge.*, concept AS glossarypivot"; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge LEFT JOIN {$CFG->prefix}glossary_entries_categories gec ON ge.id = gec.entryid"; $sqlwhere = "WHERE (glossaryid = '$glossary->id' OR sourceglossaryid = '$glossary->id') AND @@ -73,7 +71,7 @@ } else { $printpivot = 0; - $sqlselect = "SELECT ge.*, ce.entryid, c.name $as pivot"; + $sqlselect = "SELECT ge.*, ce.entryid, c.name AS glossarypivot"; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories ce, {$CFG->prefix}glossary_categories c"; $sqlwhere = "WHERE ge.id = ce.entryid AND ce.categoryid = '$hook' AND ce.categoryid = c.id AND ge.approved != 0 AND @@ -87,30 +85,19 @@ case GLOSSARY_AUTHOR_VIEW: $where = ''; - switch ($CFG->dbtype) { - case 'postgres7': - $usernametoshow = "u.firstname || ' ' || u.lastname"; - if ( $sqlsortkey == 'FIRSTNAME' ) { - $usernamefield = "u.firstname || ' ' || u.lastname"; - } else { - $usernamefield = "u.lastname || ' ' || u.firstname"; - } - $where = "AND substr(upper($usernamefield),1," . $textlib->strlen($hook, current_charset()) . ") = '" . $textlib->strtoupper($hook, current_charset()) . "'"; - break; - case 'mysql': - if ( $sqlsortkey == 'FIRSTNAME' ) { - $usernamefield = "CONCAT(CONCAT(u.firstname,' '), u.lastname)"; - } else { - $usernamefield = "CONCAT(CONCAT(u.lastname,' '), u.firstname)"; - } - $where = "AND left(ucase($usernamefield)," . $textlib->strlen($hook, current_charset()) . ") = '$hook'"; - break; + + if ( $sqlsortkey == 'firstname' ) { + $usernamefield = sql_fullname('u.firstname' , 'u.lastname'); + } else { + $usernamefield = sql_fullname('u.lastname' , 'u.firstname'); } + $where = "AND " . sql_substr() . "(upper($usernamefield),1," . $textlib->strlen($hook) . ") = '" . $textlib->strtoupper($hook) . "'"; + if ( $hook == 'ALL' ) { $where = ''; } - $sqlselect = "SELECT ge.id, $usernamefield $as pivot, u.id as uid, ge.*"; + $sqlselect = "SELECT ge.*, $usernamefield AS glossarypivot, 1 AS userispivot "; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}user u"; $sqlwhere = "WHERE ge.userid = u.id AND (ge.approved != 0 $userid) @@ -124,17 +111,10 @@ $where = ''; if ($hook != 'ALL' and $hook != 'SPECIAL') { - switch ($CFG->dbtype) { - case 'postgres7': - $where = 'AND substr(upper(concept),1,' . $textlib->strlen($hook, current_charset()) . ') = \'' . $textlib->strtoupper($hook, current_charset()) . '\''; - break; - case 'mysql': - $where = 'AND left(ucase(concept),' . $textlib->strlen($hook, current_charset()) . ") = '$hook'"; - break; - } + $where = 'AND ' . sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = \'' . $textlib->strtoupper($hook) . '\''; } - $sqlselect = "SELECT ge.*, ge.concept $as pivot"; + $sqlselect = "SELECT ge.*, ge.concept AS glossarypivot"; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge"; $sqlwhere = "WHERE (ge.glossaryid = '$glossary->id' OR ge.sourceglossaryid = '$glossary->id') AND ge.approved = 0 $where"; @@ -149,29 +129,22 @@ $printpivot = 0; case GLOSSARY_STANDARD_VIEW: default: - $sqlselect = "SELECT ge.*, ge.concept $as pivot"; + $sqlselect = "SELECT ge.*, ge.concept AS glossarypivot"; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge"; $where = ''; $fullpivot = 0; - if ($CFG->dbtype == "postgres7") { - $LIKE = "ILIKE"; // case-insensitive - } else { - $LIKE = "LIKE"; - } + $LIKE = sql_ilike(); + $NOTLIKE = 'NOT ' . $LIKE; switch ( $mode ) { case 'search': /// Some differences in syntax for PostgreSQL - if ($CFG->dbtype == "postgres7") { - $LIKE = "ILIKE"; // case-insensitive - $NOTLIKE = "NOT ILIKE"; // case-insensitive + if ($CFG->dbfamily == "postgres") { $REGEXP = "~*"; $NOTREGEXP = "!~*"; } else { - $LIKE = "LIKE"; - $NOTLIKE = "NOT LIKE"; $REGEXP = "REGEXP"; $NOTREGEXP = "NOT REGEXP"; } @@ -193,6 +166,13 @@ if ($definitionsearch) { $definitionsearch .= " AND "; } + + /// Under Oracle and MSSQL, trim the + and - operators and perform + /// simpler LIKE search + if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') { + $searchterm = trim($searchterm, '+-'); + } + if (substr($searchterm,0,1) == "+") { $searchterm = substr($searchterm,1); $conceptsearch .= " ge.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; @@ -261,14 +241,7 @@ case 'letter': if ($hook != 'ALL' and $hook != 'SPECIAL') { - switch ($CFG->dbtype) { - case 'postgres7': - $where = 'AND substr(upper(concept),1,' . $textlib->strlen($hook, current_charset()) . ') = \'' . $textlib->strtoupper($hook, current_charset()) . '\''; - break; - case 'mysql': - $where = 'AND left(ucase(concept),' . $textlib->strlen($hook, current_charset()) . ") = '" . $textlib->strtoupper($hook, current_charset()) . "'"; - break; - } + $where = 'AND ' . sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = \'' . $textlib->strtoupper($hook) . '\''; } if ($hook == 'SPECIAL') { //Create appropiate IN contents @@ -280,14 +253,7 @@ } $sqlalphabet .= '\''.$alphabet[$i].'\''; } - switch ($CFG->dbtype) { - case 'postgres7': - $where = 'AND substr(upper(concept),1,1) NOT IN (' . $textlib->strtoupper($sqlalphabet, current_charset()) . ')'; - break; - case 'mysql': - $where = 'AND left(ucase(concept),1) NOT IN (' . $textlib->strtoupper($sqlalphabet, current_charset()) . ')'; - break; - } + $where = 'AND ' . sql_substr() . '(upper(concept),1,1) NOT IN (' . $textlib->strtoupper($sqlalphabet) . ')'; } break; } @@ -309,17 +275,12 @@ } $count = count_records_sql("select count(*) $sqlfrom $sqlwhere"); - $sqllimit = ''; + $limitfrom = $offset; + $limitnum = 0; if ( $offset >= 0 ) { - switch ($CFG->dbtype) { - case 'postgres7': - $sqllimit = " LIMIT $entriesbypage OFFSET $offset"; - break; - case 'mysql': - $sqllimit = " LIMIT $offset, $entriesbypage"; - break; - } + $limitnum = $entriesbypage; } - $allentries = get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby $sqllimit"); + + $allentries = get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby", $limitfrom, $limitnum); ?>