Skip to content

Commit 4232d06

Browse files
authored
Added badly documented data type alias (#356)
Added a number of badly documented data type aliases which are valid data types in MySQL and map to more familiar ones when a table is created using them. For example `MIDDLEINT` is an alias for `MEDIUMINT`. This is needed if you're going to parse table create query digests from the performance_schema as they sometimes reference the alias types.
1 parent 0780ea5 commit 4232d06

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/PHPSQLParser/processors/ColumnDefinitionProcessor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function process($tokens) {
123123
break 2;
124124

125125
case 'VARCHAR':
126+
case 'VARCHARACTER': // Alias for VARCHAR
126127
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'length' => false);
127128
$prevCategory = 'TEXT';
128129
$currCategory = 'SINGLE_PARAM_PARENTHESIS';
@@ -155,10 +156,15 @@ public function process($tokens) {
155156
case 'TINYBIT':
156157
case 'TINYINT':
157158
case 'SMALLINT':
159+
case 'INT2': // Alias of SMALLINT
158160
case 'MEDIUMINT':
161+
case 'INT3': // Alias of MEDIUMINT
162+
case 'MIDDLEINT': // Alias of MEDIUMINT
159163
case 'INT':
160164
case 'INTEGER':
165+
case 'INT4': // Alias of INT
161166
case 'BIGINT':
167+
case 'INT8': // Alias of BIGINT
162168
case 'BOOL':
163169
case 'BOOLEAN':
164170
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'unsigned' => false,
@@ -181,14 +187,17 @@ public function process($tokens) {
181187
continue 2;
182188

183189
case 'CHAR':
190+
case 'CHARACTER': // Alias for CHAR
184191
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'length' => false);
185192
$currCategory = 'SINGLE_PARAM_PARENTHESIS';
186193
$prevCategory = 'TEXT';
187194
continue 2;
188195

189196
case 'REAL':
190197
case 'DOUBLE':
198+
case 'FLOAT8': // Alias for DOUBLE
191199
case 'FLOAT':
200+
case 'FLOAT4': // Alias for FLOAT
192201
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'unsigned' => false,
193202
'zerofill' => false);
194203
$currCategory = 'TWO_PARAM_PARENTHESIS';

tests/cases/parser/issue355Test.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PHPSQLParser\Test\Parser;
4+
use PHPSQLParser\PHPSQLParser;
5+
use PHPSQLParser\PHPSQLCreator;
6+
7+
class issue355Test extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testIssue322()
10+
{
11+
$sql = "
12+
CREATE TABLE `test_alias` (
13+
`a` INTEGER,
14+
`b` CHARACTER(10),
15+
`c` VARCHARACTER(10),
16+
`d` INT2,
17+
`e` INT3,
18+
`f` INT4,
19+
`g` INT8,
20+
`h` FLOAT4,
21+
`i` FLOAT8,
22+
`j` MIDDLEINT
23+
);
24+
";
25+
$parser = new PHPSQLParser();
26+
$parser->parse($sql, true);
27+
// We expect to see 10 parsed columns
28+
$this->assertEquals(
29+
10,
30+
count($parser->parsed['TABLE']['create-def']['sub_tree'])
31+
);
32+
33+
}
34+
}
35+

0 commit comments

Comments
 (0)