@@ -2,6 +2,7 @@ package cli
22
33import (
44 "bufio"
5+ "fmt"
56 "io"
67 "os"
78 "os/exec"
@@ -39,6 +40,7 @@ var _ metaCommand = (*metaBang)(nil)
3940var _ metaCommand = (* metaBorder )(nil )
4041var _ metaCommand = (* metaChangeDirectory )(nil )
4142var _ metaCommand = (* metaConnect )(nil )
43+ var _ metaCommand = (* metaDescribe )(nil )
4244var _ metaCommand = (* metaEcho )(nil )
4345var _ metaCommand = (* metaExpanded )(nil )
4446var _ metaCommand = (* metaFile )(nil )
@@ -363,6 +365,7 @@ Input/Output
363365
364366Informational
365367 \d list tables
368+ \d NAME describe table
366369 \dt list tables
367370 \dv list views
368371 \l[ist] list databases
@@ -375,6 +378,7 @@ Formatting
375378
376379Connection
377380 \c[onnect] [DBNAME] connect to new database
381+ disconnect by sending DBNAME "-"
378382 \org [ORGNAME] set organization id
379383
380384Operating System
@@ -484,7 +488,44 @@ func (m *metaListDatabases) execute(cmd *Command) (responseAction, error) {
484488}
485489
486490// ////////////////////////////////////////////////////////////////////////////
487- // list tables (d or dt)
491+ // describe (d)
492+ // ////////////////////////////////////////////////////////////////////////////
493+ type metaDescribe struct {
494+ args []string
495+ }
496+
497+ func newMetaDescribe (args []string ) * metaDescribe {
498+ return & metaDescribe {
499+ args : args ,
500+ }
501+ }
502+
503+ func (m * metaDescribe ) execute (cmd * Command ) (responseAction , error ) {
504+ switch len (m .args ) {
505+ case 0 :
506+ // Describe with no args should list all relations (tables, views,
507+ // etc.). For now, we're just going to list the tables.
508+ return newMetaListTables ().execute (cmd )
509+
510+ case 1 :
511+ // Describe with a single arg will assume the arg is a table name, so it
512+ // runs a `SHOW COLUMNS` for that table.
513+ qry := []queryPart {
514+ newPartRaw (fmt .Sprintf (`SHOW COLUMNS FROM "%s"` , m .args [0 ])),
515+ }
516+
517+ if err := cmd .executeAndWriteQuery (qry ); err != nil {
518+ return actionNone , errors .Wrap (err , "executing query" )
519+ }
520+
521+ return actionReset , nil
522+ default :
523+ return actionNone , errors .Errorf ("meta command 'describe' takes zero or one argument" )
524+ }
525+ }
526+
527+ // ////////////////////////////////////////////////////////////////////////////
528+ // describe (dt)
488529// ////////////////////////////////////////////////////////////////////////////
489530type metaListTables struct {}
490531
@@ -505,7 +546,7 @@ func (m *metaListTables) execute(cmd *Command) (responseAction, error) {
505546}
506547
507548// ////////////////////////////////////////////////////////////////////////////
508- // list views (dv)
549+ // describe views (dv)
509550// ////////////////////////////////////////////////////////////////////////////
510551type metaListViews struct {}
511552
@@ -1064,7 +1105,9 @@ func splitMetaCommand(in string, replacer *replacer) (metaCommand, error) {
10641105 return newMetaChangeDirectory (args ), nil
10651106 case "c" , "connect" :
10661107 return newMetaConnect (args ), nil
1067- case "d" , "dt" :
1108+ case "d" :
1109+ return newMetaDescribe (args ), nil
1110+ case "dt" :
10681111 return newMetaListTables (), nil
10691112 case "dv" :
10701113 return newMetaListViews (), nil
0 commit comments