protected function DbDumpCommand::getFieldOrder

protected DbDumpCommand::getFieldOrder(Connection $connection, $table)

Gets field ordering for a given table.

Parameters

\Drupal\Core\Database\Connection $connection: The database connection to use.

string $table: The table name.

Return value

string The order string to append to the query.

File

core/lib/Drupal/Core/Command/DbDumpCommand.php, line 345

Class

DbDumpCommand
Provides a command to dump the current database to a script.

Namespace

Drupal\Core\Command

Code

protected function getFieldOrder(Connection $connection, $table) {
  // @todo this is MySQL only since there are no Database API functions for
  // table column data.
  // @todo this code is duplicated in `core/scripts/migrate-db.sh`.
  $connection_info = $connection->getConnectionOptions();
  // Order by primary keys.
  $order = '';
  $query = "SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS`
    WHERE (`TABLE_SCHEMA` = '" . $connection_info['database'] . "')
    AND (`TABLE_NAME` = '{" . $table . "}') AND (`COLUMN_KEY` = 'PRI')
    ORDER BY COLUMN_NAME";
  $results = $connection->query($query);
  while (($row = $results->fetchAssoc()) !== FALSE) {
    $order .= $row['COLUMN_NAME'] . ', ';
  }
  if (!empty($order)) {
    $order = ' ORDER BY ' . rtrim($order, ', ');
  }
  return $order;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Command!DbDumpCommand.php/function/DbDumpCommand::getFieldOrder/8.1.x