drupal_get_schema

Versions
6 – 7
drupal_get_schema($table = NULL, $rebuild = FALSE)

Get the schema definition of a table, or the whole database schema.

The returned schema will include any modifications made by any module that implements hook_schema_alter().

Parameters

$table The name of the table. If not given, the schema of all tables is returned.

$rebuild If true, the schema will be rebuilt instead of retrieved from the cache.

Related topics

▾ 11 functions call drupal_get_schema()

drupal_schema_fields_sql in includes/common.inc
Retrieve a list of fields from a table schema. The list is suitable for use in a SQL query.
drupal_write_record in includes/common.inc
Save a record to the database based upon the schema.
field_sql_storage_field_storage_create_field in modules/field/modules/field_sql_storage/field_sql_storage.module
Implement hook_field_storage_create_field().
field_sql_storage_field_storage_delete_field in modules/field/modules/field_sql_storage/field_sql_storage.module
Implement hook_field_storage_delete_field().
field_sql_storage_field_storage_update_field in modules/field/modules/field_sql_storage/field_sql_storage.module
Implement hook_field_storage_update_field().
install_configure_form in ./install.php
Installation task; configure settings for the new site.
install_finished in ./install.php
Installation task; perform final steps and display a 'finished' page.
module_enable in includes/module.inc
Enable a given list of modules.
profile_field_form_validate in modules/profile/profile.admin.inc
Validate profile_field_form submissions.
user_account_form_validate in modules/user/user.module
Form validation handler for user_account_form().
user_save in modules/user/user.module
Save changes to a user account or add a new user.

Code

includes/bootstrap.inc, line 1898

<?php
function drupal_get_schema($table = NULL, $rebuild = FALSE) {
  static $schema = array();

  if (empty($schema) || $rebuild) {
    // Try to load the schema from cache.
    if (!$rebuild && $cached = cache_get('schema')) {
      $schema = $cached->data;
    }
    // Otherwise, rebuild the schema cache.
    else {
      $schema = array();
      // Load the .install files to get hook_schema.
      // On some databases this function may be called before bootstrap has
      // been completed, so we force the functions we need to load just in case.
      if (function_exists('module_load_all_includes')) {

        // There is currently a bug in module_list() where it caches what it
        // was last called with, which is not always what you want.
        // module_load_all_includes() calls module_list(), but if this function
        // is called very early in the bootstrap process then it will be
        // uninitialized and therefore return no modules. Instead, we have to
        // "prime" module_list() here to to values we want, specifically
        // "yes rebuild the list and don't limit to bootstrap".
        // TODO: Remove this call after http://drupal.org/node/222109 is fixed.
        module_list(TRUE, FALSE);
        module_load_all_includes('install');
      }

      require_once DRUPAL_ROOT . '/includes/common.inc';
      // Invoke hook_schema for all modules.
      foreach (module_implements('schema') as $module) {
        // Cast the result of hook_schema() to an array, as a NULL return value
        // would cause array_merge() to set the $schema variable to NULL as well.
        // That would break modules which use $schema further down the line.
        $current = (array) module_invoke($module, 'schema');
        _drupal_schema_initialize($module, $current);
        $schema = array_merge($schema, $current);
      }

      drupal_alter('schema', $schema);
      // If the schema is empty, avoid saving it: some database engines require
      // the schema to perform queries, and this could lead to infinite loops.
      if (!empty($schema) && (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL)) {
        cache_set('schema', $schema);
      }
    }
  }

  if (!isset($table)) {
    return $schema;
  }
  elseif (isset($schema[$table])) {
    return $schema[$table];
  }
  else {
    return FALSE;
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.