2020-08-06 20:58:47 +03:00
|
|
|
package tablew
|
|
|
|
|
|
|
|
import (
|
2023-08-12 21:54:14 +03:00
|
|
|
"cmp"
|
2024-01-15 04:45:34 +03:00
|
|
|
"context"
|
2020-08-06 20:58:47 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-08-12 21:54:14 +03:00
|
|
|
"slices"
|
2020-08-06 20:58:47 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/samber/lo"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output/yamlw"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
2024-01-25 09:29:55 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/location"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/metadata"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
var _ output.MetadataWriter = (*mdWriter)(nil)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
type mdWriter struct {
|
|
|
|
tbl *table
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetadataWriter returns a new output.MetadataWriter instance
|
|
|
|
// that outputs metadata in table format.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewMetadataWriter(out io.Writer, pr *output.Printing) output.MetadataWriter {
|
|
|
|
tbl := &table{out: out, pr: pr, header: true}
|
2020-08-06 20:58:47 +03:00
|
|
|
w := &mdWriter{tbl: tbl}
|
|
|
|
w.tbl.reset()
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverMetadata implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) DriverMetadata(drvrs []driver.Metadata) error {
|
|
|
|
headers := []string{"DRIVER", "DESCRIPTION", "USER-DEFINED", "DOC"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
2023-04-22 06:36:32 +03:00
|
|
|
w.tbl.tblImpl.SetColTrans(2, w.tbl.pr.Bool.SprintFunc())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
var rows [][]string
|
|
|
|
for _, md := range drvrs {
|
|
|
|
row := []string{string(md.Type), md.Description, strconv.FormatBool(md.UserDefined), md.Doc}
|
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableMetadata implements output.MetadataWriter.
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) TableMetadata(tblMeta *metadata.Table) error {
|
2023-06-22 08:48:58 +03:00
|
|
|
if w.tbl.pr.Verbose {
|
|
|
|
return w.doTableMetaVerbose(tblMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.doTableMeta(tblMeta)
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) doTableMeta(md *metadata.Table) error {
|
2022-12-17 06:46:37 +03:00
|
|
|
var headers []string
|
2020-08-06 20:58:47 +03:00
|
|
|
var rows [][]string
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
colNames := make([]string, len(md.Columns))
|
|
|
|
colTypes := make([]string, len(md.Columns))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
for i, col := range md.Columns {
|
2020-08-06 20:58:47 +03:00
|
|
|
colNames[i] = col.Name
|
|
|
|
colTypes[i] = col.ColumnType
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
headers = []string{"NAME", "TYPE", "ROWS", "COLS"}
|
|
|
|
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, w.tbl.pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(2, w.tbl.pr.Number.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(3, w.tbl.pr.String.SprintFunc())
|
|
|
|
|
|
|
|
row := []string{
|
|
|
|
md.Name,
|
|
|
|
md.TableType,
|
2024-01-27 16:43:17 +03:00
|
|
|
strconv.FormatInt(md.RowCount, 10),
|
2023-06-22 08:48:58 +03:00
|
|
|
strings.Join(colNames, ", "),
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-06-22 08:48:58 +03:00
|
|
|
rows = append(rows, row)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
2022-12-17 06:46:37 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) doTableMetaVerbose(tblMeta *metadata.Table) error {
|
|
|
|
return w.printTablesVerbose([]*metadata.Table{tblMeta})
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
2022-12-17 06:46:37 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
// SourceMetadata implements output.MetadataWriter.
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) SourceMetadata(md *metadata.Source, showSchema bool) error {
|
2023-06-22 08:48:58 +03:00
|
|
|
if !showSchema {
|
|
|
|
return w.doSourceMetaNoSchema(md)
|
2022-12-17 06:46:37 +03:00
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
return w.doSourceMetaFull(md)
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) doSourceMetaNoSchema(md *metadata.Source) error {
|
2023-06-22 08:48:58 +03:00
|
|
|
headers := []string{
|
|
|
|
"SOURCE",
|
|
|
|
"DRIVER",
|
|
|
|
"NAME",
|
|
|
|
"FQ NAME",
|
|
|
|
"SIZE",
|
|
|
|
"LOCATION",
|
|
|
|
}
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, w.tbl.pr.Handle.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(2, w.tbl.pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(3, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(4, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(5, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
|
|
|
|
row := []string{
|
|
|
|
md.Handle,
|
|
|
|
md.Driver.String(),
|
|
|
|
md.Name,
|
|
|
|
md.FQName,
|
|
|
|
w.tbl.pr.Number.Sprint(stringz.ByteSized(md.Size, 1, "")),
|
2024-01-25 09:29:55 +03:00
|
|
|
location.Redact(md.Location),
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.writeRow(context.TODO(), row)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) printTablesVerbose(tbls []*metadata.Table) error {
|
2023-06-22 08:48:58 +03:00
|
|
|
w.tbl.reset()
|
|
|
|
|
|
|
|
headers := []string{
|
|
|
|
"NAME",
|
|
|
|
"TYPE",
|
|
|
|
"ROWS",
|
|
|
|
"COLS",
|
|
|
|
"NAME",
|
|
|
|
"TYPE",
|
|
|
|
"PK",
|
|
|
|
}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, w.tbl.pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(2, w.tbl.pr.Number.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(3, w.tbl.pr.Number.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(4, w.tbl.pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(5, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(6, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
|
|
|
|
var rows [][]string
|
2020-08-06 20:58:47 +03:00
|
|
|
var row []string
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
getPK := func(col *metadata.Column) string {
|
2023-06-22 08:48:58 +03:00
|
|
|
if !col.PrimaryKey {
|
|
|
|
return ""
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
return w.tbl.pr.Bool.Sprint("pk")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tbl := range tbls {
|
2020-08-06 20:58:47 +03:00
|
|
|
row = []string{
|
2023-06-22 08:48:58 +03:00
|
|
|
tbl.Name,
|
|
|
|
tbl.TableType,
|
2024-01-27 16:43:17 +03:00
|
|
|
strconv.FormatInt(tbl.RowCount, 10),
|
2023-06-22 08:48:58 +03:00
|
|
|
w.tbl.pr.Faint.Sprintf("%d", len(tbl.Columns)),
|
|
|
|
tbl.Columns[0].Name,
|
|
|
|
tbl.Columns[0].BaseType,
|
|
|
|
getPK(tbl.Columns[0]),
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = append(rows, row)
|
|
|
|
|
|
|
|
for i := 1; i < len(tbl.Columns); i++ {
|
|
|
|
row = []string{
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
tbl.Columns[i].Name,
|
|
|
|
tbl.Columns[i].BaseType,
|
|
|
|
getPK(tbl.Columns[i]),
|
|
|
|
}
|
|
|
|
rows = append(rows, row)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) printTables(tables []*metadata.Table) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
w.tbl.reset()
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
headers := []string{"NAME", "TYPE", "ROWS", "COLS"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, w.tbl.pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(2, w.tbl.pr.Number.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(3, w.tbl.pr.Faint.SprintFunc())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
var rows [][]string
|
2023-06-22 08:48:58 +03:00
|
|
|
var row []string
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
for _, tbl := range tables {
|
2020-08-06 20:58:47 +03:00
|
|
|
colNames := make([]string, len(tbl.Columns))
|
|
|
|
|
|
|
|
for i, col := range tbl.Columns {
|
|
|
|
colNames[i] = col.Name
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
row = []string{
|
|
|
|
tbl.Name,
|
|
|
|
tbl.TableType,
|
2024-01-27 16:43:17 +03:00
|
|
|
strconv.FormatInt(tbl.RowCount, 10),
|
2023-06-22 08:48:58 +03:00
|
|
|
w.tbl.pr.String.Sprint(strings.Join(colNames, ", ")),
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2022-12-17 06:46:37 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-05-19 17:24:18 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) doSourceMetaFull(md *metadata.Source) error {
|
2023-06-22 08:48:58 +03:00
|
|
|
var headers []string
|
|
|
|
var row []string
|
|
|
|
|
|
|
|
headers = []string{
|
|
|
|
"SOURCE",
|
|
|
|
"DRIVER",
|
|
|
|
"NAME",
|
|
|
|
"FQ NAME",
|
|
|
|
"SIZE",
|
|
|
|
"TABLES",
|
|
|
|
"VIEWS",
|
|
|
|
"LOCATION",
|
|
|
|
}
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, w.tbl.pr.Handle.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(2, w.tbl.pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(3, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(4, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(5, w.tbl.pr.Number.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(6, w.tbl.pr.Number.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(7, w.tbl.pr.Faint.SprintFunc())
|
|
|
|
|
|
|
|
row = []string{
|
|
|
|
md.Handle,
|
|
|
|
md.Driver.String(),
|
|
|
|
md.Name,
|
|
|
|
md.FQName,
|
|
|
|
w.tbl.pr.Number.Sprint(stringz.ByteSized(md.Size, 1, "")),
|
2024-01-27 16:43:17 +03:00
|
|
|
strconv.FormatInt(md.TableCount, 10),
|
|
|
|
strconv.FormatInt(md.ViewCount, 10),
|
2024-01-25 09:29:55 +03:00
|
|
|
location.Redact(md.Location),
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
2024-01-15 04:45:34 +03:00
|
|
|
if err := w.tbl.writeRow(context.TODO(), row); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
if len(md.Tables) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w.tbl.out)
|
|
|
|
w.tbl.reset()
|
|
|
|
|
|
|
|
// Sort by type (view/table) and name
|
2023-11-21 00:42:38 +03:00
|
|
|
slices.SortFunc(md.Tables, func(a, b *metadata.Table) int {
|
2023-06-22 08:48:58 +03:00
|
|
|
if a.TableType == b.TableType {
|
2023-08-12 21:54:14 +03:00
|
|
|
return cmp.Compare(a.Name, b.Name)
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 21:54:14 +03:00
|
|
|
return cmp.Compare(a.TableType, b.TableType)
|
2023-06-22 08:48:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
if w.tbl.pr.Verbose {
|
|
|
|
return w.printTablesVerbose(md.Tables)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.printTables(md.Tables)
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// DBProperties implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) DBProperties(props map[string]any) error {
|
|
|
|
if len(props) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// For nested values, we make use of yamlw's rendering.
|
|
|
|
yamlPr := w.tbl.pr.Clone()
|
|
|
|
yamlPr.Key = yamlPr.Faint
|
|
|
|
|
|
|
|
headers := []string{"KEY", "VALUE"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, w.tbl.pr.Key.SprintFunc())
|
|
|
|
|
|
|
|
rows := make([][]string, 0, len(props))
|
|
|
|
|
|
|
|
keys := lo.Keys(props)
|
|
|
|
slices.Sort(keys)
|
|
|
|
for _, key := range keys {
|
|
|
|
val, ok := props[key]
|
|
|
|
if !ok || val == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var row []string
|
|
|
|
|
|
|
|
// Most properties have scalar values. However, some are nested
|
|
|
|
// arrays of maps (I'm looking at you, SQLite). YAML output is preferred
|
|
|
|
// for this sort of nested structure, but we'll hack an ugly solution
|
|
|
|
// here for text output.
|
|
|
|
switch val := val.(type) {
|
|
|
|
case map[string]any:
|
|
|
|
s := fmt.Sprintf("%v", val)
|
|
|
|
row = []string{key, s}
|
|
|
|
case []any:
|
|
|
|
var elements []string
|
|
|
|
|
|
|
|
for _, item := range val {
|
|
|
|
switch item := item.(type) {
|
|
|
|
case map[string]any:
|
|
|
|
s, err := yamlw.MarshalToString(yamlPr, item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.ReplaceAll(s, "\n", " ")
|
|
|
|
elements = append(elements, s)
|
|
|
|
case []string:
|
|
|
|
s := strings.Join(item, " ")
|
|
|
|
elements = append(elements, s)
|
|
|
|
default:
|
|
|
|
s := w.tbl.renderResultCell(kind.Text, item)
|
|
|
|
elements = append(elements, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
row = []string{key, strings.Join(elements, "\n")}
|
|
|
|
default:
|
|
|
|
s := w.tbl.renderResultCell(kind.Text, val)
|
|
|
|
row = []string{key, s}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-05-19 17:24:18 +03:00
|
|
|
}
|
2023-11-21 00:42:38 +03:00
|
|
|
|
|
|
|
// Catalogs implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) Catalogs(currentCatalog string, catalogs []string) error {
|
|
|
|
if len(catalogs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pr := w.tbl.pr
|
|
|
|
|
|
|
|
if !pr.Verbose {
|
|
|
|
if pr.ShowHeader {
|
|
|
|
headers := []string{"CATALOG"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
}
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, pr.String.SprintFunc())
|
|
|
|
|
|
|
|
var rows [][]string
|
|
|
|
for _, catalog := range catalogs {
|
|
|
|
if catalog == currentCatalog {
|
|
|
|
catalog = pr.Active.Sprintf(catalog)
|
|
|
|
}
|
|
|
|
rows = append(rows, []string{catalog})
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-11-21 00:42:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verbose mode
|
|
|
|
if pr.ShowHeader {
|
|
|
|
headers := []string{"CATALOG", "ACTIVE"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, pr.Bool.SprintFunc())
|
|
|
|
|
|
|
|
var rows [][]string
|
|
|
|
for _, catalog := range catalogs {
|
|
|
|
var active string
|
|
|
|
if catalog == currentCatalog {
|
|
|
|
catalog = pr.Active.Sprintf(catalog)
|
|
|
|
active = pr.Bool.Sprint("active")
|
|
|
|
}
|
|
|
|
rows = append(rows, []string{catalog, active})
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-11-21 00:42:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schemata implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) Schemata(currentSchema string, schemas []*metadata.Schema) error {
|
|
|
|
if len(schemas) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pr := w.tbl.pr
|
|
|
|
if !pr.Verbose {
|
|
|
|
if pr.ShowHeader {
|
|
|
|
headers := []string{"SCHEMA"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
}
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, pr.String.SprintFunc())
|
|
|
|
var rows [][]string
|
|
|
|
for _, schema := range schemas {
|
|
|
|
s := schema.Name
|
|
|
|
if schema.Name == currentSchema {
|
|
|
|
s = pr.Active.Sprintf(s)
|
|
|
|
}
|
|
|
|
rows = append(rows, []string{s})
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-11-21 00:42:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verbose mode
|
|
|
|
if pr.ShowHeader {
|
|
|
|
headers := []string{"SCHEMA", "CATALOG", "OWNER", "ACTIVE"}
|
|
|
|
w.tbl.tblImpl.SetHeader(headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.tbl.tblImpl.SetColTrans(0, pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(1, pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(2, pr.String.SprintFunc())
|
|
|
|
w.tbl.tblImpl.SetColTrans(3, pr.Bool.SprintFunc())
|
|
|
|
|
|
|
|
var rows [][]string
|
|
|
|
for _, schema := range schemas {
|
|
|
|
row := []string{schema.Name, schema.Catalog, schema.Owner, ""}
|
|
|
|
|
|
|
|
if schema.Name == currentSchema {
|
|
|
|
row[0] = pr.Active.Sprintf(row[0])
|
|
|
|
row[3] = "active"
|
|
|
|
}
|
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
return w.tbl.appendRowsAndRenderAll(context.TODO(), rows)
|
2023-11-21 00:42:38 +03:00
|
|
|
}
|