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

feat: generics #833

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
23 changes: 10 additions & 13 deletions field/bool.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package field

// Bool boolean type field
type Bool Field
type Bool struct {
GenericsField[bool]
}

// Not ...
func (field Bool) Not() Bool {
return Bool{field.not()}
return Bool{GenericsField[bool]{field.not()}}
}

// Is ...
Expand All @@ -15,37 +17,32 @@ func (field Bool) Is(value bool) Expr {

// And boolean and
func (field Bool) And(value bool) Expr {
return Bool{field.and(value)}
return Bool{GenericsField[bool]{field.and(value)}}
}

// Or boolean or
func (field Bool) Or(value bool) Expr {
return Bool{field.or(value)}
return Bool{GenericsField[bool]{field.or(value)}}
}

// Xor ...
func (field Bool) Xor(value bool) Expr {
return Bool{field.xor(value)}
return Bool{GenericsField[bool]{field.xor(value)}}
}

// BitXor ...
func (field Bool) BitXor(value bool) Expr {
return Bool{field.bitXor(value)}
return Bool{GenericsField[bool]{field.bitXor(value)}}
}

// BitAnd ...
func (field Bool) BitAnd(value bool) Expr {
return Bool{field.bitAnd(value)}
return Bool{GenericsField[bool]{field.bitAnd(value)}}
}

// BitOr ...
func (field Bool) BitOr(value bool) Expr {
return Bool{field.bitOr(value)}
}

// Value ...
func (field Bool) Value(value bool) AssignExpr {
return field.value(value)
return Bool{GenericsField[bool]{field.bitOr(value)}}
}

// Zero ...
Expand Down
36 changes: 19 additions & 17 deletions field/export.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package field

import (
"database/sql/driver"
"fmt"
"strings"
"time"

"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand All @@ -29,7 +31,7 @@ var (

// NewField create new field
func NewField(table, column string, opts ...Option) Field {
return Field{expr: expr{col: toColumn(table, column, opts...)}}
return Field{GenericsField: GenericsField[driver.Valuer]{expr{col: toColumn(table, column, opts...)}}}
}

// NewSerializer create new field2
Expand All @@ -46,90 +48,90 @@ func NewAsterisk(table string, opts ...Option) Asterisk {

// NewInt create new Int
func NewInt(table, column string, opts ...Option) Int {
return Int{expr: expr{col: toColumn(table, column, opts...)}}
return Int{NewGenericsInt[int](expr{col: toColumn(table, column, opts...)})}
}

// NewInt8 create new Int8
func NewInt8(table, column string, opts ...Option) Int8 {
return Int8{expr: expr{col: toColumn(table, column, opts...)}}
return Int8{NewGenericsInt[int8](expr{col: toColumn(table, column, opts...)})}
}

// NewInt16 ...
func NewInt16(table, column string, opts ...Option) Int16 {
return Int16{expr: expr{col: toColumn(table, column, opts...)}}
return Int16{NewGenericsInt[int16](expr{col: toColumn(table, column, opts...)})}
}

// NewInt32 ...
func NewInt32(table, column string, opts ...Option) Int32 {
return Int32{expr: expr{col: toColumn(table, column, opts...)}}
return Int32{NewGenericsInt[int32](expr{col: toColumn(table, column, opts...)})}
}

// NewInt64 ...
func NewInt64(table, column string, opts ...Option) Int64 {
return Int64{expr: expr{col: toColumn(table, column, opts...)}}
return Int64{NewGenericsInt[int64](expr{col: toColumn(table, column, opts...)})}
}

// NewUint ...
func NewUint(table, column string, opts ...Option) Uint {
return Uint{expr: expr{col: toColumn(table, column, opts...)}}
return Uint{NewGenericsInt[uint](expr{col: toColumn(table, column, opts...)})}
}

// NewUint8 ...
func NewUint8(table, column string, opts ...Option) Uint8 {
return Uint8{expr: expr{col: toColumn(table, column, opts...)}}
return Uint8{NewGenericsInt[uint8](expr{col: toColumn(table, column, opts...)})}
}

// NewUint16 ...
func NewUint16(table, column string, opts ...Option) Uint16 {
return Uint16{expr: expr{col: toColumn(table, column, opts...)}}
return Uint16{NewGenericsInt[uint16](expr{col: toColumn(table, column, opts...)})}
}

// NewUint32 ...
func NewUint32(table, column string, opts ...Option) Uint32 {
return Uint32{expr: expr{col: toColumn(table, column, opts...)}}
return Uint32{NewGenericsInt[uint32](expr{col: toColumn(table, column, opts...)})}
}

// NewUint64 ...
func NewUint64(table, column string, opts ...Option) Uint64 {
return Uint64{expr: expr{col: toColumn(table, column, opts...)}}
return Uint64{NewGenericsInt[uint64](expr{col: toColumn(table, column, opts...)})}
}

// ======================== float =======================

// NewFloat32 ...
func NewFloat32(table, column string, opts ...Option) Float32 {
return Float32{expr: expr{col: toColumn(table, column, opts...)}}
return Float32{NewGenericsInt[float32](expr{col: toColumn(table, column, opts...)})}
}

// NewFloat64 ...
func NewFloat64(table, column string, opts ...Option) Float64 {
return Float64{expr: expr{col: toColumn(table, column, opts...)}}
return Float64{NewGenericsInt[float64](expr{col: toColumn(table, column, opts...)})}
}

// ======================== string =======================

// NewString ...
func NewString(table, column string, opts ...Option) String {
return String{expr: expr{col: toColumn(table, column, opts...)}}
return String{NewGenericsString[string](expr{col: toColumn(table, column, opts...)})}
}

// NewBytes ...
func NewBytes(table, column string, opts ...Option) Bytes {
return Bytes{expr: expr{col: toColumn(table, column, opts...)}}
return Bytes{NewGenericsString[[]byte](expr{col: toColumn(table, column, opts...)})}
}

// ======================== bool =======================

// NewBool ...
func NewBool(table, column string, opts ...Option) Bool {
return Bool{expr: expr{col: toColumn(table, column, opts...)}}
return Bool{NewGenerics[bool](expr{col: toColumn(table, column, opts...)})}
}

// ======================== time =======================

// NewTime ...
func NewTime(table, column string, opts ...Option) Time {
return Time{expr: expr{col: toColumn(table, column, opts...)}}
return Time{NewGenerics[time.Time](expr{col: toColumn(table, column, opts...)})}
}

func toColumn(table, column string, opts ...Option) clause.Column {
Expand Down
30 changes: 17 additions & 13 deletions field/expr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package field

import (
"database/sql/driver"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -172,31 +173,34 @@ func (e expr) IsNotNull() Expr {
}

func (e expr) Count() Int {
return Int{e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}})}
return Int{NewGenericsInt[int](e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}}))}
}

func (e expr) Distinct() Int {
return Int{e.setE(clause.Expr{SQL: "DISTINCT ?", Vars: []interface{}{e.RawExpr()}})}
return Int{NewGenericsInt[int](e.setE(clause.Expr{SQL: "DISTINCT ?", Vars: []interface{}{e.RawExpr()}}))}

}

func (e expr) Length() Int {
return Int{e.setE(clause.Expr{SQL: "LENGTH(?)", Vars: []interface{}{e.RawExpr()}})}
return Int{NewGenericsInt[int](e.setE(clause.Expr{SQL: "LENGTH(?)", Vars: []interface{}{e.RawExpr()}}))}
}

func (e expr) Max() Float64 {
return Float64{e.setE(clause.Expr{SQL: "MAX(?)", Vars: []interface{}{e.RawExpr()}})}
return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "MAX(?)", Vars: []interface{}{e.RawExpr()}}))}
}

func (e expr) Min() Float64 {
return Float64{e.setE(clause.Expr{SQL: "MIN(?)", Vars: []interface{}{e.RawExpr()}})}
return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "MIN(?)", Vars: []interface{}{e.RawExpr()}}))}

}

func (e expr) Avg() Float64 {
return Float64{e.setE(clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.RawExpr()}})}
return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.RawExpr()}}))}

}

func (e expr) Abs() Float64 {
return Float64{e.setE(clause.Expr{SQL: "ABS(?)", Vars: []interface{}{e.RawExpr()}})}
return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "ABS(?)", Vars: []interface{}{e.RawExpr()}}))}
}

func (e expr) Null() AssignExpr {
Expand Down Expand Up @@ -238,19 +242,19 @@ func (e expr) SetCol(col Expr) AssignExpr {

// ======================== operate columns ========================
func (e expr) AddCol(col Expr) Expr {
return Field{e.setE(clause.Expr{SQL: "? + ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}
return Field{GenericsField[driver.Valuer]{e.setE(clause.Expr{SQL: "? + ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}}
}

func (e expr) SubCol(col Expr) Expr {
return Field{e.setE(clause.Expr{SQL: "? - ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}
return Field{GenericsField[driver.Valuer]{e.setE(clause.Expr{SQL: "? - ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}}
}

func (e expr) MulCol(col Expr) Expr {
return Field{e.setE(clause.Expr{SQL: "(?) * (?)", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}
return Field{GenericsField[driver.Valuer]{e.setE(clause.Expr{SQL: "(?) * (?)", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}}
}

func (e expr) DivCol(col Expr) Expr {
return Field{e.setE(clause.Expr{SQL: "(?) / (?)", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}
return Field{GenericsField[driver.Valuer]{e.setE(clause.Expr{SQL: "(?) / (?)", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})}}
}

func (e expr) ConcatCol(cols ...Expr) Expr {
Expand All @@ -260,10 +264,10 @@ func (e expr) ConcatCol(cols ...Expr) Expr {
placeholders = append(placeholders, "?")
vars = append(vars, col.RawExpr())
}
return Field{e.setE(clause.Expr{
return Field{GenericsField[driver.Valuer]{e.setE(clause.Expr{
SQL: fmt.Sprintf("Concat(%s)", strings.Join(placeholders, ",")),
Vars: vars,
})}
})}}
}

// ======================== keyword ========================
Expand Down
82 changes: 3 additions & 79 deletions field/field.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,8 @@
package field

import (
"database/sql/driver"

"gorm.io/gorm/clause"
)

// ScanValuer interface for Field
type ScanValuer interface {
Scan(src interface{}) error // sql.Scanner
Value() (driver.Value, error) // driver.Valuer
}
import "database/sql/driver"

// Field a standard field struct
type Field struct{ expr }

// Eq judge equal
func (field Field) Eq(value driver.Valuer) Expr {
return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}}
}

// Neq judge not equal
func (field Field) Neq(value driver.Valuer) Expr {
return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}}
}

// In ...
func (field Field) In(values ...driver.Valuer) Expr {
return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values...)}}
}

// Gt ...
func (field Field) Gt(value driver.Valuer) Expr {
return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}}
}

// Gte ...
func (field Field) Gte(value driver.Valuer) Expr {
return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}}
}

// Lt ...
func (field Field) Lt(value driver.Valuer) Expr {
return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}}
}

// Lte ...
func (field Field) Lte(value driver.Valuer) Expr {
return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}}
}

// Like ...
func (field Field) Like(value driver.Valuer) Expr {
return expr{e: clause.Like{Column: field.RawExpr(), Value: value}}
}

// Value ...
func (field Field) Value(value driver.Valuer) AssignExpr {
return field.value(value)
}

// Sum ...
func (field Field) Sum() Field {
return Field{field.sum()}
}

// IfNull ...
func (field Field) IfNull(value driver.Valuer) Expr {
return field.ifNull(value)
}

// Field ...
func (field Field) Field(value []interface{}) Expr {
return field.field(value)
}

func (field Field) toSlice(values ...driver.Valuer) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
slice[i] = v
}
return slice
type Field struct {
GenericsField[driver.Valuer]
}
Loading
Loading