Skip to content

Commit fb5398f

Browse files
committed
chore(cmd/immuclient): add describe, list, exec and query commands to immuclient shell
Signed-off-by: Michele Meloni <[email protected]>
1 parent a947006 commit fb5398f

File tree

5 files changed

+108
-99
lines changed

5 files changed

+108
-99
lines changed

cmd/immuclient/cli/register.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ func (cli *cli) initCommands() {
5757
cli.Register(&command{"status", "", cli.healthCheck, nil, false})
5858
cli.Register(&command{"history", "Fetch history for the item having the specified key", cli.history, []string{"key"}, false})
5959
cli.Register(&command{"version", "Print version", cli.version, nil, false})
60+
61+
// SQL
62+
cli.Register(&command{"exec", "Executes sql statement", cli.sqlExec, []string{"statement"}, false})
63+
cli.Register(&command{"query", "Query sql statement", cli.sqlQuery, []string{"statement"}, false})
64+
cli.Register(&command{"describe", "Describe table", cli.describeTable, []string{"table"}, false})
65+
cli.Register(&command{"tables", "List tables", cli.listTables, nil, false})
6066
}

cmd/immuclient/cli/sql.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2021 CodeNotary, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cli
18+
19+
func (cli *cli) sqlExec(args []string) (string, error) {
20+
return cli.immucl.SQLExec(args)
21+
}
22+
23+
func (cli *cli) sqlQuery(args []string) (string, error) {
24+
return cli.immucl.SQLQuery(args)
25+
}
26+
27+
func (cli *cli) describeTable(args []string) (string, error) {
28+
return cli.immucl.DescribeTable(args)
29+
}
30+
31+
func (cli *cli) listTables(args []string) (string, error) {
32+
return cli.immucl.ListTables()
33+
}
34+
35+
func (cli *cli) useDatabase(args []string) (string, error) {
36+
return cli.immucl.UseDatabase(args)
37+
}

cmd/immuclient/command/sql.go

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
1617
package immuclient
1718

1819
import (
1920
"fmt"
2021

21-
"github.com/codenotary/immudb/pkg/api/schema"
22-
"github.com/olekukonko/tablewriter"
2322
"github.com/spf13/cobra"
2423
)
2524

@@ -55,28 +54,8 @@ func (cl *commandline) sqlQuery(cmd *cobra.Command) {
5554
if err != nil {
5655
cl.quit(err)
5756
}
58-
59-
consoleTable := tablewriter.NewWriter(cmd.OutOrStdout())
60-
61-
cols := make([]string, len(resp.Columns))
62-
for i, c := range resp.Columns {
63-
cols[i] = c.Name
64-
}
65-
consoleTable.SetHeader(cols)
66-
67-
for _, r := range resp.Rows {
68-
row := make([]string, len(r.Values))
69-
70-
for i, v := range r.Values {
71-
row[i] = schema.RenderValue(v.Value)
72-
}
73-
74-
consoleTable.Append(row)
75-
}
76-
77-
consoleTable.Render()
78-
79-
return nil
57+
_, err = fmt.Fprint(cmd.OutOrStdout(), resp)
58+
return err
8059
},
8160
Args: cobra.MinimumNArgs(1),
8261
}
@@ -95,28 +74,8 @@ func (cl *commandline) listTables(cmd *cobra.Command) {
9574
if err != nil {
9675
cl.quit(err)
9776
}
98-
99-
consoleTable := tablewriter.NewWriter(cmd.OutOrStdout())
100-
101-
cols := make([]string, len(resp.Columns))
102-
for i, c := range resp.Columns {
103-
cols[i] = c.Name
104-
}
105-
consoleTable.SetHeader(cols)
106-
107-
for _, r := range resp.Rows {
108-
row := make([]string, len(r.Values))
109-
110-
for i, v := range r.Values {
111-
row[i] = schema.RenderValue(v.Value)
112-
}
113-
114-
consoleTable.Append(row)
115-
}
116-
117-
consoleTable.Render()
118-
119-
return nil
77+
_, err = fmt.Fprint(cmd.OutOrStdout(), resp)
78+
return err
12079
},
12180
Args: cobra.ExactArgs(0),
12281
}
@@ -135,28 +94,9 @@ func (cl *commandline) describeTable(cmd *cobra.Command) {
13594
if err != nil {
13695
cl.quit(err)
13796
}
97+
_, err = fmt.Fprint(cmd.OutOrStdout(), resp)
98+
return err
13899

139-
consoleTable := tablewriter.NewWriter(cmd.OutOrStdout())
140-
141-
cols := make([]string, len(resp.Columns))
142-
for i, c := range resp.Columns {
143-
cols[i] = c.Name
144-
}
145-
consoleTable.SetHeader(cols)
146-
147-
for _, r := range resp.Rows {
148-
row := make([]string, len(r.Values))
149-
150-
for i, v := range r.Values {
151-
row[i] = schema.RenderValue(v.Value)
152-
}
153-
154-
consoleTable.Append(row)
155-
}
156-
157-
consoleTable.Render()
158-
159-
return nil
160100
},
161101
Args: cobra.ExactArgs(1),
162102
}

cmd/immuclient/immuc/init.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222

2323
c "github.com/codenotary/immudb/cmd/helper"
24-
"github.com/codenotary/immudb/pkg/api/schema"
2524
"github.com/codenotary/immudb/pkg/auth"
2625
"github.com/codenotary/immudb/pkg/client"
2726
"github.com/spf13/viper"
@@ -71,9 +70,9 @@ type Client interface {
7170
ValueOnly() bool // TODO: ?
7271
SetValueOnly(v bool) // TODO: ?
7372
SQLExec(args []string) (string, error)
74-
SQLQuery(args []string) (*schema.SQLQueryResult, error)
75-
ListTables() (*schema.SQLQueryResult, error)
76-
DescribeTable(args []string) (*schema.SQLQueryResult, error)
73+
SQLQuery(args []string) (string, error)
74+
ListTables() (string, error)
75+
DescribeTable(args []string) (string, error)
7776
}
7877

7978
// Init ...

cmd/immuclient/immuc/sql.go

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2828
func (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

Comments
 (0)