@@ -84,9 +84,9 @@ class ColumnToCode
8484 */
8585 private $ isPk = false ;
8686
87- private $ rawParts = ['type ' => null , 'nullable ' => null , 'default ' => null , 'position ' => null ];
87+ private $ rawParts = ['type ' => null , 'nullable ' => null , 'default ' => null , 'position ' => null , ' comment ' => null ];
8888
89- private $ fluentParts = ['type ' => null , 'nullable ' => null , 'default ' => null , 'position ' => null ];
89+ private $ fluentParts = ['type ' => null , 'nullable ' => null , 'default ' => null , 'position ' => null , ' comment ' => null ];
9090
9191 /**
9292 * @var bool
@@ -150,6 +150,60 @@ public function __construct(
150150 $ this ->resolve ();
151151 }
152152
153+ private function resolve ():void
154+ {
155+ $ dbType = $ this ->typeWithoutSize (strtolower ($ this ->column ->dbType ));
156+ $ type = $ this ->column ->type ;
157+ $ this ->resolvePosition ();
158+ //Primary Keys
159+ if (array_key_exists ($ type , self ::PK_TYPE_MAP )) {
160+ $ this ->rawParts ['type ' ] = $ type ;
161+ $ this ->fluentParts ['type ' ] = self ::PK_TYPE_MAP [$ type ];
162+ $ this ->isPk = true ;
163+ return ;
164+ }
165+ if (array_key_exists ($ dbType , self ::PK_TYPE_MAP )) {
166+ $ this ->rawParts ['type ' ] = $ dbType ;
167+ $ this ->fluentParts ['type ' ] = self ::PK_TYPE_MAP [$ dbType ];
168+ $ this ->isPk = true ;
169+ return ;
170+ }
171+
172+ if ($ dbType === 'varchar ' ) {
173+ $ type = $ dbType = 'string ' ;
174+ }
175+ $ fluentSize = $ this ->column ->size ? '( ' . $ this ->column ->size . ') ' : '() ' ;
176+ $ rawSize = $ this ->column ->size ? '( ' . $ this ->column ->size . ') ' : '' ;
177+ $ this ->rawParts ['nullable ' ] = $ this ->column ->allowNull ? 'NULL ' : 'NOT NULL ' ;
178+ $ this ->fluentParts ['nullable ' ] = $ this ->column ->allowNull === true ? 'null() ' : 'notNull() ' ;
179+
180+ $ this ->fluentParts ['comment ' ] = $ this ->column ->comment ? 'comment( ' .var_export ($ this ->column ->comment , true ).') ' : $ this ->fluentParts ['comment ' ];
181+ $ this ->rawParts ['comment ' ] = $ this ->column ->comment ? 'COMMENT ' .var_export ($ this ->column ->comment , true ) : $ this ->rawParts ['comment ' ];
182+
183+ if (array_key_exists ($ dbType , self ::INT_TYPE_MAP )) {
184+ $ this ->fluentParts ['type ' ] = self ::INT_TYPE_MAP [$ dbType ] . $ fluentSize ;
185+ $ this ->rawParts ['type ' ] =
186+ $ this ->column ->dbType . (strpos ($ this ->column ->dbType , '( ' ) !== false ? '' : $ rawSize );
187+ } elseif (array_key_exists ($ type , self ::INT_TYPE_MAP )) {
188+ $ this ->fluentParts ['type ' ] = self ::INT_TYPE_MAP [$ type ] . $ fluentSize ;
189+ $ this ->rawParts ['type ' ] =
190+ $ this ->column ->dbType . (strpos ($ this ->column ->dbType , '( ' ) !== false ? '' : $ rawSize );
191+ } elseif ($ this ->isEnum ()) {
192+ $ this ->resolveEnumType ();
193+ } elseif ($ this ->isDecimal ()) {
194+ $ this ->fluentParts ['type ' ] = $ this ->column ->dbType ;
195+ $ this ->rawParts ['type ' ] = $ this ->column ->dbType ;
196+ } else {
197+ $ this ->fluentParts ['type ' ] = $ type . $ fluentSize ;
198+ $ this ->rawParts ['type ' ] =
199+ $ this ->column ->dbType . (strpos ($ this ->column ->dbType , '( ' ) !== false ? '' : $ rawSize );
200+ }
201+
202+ $ this ->isBuiltinType = $ this ->raw ? false : $ this ->getIsBuiltinType ($ type , $ dbType );
203+
204+ $ this ->resolveDefaultValue ();
205+ }
206+
153207 public function getCode (bool $ quoted = false ):string
154208 {
155209 if ($ this ->isPk ) {
@@ -160,7 +214,8 @@ public function getCode(bool $quoted = false):string
160214 $ this ->fluentParts ['type ' ],
161215 $ this ->fluentParts ['nullable ' ],
162216 $ this ->fluentParts ['default ' ],
163- $ this ->fluentParts ['position ' ]
217+ $ this ->fluentParts ['position ' ],
218+ $ this ->fluentParts ['comment ' ],
164219 ]);
165220 array_unshift ($ parts , '$this ' );
166221 return implode ('-> ' , array_filter (array_map ('trim ' , $ parts )));
@@ -175,9 +230,12 @@ public function getCode(bool $quoted = false):string
175230 }
176231
177232 $ code = $ this ->rawParts ['type ' ] . ' ' . $ this ->rawParts ['nullable ' ] . $ default ;
178- if ((ApiGenerator::isMysql () || ApiGenerator::isMariaDb ()) && $ this ->rawParts ['position ' ]) {
179- $ code .= ' ' . $ this ->rawParts ['position ' ];
233+
234+ if ((ApiGenerator::isMysql () || ApiGenerator::isMariaDb ())) {
235+ $ code .= $ this ->rawParts ['position ' ] ? ' ' . $ this ->rawParts ['position ' ] : '' ;
236+ $ code .= $ this ->rawParts ['comment ' ] ? ' ' .$ this ->rawParts ['comment ' ] : '' ;
180237 }
238+
181239 if (ApiGenerator::isPostgres () && $ this ->alterByXDbType ) {
182240 return $ quoted ? VarDumper::export ($ this ->rawParts ['type ' ]) : $ this ->rawParts ['type ' ];
183241 }
@@ -320,56 +378,6 @@ private function defaultValueArray(array $value):string
320378 return "'{ " . trim (Json::encode ($ value , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT ), '[] ' ) . "}' " ;
321379 }
322380
323- private function resolve ():void
324- {
325- $ dbType = $ this ->typeWithoutSize (strtolower ($ this ->column ->dbType ));
326- $ type = $ this ->column ->type ;
327- $ this ->resolvePosition ();
328- //Primary Keys
329- if (array_key_exists ($ type , self ::PK_TYPE_MAP )) {
330- $ this ->rawParts ['type ' ] = $ type ;
331- $ this ->fluentParts ['type ' ] = self ::PK_TYPE_MAP [$ type ];
332- $ this ->isPk = true ;
333- return ;
334- }
335- if (array_key_exists ($ dbType , self ::PK_TYPE_MAP )) {
336- $ this ->rawParts ['type ' ] = $ dbType ;
337- $ this ->fluentParts ['type ' ] = self ::PK_TYPE_MAP [$ dbType ];
338- $ this ->isPk = true ;
339- return ;
340- }
341-
342- if ($ dbType === 'varchar ' ) {
343- $ type = $ dbType = 'string ' ;
344- }
345- $ fluentSize = $ this ->column ->size ? '( ' . $ this ->column ->size . ') ' : '() ' ;
346- $ rawSize = $ this ->column ->size ? '( ' . $ this ->column ->size . ') ' : '' ;
347- $ this ->rawParts ['nullable ' ] = $ this ->column ->allowNull ? 'NULL ' : 'NOT NULL ' ;
348- $ this ->fluentParts ['nullable ' ] = $ this ->column ->allowNull === true ? 'null() ' : 'notNull() ' ;
349- if (array_key_exists ($ dbType , self ::INT_TYPE_MAP )) {
350- $ this ->fluentParts ['type ' ] = self ::INT_TYPE_MAP [$ dbType ] . $ fluentSize ;
351- $ this ->rawParts ['type ' ] =
352- $ this ->column ->dbType . (strpos ($ this ->column ->dbType , '( ' ) !== false ? '' : $ rawSize );
353- } elseif (array_key_exists ($ type , self ::INT_TYPE_MAP )) {
354- $ this ->fluentParts ['type ' ] = self ::INT_TYPE_MAP [$ type ] . $ fluentSize ;
355- $ this ->rawParts ['type ' ] =
356- $ this ->column ->dbType . (strpos ($ this ->column ->dbType , '( ' ) !== false ? '' : $ rawSize );
357- } elseif ($ this ->isEnum ()) {
358- $ this ->resolveEnumType ();
359- } elseif ($ this ->isDecimal ()) {
360- $ this ->fluentParts ['type ' ] = $ this ->column ->dbType ;
361- $ this ->rawParts ['type ' ] = $ this ->column ->dbType ;
362- } else {
363- $ this ->fluentParts ['type ' ] = $ type . $ fluentSize ;
364- $ this ->rawParts ['type ' ] =
365- $ this ->column ->dbType . (strpos ($ this ->column ->dbType , '( ' ) !== false ? '' : $ rawSize );
366- }
367-
368- $ this ->isBuiltinType = $ this ->raw ? false : $ this ->getIsBuiltinType ($ type , $ dbType );
369-
370- $ this ->resolveDefaultValue ();
371- }
372-
373381 /**
374382 * @param $type
375383 * @param $dbType
0 commit comments