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

WIP: refactor: refactor funcs with generics #1123

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const (

// WithQueryInterface generate code with exported interface object
WithQueryInterface

// WithGeneric generate code with generic
WithGeneric
)

// Config generator's basic configuration
Expand Down
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
72 changes: 43 additions & 29 deletions field/export.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package field

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

"golang.org/x/exp/constraints"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand All @@ -15,6 +18,12 @@ var (
ALL = Star
)

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

// Option field option
type Option func(clause.Column) clause.Column

Expand All @@ -29,7 +38,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[driver.Valuer]{expr{col: toColumn(table, column, opts...)}})
}

// NewSerializer create new field2
Expand All @@ -44,92 +53,97 @@ func NewAsterisk(table string, opts ...Option) Asterisk {

// ======================== integer =======================

// NewInt create new Int
// NewNumber build number type field
func NewNumber[T constraints.Integer | constraints.Float](table, column string, opts ...Option) Number[T] {
return newNumber[T](expr{col: toColumn(table, column, opts...)})
}

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

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

// NewInt16 ...
// NewInt16 create new field for int16
func NewInt16(table, column string, opts ...Option) Int16 {
return Int16{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[int16](table, column, opts...)
}

// NewInt32 ...
// NewInt32 create new field for int32
func NewInt32(table, column string, opts ...Option) Int32 {
return Int32{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[int32](table, column, opts...)
}

// NewInt64 ...
// NewInt64 create new field for int64
func NewInt64(table, column string, opts ...Option) Int64 {
return Int64{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[int64](table, column, opts...)
}

// NewUint ...
// NewUint create new field for uint
func NewUint(table, column string, opts ...Option) Uint {
return Uint{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[uint](table, column, opts...)
}

// NewUint8 ...
// NewUint8 create new field for uint8
func NewUint8(table, column string, opts ...Option) Uint8 {
return Uint8{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[uint8](table, column, opts...)
}

// NewUint16 ...
// NewUint16 create new field for uint16
func NewUint16(table, column string, opts ...Option) Uint16 {
return Uint16{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[uint16](table, column, opts...)
}

// NewUint32 ...
// NewUint32 create new field for uint32
func NewUint32(table, column string, opts ...Option) Uint32 {
return Uint32{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[uint32](table, column, opts...)
}

// NewUint64 ...
// NewUint64 create new field for uint64
func NewUint64(table, column string, opts ...Option) Uint64 {
return Uint64{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[uint64](table, column, opts...)
}

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

// NewFloat32 ...
// NewFloat32 create new field for float32
func NewFloat32(table, column string, opts ...Option) Float32 {
return Float32{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[float32](table, column, opts...)
}

// NewFloat64 ...
// NewFloat64 create new field for float64
func NewFloat64(table, column string, opts ...Option) Float64 {
return Float64{expr: expr{col: toColumn(table, column, opts...)}}
return NewNumber[float64](table, column, opts...)
}

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

// NewString ...
func NewString(table, column string, opts ...Option) String {
return String{expr: expr{col: toColumn(table, column, opts...)}}
return newChars[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 newChars[[]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
41 changes: 21 additions & 20 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 @@ -171,32 +172,32 @@ func (e expr) IsNotNull() Expr {
return e.setE(clause.Expr{SQL: "? IS NOT NULL", Vars: []interface{}{e.RawExpr()}})
}

func (e expr) Count() Int {
return Int{e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}})}
func (e expr) Count() Number[int] {
return newNumber[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()}})}
func (e expr) Distinct() Number[int] {
return newNumber[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()}})}
func (e expr) Length() Number[int] {
return newNumber[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()}})}
func (e expr) Max() Number[float64] {
return newNumber[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()}})}
func (e expr) Min() Number[float64] {
return newNumber[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()}})}
func (e expr) Avg() Number[float64] {
return newNumber[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()}})}
func (e expr) Abs() Number[float64] {
return newNumber[float64](e.setE(clause.Expr{SQL: "ABS(?)", Vars: []interface{}{e.RawExpr()}}))
}

func (e expr) Null() AssignExpr {
Expand Down Expand Up @@ -238,19 +239,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 +261,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
Loading
Loading