Skip to content

Commit e570a83

Browse files
committed
Not all column definitions ends with a comma.
1 parent 980117c commit e570a83

File tree

12 files changed

+351
-19
lines changed

12 files changed

+351
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
/phpunit.xml
88
/test/coverage.xml
99
/test/report/
10+
/test/result/
1011
/vendor/

src/MySqlFix.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MySqlFix
1515

1616
//--------------------------------------------------------------------------------------------------------------------
1717
/**
18-
* Add comments to column definitions based on commented comments.
18+
* Add comments to column definitions based on commented column comments.
1919
*
2020
* @param string $source The SQL code generated by ERD concepts.
2121
*
@@ -34,7 +34,7 @@ public static function fixColumnComments(string $source): string
3434
{
3535
if (isset($table_name))
3636
{
37-
if (preg_match('/^ (`?\w+`?)/', $line, $matches))
37+
if (preg_match('/^ {2}(`?\w+`?)/', $line, $matches))
3838
{
3939
$map[$table_name][$matches[1]] = $i;
4040
}
@@ -77,7 +77,7 @@ public static function fixColumnComments(string $source): string
7777
$tableName));
7878
}
7979

80-
$line_number = $map[$tableName][$columnName];
80+
$lineNumber = $map[$tableName][$columnName];
8181

8282
// Truncate comments longer than 60 characters.
8383
if (strlen($comment)>self::MAX_COLUMN_COMMENT_LENGTH)
@@ -86,8 +86,17 @@ public static function fixColumnComments(string $source): string
8686
}
8787

8888
// Enhance the column definition with comment.
89-
$lines[$line_number] = mb_substr(rtrim($lines[$line_number]), 0, -1);
90-
$lines[$line_number] .= " COMMENT '".self::escapeMysqlString($comment)."',";
89+
$lines[$lineNumber] = rtrim($lines[$lineNumber]);
90+
if (str_ends_with($lines[$lineNumber], ','))
91+
{
92+
$lines[$lineNumber] = mb_substr($lines[$lineNumber], 0, -1);
93+
$last = ',';
94+
}
95+
else
96+
{
97+
$last = '';
98+
}
99+
$lines[$lineNumber] .= " COMMENT '".self::escapeMysqlString($comment)."'".$last;
91100
}
92101
}
93102

@@ -97,7 +106,7 @@ public static function fixColumnComments(string $source): string
97106

98107
//--------------------------------------------------------------------------------------------------------------------
99108
/**
100-
* Add comments to index definitions based on commented comments.
109+
* Add comments to index definitions based on commented index comments.
101110
*
102111
* @param string $source The SQL code generated by ERD concepts.
103112
*
@@ -156,7 +165,7 @@ public static function fixIndexComments(string $source): string
156165

157166
//--------------------------------------------------------------------------------------------------------------------
158167
/**
159-
* Add comments to table definitions based on commented comments.
168+
* Add comments to table definitions based on commented table comments.
160169
*
161170
* @param string $source The SQL code generated by ERD concepts.
162171
*
@@ -176,7 +185,7 @@ public static function fixTableComments(string $source): string
176185
{
177186
if (isset($table_name))
178187
{
179-
if (preg_match('/\)|\(/', $line, $matches))
188+
if (preg_match('/[)(]/', $line, $matches))
180189
{
181190
if ($matches[0]=='(')
182191
{
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,25 @@
88
/**
99
* Unit test for class MySqlFix.
1010
*/
11-
class ErdConceptMySQLTest extends TestCase
11+
class MySqlFixTest extends TestCase
1212
{
13+
//--------------------------------------------------------------------------------------------------------------------
14+
/**
15+
Creates empty directories for storing results.
16+
*/
17+
public static function setUpBeforeClass(): void
18+
{
19+
$dirs = [realpath(__DIR__).'/result/quoted/',
20+
realpath(__DIR__).'/result/unquoted/'];
21+
foreach ($dirs as $dir)
22+
{
23+
if (!is_dir($dir))
24+
{
25+
mkdir($dir, recursive: true);
26+
}
27+
}
28+
}
29+
1330
//--------------------------------------------------------------------------------------------------------------------
1431
/**
1532
* Test column comments with quoted identifiers.
@@ -20,6 +37,7 @@ public function testColumns1()
2037
$expected = file_get_contents(realpath(__DIR__).'/template/quoted/column.ddl');
2138

2239
$result = MySqlFix::fixColumnComments($source);
40+
file_put_contents(realpath(__DIR__).'/result/quoted/column.ddl', $result);
2341
self::assertEquals($expected, $result);
2442
}
2543

@@ -33,6 +51,7 @@ public function testColumns2()
3351
$expected = file_get_contents(realpath(__DIR__).'/template/unquoted/column.ddl');
3452

3553
$result = MySqlFix::fixColumnComments($source);
54+
file_put_contents(realpath(__DIR__).'/result/unquoted/column.ddl', $result);
3655
self::assertEquals($expected, $result);
3756
}
3857

@@ -46,6 +65,7 @@ public function testIndex1()
4665
$expected = file_get_contents(realpath(__DIR__).'/template/quoted/index.ddl');
4766

4867
$result = MySqlFix::fixIndexComments($source);
68+
file_put_contents(realpath(__DIR__).'/result/quoted/index.ddl', $result);
4969
self::assertEquals($expected, $result);
5070
}
5171

@@ -59,6 +79,7 @@ public function testIndex2()
5979
$expected = file_get_contents(realpath(__DIR__).'/template/unquoted/index.ddl');
6080

6181
$result = MySqlFix::fixIndexComments($source);
82+
file_put_contents(realpath(__DIR__).'/result/unquoted/index.ddl', $result);
6283
self::assertEquals($expected, $result);
6384
}
6485

@@ -72,6 +93,7 @@ public function testTables1()
7293
$expected = file_get_contents(realpath(__DIR__).'/template/quoted/table.ddl');
7394

7495
$result = MySqlFix::fixTableComments($source);
96+
file_put_contents(realpath(__DIR__).'/result/quoted/table.ddl', $result);
7597
self::assertEquals($expected, $result);
7698
}
7799

@@ -85,6 +107,7 @@ public function testTables2()
85107
$expected = file_get_contents(realpath(__DIR__).'/template/unquoted/table.ddl');
86108

87109
$result = MySqlFix::fixTableComments($source);
110+
file_put_contents(realpath(__DIR__).'/result/unquoted/table.ddl', $result);
88111
self::assertEquals($expected, $result);
89112
}
90113

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<COPYRIGHT></COPYRIGHT>
99
<VERSION>Concept</VERSION>
1010
<CREATED>2015-01-03</CREATED>
11-
<MODIFIED>2015-02-27</MODIFIED>
11+
<MODIFIED>2023-03-19</MODIFIED>
1212
<COMMENTS></COMMENTS>
1313
<DEFAULTPAGEID>0</DEFAULTPAGEID>
1414
<DEFAULT_SCHEMA></DEFAULT_SCHEMA>
@@ -1588,6 +1588,68 @@
15881588
<STORAGE></STORAGE>
15891589
<VARIABLES></VARIABLES>
15901590
</DATA>
1591+
<DATA>
1592+
<ID>{23C9205E-80F7-4D3D-9EF8-AAC10B517D83}</ID>
1593+
<NAME>FOO2</NAME>
1594+
<COMMENT>This is table FOO2. Same as FOO1, but without a primary key.</COMMENT>
1595+
<COLUMNS>
1596+
<COLUMN>
1597+
<OID>{AC91BFC9-FA9C-4BA3-ADB5-8FCAE83404DE}</OID>
1598+
<NAME>c1</NAME>
1599+
<TYPE>VARCHAR</TYPE>
1600+
<FULLTYPE>VARCHAR(40)</FULLTYPE>
1601+
<PROPERTIES>
1602+
<CHARACTERSET></CHARACTERSET>
1603+
<CHECK></CHECK>
1604+
<COLLATE></COLLATE>
1605+
<DEFAULT></DEFAULT>
1606+
<LENGTH>40</LENGTH>
1607+
</PROPERTIES>
1608+
<DESCRIPTION>Column 1.</DESCRIPTION>
1609+
<PRIMARYKEY>0</PRIMARYKEY>
1610+
<REQUIRED>0</REQUIRED>
1611+
<AUTOINCREMENT>0</AUTOINCREMENT>
1612+
</COLUMN>
1613+
<COLUMN>
1614+
<OID>{4CFCBBED-85DA-448A-B50D-F0E15C16127C}</OID>
1615+
<NAME>c2</NAME>
1616+
<TYPE>VARCHAR</TYPE>
1617+
<FULLTYPE>VARCHAR(40)</FULLTYPE>
1618+
<PROPERTIES>
1619+
<CHARACTERSET></CHARACTERSET>
1620+
<CHECK></CHECK>
1621+
<COLLATE></COLLATE>
1622+
<DEFAULT></DEFAULT>
1623+
<LENGTH>40</LENGTH>
1624+
</PROPERTIES>
1625+
<DESCRIPTION>Column 2.</DESCRIPTION>
1626+
<PRIMARYKEY>0</PRIMARYKEY>
1627+
<REQUIRED>0</REQUIRED>
1628+
<AUTOINCREMENT>0</AUTOINCREMENT>
1629+
</COLUMN>
1630+
<COLUMN>
1631+
<OID>{9A549FAE-0F6E-4AE9-A470-3D631DC316B1}</OID>
1632+
<NAME>c3</NAME>
1633+
<TYPE>VARCHAR</TYPE>
1634+
<FULLTYPE>VARCHAR(40)</FULLTYPE>
1635+
<PROPERTIES>
1636+
<CHARACTERSET></CHARACTERSET>
1637+
<CHECK></CHECK>
1638+
<COLLATE></COLLATE>
1639+
<DEFAULT></DEFAULT>
1640+
<LENGTH>40</LENGTH>
1641+
</PROPERTIES>
1642+
<DESCRIPTION>Column 3.</DESCRIPTION>
1643+
<PRIMARYKEY>0</PRIMARYKEY>
1644+
<REQUIRED>0</REQUIRED>
1645+
<AUTOINCREMENT>0</AUTOINCREMENT>
1646+
</COLUMN>
1647+
</COLUMNS>
1648+
<BC_SCRIPT></BC_SCRIPT>
1649+
<AC_SCRIPT></AC_SCRIPT>
1650+
<STORAGE></STORAGE>
1651+
<VARIABLES></VARIABLES>
1652+
</DATA>
15911653
</TABLEDATA>
15921654
<PAGES>
15931655
<PAGE>
@@ -1630,7 +1692,7 @@
16301692
<TABLE>
16311693
<OID>{C8ACA941-3953-4662-B775-071A49D6B4D7}</OID>
16321694
<X>380</X>
1633-
<Y>380</Y>
1695+
<Y>460</Y>
16341696
<WIDTH>222</WIDTH>
16351697
<HEIGHT>208</HEIGHT>
16361698
<GEN_DDL>1</GEN_DDL>
@@ -1652,6 +1714,31 @@
16521714
<FKTEXTSTYLE>U</FKTEXTSTYLE>
16531715
<IXTEXTSTYLE>BI</IXTEXTSTYLE>
16541716
</TABLE>
1717+
<TABLE>
1718+
<OID>{23C9205E-80F7-4D3D-9EF8-AAC10B517D83}</OID>
1719+
<X>380</X>
1720+
<Y>320</Y>
1721+
<WIDTH>185</WIDTH>
1722+
<HEIGHT>98</HEIGHT>
1723+
<GEN_DDL>1</GEN_DDL>
1724+
<FONTDATA></FONTDATA>
1725+
<BACKCOLOR>255,255,255</BACKCOLOR>
1726+
<DIVCOLOR>128,128,128</DIVCOLOR>
1727+
<CAPTEXTCOLOR>128,0,0</CAPTEXTCOLOR>
1728+
<CAPBACKCOLOR>255,255,255</CAPBACKCOLOR>
1729+
<CAPGRADIENTCOLOR>255,255,255</CAPGRADIENTCOLOR>
1730+
<CAPGRADIENT>0</CAPGRADIENT>
1731+
<CAPALIGNMENT>0</CAPALIGNMENT>
1732+
<BORDERCOLOR>0,0,0</BORDERCOLOR>
1733+
<CLTEXTCOLOR>0,0,0</CLTEXTCOLOR>
1734+
<PKTEXTCOLOR>0,0,0</PKTEXTCOLOR>
1735+
<FKTEXTCOLOR>0,128,0</FKTEXTCOLOR>
1736+
<IXTEXTCOLOR>0,0,0</IXTEXTCOLOR>
1737+
<HDTEXTSTYLE>B</HDTEXTSTYLE>
1738+
<PKTEXTSTYLE></PKTEXTSTYLE>
1739+
<FKTEXTSTYLE>U</FKTEXTSTYLE>
1740+
<IXTEXTSTYLE>BI</IXTEXTSTYLE>
1741+
</TABLE>
16551742
</PAGE>
16561743
</PAGES>
16571744
</ERDCONCEPTS>

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* FileName : php-erd-concepts.ecm */
66
/* Platform : MySQL 5 */
77
/* Version : Concept */
8-
/* Date : vrijdag 27 februari 2015 */
8+
/* Date : Sunday, March 19, 2023 */
99
/*================================================================================*/
1010
/*================================================================================*/
1111
/* CREATE TABLES */
@@ -62,6 +62,32 @@ COMMENT ON COLUMN `FOO1`.`c3`
6262
Column 3.
6363
*/
6464

65+
CREATE TABLE `FOO2` (
66+
`c1` VARCHAR(40),
67+
`c2` VARCHAR(40),
68+
`c3` VARCHAR(40)
69+
);
70+
71+
/*
72+
COMMENT ON TABLE `FOO2`
73+
This is table FOO2. Same as FOO1, but without a primary key.
74+
*/
75+
76+
/*
77+
COMMENT ON COLUMN `FOO2`.`c1`
78+
Column 1.
79+
*/
80+
81+
/*
82+
COMMENT ON COLUMN `FOO2`.`c2`
83+
Column 2.
84+
*/
85+
86+
/*
87+
COMMENT ON COLUMN `FOO2`.`c3`
88+
Column 3.
89+
*/
90+
6591
/*================================================================================*/
6692
/* CREATE INDEXES */
6793
/*================================================================================*/

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* FileName : php-erd-concepts.ecm */
66
/* Platform : MySQL 5 */
77
/* Version : Concept */
8-
/* Date : zondag 20 augustus 2017 */
8+
/* Date : Sunday, March 19, 2023 */
99
/*================================================================================*/
1010
/*================================================================================*/
1111
/* CREATE TABLES */
@@ -62,6 +62,32 @@ COMMENT ON COLUMN FOO1.c3
6262
Column 3.
6363
*/
6464

65+
CREATE TABLE FOO2 (
66+
c1 VARCHAR(40),
67+
c2 VARCHAR(40),
68+
c3 VARCHAR(40)
69+
);
70+
71+
/*
72+
COMMENT ON TABLE FOO2
73+
This is table FOO2. Same as FOO1, but without a primary key.
74+
*/
75+
76+
/*
77+
COMMENT ON COLUMN FOO2.c1
78+
Column 1.
79+
*/
80+
81+
/*
82+
COMMENT ON COLUMN FOO2.c2
83+
Column 2.
84+
*/
85+
86+
/*
87+
COMMENT ON COLUMN FOO2.c3
88+
Column 3.
89+
*/
90+
6591
/*================================================================================*/
6692
/* CREATE INDEXES */
6793
/*================================================================================*/
@@ -79,3 +105,4 @@ CREATE INDEX IX_BAR12 ON BAR1 (c3, c4, c5);
79105
COMMENT ON INDEX IX_BAR12
80106
This is a multi column index.
81107
*/
108+

0 commit comments

Comments
 (0)