Skip to content

Commit

Permalink
fix(cockroach): fix write data values separation mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadv184 committed Feb 21, 2023
1 parent 67d75fb commit 9ee8cf4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 29 additions & 0 deletions data/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,35 @@ func (d *Set) GetKeys() []string {
return keys
}

// GetValues returns the values of the data set.
func (d *Set) GetValues() []ValueType {
values := make([]ValueType, len(*d))
for i, data := range *d {
values[i] = data.GetValue()
}
return values
}

// GetStringValues returns the string values of the data set.
func (d *Set) GetStringValues() []string {
values := make([]string, len(*d))
for i, data := range *d {
switch data.GetValue().GetTypeKind() {
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64,
KindUint, KindUint8, KindUint16, KindUint32, KindUint64,
KindFloat32, KindFloat64:
values[i] = fmt.Sprintf("%v", data.GetValue().GetValue())
case KindBool:
values[i] = fmt.Sprintf("%t", data.GetValue().GetValue())
case KindDateTime:
values[i] = fmt.Sprintf("%s", data.GetValue().GetValue().(time.Time).Format("2006-01-02 15:04:05"))
default:
values[i] = fmt.Sprintf("%s", data.GetValue().GetValue())
}
}
return values
}

// Clone returns a clone of the data set.
func (d *Set) Clone() *Set {
clone := NewDataSet()
Expand Down
3 changes: 1 addition & 2 deletions driver/cockroach/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cockroach
import (
"database/sql"
"fmt"
"strings"

"github.com/mohammadv184/gloader/data"
"github.com/mohammadv184/gloader/driver"
Expand Down Expand Up @@ -90,7 +89,7 @@ func (m *Connection) Write(table string, dataBatch *data.Batch) error {

for _, dataSet := range *dataBatch {
values := make([]interface{}, dataSet.GetLength())
for i, key := range strings.Split(dataSet.String(", "), ", ") {
for i, key := range dataSet.GetStringValues() {
values[i] = key
}
_, err = stmt.Exec(values...)
Expand Down

0 comments on commit 9ee8cf4

Please sign in to comment.