note 92714 deleted from function.sqlite-query by cmb
| From: | cmb@php.net | Date: | Mon, 02 Nov 2020 15:02:53 +0000 |
| Subject: | note 92714 deleted from function.sqlite-query by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215753@lists.php.net to get a copy of this message | ||
Note Submitter: bruce at causal dot com
----
In follow up to Csaba Gabor's function (see below) that allows for multi-statement queries to
be executed via sqlite_query() this alternative implementation avoids a problem with Csaba's
version (which prevents multi-statement constructs like CREATE TRIGGER from being parsed correctly).
<?php
function sqlite_query_multi ($db, $query) {
//
// sqlite_query() rewritten to support concatenated SQL statements.
//
// This method works around the sqlite_query() deficiency by splitting
// queries into two parts; the first part comprising all the statements
// except the last one (executed via sqlite_exec) and then the last
// statement (executed via sqlite_query).
//
// This allows the result of the last SELECT statement in a multi-statement
// query to be accessed regardless of the SQL statements that came before it.
// It also allows execution of arbitrary multi-statement programs regardless
// whether the result is needed, subject to the following constraint:
//
// This method requires that the last statement in a multi-statement query
// is a valid "stand-alone" SQL statement. If it is not (eg, the "END;" of
// a multi-statement construct such as CREATE TRIGGER) append an additional
// ";" (ie, a NULL statement). Normally this should not be needed because
// almost all the multi-statement constructs I can think of should have a
// stand-alone statement following them (ie, to makes use of the construct).
//
$pattern = '/^(.*;)(.*;)/s';
if ( preg_match($pattern,$query,$match) ) {
// multi-statement query
sqlite_exec($db,$match[1]);
$result = sqlite_query($db,$match[2]);
} else {
// single-statement query
$result = sqlite_query($db,$query);
}
return (@$result);
}
?>