datatypes

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 14 Imported by: 0

README

GORM Data Types

install

go get -u github.com/wuruipeng404/datatypes@v1.1.1

JSON

sqlite, mysql, postgres supported

import "github.com/wuruipeng404/datatypes"

type UserWithJSON struct {
	gorm.Model
	Name       string
	Attributes datatypes.JSON
}

DB.Create(&User{
	Name:       "json-1",
	Attributes: datatypes.JSON([]byte(`{"name": "jinzhu", "age": 18, "tags": ["tag1", "tag2"], "orgs": {"orga": "orga"}}`)),
}

// Check JSON has keys
datatypes.JSONQuery("attributes").HasKey(value, keys...)

db.Find(&user, datatypes.JSONQuery("attributes").HasKey("role"))
db.Find(&user, datatypes.JSONQuery("attributes").HasKey("orgs", "orga"))
// MySQL
// SELECT * FROM `users` WHERE JSON_EXTRACT(`attributes`, '$.role') IS NOT NULL
// SELECT * FROM `users` WHERE JSON_EXTRACT(`attributes`, '$.orgs.orga') IS NOT NULL

// PostgreSQL
// SELECT * FROM "user" WHERE "attributes"::jsonb ? 'role'
// SELECT * FROM "user" WHERE "attributes"::jsonb -> 'orgs' ? 'orga'


// Check JSON extract value from keys equal to value
datatypes.JSONQuery("attributes").Equals(value, keys...)

DB.First(&user, datatypes.JSONQuery("attributes").Equals("jinzhu", "name"))
DB.First(&user, datatypes.JSONQuery("attributes").Equals("orgb", "orgs", "orgb"))
// MySQL
// SELECT * FROM `user` WHERE JSON_EXTRACT(`attributes`, '$.name') = "jinzhu"
// SELECT * FROM `user` WHERE JSON_EXTRACT(`attributes`, '$.orgs.orgb') = "orgb"

// PostgreSQL
// SELECT * FROM "user" WHERE json_extract_path_text("attributes"::json,'name') = 'jinzhu'
// SELECT * FROM "user" WHERE json_extract_path_text("attributes"::json,'orgs','orgb') = 'orgb'

NOTE: SQlite need to build with json1 tag, e.g: go build --tags json1, refer https://github.com/mattn/go-sqlite3#usage

Date

import "github.com/wuruipeng404/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 "github.com/wuruipeng404/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

func Column

func Column(col string) columnExpression

Types

type Date

type Date 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 any) (err error)

func (*Date) UnmarshalJSON

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

func (*Date) Value

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

type JSONOverlapsExpression

type JSONOverlapsExpression struct {
	// contains filtered or unexported fields
}

JSONOverlapsExpression JSON_OVERLAPS expression, implements clause.Expression interface to use as querier

func JSONOverlaps

func JSONOverlaps(column clause.Expression, value string) *JSONOverlapsExpression

JSONOverlaps query column as json

func (*JSONOverlapsExpression) Build

func (json *JSONOverlapsExpression) Build(builder clause.Builder)

Build implements clause.Expression only mysql support JSON_OVERLAPS

type JSONQueryExpression

type JSONQueryExpression struct {
	// contains filtered or unexported fields
}

JSONQueryExpression json query expression, implements clause.Expression interface to use as querier

func JSONQuery

func JSONQuery(column string) *JSONQueryExpression

JSONQuery query column as json

func (*JSONQueryExpression) Build

func (jsonQuery *JSONQueryExpression) Build(builder clause.Builder)

Build implements clause.Expression

func (*JSONQueryExpression) Equals

func (jsonQuery *JSONQueryExpression) Equals(value any, keys ...string) *JSONQueryExpression

Equals Keys returns clause.Expression

func (*JSONQueryExpression) Extract

func (jsonQuery *JSONQueryExpression) Extract(path string) *JSONQueryExpression

Extract extract json with path

func (*JSONQueryExpression) HasKey

func (jsonQuery *JSONQueryExpression) HasKey(keys ...string) *JSONQueryExpression

HasKey returns clause.Expression

type Json

type Json json.RawMessage

Json defined Json data type, need to implements driver.Valuer, sql.Scanner interface

func (*Json) GormDBDataType

func (j *Json) GormDBDataType(db *gorm.DB, _ *schema.Field) string

GormDBDataType gorm db data type

func (*Json) GormDataType

func (j *Json) GormDataType() string

GormDataType gorm common data type

func (*Json) GormValue

func (j *Json) GormValue(_ context.Context, db *gorm.DB) clause.Expr

func (*Json) MarshalJSON

func (j *Json) MarshalJSON() ([]byte, error)

MarshalJSON to output non base64 encoded []byte

func (*Json) Scan

func (j *Json) Scan(value any) error

Scan scan value into Jsonb, implements sql.Scanner interface

func (*Json) String

func (j *Json) String() string

func (*Json) UnmarshalJSON

func (j *Json) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (*Json) Value

func (j *Json) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface

type JsonMap

type JsonMap map[string]any

JsonMap defined Json data type, need to implements driver.Valuer, sql.Scanner interface

func (*JsonMap) GormDBDataType

func (m *JsonMap) GormDBDataType(db *gorm.DB, _ *schema.Field) string

GormDBDataType gorm db data type

func (*JsonMap) GormDataType

func (m *JsonMap) GormDataType() string

GormDataType gorm common data type

func (*JsonMap) GormValue

func (m *JsonMap) GormValue(_ context.Context, db *gorm.DB) clause.Expr

func (*JsonMap) MarshalJSON

func (m *JsonMap) MarshalJSON() ([]byte, error)

MarshalJSON to output non base64 encoded []byte

func (*JsonMap) Scan

func (m *JsonMap) Scan(val any) error

Scan scan value into Jsonb, implements sql.Scanner interface

func (*JsonMap) UnmarshalJSON

func (m *JsonMap) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (*JsonMap) Value

func (m *JsonMap) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface

type Slice

type Slice[T any] []T

func (*Slice[T]) GormDBDataType

func (s *Slice[T]) GormDBDataType(db *gorm.DB, _ *schema.Field) string

GormDBDataType gorm db data type

func (*Slice[T]) GormDataType

func (s *Slice[T]) GormDataType() string

func (*Slice[T]) MarshalJSON

func (s *Slice[T]) MarshalJSON() ([]byte, error)

MarshalJSON to output non base64 encoded []byte

func (*Slice[T]) Scan

func (s *Slice[T]) Scan(value any) (err error)

func (*Slice[T]) UnmarshalJSON

func (s *Slice[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (*Slice[T]) Value

func (s *Slice[T]) 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 (t *Time) GormDBDataType(db *gorm.DB, _ *schema.Field) string

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

func (*Time) GormDataType

func (t *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 any) 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) 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 (u *URL) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (*URL) GormDataType

func (u *URL) GormDataType() string

func (*URL) MarshalJSON

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

func (*URL) Scan

func (u *URL) Scan(value any) error

func (*URL) String

func (u *URL) String() string

func (*URL) UnmarshalJSON

func (u *URL) UnmarshalJSON(data []byte) 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