Support from Acquia helps fund testing for Drupal Acquia logo

Comments

phoolish’s picture

Assigned: Unassigned » phoolish
phoolish’s picture

FileSize
6.96 KB
Crell’s picture

Status: Active » Needs review
csevb10’s picture

Status: Needs review » Needs work

Certain aspects still need to be rewritten.
Elements like this

$result = db_query("SELECT aa.aid, a.type FROM {trigger_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = '%s' AND aa.op = '%s' ORDER BY weight", $hook, $op);

should be rewritten to use the new replacement syntax.

In addition, db_query return an object that can be iterated over, so this

while ($action = $result->fetchObject()) {

can be rewritten as a foreach that iterates over the $result object.

csevb10’s picture

Status: Needs work » Needs review
FileSize
8.33 KB

Re-roll of the trigger module patch with modifications for db_query calls.

Crell’s picture

Status: Needs review » Needs work
+    if (db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND op = :op AND aid = :aid", array(':hook' => $form_values['hook'], ':op' => $form_values['operation'], ':aid' => $aid))->fetchField()) {

That line is way too long to be pretty. :-) Let's split the query out from the if statement so that we can break out the placeholders array vertically, save to a variable, and then if() on that.

+    $weight = db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = :hook AND op = :op", array(':hook' => $form_values['hook'], ':op' => $form_values['operation']))->fetchField();

This should also be spread out to multiple lines. Any time you have more than one placeholder the array should be broken out vertically. There are a couple such queries.

In almost all cases, a result set should be named $result to avoid confusion. Only give it a name when there's a couple of active result sets operating at once. (See _trigger_get_hook_aids())

csevb10’s picture

Status: Needs work » Needs review
FileSize
8.49 KB

1 step closer every time, that's what I say. Sooner or later, I'll just do it right the first time.
I reformatted the queries to break across lines, but I still wasn't 100% sure how to format 1 aspect:

    $aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND op = :op AND aid = :aid", array(
      ':hook' => $form_values['hook'],
      ':op' => $form_values['operation'],
      ':aid' => $aid,
    ))
    ->fetchField();

Would you like the array indented more and the fetchField in its current location, or is this format the preferred format?

catch’s picture

Status: Needs review » Needs work

The straight select queries/db_query() should just be on one line, no need to split the array for those. Otherwise looks good.

Crell’s picture

Actually if there's 2 or more placeholders, the placeholders array should be multi-line.

-    $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module));
+    $info = db_select('system')
+      ->condition('name', $module)
+      ->execute()
+      ->fetchField();

That has no need to be dynamic, though. Otherwise it looks fine.

Berdir’s picture

There are two possible ways to indent, I'd prefer B) because we actually do chaining, even if it's only a single method and because the statement is not finished.

A) This is used if the statement is finished after the closing array, for example in hook_menu()

    $aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND op = :op AND aid = :aid", array(
      ':hook' => $form_values['hook'],
      ':op' => $form_values['operation'],
      ':aid' => $aid,
    ))
    ->fetchField();

B) This is used in chaining, when there are multiple chaining methods

    $aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND op = :op AND aid = :aid", array(
        ':hook' => $form_values['hook'],
        ':op' => $form_values['operation'],
        ':aid' => $aid,
      ))
      ->fetchField();
Crell’s picture

I believe most of core is already using A.

Berdir’s picture

Ok, we pretty much agreed on the following coding style:

$aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND op = :op AND aid = :aid", array(
  ':hook' => $form_values['hook'],
  ':op' => $form_values['operation'],
  ':aid' => $aid,
))->fetchField();

So the following needs to be updated, the rest of the patch looks good.

+    ))
+    ->fetchField();
Crell’s picture

Um, how do you get from "most of core is already doing A" to "let's do B"? Where did "we pretty much agree" on changing it?

catch’s picture

Berdir’s picture

Seems I copied the wrong example, I meant A. What I wanted to point out is that fetchField() should be on the same line. Updated the above comment.

andypost’s picture

Status: Needs work » Needs review
FileSize
8.72 KB

Here reroll after #246096: Cron triggers are not executed

With modifications from #12

Crell’s picture

*sigh* Dries, we had been over this before and settled on separate line, which is all over core now in a thousand places. I'm not rolling the patch to change all of them now, especially when I don't agree with it.

Dries’s picture

My preference is ))->fetchField().

Crell, I don't remember having settled on that. I don't feel that strong about it though, and it certainly shouldn't hold up this patch.

andypost’s picture

FileSize
8.7 KB

Reroll with ))->

Berdir’s picture

Status: Needs review » Needs work
-  while ($action = db_fetch_object($result)) {
+  foreach ($result as $action) {
     $actions[$action->aid] = $action->description;
   }
   return $actions;

You can replace that with:

 return $result->fetchAllKeyed();
   $aids = array();
-  $result = db_query("SELECT aa.aid, a.type FROM {trigger_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = '%s' AND aa.op = '%s' ORDER BY weight", $hook, $op);
-  while ($action = db_fetch_object($result)) {
+  $result = db_query("SELECT aa.aid, a.type FROM {trigger_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = :hook AND aa.op = :op ORDER BY weight", array(
+    ':hook' => $hook,
+    ':op' => $op,
+  ));
+  foreach ($result as $action) {
     $aids[$action->aid]['type'] = $action->type;
   }

And that one probably with: (untested)

return db_query("SELECT aa.aid, a.type FROM {trigger_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = :hook AND aa.op = :op ORDER BY weight", array(
  ':hook' => $hook,
  ':op' => $op,
))->fetchAllAssoc('aid');
andypost’s picture

Status: Needs work » Needs review
FileSize
8.71 KB

First is chandged

Second is not working - aids holds keyed subarray with only 1 key - 'type' but if fetchassoc then we get whole record in sub array

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now.

We agreed that we could probably simplify the array because the sub-array contains only a single key -> value pair, but we can do that in another issue.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD.

andypost’s picture

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

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