So I'm working on a class to help me display paged tables of data. I want to let the user specify whatever query they are working on and then I need to make sure the MySQL query (sorry mysql only) has SQL_CALC_FOUND_ROWS in it so that I can easily get a total record count later.
I read the MySQL select syntax here and came up with this code:
if (strpos($this->sql, 'SQL_CALC_FOUND_ROWS') === false) {
// CRIKEY! get a load of this cumbersome expression.
$this->sql = preg_replace('#(^\s*SELECT\s*(ALL|DISTINCT|DISTINCTROW){0,1}\s*(HIGH_PRIORITY){0,1}\s*(STRAIGHT_JOIN){0,1}\s*(SQL_SMALL_RESULT){0,1}\s*(SQL_BIG_RESULT){0,1}\s*(SQL_BUFFER_RESULT){0,1}\s*(SQL_CACHE|SQL_NO_CACHE){0,1}\s*)(.*)(\sFROM\s)#is', '\1SQL_CALC_FOUND_ROWS \9\10', $str);
}
1) Do we think that will always work? Can anyone think of a counterexample that breaks it?
2) Is there a simpler expression that'll work? That one seems like a real pain in the ass.