This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func makeWhere(table *Table, filter Filter) (*SimpleWhere, error) { | |
for name, value := range filter { | |
// ... | |
- l = append(l, whereElem{column: column, value: column.Descriptor.Valuer(reflect.ValueOf(value))}) | |
+ v, err := column.Descriptor.Valuer(reflect.ValueOf(value)).Value() | |
+ if err != nil { | |
+ return nil, err | |
+ } | |
+ l = append(l, whereElem{column: column, value: v}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parseQueryRow(table *Table, rows *sql.Rows) (interface{}, error) { | |
// ... | |
- values := make([]interface{}, len(table.Columns)) | |
+ values := table.Scanners.Get().([]interface{}) | |
+ defer table.Scanners.Put(values) | |
for i, column := range table.Columns { | |
- scanner := column.Descriptor.Scanner() | |
- scanner.Target(field) | |
- values[i] = scanner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parseQueryRow(table *Table, rows *sql.Rows) (interface{}, error) { | |
ptr := reflect.New(table.Type) | |
elem := ptr.Elem() | |
values := make([]interface{}, len(table.Columns)) | |
- [... populate scanners slice with column.Descriptor.Scanner() ...] | |
+ for i, column := range table.Columns { | |
+ scanner := column.Descriptor.Scanner() | |
+ scanner.Target(field) | |
+ values[i] = scanner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// goodbye reflect.New in Scanner.Scan | |
func (s *Scanner) Scan(src interface{}) error { | |
- s.value = reflect.New(s.Type) | |
+ // Clear out the value after a scan so we aren't holding onto references. | |
+ defer func() { s.value = reflect.Value{} }() | |
// ... | |
} | |
// and hello Scanner.Target | |
+func (s *Scanner) Target(value reflect.Value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Valuer creates a sql/driver.Valuer from the type and value. | |
-func (d Descriptor) Valuer(val reflect.Value) Valuer { | |
+func (d *Descriptor) Valuer(val reflect.Value) Valuer { | |
- return Valuer{Descriptor: &d, value: val} | |
+ return Valuer{Descriptor: d, value: val} | |
} | |
// Scanner creates a sql.Scanner from the descriptor. | |
-func (d Descriptor) Scanner() *Scanner { return &Scanner{Descriptor: &d} } | |
+func (d *Descriptor) Scanner() *Scanner { return &Scanner{Descriptor: d} } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Descriptor struct { | |
Tags TagSet // struct tags | |
Type reflect.Type // the original type | |
Kind reflect.Kind // the underlying kind | |
Ptr bool // whether or not it's a ptr | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Scanner interface { | |
// the docs tell us that the src will be a valid driver.Value | |
Scan(src interface{}) error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Valuer interface { | |
Value() (driver.Value, error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id := 5 | |
var name string | |
err := db.QueryRowContext(ctx, "SELECT name FROM users WHERE id = ?", id).Scan(&name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type User struct { | |
ID int64 `sql:",primary"` | |
Name string | |
Configuration Configuration `sql:",json"` | |
} |
NewerOlder