You are here

protected function DatabaseSchema_sqlsrv::createTableSql in Drupal driver for SQL Server and SQL Azure 7

Same name and namespace in other branches
  1. 7.3 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::createTableSql()
  2. 7.2 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::createTableSql()

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

The SQL statement to create the table.

1 call to DatabaseSchema_sqlsrv::createTableSql()
DatabaseSchema_sqlsrv::createTable in sqlsrv/schema.inc
Create a new table from a Drupal table definition.

File

sqlsrv/schema.inc, line 226
Database schema code for Microsoft SQL Server database servers.

Class

DatabaseSchema_sqlsrv

Code

protected function createTableSql($name, $table) {
  $sql_fields = array();
  foreach ($table['fields'] as $field_name => $field) {
    $sql_fields[] = $this
      ->createFieldSql($name, $field_name, $this
      ->processField($field));
  }

  // If the table has no primary key, create one for us.
  // TODO: only necessary on Azure.
  if (isset($table['primary key']) && is_array($table['primary key'])) {
    $sql_fields[] = 'CONSTRAINT {' . $name . '}_pkey PRIMARY KEY CLUSTERED (' . implode(', ', $this->connection
      ->quoteIdentifiers($table['primary key'])) . ')';
  }
  else {
    $sql_fields[] = '__pk UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL';
    $sql_fields[] = 'CONSTRAINT {' . $name . '}_pkey_technical PRIMARY KEY CLUSTERED (__pk)';
  }
  $sql = "CREATE TABLE [{" . $name . "}] (\n\t";
  $sql .= implode(",\n\t", $sql_fields);
  $sql .= "\n)";
  return $sql;
}