Skip to content

Commit

Permalink
Merge pull request #257 from noborus/refactor-1
Browse files Browse the repository at this point in the history
Minor refactoring
  • Loading branch information
noborus authored Nov 10, 2023
2 parents 9ed6c16 + ea3375c commit b805621
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,7 @@ func main() {
trdsql.NewImporter(trdsql.InDelimiter(":")),
trdsql.NewExporter(trdsql.NewWriter()),
)
err := trd.Exec("SELECT c1 FROM /etc/passwd")
if err != nil {
if err := trd.Exec("SELECT c1 FROM /etc/passwd"); err != nil {
log.Fatal(err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions _example/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ func main() {
trdsql.NewImporter(trdsql.InDelimiter(":")),
trdsql.NewExporter(trdsql.NewWriter()),
)
err := trd.Exec("SELECT c1 FROM /etc/passwd")
if err != nil {
if err := trd.Exec("SELECT c1 FROM /etc/passwd"); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion importer_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func (i *BufferImporter) ImportContext(ctx context.Context, db *DB, query string
if err := db.CreateTable(i.tableName, names, types, true); err != nil {
return query, err
}
return query, db.Import(i.tableName, names, i.Reader)
return query, db.ImportContext(ctx, i.tableName, names, i.Reader)
}
2 changes: 1 addition & 1 deletion importer_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func (i *SliceImporter) ImportContext(ctx context.Context, db *DB, query string)
if err := db.CreateTable(i.tableName, names, types, true); err != nil {
return query, err
}
return query, db.Import(i.tableName, names, i.SliceReader)
return query, db.ImportContext(ctx, i.tableName, names, i.SliceReader)
}
9 changes: 5 additions & 4 deletions output_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ type RAWWriter struct {

// NewRAWWriter returns RAWWriter.
func NewRAWWriter(writeOpts *WriteOpts) *RAWWriter {
var err error
w := &RAWWriter{}
w.writer = bufio.NewWriter(writeOpts.OutStream)
w.delimiter, err = strconv.Unquote(`"` + writeOpts.OutDelimiter + `"`)
delimiter, err := strconv.Unquote(`"` + writeOpts.OutDelimiter + `"`)
if err != nil {
debug.Printf("%s\n", err)
}

w := &RAWWriter{}
w.writer = bufio.NewWriter(writeOpts.OutStream)
w.delimiter = delimiter
w.outHeader = writeOpts.OutHeader
if writeOpts.OutUseCRLF {
w.endLine = "\r\n"
Expand Down
8 changes: 6 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ var readerFuncs = map[Format]ReaderFunc{
},
}

var extFormat Format = 100
var registerMux = &sync.Mutex{}
var (
// extFormat is the next format number to be assigned.
extFormat Format = 100
// registerMux is a mutex to protect access to the register.
registerMux = &sync.Mutex{}
)

func RegisterReaderFunc(ext string, readerFunc ReaderFunc) {
registerMux.Lock()
Expand Down
4 changes: 2 additions & 2 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// ValString converts database value to string.
func ValString(v interface{}) string {
func ValString(v any) string {
switch t := v.(type) {
case nil:
return ""
Expand All @@ -33,7 +33,7 @@ func ValString(v interface{}) string {
}
}

func replaceNULL(nullString string, v interface{}) interface{} {
func replaceNULL(nullString string, v any) any {
switch t := v.(type) {
case nil:
return nil
Expand Down
10 changes: 5 additions & 5 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestValString(t *testing.T) {
type args struct {
v interface{}
v any
}
tests := []struct {
name string
Expand Down Expand Up @@ -53,21 +53,21 @@ func TestValString(t *testing.T) {
func Test_replaceNULL(t *testing.T) {
type args struct {
NULLString string
v interface{}
v any
}
tests := []struct {
name string
args args
want interface{}
want any
}{
{
name: "test1",
args: args{NULLString: "NULL", v: interface{}("N")},
args: args{NULLString: "NULL", v: any("N")},
want: "N",
},
{
name: "testMatch",
args: args{NULLString: "NULL", v: interface{}("NULL")},
args: args{NULLString: "NULL", v: any("NULL")},
want: nil,
},
{
Expand Down

0 comments on commit b805621

Please sign in to comment.