@@ -26,7 +26,7 @@ class MySqlFix
2626 *
2727 * @return string
2828 */
29- static public function fixComments ( $ theSourceCode )
29+ static public function fixColumnComments ( $ theSourceCode )
3030 {
3131 $ source_lines = explode ( "\n" , $ theSourceCode );
3232
@@ -61,7 +61,7 @@ static public function fixComments( $theSourceCode )
6161 {
6262 if (preg_match ( '/^COMMENT ON COLUMN `(\w+)`.`(\w+)`/ ' , $ line , $ matches ))
6363 {
64- $ comments [$ matches [1 ]][$ matches [2 ]] = $ source_lines [$ i +1 ];
64+ $ comments [$ matches [1 ]][$ matches [2 ]] = trim ( $ source_lines [$ i +1 ]) ;
6565 }
6666 }
6767
@@ -88,7 +88,7 @@ static public function fixComments( $theSourceCode )
8888 }
8989
9090 // Enhance the column definition with comment.
91- $ source_lines [$ line_number ] = mb_substr ( $ source_lines [$ line_number ], 0 , -1 );
91+ $ source_lines [$ line_number ] = mb_substr ( rtrim ( $ source_lines [$ line_number ]) , 0 , -1 );
9292 $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ( $ comment )."', " ;
9393 }
9494 }
@@ -98,6 +98,142 @@ static public function fixComments( $theSourceCode )
9898 return $ new_source_code ;
9999 }
100100
101+
102+ //--------------------------------------------------------------------------------------------------------------------
103+ /**
104+ * Add comments to table definitions based on commented comments.
105+ *
106+ * @param string $theSourceCode The SQL code generated by ERD concepts.
107+ *
108+ * @return string
109+ */
110+ static public function fixTableComments ( $ theSourceCode )
111+ {
112+ $ source_lines = explode ( "\n" , $ theSourceCode );
113+
114+ // Map from (table_name,column_name) to line number
115+ $ map = array ();
116+
117+ // Scan the source for column definitions.
118+ $ table_name = null ;
119+ $ level = 0 ;
120+ foreach ($ source_lines as $ i => $ line )
121+ {
122+ if ($ table_name )
123+ {
124+ if (preg_match ( '/\)|\(/ ' , $ source_lines [$ i ], $ matches ))
125+ {
126+ if ($ matches [0 ] == '( ' ) $ level =+ 1 ;
127+ if ($ matches [0 ] == ') ' ) $ level =- 1 ;
128+
129+ if ($ level < 0 )
130+ {
131+ $ map [$ table_name ] = $ i ;
132+ $ table_name = null ;
133+ }
134+ }
135+ }
136+
137+ if (!$ table_name && preg_match ( '/^CREATE TABLE `(\w+)`(\s*\()?/ ' , $ line , $ matches ))
138+ {
139+ $ table_name = $ matches [1 ];
140+ if ($ matches [2 ]) $ level = 1 ;
141+ }
142+ }
143+
144+ // Scan the source for comments.
145+ $ comments = array ();
146+ foreach ($ source_lines as $ i => $ line )
147+ {
148+ if (preg_match ( '/^COMMENT ON TABLE `(\w+)`/ ' , $ line , $ matches ))
149+ {
150+ $ comments [$ matches [1 ]] = trim ($ source_lines [$ i +1 ]);
151+ }
152+ }
153+
154+ // Enhance the column definitions with comments.
155+ foreach ($ comments as $ table_name => $ comment )
156+ {
157+ if (!isset ($ map [$ table_name ])) Affirm::assertFailed ( "Table '%s' is not defined. " , $ table_name );
158+
159+ $ line_number = $ map [$ table_name ];
160+
161+ // Truncate comments longer than 60 characters.
162+ if (strlen ( $ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
163+ {
164+ $ comment = trim ( mb_substr ( $ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
165+ }
166+
167+ // Enhance the column definition with comment.
168+ $ source_lines [$ line_number ] = mb_substr ( rtrim ($ source_lines [$ line_number ]), 0 , -1 );
169+ $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ( $ comment )."'; " ;
170+ }
171+
172+ $ new_source_code = implode ( "\n" , $ source_lines );
173+
174+
175+ return $ new_source_code ;
176+ }
177+
178+ //--------------------------------------------------------------------------------------------------------------------
179+ /**
180+ * Add comments to index definitions based on commented comments.
181+ *
182+ * @param string $theSourceCode The SQL code generated by ERD concepts.
183+ *
184+ * @return string
185+ */
186+ static public function fixIndexComments ( $ theSourceCode )
187+ {
188+ $ source_lines = explode ( "\n" , $ theSourceCode );
189+
190+ // Map from (table_name,column_name) to line number
191+ $ map = array ();
192+
193+ // Scan the source for column definitions.
194+ $ index_name = null ;
195+ foreach ($ source_lines as $ i => $ line )
196+ {
197+ if (preg_match ( '/^CREATE INDEX `(\w+)`(\s*\()?/ ' , $ line , $ matches ))
198+ {
199+ $ map [$ matches [1 ]] = $ i ;
200+ }
201+ }
202+
203+ // Scan the source for comments.
204+ $ comments = array ();
205+ foreach ($ source_lines as $ i => $ line )
206+ {
207+ if (preg_match ( '/^COMMENT ON INDEX `(\w+)`/ ' , $ line , $ matches ))
208+ {
209+ $ comments [$ matches [1 ]] = trim ($ source_lines [$ i +1 ]);
210+ }
211+ }
212+
213+ // Enhance the column definitions with comments.
214+ foreach ($ comments as $ index_name => $ comment )
215+ {
216+ if (!isset ($ map [$ index_name ])) Affirm::assertFailed ( "Table '%s' is not defined. " , $ index_name );
217+
218+ $ line_number = $ map [$ index_name ];
219+
220+ // Truncate comments longer than 60 characters.
221+ if (strlen ( $ comment )>self ::MAX_COLUMN_COMMENT_LENGTH )
222+ {
223+ $ comment = trim ( mb_substr ( $ comment , 0 , self ::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'... ' ;
224+ }
225+
226+ // Enhance the column definition with comment.
227+ $ source_lines [$ line_number ] = mb_substr ( rtrim ($ source_lines [$ line_number ]), 0 , -1 );
228+ $ source_lines [$ line_number ] .= " COMMENT ' " .self ::escapeMysqlString ( $ comment )."'; " ;
229+ }
230+
231+ $ new_source_code = implode ( "\n" , $ source_lines );
232+
233+
234+ return $ new_source_code ;
235+ }
236+
101237 //--------------------------------------------------------------------------------------------------------------------
102238 /**
103239 * Escapes special characters in a string for use in an SQL statement.
0 commit comments