Schema::createFieldSql

protected Schema::createFieldSql($name, $spec)

Create an SQL string for a field to be used in table creation or alteration.

Before passing a field out of a schema definition into this function it has to be processed by db_processField().

Parameters

$name: Name of the field.

$spec: The field specification, as per the schema data structure format.

File

core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php, line 156

Class

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

Namespace

Drupal\Core\Database\Driver\sqlite

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
protected function createFieldSql($name, $spec) {
  if (!empty($spec['auto_increment'])) {
    $sql = $name . " INTEGER PRIMARY KEY AUTOINCREMENT";
    if (!empty($spec['unsigned'])) {
      $sql .= ' CHECK (' . $name . '>= 0)';
    }
  }
  else {
    $sql = $name . ' ' . $spec['sqlite_type'];
 
    if (in_array($spec['sqlite_type'], array('VARCHAR', 'TEXT'))) {
      if (isset($spec['length'])) {
        $sql .= '(' . $spec['length'] . ')';
      }
 
      if (isset($spec['binary']) && $spec['binary'] === FALSE) {
        $sql .= ' COLLATE NOCASE_UTF8';
      }
    }
 
    if (isset($spec['not null'])) {
      if ($spec['not null']) {
        $sql .= ' NOT NULL';
      }
      else {
        $sql .= ' NULL';
      }
    }
 
    if (!empty($spec['unsigned'])) {
      $sql .= ' CHECK (' . $name . '>= 0)';
    }
 
    if (isset($spec['default'])) {
      if (is_string($spec['default'])) {
        $spec['default'] = $this->connection->quote($spec['default']);
      }
      $sql .= ' DEFAULT ' . $spec['default'];
    }
 
    if (empty($spec['not null']) && !isset($spec['default'])) {
      $sql .= ' DEFAULT NULL';
    }
  }
  return $sql;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.