22//----------------------------------------------------------------------------------------------------------------------
33namespace SetBased \ErdConcepts ;
44
5- //----------------------------------------------------------------------------------------------------------------------
65/**
76 * Class for fixing issues in SQL code generated by ERD Concepts with MySQL as target database.
87 */
@@ -18,24 +17,24 @@ class MySqlFix
1817 /**
1918 * Add comments to column definitions based on commented comments.
2019 *
21- * @param string $theSourceCode The SQL code generated by ERD concepts.
20+ * @param string $source The SQL code generated by ERD concepts.
2221 *
2322 * @return string
2423 */
25- public static function fixColumnComments ($ theSourceCode )
24+ public static function fixColumnComments (string $ source ): string
2625 {
27- $ source_lines = explode ("\n" , $ theSourceCode );
26+ $ lines = explode (PHP_EOL , $ source );
2827
2928 // Map from (table_name,column_name) to line number
3029 $ map = [];
3130
3231 // Scan the source for column definitions.
3332 $ table_name = null ;
34- foreach ($ source_lines as $ i => $ line )
33+ foreach ($ lines as $ i => $ line )
3534 {
3635 if (isset ($ table_name ))
3736 {
38- if (preg_match ('/^ (`?\w+`?)/ ' , $ source_lines [ $ i ] , $ matches ))
37+ if (preg_match ('/^ (`?\w+`?)/ ' , $ line , $ matches ))
3938 {
4039 $ map [$ table_name ][$ matches [1 ]] = $ i ;
4140 }
@@ -53,32 +52,32 @@ public static function fixColumnComments($theSourceCode)
5352
5453 // Scan the source for comments.
5554 $ comments = [];
56- foreach ($ source_lines as $ i => $ line )
55+ foreach ($ lines as $ i => $ line )
5756 {
5857 if (preg_match ('/^COMMENT ON COLUMN (`?\w+`?).(`?\w+`?)/ ' , $ line , $ matches ))
5958 {
60- $ comments [$ matches [1 ]][$ matches [2 ]] = trim ($ source_lines [$ i + 1 ]);
59+ $ comments [$ matches [1 ]][$ matches [2 ]] = trim ($ lines [$ i + 1 ]);
6160 }
6261 }
6362
6463 // Enhance the column definitions with comments.
65- foreach ($ comments as $ table_name => $ columns )
64+ foreach ($ comments as $ tableName => $ columns )
6665 {
67- if (!isset ($ map [$ table_name ]))
66+ if (!isset ($ map [$ tableName ]))
6867 {
69- throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ table_name ));
68+ throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ tableName ));
7069 }
7170
72- foreach ($ columns as $ column_name => $ comment )
71+ foreach ($ columns as $ columnName => $ comment )
7372 {
74- if (!isset ($ map [$ table_name ][$ column_name ]))
73+ if (!isset ($ map [$ tableName ][$ columnName ]))
7574 {
7675 throw new \RuntimeException (sprintf ("Column '%s' is not defined in '%s' table statements. " ,
77- $ column_name ,
78- $ table_name ));
76+ $ columnName ,
77+ $ tableName ));
7978 }
8079
81- $ line_number = $ map [$ table_name ][$ column_name ];
80+ $ line_number = $ map [$ tableName ][$ columnName ];
8281
8382 // Truncate comments longer than 60 characters.
8483 if (strlen ($ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
@@ -87,35 +86,33 @@ public static function fixColumnComments($theSourceCode)
8786 }
8887
8988 // Enhance the column definition with comment.
90- $ source_lines [$ line_number ] = mb_substr (rtrim ($ source_lines [$ line_number ]), 0 , -1 );
91- $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."', " ;
89+ $ lines [$ line_number ] = mb_substr (rtrim ($ lines [$ line_number ]), 0 , -1 );
90+ $ lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."', " ;
9291 }
9392 }
9493
95- $ new_source_code = implode ("\n" , $ source_lines );
96-
97- return $ new_source_code ;
94+ return implode (PHP_EOL , $ lines );
9895 }
9996
10097
10198 //--------------------------------------------------------------------------------------------------------------------
10299 /**
103100 * Add comments to index definitions based on commented comments.
104101 *
105- * @param string $theSourceCode The SQL code generated by ERD concepts.
102+ * @param string $source The SQL code generated by ERD concepts.
106103 *
107104 * @return string
108105 */
109- public static function fixIndexComments ($ theSourceCode )
106+ public static function fixIndexComments (string $ source ): string
110107 {
111- $ source_lines = explode ("\n" , $ theSourceCode );
108+ $ lines = explode (PHP_EOL , $ source );
112109
113110 // Map from (table_name,column_name) to line number
114111 $ map = [];
115112
116113 // Scan the source for column definitions.
117114 $ index_name = null ;
118- foreach ($ source_lines as $ i => $ line )
115+ foreach ($ lines as $ i => $ line )
119116 {
120117 if (preg_match ('/^CREATE INDEX (`?\w+`?)(\s*\()?/ ' , $ line , $ matches ))
121118 {
@@ -125,23 +122,23 @@ public static function fixIndexComments($theSourceCode)
125122
126123 // Scan the source for comments.
127124 $ comments = [];
128- foreach ($ source_lines as $ i => $ line )
125+ foreach ($ lines as $ i => $ line )
129126 {
130127 if (preg_match ('/^COMMENT ON INDEX (`?\w+`?)/ ' , $ line , $ matches ))
131128 {
132- $ comments [$ matches [1 ]] = trim ($ source_lines [$ i + 1 ]);
129+ $ comments [$ matches [1 ]] = trim ($ lines [$ i + 1 ]);
133130 }
134131 }
135132
136133 // Enhance the column definitions with comments.
137- foreach ($ comments as $ index_name => $ comment )
134+ foreach ($ comments as $ indexName => $ comment )
138135 {
139- if (!isset ($ map [$ index_name ]))
136+ if (!isset ($ map [$ indexName ]))
140137 {
141- throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ index_name ));
138+ throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ indexName ));
142139 }
143140
144- $ line_number = $ map [$ index_name ];
141+ $ lineNumber = $ map [$ indexName ];
145142
146143 // Truncate comments longer than 60 characters.
147144 if (strlen ($ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
@@ -150,42 +147,45 @@ public static function fixIndexComments($theSourceCode)
150147 }
151148
152149 // Enhance the column definition with comment.
153- $ source_lines [ $ line_number ] = mb_substr (rtrim ($ source_lines [ $ line_number ]), 0 , -1 );
154- $ source_lines [ $ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."'; " ;
150+ $ lines [ $ lineNumber ] = mb_substr (rtrim ($ lines [ $ lineNumber ]), 0 , -1 );
151+ $ lines [ $ lineNumber ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."'; " ;
155152 }
156153
157- $ new_source_code = implode ("\n" , $ source_lines );
158-
159-
160- return $ new_source_code ;
154+ return implode (PHP_EOL , $ lines );
161155 }
162156
163157 //--------------------------------------------------------------------------------------------------------------------
164158 /**
165159 * Add comments to table definitions based on commented comments.
166160 *
167- * @param string $theSourceCode The SQL code generated by ERD concepts.
161+ * @param string $source The SQL code generated by ERD concepts.
168162 *
169163 * @return string
170164 */
171- public static function fixTableComments ($ theSourceCode )
165+ public static function fixTableComments (string $ source ): string
172166 {
173- $ source_lines = explode ("\n" , $ theSourceCode );
167+ $ lines = explode (PHP_EOL , $ source );
174168
175169 // Map from (table_name,column_name) to line number
176170 $ map = [];
177171
178172 // Scan the source for column definitions.
179173 $ table_name = null ;
180174 $ level = 0 ;
181- foreach ($ source_lines as $ i => $ line )
175+ foreach ($ lines as $ i => $ line )
182176 {
183177 if (isset ($ table_name ))
184178 {
185- if (preg_match ('/\)|\(/ ' , $ source_lines [ $ i ] , $ matches ))
179+ if (preg_match ('/\)|\(/ ' , $ line , $ matches ))
186180 {
187- if ($ matches [0 ]=='( ' ) $ level = +1 ;
188- if ($ matches [0 ]==') ' ) $ level = -1 ;
181+ if ($ matches [0 ]=='( ' )
182+ {
183+ $ level = +1 ;
184+ }
185+ if ($ matches [0 ]==') ' )
186+ {
187+ $ level = -1 ;
188+ }
189189
190190 if ($ level <0 )
191191 {
@@ -198,17 +198,20 @@ public static function fixTableComments($theSourceCode)
198198 if ($ table_name ===null && preg_match ('/^CREATE TABLE (`?\w+`?)(\s*\()?/ ' , $ line , $ matches ))
199199 {
200200 $ table_name = $ matches [1 ];
201- if ($ matches [2 ]) $ level = 1 ;
201+ if ($ matches [2 ])
202+ {
203+ $ level = 1 ;
204+ }
202205 }
203206 }
204207
205208 // Scan the source for comments.
206209 $ comments = [];
207- foreach ($ source_lines as $ i => $ line )
210+ foreach ($ lines as $ i => $ line )
208211 {
209212 if (preg_match ('/^COMMENT ON TABLE (`?\w+`?)/ ' , $ line , $ matches ))
210213 {
211- $ comments [$ matches [1 ]] = trim ($ source_lines [$ i + 1 ]);
214+ $ comments [$ matches [1 ]] = trim ($ lines [$ i + 1 ]);
212215 }
213216 }
214217
@@ -229,29 +232,26 @@ public static function fixTableComments($theSourceCode)
229232 }
230233
231234 // Enhance the column definition with comment.
232- $ source_lines [$ line_number ] = mb_substr (rtrim ($ source_lines [$ line_number ]), 0 , -1 );
233- $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."'; " ;
235+ $ lines [$ line_number ] = mb_substr (rtrim ($ lines [$ line_number ]), 0 , -1 );
236+ $ lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."'; " ;
234237 }
235238
236- $ new_source_code = implode ("\n" , $ source_lines );
237-
238-
239- return $ new_source_code ;
239+ return implode (PHP_EOL , $ lines );
240240 }
241241
242242 //--------------------------------------------------------------------------------------------------------------------
243243 /**
244244 * Escapes special characters in a string for use in an SQL statement.
245245 *
246- * @param string $unescaped_string The string that is to be escaped.
246+ * @param string $string The string that is to be escaped.
247247 *
248248 * @return string
249249 */
250- protected static function escapeMysqlString ($ unescaped_string )
250+ protected static function escapeMysqlString (string $ string ): string
251251 {
252252 // We prefer to use mysqli::escape_string but this method requires a connection. Since ERD Concepts generates
253253 // SQL code in UTF-8 and $unescaped_string is not user input (from the evil internet) we can safely use addslashes.
254- return addslashes ($ unescaped_string );
254+ return addslashes ($ string );
255255 }
256256
257257 //--------------------------------------------------------------------------------------------------------------------
0 commit comments