vitess.io/vitess@v0.16.2/go/test/endtoend/onlineddl/query_util.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     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 onlineddl
    18  
    19  import (
    20  	"io"
    21  	"strings"
    22  
    23  	"vitess.io/vitess/go/sqltypes"
    24  
    25  	"github.com/olekukonko/tablewriter"
    26  )
    27  
    28  // PrintQueryResult will pretty-print a QueryResult to the logger.
    29  func PrintQueryResult(writer io.Writer, qr *sqltypes.Result) {
    30  	if qr == nil {
    31  		return
    32  	}
    33  	if len(qr.Rows) == 0 {
    34  		return
    35  	}
    36  
    37  	table := tablewriter.NewWriter(writer)
    38  	table.SetAutoFormatHeaders(false)
    39  
    40  	// Make header.
    41  	header := make([]string, 0, len(qr.Fields))
    42  	for _, field := range qr.Fields {
    43  		header = append(header, field.Name)
    44  	}
    45  	table.SetHeader(header)
    46  
    47  	// Add rows.
    48  	for _, row := range qr.Rows {
    49  		vals := make([]string, 0, len(row))
    50  		for _, val := range row {
    51  			v := val.ToString()
    52  			v = strings.ReplaceAll(v, "\r", " ")
    53  			v = strings.ReplaceAll(v, "\n", " ")
    54  			vals = append(vals, v)
    55  		}
    56  		table.Append(vals)
    57  	}
    58  
    59  	// Print table.
    60  	table.Render()
    61  }