PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Database » SQL_Parser » Bug #4082

MySQL: PRIMARY KEY(id) line breaks

Details

Submitted2005-04-06 15:55 UTC
Fromepte at ruffdogs dot com
Assignedepte
StatusClosed
PackageSQL_Parser
PHP Version4.3.8
Roadmaps(Not assigned)

Comments

[2005-04-06 15:55 UTC] epte at ruffdogs dot com

Description:
------------
I don't know if a "PRIMARY KEY (id)" line is MySQL-specific or not.

Reproduce code:
---------------
$sql = "
CREATE TABLE schedules (
id int(11) NOT NULL auto_increment,
PRIMARY KEY (id)
) TYPE=MyISAM;";

$_SQLParser = new SQL_Parser(NULL, 'MySQL');
$parseRes = $_SQLParser->parse($sql);

Actual result:
--------------
Parse error: Expected a valid type on line 4
PRIMARY KEY (id)
^ found: "KEY"

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

Fixed, assuming that the fix is needed for all dialects. The patch:

Index: Parser.php
===================================================================
--- Parser.php (revision 2690)
+++ Parser.php (working copy)
@@ -500,8 +500,27 @@
// parse field identifier
$this->getTok();
// In this context, field names can be reserved words or function names
- if ($this->token == 'ident' || $this->isFunc() || $this->isReserved()) {
+ if ($this->token == 'primary') {
+ $this->getTok();
+ if ($this->token != 'key') {
+ $this->raiseError('Expected key');
+ }
+ $this->getTok();
+ if ($this->token != '(') {
+ $this->raiseError('Expected (');
+ }
+ $this->getTok();
+ if ($this->token != 'ident') {
+ $this->raiseError('Expected identifier');
+ }
$name = $this->lexer->tokText;
+ if ($this->token != ')') {
+ $this->raiseError('Expected )');
+ }
+ $fields[$name]['constraints'][] = array('type'=>'primary_key', 'value'=>true);
+ continue;
+ } elseif ($this->token == 'ident' || $this->isFunc() || $this->isReserved()) {
+ $name = $this->lexer->tokText;
} elseif ($this->token == ')') {
return $fields;
} else {

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

I did test it before submitting the patch. Oh well...

I missed a getTok(). Here's the patch adding it in:

Index: Parser.php
===================================================================
--- Parser.php (revision 2692)
+++ Parser.php (working copy)
@@ -514,6 +514,7 @@
$this->raiseError('Expected identifier');
}
$name = $this->lexer->tokText;
+ $this->getTok();
if ($this->token != ')') {
$this->raiseError('Expected )');
}