datatypes

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 10 Imported by: 0

README

GORM Data Types

Date

import "gorm.io/datatypes"

type UserWithDate struct {
	gorm.Model
	Name string
	Date datatypes.Date
}

user := UserWithDate{Name: "jinzhu", Date: datatypes.Date(time.Now())}
DB.Create(&user)
// INSERT INTO `user_with_dates` (`name`,`date`) VALUES ("jinzhu","2020-07-17 00:00:00")

DB.First(&result, "name = ? AND date = ?", "jinzhu", datatypes.Date(curTime))
// SELECT * FROM user_with_dates WHERE name = "jinzhu" AND date = "2020-07-17 00:00:00" ORDER BY `user_with_dates`.`id` LIMIT 1

Time

MySQL, PostgreSQL, SQLite, SQLServer are supported.

Time with nanoseconds is supported for some databases which support for time with fractional second scale.

import "gorm.io/datatypes"

type UserWithTime struct {
    gorm.Model
    Name string
    Time datatypes.Time
}

user := UserWithTime{Name: "jinzhu", Time: datatypes.NewTime(1, 2, 3, 0)}
DB.Create(&user)
// INSERT INTO `user_with_times` (`name`,`time`) VALUES ("jinzhu","01:02:03")

DB.First(&result, "name = ? AND time = ?", "jinzhu", datatypes.NewTime(1, 2, 3, 0))
// SELECT * FROM user_with_times WHERE name = "jinzhu" AND time = "01:02:03" ORDER BY `user_with_times`.`id` LIMIT 1

NOTE: If the current using database is SQLite, the field column type is defined as TEXT type when GORM AutoMigrate because SQLite doesn't have time type.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct {
	time.Time
}

func (*Date) GobDecode

func (date *Date) GobDecode(b []byte) error

func (Date) GobEncode

func (date Date) GobEncode() ([]byte, error)

func (Date) GormDataType

func (date Date) GormDataType() string

GormDataType gorm common data type

func (Date) MarshalJSON

func (date Date) MarshalJSON() ([]byte, error)

func (*Date) Scan

func (date *Date) Scan(value interface{}) (err error)

func (*Date) UnmarshalJSON

func (date *Date) UnmarshalJSON(b []byte) error

func (*Date) UnmarshalParam

func (date *Date) UnmarshalParam(param string) error

func (Date) Value

func (date Date) Value() (driver.Value, error)

type Time

type Time time.Duration

Time is time data type.

func NewTime

func NewTime(hour, min, sec, nsec int) Time

NewTime is a constructor for Time and returns new Time.

func (Time) GormDBDataType

func (Time) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType returns gorm DB data type based on the current using database.

func (Time) GormDataType

func (Time) GormDataType() string

GormDataType returns gorm common data type. This type is used for the field's column type.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler to convert Time to json serialization.

func (*Time) Scan

func (t *Time) Scan(src interface{}) error

Scan implements sql.Scanner interface and scans value into Time,

func (Time) String

func (t Time) String() string

String implements fmt.Stringer interface.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler to deserialize json data.

func (*Time) UnmarshalParam

func (t *Time) UnmarshalParam(param string) error

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements driver.Valuer interface and returns string format of Time.

type URL

type URL url.URL

func (URL) GormDBDataType

func (URL) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (URL) GormDataType

func (URL) GormDataType() string

func (URL) MarshalJSON

func (u URL) MarshalJSON() ([]byte, error)

func (*URL) Scan

func (u *URL) Scan(value interface{}) error

func (*URL) String

func (u *URL) String() string

func (*URL) UnmarshalJSON

func (u *URL) UnmarshalJSON(data []byte) error

func (*URL) UnmarshalParam

func (u *URL) UnmarshalParam(param string) error

func (URL) Value

func (u URL) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL