Comments

berdir’s picture

Status: Active » Needs review
StatusFileSize
new13.37 KB

Attached is a first patch, mostly static queries..

A few notes:
- I used fetchAssoc() and while (instead of foreach) all over the place because of: #423690-2: menu_link_maintain(), $op update critically broken.. I'm not sure if comment #3 of there would be the better approach.
- There are multiple range(0,1) queries which are just used to see if there is altleast a single row. Not sure if we can replace these with static queries..
- There were multiple "half-converted" queries, which missed the ":" in the key of the arguments array. It seems that one is not necessary, but I added it anyway to have it consistent

Edit: Phew, that patch is leet... "13.37 KB" ;)

Crell’s picture

Status: Needs review » Needs work

All built queries that have more than one chained method should be multi-line. Eg:

// No
db_delete('menu_links')->condition('menu_name', $menu['menu_name'])->execute();

// Yes
db_delete('menu_links')
  ->condition('menu_name', $menu['menu_name'])
  ->execute();

Sadly that will change the size of the patch. :-(

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new13.18 KB

- Converted those db_delete() calls to multi-line
- Removed the range stuff from those two queries in menu_node_prepare(). I don't think they're needed and save us two db_select() calls.

pwolanin’s picture

Status: Needs review » Needs work

Based on discussion and benchmarking,

Is seems to be quite a bit faster to do this (or to set the fetch mode in the db_query() call):

  $result->setFetchMode(PDO::FETCH_ASSOC);
  foreach ($result as $m) {
    ...
   }

versus:

    while ($m = $result->fetchAssoc()) {
      ...
    }

see: #430986: Compare DB fetch options for speed.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new13.34 KB

Updated to use setFetchMode and foreach

pwolanin’s picture

Instead of calling setFetchMode(), other places in core the queries are called like:

$result = db_query($sql, array(':name' => $menu['menu_name']),  array('fetch' => PDO::FETCH_ASSOC));
);

or:

 $query = db_select('book', 'b', array('fetch' => PDO::FETCH_ASSOC));

probably some of the complex queries shoudl be converte to db_select()?

Status: Needs review » Needs work

The last submitted patch failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new13.63 KB

- Re-rolled
- Updated FETCH configuration
- Queries should only be dynamic if they need to, afaik.

pwolanin’s picture

possibly this one query should be db_select():

    SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
    FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
    WHERE ml.menu_name = :name
    ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";

But overall looks good.

Crell’s picture

Status: Needs review » Needs work

The only reason to make that query dynamic would be if we wanted to make the order by statement dynamically long based on the constant. That's a critical-path query, though, so it's probably not worth the performance cost.

+    $link_exists = db_select('menu_links')
+      ->fields('menu_links', array('menu_name'))
+      ->condition('menu_name', $item['menu_name'])
+      ->range(0, 1)
+      ->execute()->fetchField();

That doesn't need to be dynamic. db_query_range() still works.

$link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(':link' => 'admin/build/menu', ':module' => 'system'))->fetchField();
// ...
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));

Elsewhere in core (and in this patch) we've been saying that if the placeholders array is 2 or more elements it should be broken across multiple lines. Let's do that here as well.

Otherwise looks good to me.

berdir’s picture

The second example is actually two arrays and each of them does only have a single value. I agree that the line is too long, but how should it be broken up?

Crell’s picture

Ah! You are correct. My error, ignore that one.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new14.09 KB

Re-roll with some minor improvements.

cburschka’s picture

Status: Needs work » Needs review

Quick code-style question: Is it okay to put strings (ie. SQL queries) in double quotes if this is not made necessary by single-quoted strings inside the queries? Your patch uses double quotes for all queries.

Coding standards doesn't make an explicit ruling, but recommends using single quotes wherever possible.

Status: Needs review » Needs work

The last submitted patch failed testing.

cburschka’s picture

Status: Needs review » Needs work

Wow, that's a big failure. Almost a third of menu module fails.

berdir’s picture

Hum. I should it test locally before submitting the patch :-/

@Arancaytar:
The DBTNG Documentation can be considered as "DBTNG coding standards guidelines", and http://drupal.org/node/310072 always uses double quotes.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new14.24 KB

Mixed up :name and :menu parameter names.. renamed all of them to :menu to be more consistent.

pwolanin’s picture

Somehow I thought db_query_range() was deprecated?

berdir’s picture

Crell’s picture

db_query_range() is not deprecated. I don't know why people keep thinking that. :-) It's the one bit of dynamic logic that we do allow on static queries, because it's fairly easy to do and is also very common, so it's a good optimization point.

Regarding quotation marks, traditionally queries always used double quotes because they almost always had single quotes in them as part of '%s'. Of course, that's no longer the case so we may want to revisit that. This is not the issue where we should do so, however.

Status: Needs review » Needs work

The last submitted patch failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new13.76 KB

Re-roll of the above patch, no changes.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

I don't see anything on visual inspection that would hold this up any longer. w00t!

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

Status: Fixed » Closed (fixed)
Issue tags: -DBTNG Conversion

Automatically closed -- issue fixed for 2 weeks with no activity.