Drupal database abstraction layer - Part III

03 Dec

Drupal database abstraction layer - Part III

in database abstraction coding module development, Drupal, Drupal, Drupal Planet, Drupal Planet

This is the part III of the Drupal Database Abstraction Layer

In part I of the database series we saw how to

1. Creating and deleting the tables when a module is installed or uninstalled.

2. Updating the table structure once the module is in use. So the users can apply a patch easily

In Part II we visited 

1. Ways to write secure database calls that can eliminate SQL injection

2. Reading from the database and iterating over results

 

Here we will look at the following mechaisms in Drupal Database Abstraction

1. Reading only a certain number of records where the query can specify start and end result number.

2. Making table names unique in a shared hosting environment

 

1. Reading only a certain number of records where the query can specify start and end result number.

If you substitute db_query with db_query_range($query), you can specify start and end of the result. This function will run a limited-range query.

db_query_range($query, $from, $count, array $args = array(), array $options = array())

$from = starting result number

$count = number of results

This can be a great way to implement stateless pagination.

 

2. Making table names unique in a shared hosting environment

While writing your queries did you notice that you wrap your tabel names in curly braces {}? There is no evil intention behind this. This actually is a very interesting feature from Drupal's database abstraction layer. The {} tells the query processor to add a unique prefix to your table names. The prefix name resolution is done via the function db_prefix_tables(). This function searches for this syntax and adds Drupal's table prefix to all tables, allowing Drupal to coexist with other systems in the same database if necessary. The prefix is defined in the settings.php with key $db_prefix.

This is useful in a shared hosting environment to resolve any naming clashes.

Hope you find it useful. Please refer api.drupal.org for more details

copyright 2012 10jumps Llc.

copyright 2012 10jumps LLC.