44
55//----------------------------------------------------------------------------------------------------------------------
66/**
7- * Class MySql
8- *
9- * @package SetBased\ErdConcepts\MySql
7+ * Class for fixing issues in SQL code generated by ERD Concepts with MySQL as target database.
108 */
119class MySqlFix
1210{
@@ -24,20 +22,20 @@ class MySqlFix
2422 *
2523 * @return string
2624 */
27- static public function fixColumnComments ( $ theSourceCode )
25+ static public function fixColumnComments ($ theSourceCode )
2826 {
29- $ source_lines = explode ( "\n" , $ theSourceCode );
27+ $ source_lines = explode ("\n" , $ theSourceCode );
3028
3129 // Map from (table_name,column_name) to line number
32- $ map = array () ;
30+ $ map = [] ;
3331
3432 // Scan the source for column definitions.
3533 $ table_name = null ;
3634 foreach ($ source_lines as $ i => $ line )
3735 {
3836 if ($ table_name )
3937 {
40- if (preg_match ( '/^ `(\w+)`/ ' , $ source_lines [$ i ], $ matches ))
38+ if (preg_match ('/^ `(\w+)`/ ' , $ source_lines [$ i ], $ matches ))
4139 {
4240 $ map [$ table_name ][$ matches [1 ]] = $ i ;
4341 }
@@ -47,19 +45,19 @@ static public function fixColumnComments( $theSourceCode )
4745 }
4846 }
4947
50- if (!$ table_name && preg_match ( '/^CREATE TABLE `(\w+)`/ ' , $ line , $ matches ))
48+ if (!$ table_name && preg_match ('/^CREATE TABLE `(\w+)`/ ' , $ line , $ matches ))
5149 {
5250 $ table_name = $ matches [1 ];
5351 }
5452 }
5553
5654 // Scan the source for comments.
57- $ comments = array () ;
55+ $ comments = [] ;
5856 foreach ($ source_lines as $ i => $ line )
5957 {
60- if (preg_match ( '/^COMMENT ON COLUMN `(\w+)`.`(\w+)`/ ' , $ line , $ matches ))
58+ if (preg_match ('/^COMMENT ON COLUMN `(\w+)`.`(\w+)`/ ' , $ line , $ matches ))
6159 {
62- $ comments [$ matches [1 ]][$ matches [2 ]] = trim ($ source_lines [$ i+ 1 ]);
60+ $ comments [$ matches [1 ]][$ matches [2 ]] = trim ($ source_lines [$ i + 1 ]);
6361 }
6462 }
6563
@@ -68,174 +66,174 @@ static public function fixColumnComments( $theSourceCode )
6866 {
6967 if (!isset ($ map [$ table_name ]))
7068 {
71- throw new \RuntimeException ( sprintf ("Table '%s' is not defined. " , $ table_name ) );
69+ throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ table_name ));
7270 }
7371
7472 foreach ($ columns as $ column_name => $ comment )
7573 {
7674 if (!isset ($ map [$ table_name ][$ column_name ]))
7775 {
78- throw new \RuntimeException ( sprintf ("Column '%s' is not defined in '%s' table statements. " ,
79- $ column_name ,
80- $ table_name ));
76+ throw new \RuntimeException (sprintf ("Column '%s' is not defined in '%s' table statements. " ,
77+ $ column_name ,
78+ $ table_name ));
8179 }
8280
8381 $ line_number = $ map [$ table_name ][$ column_name ];
8482
8583 // Truncate comments longer than 60 characters.
86- if (strlen ( $ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
84+ if (strlen ($ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
8785 {
88- $ comment = trim ( mb_substr ( $ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
86+ $ comment = trim (mb_substr ($ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
8987 }
9088
9189 // Enhance the column definition with comment.
92- $ source_lines [$ line_number ] = mb_substr ( rtrim ($ source_lines [$ line_number ]), 0 , -1 );
93- $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ( $ comment )."', " ;
90+ $ source_lines [$ line_number ] = mb_substr (rtrim ($ source_lines [$ line_number ]), 0 , -1 );
91+ $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."', " ;
9492 }
9593 }
9694
97- $ new_source_code = implode ( "\n" , $ source_lines );
95+ $ new_source_code = implode ("\n" , $ source_lines );
9896
9997 return $ new_source_code ;
10098 }
10199
102100
103101 //--------------------------------------------------------------------------------------------------------------------
104102 /**
105- * Add comments to table definitions based on commented comments.
103+ * Add comments to index definitions based on commented comments.
106104 *
107105 * @param string $theSourceCode The SQL code generated by ERD concepts.
108106 *
109107 * @return string
110108 */
111- static public function fixTableComments ( $ theSourceCode )
109+ static public function fixIndexComments ( $ theSourceCode )
112110 {
113- $ source_lines = explode ( "\n" , $ theSourceCode );
111+ $ source_lines = explode ("\n" , $ theSourceCode );
114112
115113 // Map from (table_name,column_name) to line number
116- $ map = array () ;
114+ $ map = [] ;
117115
118116 // Scan the source for column definitions.
119- $ table_name = null ;
120- $ level = 0 ;
117+ $ index_name = null ;
121118 foreach ($ source_lines as $ i => $ line )
122119 {
123- if ($ table_name )
120+ if (preg_match ( ' /^CREATE INDEX `(\w+)`(\s*\()?/ ' , $ line , $ matches ) )
124121 {
125- if (preg_match ( '/\)|\(/ ' , $ source_lines [$ i ], $ matches ))
126- {
127- if ($ matches [0 ] == '( ' ) $ level =+ 1 ;
128- if ($ matches [0 ] == ') ' ) $ level =- 1 ;
129-
130- if ($ level < 0 )
131- {
132- $ map [$ table_name ] = $ i ;
133- $ table_name = null ;
134- }
135- }
136- }
137-
138- if (!$ table_name && preg_match ( '/^CREATE TABLE `(\w+)`(\s*\()?/ ' , $ line , $ matches ))
139- {
140- $ table_name = $ matches [1 ];
141- if ($ matches [2 ]) $ level = 1 ;
122+ $ map [$ matches [1 ]] = $ i ;
142123 }
143124 }
144125
145126 // Scan the source for comments.
146- $ comments = array () ;
127+ $ comments = [] ;
147128 foreach ($ source_lines as $ i => $ line )
148129 {
149- if (preg_match ( '/^COMMENT ON TABLE `(\w+)`/ ' , $ line , $ matches ))
130+ if (preg_match ('/^COMMENT ON INDEX `(\w+)`/ ' , $ line , $ matches ))
150131 {
151- $ comments [$ matches [1 ]] = trim ($ source_lines [$ i+ 1 ]);
132+ $ comments [$ matches [1 ]] = trim ($ source_lines [$ i + 1 ]);
152133 }
153134 }
154135
155136 // Enhance the column definitions with comments.
156- foreach ($ comments as $ table_name => $ comment )
137+ foreach ($ comments as $ index_name => $ comment )
157138 {
158- if (!isset ($ map [$ table_name ]))
139+ if (!isset ($ map [$ index_name ]))
159140 {
160- throw new \RuntimeException ( sprintf ("Table '%s' is not defined. " , $ table_name ));
141+ throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ index_name ));
161142 }
162143
163- $ line_number = $ map [$ table_name ];
144+ $ line_number = $ map [$ index_name ];
164145
165- // Truncate comments longer than 60 characters.
166- if (strlen ( $ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
167- {
168- $ comment = trim ( mb_substr ( $ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
169- }
146+ // Truncate comments longer than 60 characters.
147+ if (strlen ($ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
148+ {
149+ $ comment = trim (mb_substr ($ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
150+ }
170151
171- // Enhance the column definition with comment.
172- $ source_lines [$ line_number ] = mb_substr ( rtrim ($ source_lines [$ line_number ]), 0 , -1 );
173- $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ( $ comment )."'; " ;
152+ // 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 )."'; " ;
174155 }
175156
176- $ new_source_code = implode ( "\n" , $ source_lines );
157+ $ new_source_code = implode ("\n" , $ source_lines );
177158
178159
179160 return $ new_source_code ;
180161 }
181162
182163 //--------------------------------------------------------------------------------------------------------------------
183164 /**
184- * Add comments to index definitions based on commented comments.
165+ * Add comments to table definitions based on commented comments.
185166 *
186167 * @param string $theSourceCode The SQL code generated by ERD concepts.
187168 *
188169 * @return string
189170 */
190- static public function fixIndexComments ( $ theSourceCode )
171+ static public function fixTableComments ( $ theSourceCode )
191172 {
192- $ source_lines = explode ( "\n" , $ theSourceCode );
173+ $ source_lines = explode ("\n" , $ theSourceCode );
193174
194175 // Map from (table_name,column_name) to line number
195- $ map = array () ;
176+ $ map = [] ;
196177
197178 // Scan the source for column definitions.
198- $ index_name = null ;
179+ $ table_name = null ;
180+ $ level = 0 ;
199181 foreach ($ source_lines as $ i => $ line )
200182 {
201- if (preg_match ( ' /^CREATE INDEX `(\w+)`(\s*\()?/ ' , $ line , $ matches ) )
183+ if ($ table_name )
202184 {
203- $ map [$ matches [1 ]] = $ i ;
185+ if (preg_match ('/\)|\(/ ' , $ source_lines [$ i ], $ matches ))
186+ {
187+ if ($ matches [0 ]=='( ' ) $ level = +1 ;
188+ if ($ matches [0 ]==') ' ) $ level = -1 ;
189+
190+ if ($ level <0 )
191+ {
192+ $ map [$ table_name ] = $ i ;
193+ $ table_name = null ;
194+ }
195+ }
196+ }
197+
198+ if (!$ table_name && preg_match ('/^CREATE TABLE `(\w+)`(\s*\()?/ ' , $ line , $ matches ))
199+ {
200+ $ table_name = $ matches [1 ];
201+ if ($ matches [2 ]) $ level = 1 ;
204202 }
205203 }
206204
207205 // Scan the source for comments.
208- $ comments = array () ;
206+ $ comments = [] ;
209207 foreach ($ source_lines as $ i => $ line )
210208 {
211- if (preg_match ( '/^COMMENT ON INDEX `(\w+)`/ ' , $ line , $ matches ))
209+ if (preg_match ('/^COMMENT ON TABLE `(\w+)`/ ' , $ line , $ matches ))
212210 {
213- $ comments [$ matches [1 ]] = trim ($ source_lines [$ i+ 1 ]);
211+ $ comments [$ matches [1 ]] = trim ($ source_lines [$ i + 1 ]);
214212 }
215213 }
216214
217215 // Enhance the column definitions with comments.
218- foreach ($ comments as $ index_name => $ comment )
216+ foreach ($ comments as $ table_name => $ comment )
219217 {
220- if (!isset ($ map [$ index_name ]))
218+ if (!isset ($ map [$ table_name ]))
221219 {
222- throw new \RuntimeException ( sprintf ("Table '%s' is not defined. " , $ index_name ));
220+ throw new \RuntimeException (sprintf ("Table '%s' is not defined. " , $ table_name ));
223221 }
224222
225- $ line_number = $ map [$ index_name ];
223+ $ line_number = $ map [$ table_name ];
226224
227225 // Truncate comments longer than 60 characters.
228- if (strlen ( $ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
226+ if (strlen ($ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
229227 {
230- $ comment = trim ( mb_substr ( $ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
228+ $ comment = trim (mb_substr ($ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
231229 }
232230
233231 // Enhance the column definition with comment.
234- $ source_lines [$ line_number ] = mb_substr ( rtrim ($ source_lines [$ line_number ]), 0 , -1 );
235- $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ( $ comment )."'; " ;
232+ $ source_lines [$ line_number ] = mb_substr (rtrim ($ source_lines [$ line_number ]), 0 , -1 );
233+ $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ($ comment )."'; " ;
236234 }
237235
238- $ new_source_code = implode ( "\n" , $ source_lines );
236+ $ new_source_code = implode ("\n" , $ source_lines );
239237
240238
241239 return $ new_source_code ;
@@ -249,11 +247,11 @@ static public function fixIndexComments( $theSourceCode )
249247 *
250248 * @return string
251249 */
252- static protected function escapeMysqlString ( $ unescaped_string )
250+ static protected function escapeMysqlString ($ unescaped_string )
253251 {
254252 // We prefer to use mysqli::escape_string but this method requires a connection. Since ERD Concepts generates
255253 // SQL code in UTF-8 and $unescaped_string is not user input (from the evil internet) we can safely use addslashes.
256- return addslashes ( $ unescaped_string );
254+ return addslashes ($ unescaped_string );
257255 }
258256
259257 //--------------------------------------------------------------------------------------------------------------------
0 commit comments