DatabaseTestController.php 4.11 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\database_test\Controller;

5 6
use Symfony\Component\HttpFoundation\JsonResponse;

7 8 9 10 11 12
/**
 * Controller routines for database_test routes.
 */
class DatabaseTestController {

  /**
13 14 15 16 17 18 19
   * Runs db_query_temporary() and outputs the table name and its number of rows.
   *
   * We need to test that the table created is temporary, so we run it here, in a
   * separate menu callback request; After this request is done, the temporary
   * table should automatically dropped.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
20 21
   */
  public function dbQueryTemporary() {
22 23 24 25 26
    $table_name = db_query_temporary('SELECT age FROM {test}', array());
    return new JsonResponse(array(
      'table_name' => $table_name,
      'row_count' => db_select($table_name)->countQuery()->execute()->fetchField(),
    ));
27 28 29
  }

  /**
30 31 32 33 34 35
   * Runs a pager query and returns the results.
   *
   * This function does care about the page GET parameter, as set by the
   * simpletest HTTP call.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
36 37
   */
  public function pagerQueryEven($limit) {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    $query = db_select('test', 't');
    $query
      ->fields('t', array('name'))
      ->orderBy('age');

    // This should result in 2 pages of results.
    $query = $query
      ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
      ->limit($limit);

    $names = $query->execute()->fetchCol();

    return new JsonResponse(array(
      'names' => $names,
    ));
53 54 55
  }

  /**
56 57 58 59 60 61
   * Runs a pager query and returns the results.
   *
   * This function does care about the page GET parameter, as set by the
   * simpletest HTTP call.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
62 63
   */
  public function pagerQueryOdd($limit) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    $query = db_select('test_task', 't');
    $query
      ->fields('t', array('task'))
      ->orderBy('pid');

    // This should result in 4 pages of results.
    $query = $query
      ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
      ->limit($limit);

    $names = $query->execute()->fetchCol();

    return new JsonResponse(array(
      'names' => $names,
    ));
79 80 81
  }

  /**
82 83 84 85 86 87
   * Runs a tablesort query and returns the results.
   *
   * This function does care about the page GET parameter, as set by the
   * simpletest HTTP call.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
88 89
   */
  public function testTablesort() {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    $header = array(
      'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
      'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
      'task' => array('data' => t('Task'), 'field' => 'task'),
      'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
    );

    $query = db_select('test_task', 't');
    $query
      ->fields('t', array('tid', 'pid', 'task', 'priority'));

    $query = $query
      ->extend('Drupal\Core\Database\Query\TableSortExtender')
      ->orderByHeader($header);

    // We need all the results at once to check the sort.
    $tasks = $query->execute()->fetchAll();

    return new JsonResponse(array(
      'tasks' => $tasks,
    ));
111 112 113
  }

  /**
114 115 116 117 118 119
   * Runs a tablesort query with a second order_by after and returns the results.
   *
   * This function does care about the page GET parameter, as set by the
   * simpletest HTTP call.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
120 121
   */
  public function testTablesortFirst() {
122 123 124 125 126 127
    $header = array(
      'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
      'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
      'task' => array('data' => t('Task'), 'field' => 'task'),
      'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
    );
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    $query = db_select('test_task', 't');
    $query
      ->fields('t', array('tid', 'pid', 'task', 'priority'));

    $query = $query
      ->extend('Drupal\Core\Database\Query\TableSortExtender')
      ->orderByHeader($header)
      ->orderBy('priority');

    // We need all the results at once to check the sort.
    $tasks = $query->execute()->fetchAll();

    return new JsonResponse(array(
      'tasks' => $tasks,
    ));
  }
145

146
}