Preparing for Drupal 7 by changing the way to make parameters for db_query.

After attending DrupalCon in Szeged and following the new database stuff, I thought a bit about how to make the transition easier when Drupal 7 comes out.

I didn’t think that it was anything special, but Robert Douglass encouraged me to share it, so here you are:

I figured out that you can actually use the Drupal 7-style of arguments to db_query right now (more or less).

The thing is that db_query in Drupal 6 allows you to use either the classic way with an arbitrary number of extra arguments to the db_query function or arguments as an array which is the future style.

The deal is that Drupal 6 takes the arguments in order, where Drupal 7 will use array keys (so I’d have :title in my query instead of %s).

So you can use the same arguments in Drupal 6 and Drupal 7, because Drupal 6 doesn’t care about which keys you have in your arguments array.

So whithout further ado, an example query from my TSearch module (the implementation of PostgreSQL’s TSearch in Drupal):

  1. db_query("UPDATE {tsearch_node} SET vid = %d,
  2. node_tsvector =
  3. setweight(to_tsvector('%s'), 'A') ||
  4. setweight(to_tsvector('%s'), 'B') ||
  5. setweight(to_tsvector('%s'), 'C') ||
  6. setweight(to_tsvector('%s'), 'D'),
  7. updated = %d
  8. WHERE nid = %d",
  9. array(':vid' => $node->vid,
  10. ':title' => $text['title'],
  11. ':extra' => $text['extra'],
  12. ':teaser' => $text['teaser'],
  13. ':body' => $text['body'],
  14. ':time' => $_SERVER['REQUEST_TIME'],
  15. ':nid' => $nid,
  16. ));

Isn’t that neat? I’ve become quite fond of this syntax, because I think it’s easier to see what’s going on, and it’ll be even better in Drupal 7, where I don’t have to care about the ordering any longer.

Neat!

Entirely unintentional, but nifty! I didn’t realize D6 would handle that properly. Queries will still require some modification once you get to D7 (for the :placeholders and builders for modifier queries at least), but yeah, the named array format is probably a good habit to get into.

Cool beans!

Awesome may make switch to drupal

I have been using wordpress for a while for my community blogs. I think i may want to switch over to drupal now because it seems more flexible and easier to hack / integrate with exiting php code.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Allowed HTML tags: <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <code> <del> <blockquote> <q> <sub> <p> <br> <pre> <ul> <ol> <li> <dl> <dt> <dd> <a> <b> <u> <i> <sup> <img> <table> <tbody> <td> <th> <tr>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <d5>, <d6>, <html>, <js>, <php>, <sql>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Adds typographic refinements.

More information about formatting options

Syndicate content