function Schema::splitColumnDefinitions

Splits the column definition list of a CREATE TABLE statement.

Parameters

string $statement: The CREATE TABLE statement.

Return value

list<string> The comma separated parts of the definition list. Table constraints are included, since they are not distinguishable from columns here.

File

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

Class

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

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

protected function splitColumnDefinitions(string $statement) : array {
  $open = strpos($statement, '(');
  if ($open === FALSE) {
    return [];
  }
  $parts = [];
  $current = '';
  $depth = 0;
  $length = strlen($statement);
  for ($i = $open; $i < $length; $i++) {
    // Parentheses and commas inside a quoted run are just text.
    if (($past = $this->skipQuotedRun($statement, $i)) !== NULL) {
      $current .= substr($statement, $i, $past - $i);
      $i = $past - 1;
      continue;
    }
    // Commas and parentheses inside a comment are just text too.
    if (($past = $this->skipComment($statement, $i)) !== NULL) {
      $current .= substr($statement, $i, $past - $i);
      $i = $past - 1;
      continue;
    }
    $character = $statement[$i];
    if ($character === '(') {
      $depth++;
      // Skip the parenthesis that opens the definition list.
      if ($depth === 1) {
        continue;
      }
    }
    elseif ($character === ')') {
      $depth--;
      if ($depth === 0) {
        break;

      }
    }
    elseif ($character === ',' && $depth === 1) {
      $parts[] = trim($current);
      $current = '';
      continue;
    }
    $current .= $character;
  }
  $parts[] = trim($current);
  return array_values(array_filter($parts, fn(string $part): bool => $part !== ''));
}

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