dialect/sql: replace nulltime with official implementation on 1.13 and higher

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/169

Reviewed By: alexsn

Differential Revision: D18561431

fbshipit-source-id: d21335c073e0abed99d97c85c03cfa17c8326fef
This commit is contained in:
Ariel Mashraki
2019-11-17 09:05:28 -08:00
committed by Facebook Github Bot
parent d1089d30a5
commit d31f0828f0
3 changed files with 50 additions and 31 deletions

View File

@@ -7,10 +7,8 @@ package sql
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"strings"
"time"
"github.com/facebookincubator/ent/dialect"
)
@@ -137,32 +135,3 @@ type (
// NullFloat64 is an alias to sql.NullFloat64.
NullFloat64 = sql.NullFloat64
)
// Note:
// NullTime is a modified copy of database/sql.NullTime from Go 1.13,
// It should be replaced with standard library code when Go 1.13 is released.
// NullTime represents a time.Time that may be null.
// NullTime implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
// Scan implements the Scanner interface.
func (n *NullTime) Scan(v interface{}) error {
if v, ok := v.(time.Time); ok {
n.Time = v
n.Valid = true
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullTime) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time, nil
}

38
dialect/sql/sql_112.go Normal file
View File

@@ -0,0 +1,38 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// +build !go1.13
package sql
import (
"database/sql/driver"
"time"
)
// NullTime represents a time.Time that may be null.
//
// NullTime implements the Scanner interface so it can
// be used as a scan destination, similar to NullString.
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
// Scan implements the Scanner interface.
func (n *NullTime) Scan(v interface{}) error {
if v, ok := v.(time.Time); ok {
n.Time = v
n.Valid = true
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullTime) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time, nil
}

12
dialect/sql/sql_113.go Normal file
View File

@@ -0,0 +1,12 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// +build go1.13
package sql
import "database/sql"
// NullTime represents a time.Time that may be null.
type NullTime = sql.NullTime