You are here

protected function DatabaseSchema_sqlite::introspectSchema in Drupal 7

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.

Parameters

$table: Name of the table.

Return value

An array representing the schema, from drupal_get_schema().

See also

drupal_get_schema()

9 calls to DatabaseSchema_sqlite::introspectSchema()
DatabaseSchema_sqlite::addField in includes/database/sqlite/schema.inc
Add a new field to a table.
DatabaseSchema_sqlite::addPrimaryKey in includes/database/sqlite/schema.inc
Add a primary key.
DatabaseSchema_sqlite::changeField in includes/database/sqlite/schema.inc
Change a field definition.
DatabaseSchema_sqlite::dropField in includes/database/sqlite/schema.inc
Drop a field.
DatabaseSchema_sqlite::dropPrimaryKey in includes/database/sqlite/schema.inc
Drop the primary key.

... See full list

File

includes/database/sqlite/schema.inc, line 410
Database schema code for SQLite databases.

Class

DatabaseSchema_sqlite

Code

protected function introspectSchema($table) {
  $mapped_fields = array_flip($this
    ->getFieldTypeMap());
  $schema = array(
    'fields' => array(),
    'primary key' => array(),
    'unique keys' => array(),
    'indexes' => array(),
  );
  $info = $this
    ->getPrefixInfo($table);
  $result = $this->connection
    ->query('PRAGMA ' . $info['schema'] . '.table_info(' . $info['table'] . ')');
  foreach ($result 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();
  $result = $this->connection
    ->query('PRAGMA ' . $info['schema'] . '.index_list(' . $info['table'] . ')');
  foreach ($result as $row) {
    if (strpos($row->name, 'sqlite_autoindex_') !== 0) {
      $indexes[] = array(
        'schema_key' => $row->unique ? 'unique keys' : 'indexes',
        'name' => $row->name,
      );
    }
  }
  foreach ($indexes as $index) {
    $name = $index['name'];

    // Get index name without prefix.
    $index_name = substr($name, strlen($info['table']) + 1);
    $result = $this->connection
      ->query('PRAGMA ' . $info['schema'] . '.index_info(' . $name . ')');
    foreach ($result as $row) {
      $schema[$index['schema_key']][$index_name][] = $row->name;
    }
  }
  return $schema;
}