-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Zend\Db\Sql Allow MySQL to use limit when only offset was provided #5940
Zend\Db\Sql Allow MySQL to use limit when only offset was provided #5940
Conversation
…set was provided.
@ralphschindler it still doesn't work if I do : $table = 'users';
$sampleTable = new TableGateway($table, $adapter, null,new HydratingResultSet());
$select = new Select($table);
$select->offset(10);
echo $select->getSqlString(new \Zend\Db\Adapter\Platform\Mysql); I tried to test it in Zend\Db\Sql\SelectTest.php with $select46 = new Select;
$select46->from('foo')->offset("10");
$sqlPrep46 = 'SELECT "foo".* FROM "foo" LIMIT ? OFFSET ?';
$sqlStr46 = 'SELECT "foo".* FROM "foo" LIMIT \'18446744073709551615\' OFFSET \'10\'';
$params46 = array('limit' => "18446744073709551615", 'offset' => 10);
$internalTests46 = array(
'processSelect' => array(array(array('"foo".*')), '"foo"'),
'processLimit' => array('?'),
'processOffset' => array('?')
); and I got : --- Expected
+++ Actual
@@ @@
-'SELECT "foo".* FROM "foo" LIMIT '18446744073709551615' OFFSET '10''
+'SELECT "foo".* FROM "foo" OFFSET '10'' |
That is correct. You need to use the Zend\Db\Sql\Sql object to get a platform specific SQL string, see the original issue. He is using the SQL object to prepare a statement customized for the mysql platform. So your example would more likely be: use Zend\Db\Sql;
$sql = new Sql\Sql;
$select = $sql->select($table);
$select->offset(10);
echo $sql->getSqlStringForSqlObject($select); |
so, using getSqlString is wrong way ? |
the getSqlString passed the platform too (Zend\Db\Adapter\Platform\PlatformInterface) : echo $select->getSqlString(new \Zend\Db\Adapter\Platform\Mysql); |
echo $select->getSqlString(new \Zend\Db\Adapter\Platform\Mysql); Will produce a SQL string with the proper quoting/adapter abstraction, but not with the proper SQL abstraction applied. For SQL language abstraction, then Zend\Db\Sql\Sql should be used. |
Ok, but I tried to use big offset to test yours in ZendTest/Db/Sql/Platform/Mysql/SelectDecoratorTest.php with : $select2 = new Select;
$select2->from('foo')->limit(5)->offset(10000000000000000000);
$expectedPrepareSql2 = 'SELECT `foo`.* FROM `foo` LIMIT ? OFFSET ?';
$expectedParams2 = array('offset' => 10000000000000000000, 'limit' => 5);
$expectedSql2 = 'SELECT `foo`.* FROM `foo` LIMIT 5 OFFSET 10000000000000000000';
so I think $this->limit and $this->offset should not be cast to (int). |
Ok, we can remove the casting. |
Ok, I've fixed it and added tests. The problem with your above example is that that number you're passing into offset() is actually not an integer, it will be used as a float as it is outside the maximum allowable integer space in PHP. You'll need to pass it as a string, as I've noted in my tests. |
@ralphschindler thank you ;) |
Zend\Db\Sql Allow MySQL to use limit when only offset was provided
@ralphschindler I created PR #5941 to remove cast to (int) in Zend\Db\Sql\Select.php too for offset and limit. |
This is a fix for #5642
And would supersede PR #5643