Skip to content

Commit

Permalink
cli: remove ColumnTypeScanType
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
rafiss committed Jun 2, 2022
1 parent fc2ea6f commit 6b3d2a8
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 69 deletions.
5 changes: 0 additions & 5 deletions pkg/cli/clisqlclient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"context"
"database/sql/driver"
"io"
"reflect"
"time"
)

Expand Down Expand Up @@ -109,10 +108,6 @@ type Rows interface {
// result does not need to be constructed on each invocation.
Columns() []string

// ColumnTypeScanType returns the natural Go type of values at the
// given column index.
ColumnTypeScanType(index int) reflect.Type

// ColumnTypeDatabaseTypeName returns the database type name
// of the column at the given column index.
ColumnTypeDatabaseTypeName(index int) string
Expand Down
5 changes: 0 additions & 5 deletions pkg/cli/clisqlclient/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"context"
"database/sql/driver"
"io"
"reflect"

"github.com/jackc/pgconn"
)
Expand Down Expand Up @@ -53,10 +52,6 @@ func (c copyFromRows) Columns() []string {
return nil
}

func (c copyFromRows) ColumnTypeScanType(index int) reflect.Type {
return nil
}

func (c copyFromRows) ColumnTypeDatabaseTypeName(index int) string {
return ""
}
Expand Down
34 changes: 0 additions & 34 deletions pkg/cli/clisqlclient/row_type_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,11 @@
package clisqlclient

import (
"reflect"
"strings"
"time"

"github.com/jackc/pgtype"
)

// scanType returns the Go type used for the given column type.
func scanType(typeOID uint32, typeName string) reflect.Type {
if typeName == "" || strings.HasPrefix(typeName, "_") {
// User-defined types and array types are scanned into []byte.
// These are handled separately since we can't easily include all the OIDs
// for user-defined types and arrays in the switch statement.
return reflect.TypeOf([]byte(nil))
}
// This switch is copied from lib/pq, and modified with a few additional cases.
// https://github.com/lib/pq/blob/b2901c7946b69f1e7226214f9760e31620499595/rows.go#L24
switch typeOID {
case pgtype.Int8OID:
return reflect.TypeOf(int64(0))
case pgtype.Int4OID:
return reflect.TypeOf(int32(0))
case pgtype.Int2OID:
return reflect.TypeOf(int16(0))
case pgtype.VarcharOID, pgtype.TextOID, pgtype.NameOID,
pgtype.ByteaOID, pgtype.NumericOID, pgtype.RecordOID,
pgtype.QCharOID, pgtype.BPCharOID:
return reflect.TypeOf("")
case pgtype.BoolOID:
return reflect.TypeOf(false)
case pgtype.DateOID, pgtype.TimeOID, 1266, pgtype.TimestampOID, pgtype.TimestamptzOID:
// 1266 is the OID for TimeTZ.
// TODO(rafi): Add TimetzOID to pgtype.
return reflect.TypeOf(time.Time{})
default:
return reflect.TypeOf(new(interface{})).Elem()
}
}

// databaseTypeName returns the database type name for the given type OID.
func databaseTypeName(ci *pgtype.ConnInfo, typeOID uint32) string {
dataType, ok := ci.DataTypeForOID(typeOID)
Expand Down
8 changes: 0 additions & 8 deletions pkg/cli/clisqlclient/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package clisqlclient
import (
"database/sql/driver"
"io"
"reflect"

"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
Expand Down Expand Up @@ -87,13 +86,6 @@ func (r *sqlRows) NextResultSet() (bool, error) {
return false, nil
}

func (r *sqlRows) ColumnTypeScanType(index int) reflect.Type {
// todo: remove this?
o := r.rows.FieldDescriptions()[index].DataTypeOID
n := r.ColumnTypeDatabaseTypeName(index)
return scanType(o, n)
}

func (r *sqlRows) ColumnTypeDatabaseTypeName(index int) string {
fieldOID := r.rows.FieldDescriptions()[index].DataTypeOID
return databaseTypeName(r.connInfo, fieldOID)
Expand Down
8 changes: 0 additions & 8 deletions pkg/cli/clisqlclient/rows_multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package clisqlclient
import (
"database/sql/driver"
"io"
"reflect"

"github.com/cockroachdb/errors"
"github.com/jackc/pgconn"
Expand Down Expand Up @@ -135,13 +134,6 @@ func (r *sqlRowsMultiResultSet) NextResultSet() (bool, error) {
return next, nil
}

func (r *sqlRowsMultiResultSet) ColumnTypeScanType(index int) reflect.Type {
rd := r.rows.ResultReader()
o := rd.FieldDescriptions()[index].DataTypeOID
n := r.ColumnTypeDatabaseTypeName(index)
return scanType(o, n)
}

func (r *sqlRowsMultiResultSet) ColumnTypeDatabaseTypeName(index int) string {
rd := r.rows.ResultReader()
fieldOID := rd.FieldDescriptions()[index].DataTypeOID
Expand Down
1 change: 0 additions & 1 deletion pkg/cli/clisqlexec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ go_library(
"//pkg/util/syncutil",
"//pkg/util/timeutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_jackc_pgtype//:pgtype",
"@com_github_olekukonko_tablewriter//:tablewriter",
"@com_github_spf13_pflag//:pflag",
],
Expand Down
18 changes: 10 additions & 8 deletions pkg/cli/clisqlexec/format_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"fmt"
"html"
"io"
"reflect"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -121,16 +120,19 @@ func (iter *rowIter) Align() []int {
cols := iter.rows.Columns()
align := make([]int, len(cols))
for i := range align {
switch iter.rows.ColumnTypeScanType(i).Kind() {
case reflect.String:
typName := iter.rows.ColumnTypeDatabaseTypeName(i)
if typName == "" || strings.HasPrefix(typName, "_") {
// All array types begin with "_" and user-defined types may not have a
// type name available.
align[i] = tablewriter.ALIGN_LEFT
case reflect.Slice:
continue
}
switch typName {
case "TEXT", "BYTEA", "CHAR", "BPCHAR", "NAME", "UUID":
align[i] = tablewriter.ALIGN_LEFT
case reflect.Int64:
align[i] = tablewriter.ALIGN_RIGHT
case reflect.Float64:
case "INT2", "INT4", "INT8", "FLOAT4", "FLOAT8", "NUMERIC", "OID":
align[i] = tablewriter.ALIGN_RIGHT
case reflect.Bool:
case "BOOL":
align[i] = tablewriter.ALIGN_CENTER
default:
align[i] = tablewriter.ALIGN_DEFAULT
Expand Down

0 comments on commit 6b3d2a8

Please sign in to comment.