ModifyLimitQueryTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $table2 = new \Doctrine\DBAL\Schema\Table("modify_limit_table2");
  18. $table2->addColumn('test_int', 'integer');
  19. $sm = $this->_conn->getSchemaManager();
  20. $sm->createTable($table);
  21. $sm->createTable($table2);
  22. self::$tableCreated = true;
  23. }
  24. $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('modify_limit_table'));
  25. $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('modify_limit_table2'));
  26. }
  27. public function testModifyLimitQuerySimpleQuery()
  28. {
  29. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  30. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  31. $this->_conn->insert('modify_limit_table', array('test_int' => 3));
  32. $this->_conn->insert('modify_limit_table', array('test_int' => 4));
  33. $sql = "SELECT * FROM modify_limit_table";
  34. $this->assertLimitResult(array(1, 2, 3, 4), $sql, 10, 0);
  35. $this->assertLimitResult(array(1, 2), $sql, 2, 0);
  36. $this->assertLimitResult(array(3, 4), $sql, 2, 2);
  37. }
  38. public function testModifyLimitQueryJoinQuery()
  39. {
  40. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  41. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  42. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  43. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  44. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  45. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  46. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  47. $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";
  48. $this->assertLimitResult(array(1, 1, 1, 2, 2), $sql, 10, 0);
  49. $this->assertLimitResult(array(1, 1, 1), $sql, 3, 0);
  50. $this->assertLimitResult(array(2, 2), $sql, 2, 3);
  51. }
  52. public function testModifyLimitQueryOrderBy()
  53. {
  54. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  55. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  56. $this->_conn->insert('modify_limit_table', array('test_int' => 3));
  57. $this->_conn->insert('modify_limit_table', array('test_int' => 4));
  58. $sql = "SELECT * FROM modify_limit_table ORDER BY test_int DESC";
  59. $this->assertLimitResult(array(4, 3, 2, 1), $sql, 10, 0);
  60. $this->assertLimitResult(array(4, 3), $sql, 2, 0);
  61. $this->assertLimitResult(array(2, 1), $sql, 2, 2);
  62. }
  63. public function testModifyLimitQueryGroupBy()
  64. {
  65. $this->_conn->insert('modify_limit_table', array('test_int' => 1));
  66. $this->_conn->insert('modify_limit_table', array('test_int' => 2));
  67. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  68. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  69. $this->_conn->insert('modify_limit_table2', array('test_int' => 1));
  70. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  71. $this->_conn->insert('modify_limit_table2', array('test_int' => 2));
  72. $sql = "SELECT modify_limit_table.test_int FROM modify_limit_table " .
  73. "INNER JOIN modify_limit_table2 ON modify_limit_table.test_int = modify_limit_table2.test_int ".
  74. "GROUP BY modify_limit_table.test_int";
  75. $this->assertLimitResult(array(1, 2), $sql, 10, 0);
  76. $this->assertLimitResult(array(1), $sql, 1, 0);
  77. $this->assertLimitResult(array(2), $sql, 1, 1);
  78. }
  79. public function assertLimitResult($expectedResults, $sql, $limit, $offset)
  80. {
  81. $p = $this->_conn->getDatabasePlatform();
  82. $data = array();
  83. foreach ($this->_conn->fetchAll($p->modifyLimitQuery($sql, $limit, $offset)) AS $row) {
  84. $data[] = current($row);
  85. }
  86. $this->assertEquals($expectedResults, $data);
  87. }
  88. }