core icon indicating copy to clipboard operation
core copied to clipboard

Add CALC_FOUND_ROWS to query builder

Open lucasceballos opened this issue 12 years ago • 7 comments

I've already added this functionality by adding the following method to Database_Query_Builder_Select class

public function calc_found_rows($value = true)
{
    $this->_calc_found_rows = (bool) $value;

    return $this;
}

It's pretty simple and usefull when you have to paginate for RESTFul applications.

lucasceballos avatar May 22 '13 14:05 lucasceballos

What exactly is this supposed to do? There is absolutely nothing that uses this property, so why should this be part of the DB layer? Can you provide an explaination and a use-case?

WanWizard avatar May 23 '13 14:05 WanWizard

It's a query modifier like 'DISTINCT', It would be usefull to know how many rows would be returned without the where condition/s.

E.g:

$query = DB::select('c.name' , 'c.surname', 'c.age') ->calc_found_rows() ->from(array('customer' , 'c') ->where('c.age', '>', 21);

And then you could call $query->found_rows() which would be a new method too

I'm using it to fill datatables (http://datatables.net/)

lucasceballos avatar May 23 '13 15:05 lucasceballos

I know what CALC_FOUND_ROWS does, but this code snippit just sets a class property, which isn't used anywhere. So this is a but of a nop operation.

Second point is that it's MySQL specific, so it shouldn't belong in a generic driver, unless you intend to implement the actual functionality in the DB drivers, but that is then missing...

WanWizard avatar May 23 '13 18:05 WanWizard

Yes you're right, i forgot to mention the use of the property, sorry about that, inside the compile method you do

if ($this->_calc_found_rows === TRUE) { $query.= 'SQL_CALC_FOUND_ROWS '; }

Fuel supports Mysql, Mysqli and PDO, i guess that all of them will work with this method

lucasceballos avatar May 23 '13 19:05 lucasceballos

Unless you're not using MySQL with PDO that is... I don't like to have anything platform specific in the PDO driver.

WanWizard avatar May 24 '13 09:05 WanWizard

Yes i agree with you, but i'm not suggesting to put this method in the PDO driver, i guess that it could be added to Database_Query_Builder_Select class wich is generic and already have specific MySQL sintax methods like 'LIMIT' wich won't work with MSSQL or Oracle

lucasceballos avatar May 24 '13 13:05 lucasceballos

One of the reasons we're getting rid of this DB layer.

@FrenkyNet is adding this kind of modifiers already supported in FuelPHP/Database ?

WanWizard avatar May 24 '13 21:05 WanWizard