Skip to content

Commit f7b261d

Browse files
committed
Added support for quoted and not quoted identifiers.
1 parent 9eb3645 commit f7b261d

File tree

10 files changed

+375
-12
lines changed

10 files changed

+375
-12
lines changed

src/MySqlFix.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static function fixColumnComments($theSourceCode)
3535
{
3636
if (isset($table_name))
3737
{
38-
if (preg_match('/^ `(\w+)`/', $source_lines[$i], $matches))
38+
if (preg_match('/^ (`?\w+`?)/', $source_lines[$i], $matches))
3939
{
4040
$map[$table_name][$matches[1]] = $i;
4141
}
@@ -45,7 +45,7 @@ public static function fixColumnComments($theSourceCode)
4545
}
4646
}
4747

48-
if ($table_name===null && preg_match('/^CREATE TABLE `(\w+)`/', $line, $matches))
48+
if ($table_name===null && preg_match('/^CREATE TABLE (`?\w+`?)/', $line, $matches))
4949
{
5050
$table_name = $matches[1];
5151
}
@@ -55,7 +55,7 @@ public static function fixColumnComments($theSourceCode)
5555
$comments = [];
5656
foreach ($source_lines as $i => $line)
5757
{
58-
if (preg_match('/^COMMENT ON COLUMN `(\w+)`.`(\w+)`/', $line, $matches))
58+
if (preg_match('/^COMMENT ON COLUMN (`?\w+`?).(`?\w+`?)/', $line, $matches))
5959
{
6060
$comments[$matches[1]][$matches[2]] = trim($source_lines[$i + 1]);
6161
}
@@ -117,7 +117,7 @@ public static function fixIndexComments($theSourceCode)
117117
$index_name = null;
118118
foreach ($source_lines as $i => $line)
119119
{
120-
if (preg_match('/^CREATE INDEX `(\w+)`(\s*\()?/', $line, $matches))
120+
if (preg_match('/^CREATE INDEX (`?\w+`?)(\s*\()?/', $line, $matches))
121121
{
122122
$map[$matches[1]] = $i;
123123
}
@@ -127,7 +127,7 @@ public static function fixIndexComments($theSourceCode)
127127
$comments = [];
128128
foreach ($source_lines as $i => $line)
129129
{
130-
if (preg_match('/^COMMENT ON INDEX `(\w+)`/', $line, $matches))
130+
if (preg_match('/^COMMENT ON INDEX (`?\w+`?)/', $line, $matches))
131131
{
132132
$comments[$matches[1]] = trim($source_lines[$i + 1]);
133133
}
@@ -195,7 +195,7 @@ public static function fixTableComments($theSourceCode)
195195
}
196196
}
197197

198-
if ($table_name===null && preg_match('/^CREATE TABLE `(\w+)`(\s*\()?/', $line, $matches))
198+
if ($table_name===null && preg_match('/^CREATE TABLE (`?\w+`?)(\s*\()?/', $line, $matches))
199199
{
200200
$table_name = $matches[1];
201201
if ($matches[2]) $level = 1;
@@ -206,7 +206,7 @@ public static function fixTableComments($theSourceCode)
206206
$comments = [];
207207
foreach ($source_lines as $i => $line)
208208
{
209-
if (preg_match('/^COMMENT ON TABLE `(\w+)`/', $line, $matches))
209+
if (preg_match('/^COMMENT ON TABLE (`?\w+`?)/', $line, $matches))
210210
{
211211
$comments[$matches[1]] = trim($source_lines[$i + 1]);
212212
}

test/ErdConceptMySQLTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ public function testColumns1()
1818
$this->assertEquals($expected, $result);
1919
}
2020

21+
//--------------------------------------------------------------------------------------------------------------------
22+
/**
23+
* Test column comments without quoted identifiers.
24+
*/
25+
public function testColumns2()
26+
{
27+
$source = file_get_contents(realpath(__DIR__).'/source/unquoted/php-erd-concepts-create.ddl');
28+
$expected = file_get_contents(realpath(__DIR__).'/template/unquoted/column.ddl');
29+
30+
$result = MySqlFix::fixColumnComments($source);
31+
$this->assertEquals($expected, $result);
32+
}
33+
2134
//--------------------------------------------------------------------------------------------------------------------
2235
/**
2336
* Test index comments with quoted identifiers.
@@ -31,19 +44,45 @@ public function testIndex1()
3144
$this->assertEquals($expected, $result);
3245
}
3346

47+
//--------------------------------------------------------------------------------------------------------------------
48+
/**
49+
* Test index comments without quoted identifiers.
50+
*/
51+
public function testIndex2()
52+
{
53+
$source = file_get_contents(realpath(__DIR__).'/source/unquoted/php-erd-concepts-create.ddl');
54+
$expected = file_get_contents(realpath(__DIR__).'/template/unquoted/index.ddl');
55+
56+
$result = MySqlFix::fixIndexComments($source);
57+
$this->assertEquals($expected, $result);
58+
}
59+
3460
//--------------------------------------------------------------------------------------------------------------------
3561
/**
3662
* Test table comments with quoted identifiers.
3763
*/
3864
public function testTables1()
3965
{
40-
$source = file_get_contents(realpath(__DIR__).'/source/quoted/php-erd-concepts-create.ddl');
66+
$source = file_get_contents(realpath(__DIR__).'/source/quoted/php-erd-concepts-create.ddl');
4167
$expected = file_get_contents(realpath(__DIR__).'/template/quoted/table.ddl');
4268

4369
$result = MySqlFix::fixTableComments($source);
4470
$this->assertEquals($expected, $result);
4571
}
4672

73+
//--------------------------------------------------------------------------------------------------------------------
74+
/**
75+
* Test table comments without quoted identifiers.
76+
*/
77+
public function testTables2()
78+
{
79+
$source = file_get_contents(realpath(__DIR__).'/source/unquoted/php-erd-concepts-create.ddl');
80+
$expected = file_get_contents(realpath(__DIR__).'/template/unquoted/table.ddl');
81+
82+
$result = MySqlFix::fixTableComments($source);
83+
$this->assertEquals($expected, $result);
84+
}
85+
4786
//--------------------------------------------------------------------------------------------------------------------
4887
}
4988

test/source/quoted/php-erd-concepts-create.ddl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*================================================================================*/
1+
/*================================================================================*/
22
/* DDL SCRIPT */
33
/*================================================================================*/
44
/* Title : */
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*================================================================================*/
2+
/* DDL SCRIPT */
3+
/*================================================================================*/
4+
/* Title : */
5+
/* FileName : php-erd-concepts.ecm */
6+
/* Platform : MySQL 5 */
7+
/* Version : Concept */
8+
/* Date : zondag 20 augustus 2017 */
9+
/*================================================================================*/
10+
/*================================================================================*/
11+
/* CREATE TABLES */
12+
/*================================================================================*/
13+
14+
CREATE TABLE BAR1 (
15+
c1 VARCHAR(40) NOT NULL,
16+
c2 VARCHAR(40),
17+
c3 VARCHAR(40),
18+
c4 VARCHAR(40),
19+
c5 VARCHAR(40),
20+
CONSTRAINT PK_BAR1 PRIMARY KEY (c1)
21+
);
22+
23+
/*
24+
COMMENT ON TABLE BAR1
25+
This table is table BAR1.
26+
*/
27+
28+
/*
29+
COMMENT ON COLUMN BAR1.c1
30+
This column 1. Same name as in table FOO1.
31+
*/
32+
33+
/*
34+
COMMENT ON COLUMN BAR1.c2
35+
This column 2. Same name as in table FOO1.
36+
*/
37+
38+
CREATE TABLE FOO1 (
39+
c1 VARCHAR(40) NOT NULL,
40+
c2 VARCHAR(40),
41+
c3 VARCHAR(40),
42+
CONSTRAINT PK_FOO1 PRIMARY KEY (c1)
43+
);
44+
45+
/*
46+
COMMENT ON TABLE FOO1
47+
This is table FOO1.
48+
*/
49+
50+
/*
51+
COMMENT ON COLUMN FOO1.c1
52+
Column 1.
53+
*/
54+
55+
/*
56+
COMMENT ON COLUMN FOO1.c2
57+
Column 2.
58+
*/
59+
60+
/*
61+
COMMENT ON COLUMN FOO1.c3
62+
Column 3.
63+
*/
64+
65+
/*================================================================================*/
66+
/* CREATE INDEXES */
67+
/*================================================================================*/
68+
69+
CREATE INDEX IX_BAR11 ON BAR1 (c2);
70+
71+
/*
72+
COMMENT ON INDEX IX_BAR11
73+
Indexes can have comments to.
74+
*/
75+
76+
CREATE INDEX IX_BAR12 ON BAR1 (c3, c4, c5);
77+
78+
/*
79+
COMMENT ON INDEX IX_BAR12
80+
This is a multi column index.
81+
*/

test/template/quoted/column.ddl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*================================================================================*/
1+
/*================================================================================*/
22
/* DDL SCRIPT */
33
/*================================================================================*/
44
/* Title : */

test/template/quoted/index.ddl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*================================================================================*/
1+
/*================================================================================*/
22
/* DDL SCRIPT */
33
/*================================================================================*/
44
/* Title : */

test/template/quoted/table.ddl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*================================================================================*/
1+
/*================================================================================*/
22
/* DDL SCRIPT */
33
/*================================================================================*/
44
/* Title : */

test/template/unquoted/column.ddl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*================================================================================*/
2+
/* DDL SCRIPT */
3+
/*================================================================================*/
4+
/* Title : */
5+
/* FileName : php-erd-concepts.ecm */
6+
/* Platform : MySQL 5 */
7+
/* Version : Concept */
8+
/* Date : zondag 20 augustus 2017 */
9+
/*================================================================================*/
10+
/*================================================================================*/
11+
/* CREATE TABLES */
12+
/*================================================================================*/
13+
14+
CREATE TABLE BAR1 (
15+
c1 VARCHAR(40) NOT NULL COMMENT 'This column 1. Same name as in table FOO1.',
16+
c2 VARCHAR(40) COMMENT 'This column 2. Same name as in table FOO1.',
17+
c3 VARCHAR(40),
18+
c4 VARCHAR(40),
19+
c5 VARCHAR(40),
20+
CONSTRAINT PK_BAR1 PRIMARY KEY (c1)
21+
);
22+
23+
/*
24+
COMMENT ON TABLE BAR1
25+
This table is table BAR1.
26+
*/
27+
28+
/*
29+
COMMENT ON COLUMN BAR1.c1
30+
This column 1. Same name as in table FOO1.
31+
*/
32+
33+
/*
34+
COMMENT ON COLUMN BAR1.c2
35+
This column 2. Same name as in table FOO1.
36+
*/
37+
38+
CREATE TABLE FOO1 (
39+
c1 VARCHAR(40) NOT NULL COMMENT 'Column 1.',
40+
c2 VARCHAR(40) COMMENT 'Column 2.',
41+
c3 VARCHAR(40) COMMENT 'Column 3.',
42+
CONSTRAINT PK_FOO1 PRIMARY KEY (c1)
43+
);
44+
45+
/*
46+
COMMENT ON TABLE FOO1
47+
This is table FOO1.
48+
*/
49+
50+
/*
51+
COMMENT ON COLUMN FOO1.c1
52+
Column 1.
53+
*/
54+
55+
/*
56+
COMMENT ON COLUMN FOO1.c2
57+
Column 2.
58+
*/
59+
60+
/*
61+
COMMENT ON COLUMN FOO1.c3
62+
Column 3.
63+
*/
64+
65+
/*================================================================================*/
66+
/* CREATE INDEXES */
67+
/*================================================================================*/
68+
69+
CREATE INDEX IX_BAR11 ON BAR1 (c2);
70+
71+
/*
72+
COMMENT ON INDEX IX_BAR11
73+
Indexes can have comments to.
74+
*/
75+
76+
CREATE INDEX IX_BAR12 ON BAR1 (c3, c4, c5);
77+
78+
/*
79+
COMMENT ON INDEX IX_BAR12
80+
This is a multi column index.
81+
*/

test/template/unquoted/index.ddl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*================================================================================*/
2+
/* DDL SCRIPT */
3+
/*================================================================================*/
4+
/* Title : */
5+
/* FileName : php-erd-concepts.ecm */
6+
/* Platform : MySQL 5 */
7+
/* Version : Concept */
8+
/* Date : zondag 20 augustus 2017 */
9+
/*================================================================================*/
10+
/*================================================================================*/
11+
/* CREATE TABLES */
12+
/*================================================================================*/
13+
14+
CREATE TABLE BAR1 (
15+
c1 VARCHAR(40) NOT NULL,
16+
c2 VARCHAR(40),
17+
c3 VARCHAR(40),
18+
c4 VARCHAR(40),
19+
c5 VARCHAR(40),
20+
CONSTRAINT PK_BAR1 PRIMARY KEY (c1)
21+
);
22+
23+
/*
24+
COMMENT ON TABLE BAR1
25+
This table is table BAR1.
26+
*/
27+
28+
/*
29+
COMMENT ON COLUMN BAR1.c1
30+
This column 1. Same name as in table FOO1.
31+
*/
32+
33+
/*
34+
COMMENT ON COLUMN BAR1.c2
35+
This column 2. Same name as in table FOO1.
36+
*/
37+
38+
CREATE TABLE FOO1 (
39+
c1 VARCHAR(40) NOT NULL,
40+
c2 VARCHAR(40),
41+
c3 VARCHAR(40),
42+
CONSTRAINT PK_FOO1 PRIMARY KEY (c1)
43+
);
44+
45+
/*
46+
COMMENT ON TABLE FOO1
47+
This is table FOO1.
48+
*/
49+
50+
/*
51+
COMMENT ON COLUMN FOO1.c1
52+
Column 1.
53+
*/
54+
55+
/*
56+
COMMENT ON COLUMN FOO1.c2
57+
Column 2.
58+
*/
59+
60+
/*
61+
COMMENT ON COLUMN FOO1.c3
62+
Column 3.
63+
*/
64+
65+
/*================================================================================*/
66+
/* CREATE INDEXES */
67+
/*================================================================================*/
68+
69+
CREATE INDEX IX_BAR11 ON BAR1 (c2) COMMENT 'Indexes can have comments to.';
70+
71+
/*
72+
COMMENT ON INDEX IX_BAR11
73+
Indexes can have comments to.
74+
*/
75+
76+
CREATE INDEX IX_BAR12 ON BAR1 (c3, c4, c5) COMMENT 'This is a multi column index.';
77+
78+
/*
79+
COMMENT ON INDEX IX_BAR12
80+
This is a multi column index.
81+
*/

0 commit comments

Comments
 (0)