@@ -19,21 +19,17 @@ import (
1919 "bytes"
2020 "context"
2121 "fmt"
22- "io/ioutil"
23-
2422 "github.com/codenotary/immudb/pkg/api/schema"
2523 "github.com/codenotary/immudb/pkg/client"
24+ "github.com/olekukonko/tablewriter"
25+ "strings"
2626)
2727
2828func (i * immuc ) SQLExec (args []string ) (string , error ) {
29- sqlStmt , err := ioutil .ReadAll (bytes .NewReader ([]byte (args [0 ])))
30- if err != nil {
31- return "" , err
32- }
33-
29+ sqlStmt := strings .Join (args , " " )
3430 ctx := context .Background ()
3531 response , err := i .Execute (func (immuClient client.ImmuClient ) (interface {}, error ) {
36- return immuClient .SQLExec (ctx , string ( sqlStmt ) , nil )
32+ return immuClient .SQLExec (ctx , sqlStmt , nil )
3733 })
3834 if err != nil {
3935 return "" , err
@@ -44,47 +40,78 @@ func (i *immuc) SQLExec(args []string) (string, error) {
4440 return fmt .Sprintf ("sql ok, Ctxs: %d Dtxs: %d" , len (txMetas .Ctxs ), len (txMetas .Dtxs )), nil
4541}
4642
47- func (i * immuc ) SQLQuery (args []string ) (* schema.SQLQueryResult , error ) {
48- sqlStmt , err := ioutil .ReadAll (bytes .NewReader ([]byte (args [0 ])))
49- if err != nil {
50- return nil , err
51- }
52-
43+ func (i * immuc ) SQLQuery (args []string ) (string , error ) {
44+ sqlStmt := strings .Join (args , " " )
5345 ctx := context .Background ()
5446 response , err := i .Execute (func (immuClient client.ImmuClient ) (interface {}, error ) {
55- return immuClient .SQLQuery (ctx , string (sqlStmt ), nil , true )
47+ resp , err := immuClient .SQLQuery (ctx , sqlStmt , nil , true )
48+ if err != nil {
49+ return nil , err
50+ }
51+ return renderTableResult (resp ), nil
5652 })
5753 if err != nil {
58- return nil , err
54+ return "" , err
5955 }
6056
61- return response .(* schema. SQLQueryResult ), nil
57+ return response .(string ), nil
6258}
6359
64- func (i * immuc ) ListTables () (* schema. SQLQueryResult , error ) {
60+ func (i * immuc ) ListTables () (string , error ) {
6561 ctx := context .Background ()
6662 response , err := i .Execute (func (immuClient client.ImmuClient ) (interface {}, error ) {
67- return immuClient .ListTables (ctx )
63+ resp , err := immuClient .ListTables (ctx )
64+ if err != nil {
65+ return nil , err
66+ }
67+ return renderTableResult (resp ), nil
6868 })
6969 if err != nil {
70- return nil , err
70+ return "" , err
7171 }
72-
73- return response .(* schema.SQLQueryResult ), nil
72+ return response .(string ), nil
7473}
7574
76- func (i * immuc ) DescribeTable (args []string ) (* schema. SQLQueryResult , error ) {
75+ func (i * immuc ) DescribeTable (args []string ) (string , error ) {
7776 if len (args ) != 1 {
78- return nil , client .ErrIllegalArguments
77+ return "" , client .ErrIllegalArguments
7978 }
80-
8179 ctx := context .Background ()
8280 response , err := i .Execute (func (immuClient client.ImmuClient ) (interface {}, error ) {
83- return immuClient .DescribeTable (ctx , args [0 ])
81+ resp , err := immuClient .DescribeTable (ctx , args [0 ])
82+ if err != nil {
83+ return nil , err
84+ }
85+ return renderTableResult (resp ), nil
8486 })
8587 if err != nil {
86- return nil , err
88+ return "" , err
89+ }
90+ return response .(string ), nil
91+ }
92+
93+ func renderTableResult (resp * schema.SQLQueryResult ) string {
94+ if resp == nil {
95+ return ""
96+ }
97+ result := bytes .NewBuffer ([]byte {})
98+ consoleTable := tablewriter .NewWriter (result )
99+ cols := make ([]string , len (resp .Columns ))
100+ for i , c := range resp .Columns {
101+ cols [i ] = c .Name
102+ }
103+ consoleTable .SetHeader (cols )
104+
105+ for _ , r := range resp .Rows {
106+ row := make ([]string , len (r .Values ))
107+
108+ for i , v := range r .Values {
109+ row [i ] = schema .RenderValue (v .Value )
110+ }
111+
112+ consoleTable .Append (row )
87113 }
88114
89- return response .(* schema.SQLQueryResult ), nil
115+ consoleTable .Render ()
116+ return result .String ()
90117}
0 commit comments