introspectSchema

  1. drupal
    1. drupal7
Versionen
drupal7 protected DatabaseSchema_sqlite::introspectSchema($table)

Find out the schema of a table.

This function uses introspection methods provided by the database to create a schema array. This is useful, for example, during update when the old schema is not available.

see drupal_get_schema()

Übergabeparameter

$table Name of the table.

Rückgabewert

An array representing the schema, from drupal_get_schema().

Verwandte Themen

Code

includes/database/sqlite/schema.inc, line 301

<?php
protected function introspectSchema($table) {
  $mapped_fields = array_flip($this->getFieldTypeMap());
  $schema = array();
  foreach (db_query("PRAGMA table_info('{" . $table . "}')") as $row) {
    if (preg_match('/^([^(]+)\((.*)\)$/', $row->type, $matches)) {
      $type = $matches[1];
      $length = $matches[2];
    }
    else {
      $type = $row->type;
      $length = NULL;
    }
    if (isset($mapped_fields[$type])) {
      list($type, $size) = explode(':', $mapped_fields[$type]);
      $schema['fields'][$row->name] = array(
          'type' => $type,
          'size' => $size,
          'not null' => !empty($row->notnull),
          'default' => trim($row->dflt_value, "'"),
        );
      if ($length) {
        $schema['fields'][$row->name]['length'] = $length;
      }
      if ($row->pk) {
        $schema['primary key'][] = $row->name;
      }
    }
    else {
      new Exception("Unable to parse the column type " . $row->type);
    }
  }
  $indexes = array();
  foreach (db_query("PRAGMA index_list('{" . $table . "}')") as $row) {
    if (strpos($row->name, 'sqlite_autoindex_') !== 0) {
      $indexes[] = array(
          'schema_key' => $row->unique ? 'unique keys' : 'indexes',
          'name' => $row->name,
        );
    }
  }
  $n = strlen($table) + 1;
  foreach ($indexes as $index) {
    $name = $index['name'];
    $index_name = substr($name, $n);
    foreach (db_query("PRAGMA index_info('$name')") as $row) {
      $schema[$index['schema_key']][$index_name][] = $row->name;
    }
  }
  return $schema;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen