// couldn't use db_select() here because it would prefix
// information_schema.tables and the query would fail.
// Don't use {} around information_schema.tables table.
- $results = $this->connection->query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments());
+ $results = $this->connection->query("SELECT table_name as table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments());
foreach ($results as $table) {
// Take into account tables that have an individual prefix.
if (isset($individually_prefixed_tables[$table->table_name])) {
*/
abstract public function dropPrimaryKey($table);
+ /**
+ * Finds the primary key columns of a table, from the database.
+ *
+ * @param string $table
+ * The name of the table.
+ *
+ * @return string[]|false
+ * A simple array with the names of the columns composing the table's
+ * primary key, or FALSE if the table does not exist.
+ *
+ * @throws \RuntimeException
+ * If the driver does not override this method.
+ */
+ protected function findPrimaryKeyColumns($table) {
+ if (!$this->tableExists($table)) {
+ return FALSE;
+ }
+ throw new \RuntimeException("The '" . $this->connection->driver() . "' database driver does not implement " . __METHOD__);
+ }
+
/**
* Add a unique key.
*
return is_string($value) ? $this->connection->quote($value) : $value;
}
+ /**
+ * Ensures that all the primary key fields are correctly defined.
+ *
+ * @param array $primary_key
+ * An array containing the fields that will form the primary key of a table.
+ * @param array $fields
+ * An array containing the field specifications of the table, as per the
+ * schema data structure format.
+ *
+ * @throws \Drupal\Core\Database\SchemaException
+ * Thrown if any primary key field specification does not exist or if they
+ * do not define 'not null' as TRUE.
+ */
+ protected function ensureNotNullPrimaryKey(array $primary_key, array $fields) {
+ foreach (array_intersect($primary_key, array_keys($fields)) as $field_name) {
+ if (!isset($fields[$field_name]['not null']) || $fields[$field_name]['not null'] !== TRUE) {
+ throw new SchemaException("The '$field_name' field specification does not define 'not null' as TRUE.");
+ }
+ }
+ }
+
}