drupal 7 database api
DESCRIPTION
Drupal 7 database APITRANSCRIPT
Drupal 7 Database API
Andriy Podanenko [email protected]
Server configuration
PHP 5.0+ (5.3+ recommended)
php5-pdo
php5-pdo_mysql
php5-pdo_pgsql
php5-pdo_sqlite
mysql, postgres…
Apache1.3+ (or php cgi enabled web server)
The start…
php.net/pdo
<?php
abstract class DatabaseConnection extends PDO
…
// Drupal fun…
// Because the other methods don't seem to work right.
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
Class Hierarhy abstract class DatabaseConnection extends PDO…
DatabaseConnection DatabaseConnection::select db_select (SelectQuery) DatabaseConnection::delete db_delete (DeleteQuery) DatabaseConnection::update db_update (UpdateQuery) DatabaseConnection::query db_query (DatabaseStatementInterface) DatabaseConnection::insert db_insert (InsertQuery) DatabaseConnection::merge db_merge (MergeQuery) …
DatabaseStatementInterface
• execute ($args=array(), $options=array()) • fetchAllAssoc ($key, $fetch=NULL) • fetchAllKeyed ($key_index=0, $value_index=1) • fetchAssoc () • fetchCol ($index=0) • fetchField ($index=0) • getQueryString () • rowCount () (UPDATE, DELETE, INSERT only)
Query interface hierarchy
db_select SELECT name,description FROM taxonomy_term_data AS ttd
WHERE tid LIKE 79
<?php //Drupal
$result = db_select('taxonomy_term_data', 'ttd');
//returned SelectQuery
$ret = $result -> fields('ttd', array('name', 'description'));
//returned SelectQueryInterface
$ret = $result -> condition('tid', 79, 'like');
// returned QueryConditionInterface
$ret = $result -> execute(); // returned SelectQuery object
$data = $ret -> fetchAll(); // fetched data as object from database
Other database (target)
$result = db_select('taxonomy_term_data', 'ttd',
array('target' => 'slave'));
// adding other database at runtime
Database::addConnectionInfo('default', 'slave', array(
'database' => ‘other_db',
'username' => ‘name', 'password' => 'pass',
'host' => 'localhost', 'port' => '5432',
'driver' => 'pgsql'));
DatabaseTransaction (innodb,pgsql)
<?php
function demo_transaction()
$temp = db_transaction();
db_update… // charging account1 +100$
db_update… // charging account2 -100$
db_insert… // creating log entry for that
return TRUE; // automatically commit (no $temp)
} //
$error->handle try{} catch{}
<?php
function demo_transaction()
$temp = db_transaction();
try {
db_insert… // some ugly code here…
return TRUE; }
catch(Exception $e) { $temp->rollback();
watchdog(‘demo error', print_r($e, TRUE)}
logging -> DatabaseLog
$demo_log = new DatabaseLog;
$demo_log->start(‘some_key’);
db_update…
db_select…
// Calling functions or any…
$demo_log->end(‘some_key’);
print_r($demo_log->get(‘some_key’), TRUE);
// printed indexed array for all queries…
Drupal 6 – dbtng module (backport)
drupal.org/project/dbtng
dbtng_* (full functions list in dbtng.module)
dbtng_query
dbtng_set_active
db_select
• good for support old sites
• multi database setup possibility for Drupal 6
• preparing code for upgrade
Useful links
• api.drupal.org/api/drupal/7
• drupal.org/developing/api/database
• upgrade.boombatower.com/tools/sql/inline
• drupal.org/project/coder
• php.net/pdo
• drupal.org/project/dbtng
• drupal.org/node/310069
Drupal DB api->executed();
twitter.com/podarok
facebook.com/podarok
vkontakte.ru/podarokua
drupal.org/user/116002
drupal.ua/user/23
http://simbioz.ua