--- includes/database.inc.1 2005-01-24 19:37:30.000000000 +0100 +++ includes/database.inc 2005-01-24 23:37:43.000000000 +0100 @@ -169,6 +169,98 @@ function db_queryd($query) { } /** + * Helper function for db_rewrite_sql. + * + * Collects JOIN and WHERE statements via hook_sql. + * Decides whether to select primary_key or DISTINCT(primary_key) + * + * @param $query + * query to be rewritten + * @param $primary_table + * Name or alias of the table which has the primary key field for this query. Possible values are: comments, forum, node, taxonomy, vocabulary + * @param $primary_key + * name of the primary key field. + * @param $args + * array of additional args + * @return + * An array: join statements, where statements, field or DISTINCT(field) + */ +function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_key = 'nid', $args = array()) { + $where = array(); + $join = array(); + $distinct = FALSE; + foreach (module_implements('db_rewrite_sql') as $module) { + $result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_key, $args); + if (is_array($result)) { + if (isset($result['where'])) { + $where[] .= $result['where']; + } + if (isset($result['join'])) { + $join[] .= $result['join']; + } + if (isset($result['distinct']) && $result['distinct']) { + $distinct = TRUE; + } + } + elseif (isset($result)) { + $where[] .= $result; + } + } + + $where = empty($where) ? '' : '('. implode(') AND (',$where).')'; + $join = empty($join) ? '' : implode(' ',$join); + $field = $primary_table .'.'. $primary_key; + + return array($join, $where, $distinct ? 'DISTINCT('. $field .')' : $field); +} + +/** + * Rewrites node queries. + * + * @param $query + * query to be rewritten + * @param $primary_table + * Name or alias of the table which has the primary key field for this query. Possible values are: comments, forum, node, taxonomy, vocabulary + * @param $primary_key + * name of the primary key field. + * @param $args + * an array of arguments, passed to the implementations of hook_db_rewrite_sql + * @return + * The original query with JOIN and WHERE statements inserted from hook_db_rewrite_sql implementations. nid is rewritten if needed. + */ +function db_rewrite_sql($query, $primary_table = 'n', $primary_key = 'nid', $args = array()) { + list($join, $where, $field_to_select) = _db_rewrite_sql($query, $primary_table, $primary_key, $args); + + // (? %d OR n.changed > %d OR c.last_comment_timestamp > %d) ORDER BY GREATEST(n.created, n.changed, c.last_comment_timestamp) ASC', $last, $last, $last)); return array('remaining' => $remaining, 'total' => $total); case 'search': - list($join, $where) = _node_rewrite_sql(); + list($join, $where) = _db_rewrite_sql(); $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. $join .' INNER JOIN {users} u ON n.uid = u.uid', 'n.status = 1 AND '. $where); $results = array(); foreach ($find as $item) { @@ -1797,100 +1797,15 @@ function node_access_grants($op, $uid = */ /** - * Implementation of hook_node_rewrite_sql + * Implementation of hook_db_rewrite_sql */ -function node_node_rewrite_sql() { - $return['join'] = node_access_join_sql(); - $return['where'] = node_access_where_sql(); - $return['distinct'] = !empty($return['join']); - return $return; -} - -/** - * Helper function for node_rewrite_sql. - * - * Collects JOIN and WHERE statements via hook_sql. - * Decides whether to select nid or DISTINCT(nid) - * - * @param $query - * query to be rewritten - * @param $nid_alias - * Alias of the table which has the nid field for this query. Defaults to 'n'. - * @param $args - * array of additional args - * @return - * An associative array: join => join statements, where => where statements, nid_to_select => nid or DISTINCT(nid) - */ -function _node_rewrite_sql($query = '', $nid_alias = 'n', $args = array()) { - $where = array(); - $join = array(); - $distinct = FALSE; - foreach (module_implements('node_rewrite_sql') as $module) { - $result = module_invoke($module, 'node_rewrite_sql', $query, $nid_alias, $args); - if (is_array($result)) { - if (isset($result['where'])) { - $where[] .= $result['where']; - } - if (isset($result['join'])) { - $join[] .= $result['join']; - } - if (isset($result['distinct']) && $result['distinct']) { - $distinct = TRUE; - } - } - elseif (isset($result)) { - $where[] .= $result; - } - } - - $where = empty($where) ? '' : '('. implode(') AND (',$where).')'; - $join = empty($join) ? '' : implode(' ',$join); - - return array($join, $where, $distinct ? 'DISTINCT('. $nid_alias .'.nid)' : $nid_alias .'.nid'); -} - -/** - * Rewrites node queries. - * - * @param $query - * query to be rewritten - * @param $nid_alias - * Alias of the table which has the nid field for this query. Defaults to 'n'. - * @param $args - * an array of arguments, passed to the implementations of hook_node_rewrite_sql - * @return - * The original query with JOIN and WHERE statements inserted from hook_node_rewrite_sql implementations. nid is rewritten if needed. - */ -function node_rewrite_sql($query, $nid_alias = 'n', $args = array()) { - list($join, $where, $nid_to_select) = _node_rewrite_sql($query, $nid_alias, $args); - - // (?