summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2006-03-08 15:13:20 +0000
committerDries Buytaert <dries@buytaert.net>2006-03-08 15:13:20 +0000
commit7add598ee2105dbe022cf859f851ada4790d1c4e (patch)
tree5a824d7c17793cacf51ebf4fde1430fcccc0654d /includes
parent46ad261902db33f3f2407e1157f5ccc27f87b7e2 (diff)
downloadbrdo-7add598ee2105dbe022cf859f851ada4790d1c4e.tar.gz
brdo-7add598ee2105dbe022cf859f851ada4790d1c4e.tar.bz2
- Patch #51850 by chx, webchick et al: fixed various problems with db_rewrite_sql, made db_rewrite_sql slightly more robust.
Diffstat (limited to 'includes')
-rw-r--r--includes/database.inc23
1 files changed, 12 insertions, 11 deletions
diff --git a/includes/database.inc b/includes/database.inc
index 507a37774..5d58feff9 100644
--- a/includes/database.inc
+++ b/includes/database.inc
@@ -234,17 +234,17 @@ function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'ni
$result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_field, $args);
if (isset($result) && is_array($result)) {
if (isset($result['where'])) {
- $where[] .= $result['where'];
+ $where[] = $result['where'];
}
if (isset($result['join'])) {
- $join[] .= $result['join'];
+ $join[] = $result['join'];
}
if (isset($result['distinct']) && $result['distinct']) {
$distinct = TRUE;
}
}
elseif (isset($result)) {
- $where[] .= $result;
+ $where[] = $result;
}
}
@@ -255,7 +255,8 @@ function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'ni
}
/**
- * Rewrites node, taxonomy and comment queries. Use it for listing queries.
+ * Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not
+ * use FROM table1, table2 syntax, use JOIN instead.
*
* @param $query
* Query to be rewritten.
@@ -277,11 +278,11 @@ function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $
$query = preg_replace('/(SELECT.*)('. $primary_table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $primary_table .'\.)'. $primary_field .'(.*FROM)/AUsi', '\1'. $field_to_select .'\3', $query);
}
- if (!empty($join)) {
- $query = preg_replace('/LEFT |RIGHT |INNER |WHERE|GROUP|ORDER|$/', $join .' \0', $query, 1);
- }
-
- if (!empty($where)) {
+ if (!empty($where) || !empty($join)) {
+ if (!empty($where)) {
+ $new = " WHERE $where ";
+ }
+ $new = " $join $new";
if (strpos($query, 'WHERE')) {
$replace = 'WHERE';
$add = 'AND';
@@ -299,10 +300,10 @@ function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $
$add = 'LIMIT';
}
else {
- $query .= ' WHERE '. $where;
+ $query .= $new;
}
if (isset($replace)) {
- $query = str_replace($replace, 'WHERE '. $where .' '. $add .' ', $query);
+ $query = str_replace($replace, "$new $add ", $query);
}
}