You are here

protected function JsonItemTest::getFieldOrder in JSON Field 8

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.

1 call to JsonItemTest::getFieldOrder()
JsonItemTest::getTableData in tests/src/Kernel/JsonItemTest.php
Gets all data from a given table.

File

tests/src/Kernel/JsonItemTest.php, line 373

Class

JsonItemTest
@coversDefaultClass \Drupal\json_field\Plugin\Field\FieldType\JSONItem

Namespace

Drupal\Tests\json_field\Kernel

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`\n      FROM `information_schema`.`COLUMNS`\n      WHERE (`TABLE_SCHEMA` = '" . $connection_info['database'] . "')\n      AND (`TABLE_NAME` = '{" . $table . "}')\n      AND (`COLUMN_KEY` = 'PRI')\n      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;
}