Contributions API

Calling all Drupal developers!

Help us get this on the first page of Digg. DIGG NOW!

Modules in 5

Database abstraction layer

Allow the use of different database servers using the same code base.

Drupal provides a slim database abstraction layer to provide developers with the ability to support multiple database servers easily. The intent of this layer is to preserve the syntax and power of SQL as much as possible, while letting Drupal control the pieces of queries that need to be written differently for different servers and provide basic security checks.

Most Drupal database queries are performed by a call to db_query() or db_query_range(). Module authors should also consider using pager_query() for queries that return results that need to be presented on multiple pages, and tablesort_sql() for generating appropriate queries for sortable tables.

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

<?php

SELECT n.title, n.body, n.created FROM node n WHERE n.uid = $uid LIMIT 0, 10;

?>

one would instead call the Drupal functions:

<?php

$result = db_query_range('SELECT n.title, n.body, n.created
FROM {node} n WHERE n.uid = %d', $uid, 0, 10);
while ($node = db_fetch_object($result)) {
// Perform operations on $node->body, etc. here.
  }

?>

Curly braces are used around "node" to provide table prefixing via db_prefix_tables(). The explicit use of a user ID is pulled out into an argument passed to db_query() so that SQL injection attacks from user input can be caught and nullified. The LIMIT syntax varies between database servers, so that is abstracted into db_query_range() arguments. Finally, note the common pattern of iterating over the result set using db_fetch_object().

Constants

Namesort iconLocationDescription
DB_QUERY_REGEXP5/includes/database.incIndicates the place holders that should be replaced in _db_query_callback().

Functions

Namesort iconLocationDescription
db_affected_rows5/includes/database.pgsql.incDetermine the number of rows changed by the preceding query.
db_check_setup5/includes/database.pgsql.incVerify if the database is set up correctly.
db_connect5/includes/database.pgsql.incInitialize a database connection.
db_decode_blob5/includes/database.pgsql.incReturns text from a Binary Large OBject value. In case of PostgreSQL decodes data after select from bytea field.
db_distinct_field5/includes/database.pgsql.incWraps the given table.field entry with a DISTINCT(). The wrapper is added to the SELECT list entry of the given query and the resulting query is returned. This function only applies the wrapper if a DISTINCT doesn't already exist in the query.
db_encode_blob5/includes/database.pgsql.incReturns a properly formatted Binary Large OBject value. In case of PostgreSQL encodes data for insert into bytea field.
db_error5/includes/database.pgsql.incDetermine whether the previous query caused an error.
db_escape_string5/includes/database.pgsql.incPrepare user input for use in a database query, preventing SQL injection attacks. Note: This function requires PostgreSQL 7.2 or later.
db_escape_table5/includes/database.incRestrict a dynamic tablename to safe characters.
db_fetch_array5/includes/database.pgsql.incFetch one result row from the previous query as an array.
db_fetch_object5/includes/database.pgsql.incFetch one result row from the previous query as an object.
db_lock_table5/includes/database.pgsql.incLock a table. This function automatically starts a transaction.
db_next_id5/includes/database.pgsql.incReturn a new unique ID in the given sequence.
db_num_rows5/includes/database.pgsql.incDetermine how many result rows were found by the preceding query.
db_prefix_tables5/includes/database.incAppend a database prefix to all tables in a query.
db_query5/includes/database.incRuns a basic query in the active database.
db_query_range5/includes/database.pgsql.incRuns a limited-range query in the active database.
db_query_temporary5/includes/database.pgsql.incRuns a SELECT query and stores its results in a temporary table.
db_result5/includes/database.pgsql.incReturn an individual result field from the previous query.
db_rewrite_sql5/includes/database.incRewrites node, taxonomy and comment queries. Use it for listing queries. Do not use FROM table1, table2 syntax, use JOIN instead.
db_set_active5/includes/database.incActivate a database for future queries.
db_status_report5/includes/database.pgsql.incReport database status.
db_table_exists5/includes/database.pgsql.incCheck if a table exists.
db_unlock_tables5/includes/database.pgsql.incUnlock all locked tables. This function automatically commits a transaction.
db_version5/includes/database.pgsql.incReturns the version of the database server currently in use.
hook_versioncontrol_branch_operationcontributions/versioncontrol/hook_versioncontrol.phpAct on database changes when branch operations are inserted or deleted. Note that this hook is not necessarily called at the time when the branch operation actually happens - they can also be inserted by a cron script when the actual operation has...
hook_versioncontrol_commitcontributions/versioncontrol/hook_versioncontrol.phpAct on database changes when commits are inserted or deleted. Note that this hook is not necessarily called at the time when the commit actually happens - commits can also be inserted by a cron script when the actual commit has been accomplished for...
hook_versioncontrol_repositorycontributions/versioncontrol/hook_versioncontrol.phpAct on database changes when VCS repositories are inserted, updated or deleted.
hook_versioncontrol_tag_operationcontributions/versioncontrol/hook_versioncontrol.phpAct on database changes when tag operations are inserted or deleted. Note that this hook is not necessarily called at the time when the tag operation actually happens - they can also be inserted by a cron script when the actual operation has been...
pager_query5/includes/pager.incPerform a paged database query.
tablesort_sql5/includes/tablesort.incCreate an SQL sort clause.
_db_query5/includes/database.pgsql.incHelper function for db_query().
_db_query_callback5/includes/database.incHelper function for db_query().
_db_rewrite_sql5/includes/database.incHelper function for db_rewrite_sql.