dg / dibi

Dibi - smart database abstraction layer
https://dibiphp.com
Other
487 stars 135 forks source link

count() on \Dibi\Fluent with offset does not work #196

Open paranoiq opened 8 years ago

paranoiq commented 8 years ago

the offset is applied on the outer query instead of on the inner query. Fluent generates SQL code such as:

SELECT COUNT(*) 
FROM ( 
    SELECT * 
    FROM `table` 
) `data` 
LIMIT 25 
OFFSET 25

(with MySqliDriver)

dg commented 8 years ago

Why?

paranoiq commented 8 years ago

i am not sure

query exported from Fluent:

'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]'

where _export() is:

array (14)
0 => "%lmt %ofs" (9)
1 => 25
2 => 25
3 => "SELECT" (6)
4 => "*"
5 => "FROM" (4)
6 => "%n" (2)
7 => "table" (17)
8 => "WHERE" (5)
9 => "active = %b" (11)
10 => FALSE
11 => "ORDER BY" (8)
12 => "%n" (2)
13 => "title" (5)

is in Connection translated into:

"SELECT COUNT(*) FROM (   SELECT * FROM `table` WHERE active = 0 ORDER BY `title` ) `data` LIMIT 25 OFFSET 25"

it doesn't make sense since Translator is cloned when empty :/

dg commented 8 years ago

Hmmm, it is due to c26201c75d9ec30dcb23720a7753efc9f6727c2a.

Workaround is to use 'SELECT COUNT(*) FROM (', $fluent, ') [data]'.

dg commented 8 years ago

Problem is that translator is NOT cloned when you use %ex, see https://github.com/dg/dibi/blob/0cce3b99160d1e7638bcb24370da9b3ba3f8a8c4/src/Dibi/Translator.php#L529.