16
16
* specific language governing permissions and limitations
17
17
* under the License.
18
18
*/
19
-
20
19
package org .apache .gravitino .cli .commands ;
21
20
22
- import com .google .common .base .Joiner ;
23
21
import org .apache .gravitino .NameIdentifier ;
24
22
import org .apache .gravitino .cli .CommandContext ;
25
23
import org .apache .gravitino .cli .ErrorMessages ;
26
24
import org .apache .gravitino .exceptions .NoSuchTableException ;
27
25
import org .apache .gravitino .rel .Column ;
26
+ import com .google .common .base .Joiner ;
28
27
29
28
/** Displays the details of a table's columns. */
30
29
public class ListColumns extends TableCommand {
@@ -48,53 +47,57 @@ public ListColumns(
48
47
this .table = table ;
49
48
}
50
49
51
- /** Displays the details of a table's columns. */
52
- @ Override
53
- public void handle () {
54
- Column [] columns = null ;
50
+ /** Displays the details of a table's columns. */
51
+ @ Override
52
+ public void handle () {
53
+ try {
54
+ NameIdentifier name = NameIdentifier .of (schema , table );
55
+ Column [] columns = tableCatalog ().loadTable (name ).columns ();
55
56
56
- try {
57
- NameIdentifier name = NameIdentifier .of (schema , table );
58
- columns = tableCatalog ().loadTable (name ).columns ();
59
- } catch (NoSuchTableException noSuchTableException ) {
60
- exitWithError (
61
- ErrorMessages .UNKNOWN_TABLE + Joiner .on ("." ).join (metalake , catalog , schema , table ));
62
- return ;
63
- } catch (Exception exp ) {
64
- exitWithError (exp .getMessage ());
65
- return ;
66
- }
57
+ if (columns != null && columns .length > 0 ) {
58
+ StringBuilder all = new StringBuilder ();
59
+ boolean hasAutoIncrement = false ;
67
60
68
- // Null / empty check before processing columns
69
- if (columns == null || columns .length == 0 ) {
70
- exitWithError ("No columns found for table: " + table );
71
- return ;
72
- }
61
+ // Check if any column supports auto-increment
62
+ for (Column column : columns ) {
63
+ if (column != null && column .autoIncrement ()) {
64
+ hasAutoIncrement = true ;
65
+ break ;
66
+ }
67
+ }
73
68
74
- StringBuilder all = new StringBuilder ();
75
- all .append ("name,datatype,comment,nullable,auto_increment" ).append (System .lineSeparator ());
69
+ all .append ("name,datatype,comment,nullable" );
70
+ if (hasAutoIncrement ) {
71
+ all .append (",auto_increment" );
72
+ }
73
+ all .append (System .lineSeparator ());
76
74
77
- for (Column column : columns ) {
78
- if (column == null ) continue ; // Skip any unexpected null columns
75
+ for (Column column : columns ) {
76
+ if (column == null ) {
77
+ continue ;
78
+ }
79
79
80
- String name = column . name ();
81
- String dataType = column . dataType () != null ? column .dataType (). simpleString () : "UNKNOWN" ;
82
- String comment = column .comment () != null ? column .comment () : "N/A" ;
83
- String nullable = column .nullable () ? "true" : "false" ;
84
- String autoIncrement = column .autoIncrement () ? "true" : "false" ;
80
+ StringBuilder columnDetails = new StringBuilder ();
81
+ columnDetails . append ( column .name ()). append ( "," ) ;
82
+ columnDetails . append ( column .dataType () != null ? column .dataType (). simpleString () : "UNKNOWN" ). append ( "," ) ;
83
+ columnDetails . append ( column .comment () != null ? column . comment () : "N/A" ). append ( "," ) ;
84
+ columnDetails . append ( column .nullable () ? "true" : "false" ) ;
85
85
86
- all .append (name )
87
- .append ("," )
88
- .append (dataType )
89
- .append ("," )
90
- .append (comment )
91
- .append ("," )
92
- .append (nullable )
93
- .append ("," )
94
- .append (autoIncrement )
95
- .append (System .lineSeparator ());
96
- }
86
+ if (hasAutoIncrement ) {
87
+ columnDetails .append ("," ).append (column .autoIncrement () ? "true" : "" );
88
+ }
97
89
98
- printResults (all .toString ());
99
- }
90
+ all .append (columnDetails ).append (System .lineSeparator ());
91
+ }
92
+
93
+ printResults (all .toString ());
94
+ } else {
95
+ exitWithError ("No columns found for the specified table." );
96
+ }
97
+ } catch (NoSuchTableException noSuchTableException ) {
98
+ exitWithError (ErrorMessages .UNKNOWN_TABLE + " " + Joiner .on ("." ).join (metalake , catalog , schema , table ));
99
+ } catch (Exception exp ) {
100
+ exitWithError ("An error occurred while retrieving column details: " + exp .getMessage ());
101
+ }
102
+ }
100
103
}
0 commit comments