1

I attached a function to the REGEXP keyword in PHP and I discovered that SQLite3Result::fetchArray re-executes the query! What inefficiency!

function _sqliteRegexp($pattern, $string)
{
    echo $pattern . ' - ' . $string . '<br/>';
    return preg_match('/' . $pattern . '/', $string);
}

$handle = new SQLite3($filename);
$handle->createFunction('regexp', '_sqliteRegexp', 2);

// (1)
$result = $handle->query("SELECT * FROM pages WHERE '" . $request_uri . "' REGEXP request LIMIT 1;");

// (2)
$result->fetchArray();

Giving, for example:

(1)
\/ - /admin/settings/1
\/sqlite - /admin/settings/1
\/admin - /admin/settings/1
\/admin\/logout - /admin/settings/1
\/admin\/settings(\/[0-9]+)? - /admin/settings/1

(2)
\/ - /admin/settings/1
\/sqlite - /admin/settings/1
\/admin - /admin/settings/1
\/admin\/logout - /admin/settings/1
\/admin\/settings(\/[0-9]+)? - /admin/settings/1

Both (1) and (2) echo the pattern and string the very same. I highly suspect the query is re-executed, which would be quite redundant. Is this a bug or just me being hastily concluding?

2
  • How many times is it called with LIMIT 3? Commented Oct 19, 2012 at 20:15
  • The LIMIT isn't influential. It merely makes the query stop earlier rather than going through the whole table. I've amended my post with what is echoed. Commented Oct 19, 2012 at 20:53

2 Answers 2

2

A look into the source code shows that query fetches the first row just to determine whether the result is empty, or not. The first call to fetchArray will execute the query again. (So much for efficiency. Or side effects.)

If you are interested in no more than one row, use querySingle instead.

To answer your question: this works as designed. And that design is a bug.

Sign up to request clarification or add additional context in comments.

Comments

0

Check the methode "sqlite_regExp" on Havalite: http://www.havalite.com/?p=98

Works also with LIMIT:

if($rows = sqlite_regExp("SELECT * FROM myTable WHERE preg_match('/sql(lite)*/ui', myRow) LIMIT 1")){
    foreach($rows as $row) echo $row[0];
}

1 Comment

This is completely irrelevant. The problem is not attaching the regex functionality to SQL, but the fact that queries are executed twice. Which I discovered through using a custom regex function..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.