Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle TimeStamp columns by converting int64 to time.Time and vice versa #15

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
011e6e2
Since one cannot replace the driver.DefaultParameterConverter to prop…
echarlus May 9, 2017
0def5fb
Now that GO 1.9 is released properly handle data types in the driver …
echarlus Sep 1, 2017
6d87b62
Improved GeoPoint by using a struct rather than an array of floats.
echarlus Sep 4, 2017
7c51c1a
Handle Crate's Array column type
echarlus Oct 23, 2017
b801d28
Use our own basic json encoder rather than the GO lib one to properly…
echarlus Jan 17, 2018
cfa2930
Convert v to Float before formatting with %f format
echarlus Jan 17, 2018
a6dff48
Implemented encoding of arrays of map/arrays
echarlus Jan 19, 2018
efa5085
Handle interface types in Array & Map
echarlus Jan 19, 2018
8d02624
Fix to satisty Error interface
echarlus Jan 26, 2018
cdfdf1f
Fixed JSON encoding causing issue on crate 2.3.2 because a String was…
echarlus Jan 31, 2018
1b3ab3c
Merge branch 'time_column_handling' of https://github.com/echarlus/go…
echarlus Jan 31, 2018
7a391ae
//Prevents rounding errors seen with floats like 0.01*41 which is 0.4…
echarlus Apr 25, 2019
0a627df
Complete fix for (Fix had only been applied to float in arrays, not i…
echarlus May 10, 2019
f550e9a
Prevent crash when processing nil maps.
echarlus May 27, 2019
0c2190f
Ticket SDRMDPTH-15 : Fixed string encoding in encodeArray.
echarlus Apr 19, 2021
461e53d
Prepare for Crate 4.x : support authentication
echarlus Oct 7, 2021
11d19f0
Fixed map of map element handling and added support for Int into map …
echarlus Dec 11, 2023
c0f52ca
return a CrateErr rather than &CrateErr to allow errors.As to be used…
echarlus Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"io"
"net/http"
"net/url"
"time"
)

const (
typeTime = "11"
)

// Crate conn structure
Expand Down Expand Up @@ -65,8 +70,18 @@ func (c *CrateDriver) query(stmt string, args []driver.Value) (*endpointResponse
Stmt: stmt,
}

if len(args) > 0 {
if l:=len(args); l > 0 {
query.Args = args
//Process each column that needs conversion from time.Time to int64 for Crate
for i:= 0; i<l; i++ {
if val, ok := args[i].(time.Time) ; ok {
if val.IsZero() {
query.Args[i] = 0
} else {
query.Args[i] = val.UnixNano() / 1000000
}
}
}
}

buf, err := json.Marshal(query)
Expand Down Expand Up @@ -123,8 +138,13 @@ func (c *CrateDriver) Query(stmt string, args []driver.Value) (driver.Rows, erro
columns: res.Cols,
values: res.Rows,
rowcount: res.Rowcount,
isTime: make([]bool, len(res.ColumnTypes)),
}
tcount := len(res.ColumnTypes)
for i:=0; i<tcount; i++ {
n, ok := res.ColumnTypes[i].(json.Number)
rows.isTime[i] = (ok && (n.String() == typeTime)) //Probably faster than getting the int64 value of n ...
}

return rows, nil
}

Expand Down Expand Up @@ -161,6 +181,7 @@ func (r *Result) RowsAffected() (int64, error) {
type Rows struct {
columns []string
values [][]interface{}
isTime []bool //Flags columns to convert to time.Time (type 11)
rowcount int64
pos int64 // index position on the values array
}
Expand All @@ -175,9 +196,18 @@ func (r *Rows) Next(dest []driver.Value) error {
if r.pos >= r.rowcount {
return io.EOF
}

for i := range dest {
dest[i] = r.values[r.pos][i]
if (r.isTime[i] && (r.values[r.pos][i] != nil)) { //If column is flagged as time.Time then convert the int64 value to time.Time
if val, ok := r.values[r.pos][i].(json.Number); ok {
v , _ := val.Int64()
sec := v/int64(1000)
dest[i] = time.Unix(sec, (v-sec*int64(1000))*int64(1000000))
} else {
return errors.New(fmt.Sprintf("Failed to convert column %s=%T to time\n", r.columns[i], r.values[r.pos][i]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)

}
} else {
dest[i] = r.values[r.pos][i]
}
}

r.pos++
Expand Down Expand Up @@ -238,6 +268,8 @@ func (s *CrateStmt) NumInput() int {
return -1
}



// Register the driver
func init() {
sql.Register("crate", &CrateDriver{})
Expand Down