function Schema::getColumnDefinitions

Returns the column definitions of a table, as SQLite stored them.

SQLite has no pragma that returns the expression or the storage kind of a generated column, so the CREATE TABLE statement kept in sqlite_master is the only place to read them from.

Parameters

string $schema: Name of the attached database holding the table.

string $table: Prefixed name of the table.

Return value

array<string, string> The definition text of each column, keyed by column name. Columns whose name cannot be read are omitted.

File

core/modules/sqlite/src/Driver/Database/sqlite/Schema.php, line 640

Class

Schema
SQLite implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

protected function getColumnDefinitions(string $schema, string $table) : array {
  $statement = $this->connection
    ->query('SELECT [sql] FROM [' . $schema . '].[sqlite_master] WHERE [type] = :type AND [name] = :name', [
    ':type' => 'table',
    ':name' => $table,
  ])
    ->fetchField();
  if (!is_string($statement)) {
    return [];
  }
  $definitions = [];
  foreach ($this->splitColumnDefinitions($statement) as $definition) {
    // The column name is the first token, quoted or bare.
    if (($past = $this->skipQuotedRun($definition, 0)) !== NULL) {
      $quote = $definition[0] === '[' ? ']' : $definition[0];
      $name = str_replace($quote . $quote, $quote, substr($definition, 1, $past - 2));
    }
    elseif (preg_match('/^\\S+/', $definition, $matches) === 1) {
      $name = $matches[0];
      // Drupal quotes column names, so a bare constraint keyword here
      // belongs to a table constraint rather than to a column.
      if (in_array(strtoupper($name), [
        'CHECK',
        'CONSTRAINT',
        'FOREIGN',
        'PRIMARY',
        'UNIQUE',
      ], TRUE)) {
        continue;
      }
    }
    else {
      continue;
    }
    $definitions[$name] = $definition;
  }
  return $definitions;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.