diff --git a/core/includes/database.inc b/core/includes/database.inc index dc878ed..c8e4725 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -37,12 +37,12 @@ * For example, one might wish to return a list of the most recent 10 nodes * authored by a given user. Instead of directly issuing the SQL query * @code - * SELECT n.nid, n.title, n.created FROM node n WHERE n.uid = $uid LIMIT 0, 10; + * SELECT e.id, e.title, e.created FROM example e WHERE e.uid = $uid LIMIT 0, 10; * @endcode * one would instead call the Drupal functions: * @code - * $result = db_query_range('SELECT npd.nid, npd.title, n.created - * FROM {node_property_data} npd WHERE npd.uid = :uid', 0, 10, array(':uid' => $uid)); + * $result = db_query_range('SELECT e.id, e.title, e.created + * FROM {example} e WHERE e.uid = :uid', 0, 10, array(':uid' => $uid)); * foreach ($result as $record) { * // Perform operations on $record->title, etc. here. * } @@ -69,7 +69,7 @@ * * Named placeholders begin with a colon followed by a unique string. Example: * @code - * SELECT nid, title FROM {node_property_data} WHERE uid=:uid; + * SELECT id, title FROM {example} WHERE uid=:uid; * @endcode * * ":uid" is a placeholder that will be replaced with a literal value when @@ -81,7 +81,7 @@ * * Unnamed placeholders are simply a question mark. Example: * @code - * SELECT nid, title FROM {node_property_data} WHERE uid=?; + * SELECT id, title FROM {example} WHERE uid=?; * @endcode * * In this case, the array of arguments must be an indexed array of values to @@ -91,11 +91,11 @@ * running a LIKE query the SQL wildcard character, %, should be part of the * value, not the query itself. Thus, the following is incorrect: * @code - * SELECT nid, title FROM {node_property_data} WHERE title LIKE :title%; + * SELECT id, title FROM {example} WHERE title LIKE :title%; * @endcode * It should instead read: * @code - * SELECT nid, title FROM {node_property_data} WHERE title LIKE :title; + * SELECT id, title FROM {example} WHERE title LIKE :title; * @endcode * and the value for :title should include a % as appropriate. Again, note the * lack of quotation marks around :title. Because the value is not inserted @@ -109,7 +109,7 @@ * object-oriented API for defining a query structurally. For example, rather * than: * @code - * INSERT INTO node (nid, title, body) VALUES (1, 'my title', 'my body'); + * INSERT INTO example (id, title, body) VALUES (1, 'my title', 'my body'); * @endcode * one would instead write: * @code @@ -378,7 +378,7 @@ function db_truncate($table, array $options = array()) { * @param $options * An array of options to control how the query operates. * - * @return SelectQuery + * @return \Drupal\Core\Database\Query\SelectInterface * A new SelectQuery object for this connection. */ function db_select($table, $alias = NULL, array $options = array()) { diff --git a/core/includes/form.inc b/core/includes/form.inc index d74dcf7..2324918 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -4693,26 +4693,25 @@ function _form_set_class(&$element, $class = array()) { * $context['message'] = check_plain($node->label()); * } * - * // More advanced example: multi-step operation - load all nodes, five by five + * // More advanced example: multi-step operation - load all rows, five by five * function my_function_2(&$context) { * if (empty($context['sandbox'])) { * $context['sandbox']['progress'] = 0; - * $context['sandbox']['current_node'] = 0; - * $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField(); + * $context['sandbox']['current_id'] = 0; + * $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {example}')->fetchField(); * } * $limit = 5; - * $result = db_select('node') - * ->fields('node', array('nid')) - * ->condition('nid', $context['sandbox']['current_node'], '>') - * ->orderBy('nid') + * $result = db_select('example') + * ->fields('example', array('id')) + * ->condition('id', $context['sandbox']['current_id'], '>') + * ->orderBy('id') * ->range(0, $limit) * ->execute(); * foreach ($result as $row) { - * $node = node_load($row->nid, TRUE); - * $context['results'][] = $node->nid . ' : ' . check_plain($node->label()); + * $context['results'][] = $row->id . ' : ' . check_plain($row->title); * $context['sandbox']['progress']++; - * $context['sandbox']['current_node'] = $node->nid; - * $context['message'] = check_plain($node->label()); + * $context['sandbox']['current_id'] = $row->id; + * $context['message'] = check_plain($row->title); * } * if ($context['sandbox']['progress'] != $context['sandbox']['max']) { * $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php index 7a73269..d5509a6 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php @@ -32,11 +32,11 @@ public function orderRandom() { * yet selected. * * @code - * $query = db_select('node', 'n'); - * $query->join('node_property_revision', 'nr', 'n.vid = npr.vid'); + * $query = db_select('example', 'e'); + * $query->join('example_revision', 'er', 'e.vid = er.vid'); * $query * ->distinct() - * ->fields('n') + * ->fields('e') * ->orderBy('timestamp'); * @endcode * diff --git a/core/lib/Drupal/Core/Database/Query/SelectInterface.php b/core/lib/Drupal/Core/Database/Query/SelectInterface.php index 779e69d..7dc62b0 100644 --- a/core/lib/Drupal/Core/Database/Query/SelectInterface.php +++ b/core/lib/Drupal/Core/Database/Query/SelectInterface.php @@ -142,7 +142,7 @@ public function getArguments(PlaceholderInterface $queryPlaceholder = NULL); * * @param $distinct * TRUE to flag this query DISTINCT, FALSE to disable it. - * @return SelectQueryInterface + * @return \Drupal\Core\Database\Query\SelectInterface * The called object. */ public function distinct($distinct = TRUE); @@ -184,7 +184,7 @@ public function addField($table_alias, $field, $alias = NULL); * An indexed array of fields present in the specified table that should be * included in this query. If not specified, $table_alias.* will be generated * without any aliases. - * @return Drupal\Core\Database\Query\SelectInterface + * @return \Drupal\Core\Database\Query\SelectInterface * The called object. */ public function fields($table_alias, array $fields = array()); diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php b/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php index 02b9bce..003d99c 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php @@ -96,7 +96,7 @@ function getFeedEditArray($feed_url = NULL) { */ function getDefaultFeedItemCount() { // Our tests are based off of rss.xml, so let's find out how many elements should be related. - $feed_count = db_query_range('SELECT COUNT(*) FROM {node_property_data} npd WHERE npd.promote = 1 AND npd.status = 1', 0, config('system.rss')->get('items.limit'))->fetchField(); + $feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_property_data} npd WHERE npd.promote = 1 AND npd.status = 1', 0, config('system.rss')->get('items.limit'))->fetchField(); return $feed_count > 10 ? 10 : $feed_count; } diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 9c44bdd..3bdebcb 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -312,13 +312,13 @@ function book_block_view($delta = '') { } elseif ($current_bid) { // Only display this block when the user is browsing a book. - $select = db_select('node_property_data', 'npd') - ->fields('npd', array('title')) - ->condition('npd.nid', $node->book['bid']) + $select = db_select('node', 'n') + ->fields('n', array('nid')) + ->condition('n.nid', $node->book['bid']) ->addTag('node_access'); - $title = $select->execute()->fetchField(); + $nid = $select->execute()->fetchField(); // Only show the block if the user has view access for the top-level node. - if ($title) { + if ($nid) { $tree = menu_tree_all_data($node->book['menu_name'], $node->book); // There should only be one element at the top level. $data = array_shift($tree); @@ -397,7 +397,6 @@ function book_get_books() { $query->join('node_property_data', 'npd', 'n.nid = npd.nid'); $query->join('menu_links', 'ml', 'b.mlid = ml.mlid'); $query->addField('n', 'type', 'type'); - $query->addField('npd', 'title', 'title'); $query->fields('b'); $query->fields('ml'); $query->condition('npd.nid', $nids, 'IN'); @@ -407,9 +406,10 @@ function book_get_books() { $query->addTag('node_access'); $result2 = $query->execute(); foreach ($result2 as $link) { + $node = node_load($link['nid']); $link['href'] = $link['link_path']; $link['options'] = unserialize($link['options']); - $all_books[$link['bid']] = $link; + $all_books[$link['bid']] = $node->label(); } } } diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index 82df8d6..d66a79b 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -83,10 +83,9 @@ function comment_admin_overview($form, &$form_state, $arg) { ->extend('Drupal\Core\Database\Query\PagerSelectExtender') ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->join('node_property_data', 'npd', 'npd.nid = c.nid'); - $query->addField('npd', 'title', 'node_title'); $query->addTag('node_access'); $result = $query - ->fields('c', array('cid', 'subject', 'name', 'changed')) + ->fields('c', array('cid', 'nid', 'subject', 'name', 'changed')) ->condition('c.status', $status) ->limit(50) ->orderByHeader($header) @@ -97,8 +96,9 @@ function comment_admin_overview($form, &$form_state, $arg) { // We collect a sorted list of node_titles during the query to attach to the // comments later. foreach ($result as $row) { + $node = node_load($row->nid); $cids[] = $row->cid; - $node_titles[] = $row->node_title; + $node_titles[] = $node->label(); } $comments = comment_load_multiple($cids); diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 30bca60..f8eddc5 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -42,6 +42,7 @@ function comment_enable() { $query->addExpression('0', 'comment_count'); $query->addExpression('NULL', 'last_comment_name'); $query->isNull('ncs.comment_count'); + $query->groupBy('npd', 'nid'); db_insert('node_comment_statistics') ->from($query) diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index 6427378..6b50964 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -223,7 +223,7 @@ protected function updateNodeStatistics($nid) { } else { // Comments do not exist. - $node = db_query('SELECT uid, created FROM {node_property_data} WHERE nid = :nid', array(':nid' => $nid))->fetchObject(); + $node = db_query('SELECT uid, created FROM {node_property_data} WHERE nid = :nid LIMIT 1', array(':nid' => $nid))->fetchObject(); db_update('node_comment_statistics') ->fields(array( 'cid' => 0, diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index eac73a7..e3e3454 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -809,6 +809,7 @@ function forum_forum_load($tid = NULL) { $query->join('forum', 'f', 'npd.vid = f.vid'); $query->addExpression('COUNT(npd.nid)', 'topic_count'); $query->addExpression('SUM(ncs.comment_count)', 'comment_count'); + $query->groupBy('npd', 'nid'); $counts = $query ->fields('f', array('tid')) ->condition('npd.status', 1) @@ -976,7 +977,8 @@ function forum_get_topics($tid, $sortby, $forum_per_page) { $query ->orderBy('f.sticky', 'DESC') ->orderByHeader($forum_topic_list_header) - ->condition('npd.nid', $nids); + ->condition('npd.nid', $nids) + ->groupBy('npd', 'nid'); $result = array(); foreach ($query->execute() as $row) { @@ -1384,7 +1386,7 @@ function _forum_update_forum_index($nid) { } else { // Comments do not exist. - $node = db_query('SELECT uid, created FROM {node_property_data} WHERE nid = :nid', array(':nid' => $nid))->fetchObject(); + $node = db_query('SELECT uid, created FROM {node_property_data} WHERE nid = :nid LIMIT 1', array(':nid' => $nid))->fetchObject(); db_update('forum_index') ->fields( array( 'comment_count' => 0, diff --git a/core/modules/language/language.api.php b/core/modules/language/language.api.php index 6f19835..2ec0c4c 100644 --- a/core/modules/language/language.api.php +++ b/core/modules/language/language.api.php @@ -52,8 +52,8 @@ function hook_language_update($language) { function hook_language_delete($language) { // On nodes with this language, unset the language db_update('node') - ->fields(array('language' => '')) - ->condition('language', $language->langcode) + ->fields(array('langcode' => '')) + ->condition('langcode', $language->langcode) ->execute(); } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAdminTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAdminTest.php index 85fdae1..de68f7e 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAdminTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAdminTest.php @@ -44,6 +44,7 @@ function testContentAdminSort() { // Test that the default sort by node.changed DESC actually fires properly. $nodes_query = db_select('node_property_data', 'npd') + ->distinct(TRUE) ->fields('npd', array('nid')) ->orderBy('changed', 'DESC') ->execute() @@ -59,6 +60,7 @@ function testContentAdminSort() { // Compare the rendered HTML node list to a query for the nodes ordered by // title to account for possible database-dependent sort order. $nodes_query = db_select('node_property_data', 'npd') + ->distinct(TRUE) ->fields('npd', array('nid')) ->orderBy('title') ->execute() diff --git a/core/modules/node/node.admin.inc b/core/modules/node/node.admin.inc index 50816cc..10ab62d 100644 --- a/core/modules/node/node.admin.inc +++ b/core/modules/node/node.admin.inc @@ -495,10 +495,11 @@ function node_admin_nodes() { } $header['operations'] = array('data' => t('Operations')); - $query = db_select('node_property_data', 'npd') + $query = db_select('node', 'n') + ->distinct(TRUE) ->extend('Drupal\Core\Database\Query\PagerSelectExtender') ->extend('Drupal\Core\Database\Query\TableSortExtender'); - $query->innerJoin('node', 'n', 'n.nid = npd.nid'); + $query->innerJoin('node_property_data', 'npd', 'npd.nid = n.nid'); node_build_filter_query($query); if (!user_access('bypass node access')) { @@ -682,14 +683,14 @@ function node_admin_nodes_submit($form, &$form_state) { */ function node_multiple_delete_confirm($form, &$form_state, $nodes) { $form['nodes'] = array('#prefix' => '