summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/database.inc74
-rw-r--r--modules/node.module4
-rw-r--r--modules/node/node.module4
3 files changed, 45 insertions, 37 deletions
diff --git a/includes/database.inc b/includes/database.inc
index fe95f2d8c..e6ad4baba 100644
--- a/includes/database.inc
+++ b/includes/database.inc
@@ -178,19 +178,19 @@ function db_queryd($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, term_data, vocabulary.
- * @param $primary_key
- * Name of the primary key field.
+ * @param $primary_field
+ * Name of the primary field.
* @param $args
* Array of additional arguments.
* @return
* An array: join statements, where statements, field or DISTINCT(field).
*/
-function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_key = 'nid', $args = array()) {
+function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = '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);
+ $result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_field, $args);
if (is_array($result)) {
if (isset($result['where'])) {
$where[] .= $result['where'];
@@ -209,9 +209,8 @@ function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_key = 'nid'
$where = empty($where) ? '' : '('. implode(') AND (',$where).')';
$join = empty($join) ? '' : implode(' ',$join);
- $field = $primary_table .'.'. $primary_key;
- return array($join, $where, $distinct ? 'DISTINCT('. $field .')' : $field);
+ return array($join, $where, $distinct );
}
/**
@@ -221,42 +220,51 @@ function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_key = 'nid'
* 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, term_data, vocabulary.
- * @param $primary_key
- * Name of the primary key field.
+ * @param $primary_field
+ * Name of the primary 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);
+function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array()) {
+ list($join, $where, $distinct) = _db_rewrite_sql($query, $primary_table, $primary_field, $args);
- // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
- $query = preg_replace('/(SELECT.*)('. $primary_table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $primary_table .'\.)'. $primary_key .'(.*FROM)/AUsi', '\1'. $field_to_select .'\3', $query);
-
- $query = preg_replace('|FROM[^[:upper:]/,]+|','\0 '. $join .' ', $query);
- if (strpos($query, 'WHERE')) {
- $replace = 'WHERE';
- $add = 'AND';
- }
- elseif (strpos($query, 'GROUP')) {
- $replace = 'GROUP';
- $add = 'GROUP';
- }
- elseif (strpos($query, 'ORDER')) {
- $replace = 'ORDER';
- $add = 'ORDER';
- }
- elseif (strpos($query, 'LIMIT')) {
- $replace = 'LIMIT';
- $add = 'LIMIT';
+ if ($distinct) {
+ $field_to_select = 'DISTINCT($primary_table .'.'. $primary_field)';
+ // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
+ $query = preg_replace('/(SELECT.*)('. $primary_table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $primary_table .'\.)'. $primary_field .'(.*FROM)/AUsi', '\1'. $field_to_select .'\3', $query);
}
- else {
- $query .= ' WHERE '. $where;
+
+ if (!empty($join)) {
+ $query = preg_replace('|FROM[^[:upper:]/,]+|','\0 '. $join .' ', $query);
}
- if (isset($replace)) {
- $query = str_replace($replace, 'WHERE '. $where .' '. $add .' ', $query);
+
+ if (!empty($where)) {
+ if (strpos($query, 'WHERE')) {
+ $replace = 'WHERE';
+ $add = 'AND';
+ }
+ elseif (strpos($query, 'GROUP')) {
+ $replace = 'GROUP';
+ $add = 'GROUP';
+ }
+ elseif (strpos($query, 'ORDER')) {
+ $replace = 'ORDER';
+ $add = 'ORDER';
+ }
+ elseif (strpos($query, 'LIMIT')) {
+ $replace = 'LIMIT';
+ $add = 'LIMIT';
+ }
+ else {
+ $query .= ' WHERE '. $where;
+ }
+ if (isset($replace)) {
+ $query = str_replace($replace, 'WHERE '. $where .' '. $add .' ', $query);
+ }
}
+
return $query;
}
diff --git a/modules/node.module b/modules/node.module
index 63bc7b1df..0fd24ae6d 100644
--- a/modules/node.module
+++ b/modules/node.module
@@ -1814,8 +1814,8 @@ function node_access_grants($op, $uid = NULL) {
/**
* Implementation of hook_db_rewrite_sql
*/
-function node_db_rewrite_sql($query, $primary_table) {
- if ($primary_table=='n') {
+function node_db_rewrite_sql($query, $primary_table, $primary_field) {
+ if ($primary_field == 'nid') {
$return['join'] = node_access_join_sql();
$return['where'] = node_access_where_sql();
$return['distinct'] = !empty($return['join']);
diff --git a/modules/node/node.module b/modules/node/node.module
index 63bc7b1df..0fd24ae6d 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -1814,8 +1814,8 @@ function node_access_grants($op, $uid = NULL) {
/**
* Implementation of hook_db_rewrite_sql
*/
-function node_db_rewrite_sql($query, $primary_table) {
- if ($primary_table=='n') {
+function node_db_rewrite_sql($query, $primary_table, $primary_field) {
+ if ($primary_field == 'nid') {
$return['join'] = node_access_join_sql();
$return['where'] = node_access_where_sql();
$return['distinct'] = !empty($return['join']);