Repository: dolthub/dolt
File: go/cmd/dolt/commands/engine/sql_print.go (Go)

Code before:

	secondsSinceStart := secondsSince(start, time.Now())
	err := iohelp.WriteLine(cli.CliOut, fmt.Sprintf("%d %s in set%s (%.2f sec) %s", numRows, noun, warning, secondsSinceStart, warningsList))
	if err != nil {
		return err
	}

	return nil
}

// binaryHexIterator wraps a row iterator and transforms binary data to hex format
type binaryHexIterator struct {
	inner  sql.RowIter
	schema sql.Schema
}

// newBinaryHexIterator creates a new iterator that transforms binary data to hex format
func newBinaryHexIterator(inner sql.RowIter, schema sql.Schema) sql.RowIter {
	return &binaryHexIterator{
		inner:  inner,
		schema: schema,
	}
}

// Next returns the next row with binary data transformed to hex format
func (iter *binaryHexIterator) Next(ctx *sql.Context) (sql.Row, error) {
	row, err := iter.inner.Next(ctx)
	if err != nil {
		return nil, err
	}

	// TODO: Add support for BLOB types (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB) and BIT type
	for i, val := range row {
		if val != nil && i < len(iter.schema) {
			switch iter.schema[i].Type.Type() {
			case sqltypes.Binary, sqltypes.VarBinary:
				if bytes, ok := val.([]byte); ok {
					row[i] = sqlutil.BinaryAsHexDisplayValue(fmt.Sprintf("0x%X", bytes))
				} else {
					row[i] = sqlutil.BinaryAsHexDisplayValue(fmt.Sprintf("0x%X", []byte(fmt.Sprint(val))))
				}
			}
		}
	}

	return row, nil
}

// Close closes the wrapped iterator and releases any resources.
func (iter *binaryHexIterator) Close(ctx *sql.Context) error {
	return iter.inner.Close(ctx)

Review comment (suggestion):
```suggestion
var _ sql.RowIter = (*binaryHexIterator)(nil)
```

Code after:

	secondsSinceStart := secondsSince(start, time.Now())
	err := iohelp.WriteLine(cli.CliOut, fmt.Sprintf("%d %s in set%s (%.2f sec) %s", numRows, noun, warning, secondsSinceStart, warningsList))
	if err != nil {
		return err
	}

	return nil
}

// binaryHexIterator wraps a row iterator and transforms binary data to hex format
type binaryHexIterator struct {
	inner  sql.RowIter
	schema sql.Schema
}

var _ sql.RowIter = (*binaryHexIterator)(nil)

// newBinaryHexIterator creates a new iterator that transforms binary data to hex format
func newBinaryHexIterator(inner sql.RowIter, schema sql.Schema) sql.RowIter {
	return &binaryHexIterator{
		inner:  inner,
		schema: schema,
	}
}

// Next returns the next row with binary data transformed to hex format
func (iter *binaryHexIterator) Next(ctx *sql.Context) (sql.Row, error) {
	rowData, err := iter.inner.Next(ctx)
	if err != nil {
		return nil, err
	}

	// TODO: Add support for BLOB types (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB) and BIT type
	for i, val := range rowData {
		if val != nil && i < len(iter.schema) {
			switch iter.schema[i].Type.Type() {
			case sqltypes.Binary, sqltypes.VarBinary:
				switch v := val.(type) {
				case []byte: // hex fmt is explicitly upper case
					rowData[i] = sqlutil.BinaryAsHexDisplayValue(fmt.Sprintf("0x%X", v))
				case string: // handles results from sql-server; MySQL wire protocol returns strings
					rowData[i] = sqlutil.BinaryAsHexDisplayValue(fmt.Sprintf("0x%X", []byte(v)))
				default:
					return nil, fmt.Errorf("unexpected type %T for binary column %s", val, iter.schema[i].Name)
				}
			}
		}
	}

	return rowData, nil