ModifyLimitQueryTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\DBAL\Types\Type;
  4. use Doctrine\DBAL\Connection;
  5. use PDO;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. class ModifyLimitQueryTest extends \Doctrine\Tests\DbalFunctionalTestCase
  8. {
  9. private static $tableCreated = false;
  10. public function setUp()
  11. {
  12. parent::setUp();
  13. if (!self::$tableCreated) {
  14. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  15. $table = new \Doctrine\DBAL\Schema\Table("modify_limit_table");
  16. $table->addColumn('test_int', 'integer');
  17. $table->setPrimaryKey(array('test_int'));
  18. $table2 = new \Doctrine\DBAL\Schema\Table("modify_limit_table2");
  19. $table2->addColumn('id', 'integer', array('autoincrement' => true));
  20. $table2->addColumn('test_int', 'integer');
  21. $table2->setPrimaryKey(array('id'));
  22. $sm = $this->_conn->getSchemaManager();
  23. $sm->createTable($table);
  24. $sm->createTable($table2);
  25. self::$tableCreated = true;
  26. }
  27. $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('modify_limit_table'));
  28. $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('modify_limit_table2'));
  29. }
  30. public function testModifyLimitQuerySimpleQuery()
  31. {
  32. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  33. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  34. $this->_conn->insert('modify_limit_table', array('test_int' => 3));
  35. $this->_conn->insert('modify_limit_table', array('test_int' => 4));
  36. $sql = "SELECT * FROM modify_limit_table";
  37. $this->assertLimitResult(array(1, 2, 3, 4), $sql, 10, 0);
  38. $this->assertLimitResult(array(1, 2), $sql, 2, 0);
  39. $this->assertLimitResult(array(3, 4), $sql, 2, 2);
  40. }
  41. public function testModifyLimitQueryJoinQuery()
  42. {
  43. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  44. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  45. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  46. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  47. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  48. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  49. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  50. $sql = "SELECT modify_limit_table.test_int FROM modify_limit_table INNER JOIN modify_limit_table2 ON modify_limit_table.test_int = modify_limit_table2.test_int";
  51. $this->assertLimitResult(array(1, 1, 1, 2, 2), $sql, 10, 0);
  52. $this->assertLimitResult(array(1, 1, 1), $sql, 3, 0);
  53. $this->assertLimitResult(array(2, 2), $sql, 2, 3);
  54. }
  55. public function testModifyLimitQueryOrderBy()
  56. {
  57. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  58. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  59. $this->_conn->insert('modify_limit_table', array('test_int' => 3));
  60. $this->_conn->insert('modify_limit_table', array('test_int' => 4));
  61. $sql = "SELECT * FROM modify_limit_table ORDER BY test_int DESC";
  62. $this->assertLimitResult(array(4, 3, 2, 1), $sql, 10, 0);
  63. $this->assertLimitResult(array(4, 3), $sql, 2, 0);
  64. $this->assertLimitResult(array(2, 1), $sql, 2, 2);
  65. }
  66. public function testModifyLimitQueryGroupBy()
  67. {
  68. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  69. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  70. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  71. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  72. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  73. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  74. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  75. $sql = "SELECT modify_limit_table.test_int FROM modify_limit_table " .
  76. "INNER JOIN modify_limit_table2 ON modify_limit_table.test_int = modify_limit_table2.test_int ".
  77. "GROUP BY modify_limit_table.test_int";
  78. $this->assertLimitResult(array(1, 2), $sql, 10, 0);
  79. $this->assertLimitResult(array(1), $sql, 1, 0);
  80. $this->assertLimitResult(array(2), $sql, 1, 1);
  81. }
  82. public function assertLimitResult($expectedResults, $sql, $limit, $offset)
  83. {
  84. $p = $this->_conn->getDatabasePlatform();
  85. $data = array();
  86. foreach ($this->_conn->fetchAll($p->modifyLimitQuery($sql, $limit, $offset)) AS $row) {
  87. $row = array_change_key_case($row, CASE_LOWER);
  88. $data[] = $row['test_int'];
  89. }
  90. $this->assertEquals($expectedResults, $data);
  91. }
  92. }