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

ColumnTypes() improvement. #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
44 changes: 37 additions & 7 deletions crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ func (c *CrateDriver) Query(stmt string, args []driver.Value) (driver.Rows, erro

// Rows reader
rows := &Rows{
columns: res.Cols,
values: res.Rows,
rowcount: res.Rowcount,
columns: res.Cols,
columnTypes: res.ColumnTypes,
values: res.Rows,
rowcount: res.Rowcount,
}

return rows, nil
Expand Down Expand Up @@ -159,10 +160,11 @@ func (r *Result) RowsAffected() (int64, error) {

// Rows reader
type Rows struct {
columns []string
values [][]interface{}
rowcount int64
pos int64 // index position on the values array
columns []string
columnTypes []interface{}
values [][]interface{}
rowcount int64
pos int64 // index position on the values array
}

// Row columns
Expand Down Expand Up @@ -191,6 +193,34 @@ func (r *Rows) Close() error {
return nil
}

var typeMap = map[int]string{
2: "byte",
Copy link
Owner

Choose a reason for hiding this comment

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

Just one question, Go's documentation specifies that types should be returned uppercased, here:

https://golang.org/pkg/database/sql/driver/#RowsColumnTypeDatabaseTypeName

Also here is the discussion about this feature.

golang/go#16652

Crate documents all of their types lowercased, I see no reason for us to uppercase them.

Before merging this, I would like to know if you have any thoughts on this.

Copy link
Author

@nicetip nicetip Nov 1, 2017

Choose a reason for hiding this comment

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

hello? @herenow
I didn't know there were a spec for upper/lowercase.
I just used lowercase cause crate's document was written in lowercase and it's easy to type.
I thought about this for a while and looked for other golang drivers implementations.
Other drivers also already knows about this and implemented in uppercase, even commented it should be uppercase.
So I think uppercase would be better, cause somebody who knows about this spec, may expect that function will return uppercased.
And others may not cares about it. : )
Thanks you for asking my opinion.

3: "boolean",
4: "string",
5: "ip",
6: "double",
7: "float",
8: "short",
9: "integer",
10: "long",
11: "timestamp",
12: "object",
13: "geo_point",
14: "geo_shape",
}

func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {

Choose a reason for hiding this comment

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

exported method Rows.ColumnTypeDatabaseTypeName should have comment or be unexported

switch r.columnTypes[index].(type) {
case []interface{}:
x := r.columnTypes[index].([]interface{})
val, _ := x[1].(json.Number).Int64()
return fmt.Sprintf("array(%s)", typeMap[int(val)])
default:
val, _ := r.columnTypes[index].(json.Number).Int64()
return typeMap[int(val)]
}
}

// Yet not supported
func (c *CrateDriver) Begin() (driver.Tx, error) {
err := errors.New("Transactions are not supported by this driver.")
Expand Down