PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » SQL_Parser » Bug #4084

Ability to handle multiple queries (ala mysqldump)

Details

Request #4084Ability to handle multiple queries (ala mysqldump)
Submitted2005-04-06 16:48 UTC
Fromepte at ruffdogs dot com
Assignedcybot
StatusClosed
PackageSQL_Parser
PHP VersionIrrelevant
Roadmaps0.6

Comments

[2005-04-06 16:48 UTC] epte at ruffdogs dot com

Description:
------------
It would be nice if the parser could handle multiple queries in the same string, putting each query in their own subarray. That way, you could feed the entire output of mysqldump into the parser and get back sane output.

Thoughts?

Expected result:
----------------
Array
(
[0] => Array
(
[command] => create_table
...
)
[1] => Array
(
[command] => insert
...
)
[2] => Array
(
[command] => create_table
...
)
)

[2005-04-06 17:11 UTC] epte at ruffdogs dot com

I went ahead and implemented it. It's backwards-compatible. Here's the patch:

Index: Parser.php
===================================================================
--- Parser.php (revision 2693)
+++ Parser.php (working copy)
@@ -1273,6 +1273,8 @@
// {{{ parse($string)
function parse($string = null)
{
+ $eof = false;
+ $queries = array();
if (is_string($string)) {
// Initialize the Lexer with a 3-level look-back buffer
$this->lexer = new Lexer($string, 3, $this->lexeropts);
@@ -1283,27 +1285,46 @@
}
}

- // get query action
- $this->getTok();
- switch ($this->token) {
- case null:
- // null == end of string
- return $this->raiseError('Nothing to do');
- case 'select':
- return $this->parseSelect();
- case 'update':
- return $this->parseUpdate();
- case 'insert':
- return $this->parseInsert();
- case 'delete':
- return $this->parseDelete();
- case 'create':
- return $this->parseCreate();
- case 'drop':
- return $this->parseDrop();
- default:
- return $this->raiseError('Unknown action :'.$this->token);
+ while (!$eof) {
+ // get query action
+ $this->getTok();
+ switch ($this->token) {
+ case null: case '*end of input*':
+ // null == end of string
+ $eof = true;
+ break;
+ case 'select':
+ $queries[] = $this->parseSelect();
+ break;
+ case 'update':
+ $queries[] = $this->parseUpdate();
+ break;
+ case 'insert':
+ $queries[] = $this->parseInsert();
+ break;
+ case 'delete':
+ $queries[] = $this->parseDelete();
+ break;
+ case 'create':
+ $queries[] = $this->parseCreate();
+ break;
+ case 'drop':
+ $queries[] = $this->parseDrop();
+ break;
+ default:
+ $queries[] = $this->raiseError('Unknown action :'.$this->token);
+ break;
+ }
+
+ do {
+ $this->getTok();
+ } while ($this->token != null && $this->token != ';' && $this->token != '*end of input*');
}
+
+ if (count($queries) == 1) { // For backwards-compatibility
+ return $queries[0];
+ }
+ return $queries;
}
// }}}
}