diff options
author | Neil Drumm <drumm@3064.no-reply.drupal.org> | 2006-10-23 18:38:45 +0000 |
---|---|---|
committer | Neil Drumm <drumm@3064.no-reply.drupal.org> | 2006-10-23 18:38:45 +0000 |
commit | 1a7bac90e75ad36ea5d9c3e97baeddd87435ed52 (patch) | |
tree | 377235031ff6f5e04ac850a54c3c0303b3648c3b /includes/database.mysql.inc | |
parent | eae420960fc0b35120d15fdcfd6376391b4a5de3 (diff) | |
download | brdo-1a7bac90e75ad36ea5d9c3e97baeddd87435ed52.tar.gz brdo-1a7bac90e75ad36ea5d9c3e97baeddd87435ed52.tar.bz2 |
#76819 by smsimms and sammys. Move part of the sql rewriting down into the database abstraction layer and add extra rewriting needed only for Postgres.
Diffstat (limited to 'includes/database.mysql.inc')
-rw-r--r-- | includes/database.mysql.inc | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index b0ffd2692..18eeee019 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -418,6 +418,23 @@ function db_table_exists($table) { } /** + * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to + * the SELECT list entry of the given query and the resulting query is returned. + * This function only applies the wrapper if a DISTINCT doesn't already exist in + * the query. + * + * @param $table Table containing the field to set as DISTINCT + * @param $field Field to set as DISTINCT + * @param $query Query to apply the wrapper to + * @return SQL query with the DISTINCT wrapper surrounding the given table.field. + */ +function db_distinct_field($table, $field, $query) { + $field_to_select = 'DISTINCT('. $table .'.'. $field .')'; + // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT). + return preg_replace('/(SELECT.*)('. $table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1'. $field_to_select .'\3', $query); +} + +/** * @} End of "ingroup database". */ |