protected Schema::createTableSql($name, $table)
Generate SQL to create a new table from a Drupal schema definition.
Parameters
$name: The name of the table to create.
$table: A Schema API table definition array.
Return value
An array of SQL statements to create the table.
File
- core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php, line 221
Class
- Schema
- PostgreSQL implementation of \Drupal\Core\Database\Schema.
Namespace
Drupal\Core\Database\Driver\pgsql
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 | protected function createTableSql( $name , $table ) { $sql_fields = array (); foreach ( $table [ 'fields' ] as $field_name => $field ) { $sql_fields [] = $this ->createFieldSql( $field_name , $this ->processField( $field )); } $sql_keys = array (); if (isset( $table [ 'primary key' ]) && is_array ( $table [ 'primary key' ])) { $sql_keys [] = 'CONSTRAINT ' . $this ->ensureIdentifiersLength( $name , '' , 'pkey' ) . ' PRIMARY KEY (' . $this ->createPrimaryKeySql( $table [ 'primary key' ]) . ')' ; } if (isset( $table [ 'unique keys' ]) && is_array ( $table [ 'unique keys' ])) { foreach ( $table [ 'unique keys' ] as $key_name => $key ) { $sql_keys [] = 'CONSTRAINT ' . $this ->ensureIdentifiersLength( $name , $key_name , 'key' ) . ' UNIQUE (' . implode( ', ' , $key ) . ')' ; } } $sql = "CREATE TABLE {" . $name . "} (\n\t" ; $sql .= implode( ",\n\t" , $sql_fields ); if ( count ( $sql_keys ) > 0) { $sql .= ",\n\t" ; } $sql .= implode( ",\n\t" , $sql_keys ); $sql .= "\n)" ; $statements [] = $sql ; if (isset( $table [ 'indexes' ]) && is_array ( $table [ 'indexes' ])) { foreach ( $table [ 'indexes' ] as $key_name => $key ) { $statements [] = $this ->_createIndexSql( $name , $key_name , $key ); } } // Add table comment. if (! empty ( $table [ 'description' ])) { $statements [] = 'COMMENT ON TABLE {' . $name . '} IS ' . $this ->prepareComment( $table [ 'description' ]); } // Add column comments. foreach ( $table [ 'fields' ] as $field_name => $field ) { if (! empty ( $field [ 'description' ])) { $statements [] = 'COMMENT ON COLUMN {' . $name . '}.' . $field_name . ' IS ' . $this ->prepareComment( $field [ 'description' ]); } } return $statements ; } |
Please login to continue.