Skip to content

Commit

Permalink
feat: generics field
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed Apr 18, 2023
1 parent e9e31a7 commit 643a88b
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 2,169 deletions.
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{GenericsInt[int]{GenericsField: GenericsField[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{GenericsInt[int8]{GenericsField: GenericsField[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{GenericsInt[int16]{GenericsField: GenericsField[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{GenericsInt[int32]{GenericsField: GenericsField[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{GenericsInt[int64]{GenericsField: GenericsField[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{GenericsInt[uint]{GenericsField: GenericsField[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{GenericsInt[uint8]{GenericsField: GenericsField[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{GenericsInt[uint16]{GenericsField: GenericsField[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{GenericsInt[uint32]{GenericsField: GenericsField[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{GenericsInt[uint64]{GenericsField: GenericsField[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{GenericsInt[float32]{GenericsField: GenericsField[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{GenericsInt[float64]{GenericsField: GenericsField[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{GenericsString[string]{GenericsField: GenericsField[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{GenericsString[[]byte]{GenericsField: GenericsField[[]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{GenericsField[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{GenericsField[time.Time]{expr{col: toColumn(table, column, opts...)}}}
}

func toColumn(table, column string, opts ...Option) clause.Column {
Expand Down
25 changes: 13 additions & 12 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 @@ -167,27 +168,27 @@ func (e expr) IsNotNull() Expr {
}

func (e expr) Count() Int {
return Int{e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}})}
return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[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{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[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{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[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{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[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{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[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{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[float64]{e.setE(clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.RawExpr()}})}}}
}

func (e expr) Null() AssignExpr {
Expand Down Expand Up @@ -229,19 +230,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 @@ -251,10 +252,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
77 changes: 3 additions & 74 deletions field/field.go
Original file line number Diff line number Diff line change
@@ -1,79 +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)
}

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

0 comments on commit 643a88b

Please sign in to comment.