Skip to content

Commit

Permalink
Improvement/appearance (#60)
Browse files Browse the repository at this point in the history
* join number to appearance in dynamic and order

* join number to appearance in update

* fix lint2

* update docs

* cqlint not concerned support appearance

* cqllint: support appearance

* cqllint support true

* cqllint: support connector

* verify appearance is in range

* update docs

* fix lint

* remove unused code
  • Loading branch information
FrancoLiberali authored Jan 15, 2024
1 parent 426007c commit 8578021
Show file tree
Hide file tree
Showing 31 changed files with 817 additions and 245 deletions.
12 changes: 4 additions & 8 deletions condition/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ type Delete[T model.Model] struct {

// Ascending specify an ascending order when updating models
//
// joinNumber can be used to select the join in case the field is joined more than once
//
// available for: mysql
func (deleteS *Delete[T]) Ascending(field IField, joinNumber ...uint) *Delete[T] {
deleteS.OrderLimitReturning.Ascending(field, joinNumber...)
func (deleteS *Delete[T]) Ascending(field IField) *Delete[T] {
deleteS.OrderLimitReturning.Ascending(field)

return deleteS
}

// Descending specify a descending order when updating models
//
// joinNumber can be used to select the join in case the field is joined more than once
//
// available for: mysql
func (deleteS *Delete[T]) Descending(field IField, joinNumber ...uint) *Delete[T] {
deleteS.OrderLimitReturning.Descending(field, joinNumber...)
func (deleteS *Delete[T]) Descending(field IField) *Delete[T] {
deleteS.OrderLimitReturning.Descending(field)

return deleteS
}
Expand Down
13 changes: 0 additions & 13 deletions condition/dynamic_condition.go

This file was deleted.

22 changes: 14 additions & 8 deletions condition/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
var (
// query

ErrFieldModelNotConcerned = errors.New("field's model is not concerned by the query (not joined)")
ErrJoinMustBeSelected = errors.New("field's model is joined more than once, select which one you want to use")
ErrFieldIsRepeated = errors.New("field is repeated")
ErrFieldModelNotConcerned = errors.New("field's model is not concerned by the query (not joined)")
ErrAppearanceMustBeSelected = errors.New("field's model appears more than once, select which one you want to use with Appearance")
ErrAppearanceOutOfRange = errors.New("selected appearance is bigger than field's model number of appearances")
ErrFieldIsRepeated = errors.New("field is repeated")

// conditions

Expand Down Expand Up @@ -45,11 +46,16 @@ func fieldModelNotConcernedError(field IField) error {
)
}

func joinMustBeSelectedError(field IField) error {
return fmt.Errorf("%w; joined multiple times model: %s",
ErrJoinMustBeSelected,
field.getModelType(),
)
func fieldModelError(err error, field IField) error {
return fmt.Errorf("%w; model: %s", err, field.getModelType())
}

func appearanceMustBeSelectedError(field IField) error {
return fieldModelError(ErrAppearanceMustBeSelected, field)
}

func appearanceOutOfRangeError(field IField) error {
return fieldModelError(ErrAppearanceOutOfRange, field)
}

func fieldIsRepeatedError(field IField) error {
Expand Down
80 changes: 77 additions & 3 deletions condition/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ type IField interface {
fieldName() string
columnSQL(query *GormQuery, table Table) string
getModelType() reflect.Type
getAppearance() int
}

type Field[TModel model.Model, TAttribute any] struct {
column string
name string
columnPrefix string
column string
name string
columnPrefix string
appearance uint
appearanceSelected bool
}

// Is allows creating conditions that include the field and a static value
Expand All @@ -41,6 +44,27 @@ func (field Field[TModel, TAttribute]) Value() *FieldValue[TModel, TAttribute] {
return NewFieldValue(field)
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (field Field[TModel, TAttribute]) Appearance(number uint) Field[TModel, TAttribute] {
newField := NewField[TModel, TAttribute](
field.name, field.column, field.columnPrefix,
)

newField.appearanceSelected = true
newField.appearance = number

return newField
}

func (field Field[TModel, TAttribute]) getAppearance() int {
if !field.appearanceSelected {
return undefinedAppearance
}

return int(field.appearance)
}

func (field Field[TModel, TAttribute]) getModelType() reflect.Type {
return reflect.TypeOf(*new(TModel))
}
Expand Down Expand Up @@ -82,6 +106,12 @@ func (field UpdatableField[TModel, TAttribute]) Set() FieldSet[TModel, TAttribut
return FieldSet[TModel, TAttribute]{field: field}
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (field UpdatableField[TModel, TAttribute]) Appearance(number uint) UpdatableField[TModel, TAttribute] {
return UpdatableField[TModel, TAttribute]{Field: field.Field.Appearance(number)}
}

func NewUpdatableField[TModel model.Model, TAttribute any](name, column, columnPrefix string) UpdatableField[TModel, TAttribute] {
return UpdatableField[TModel, TAttribute]{
Field: NewField[TModel, TAttribute](name, column, columnPrefix),
Expand All @@ -96,6 +126,14 @@ func (field NullableField[TModel, TAttribute]) Set() NullableFieldSet[TModel, TA
return NullableFieldSet[TModel, TAttribute]{FieldSet[TModel, TAttribute]{field: field.UpdatableField}}
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (field NullableField[TModel, TAttribute]) Appearance(number uint) NullableField[TModel, TAttribute] {
return NullableField[TModel, TAttribute]{
UpdatableField: UpdatableField[TModel, TAttribute]{Field: field.Field.Appearance(number)},
}
}

func NewNullableField[TModel model.Model, TAttribute any](name, column, columnPrefix string) NullableField[TModel, TAttribute] {
return NullableField[TModel, TAttribute]{
UpdatableField: NewUpdatableField[TModel, TAttribute](name, column, columnPrefix),
Expand Down Expand Up @@ -151,6 +189,14 @@ func (stringField StringField[TModel]) Value() *StringFieldValue[TModel] {
return &StringFieldValue[TModel]{FieldValue: *stringField.UpdatableField.Value()}
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (stringField StringField[TModel]) Appearance(number uint) StringField[TModel] {
return StringField[TModel]{
UpdatableField: UpdatableField[TModel, string]{Field: stringField.Field.Appearance(number)},
}
}

func NewStringField[TModel model.Model](name, column, columnPrefix string) StringField[TModel] {
return StringField[TModel]{
UpdatableField: NewUpdatableField[TModel, string](name, column, columnPrefix),
Expand All @@ -174,6 +220,16 @@ func (stringField NullableStringField[TModel]) Value() *StringFieldValue[TModel]
return &StringFieldValue[TModel]{FieldValue: *stringField.UpdatableField.Value()}
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (stringField NullableStringField[TModel]) Appearance(number uint) NullableStringField[TModel] {
return NullableStringField[TModel]{
NullableField: NullableField[TModel, string]{
UpdatableField: UpdatableField[TModel, string]{Field: stringField.Field.Appearance(number)},
},
}
}

func NewNullableStringField[TModel model.Model](name, column, columnPrefix string) NullableStringField[TModel] {
return NullableStringField[TModel]{
NullableField: NewNullableField[TModel, string](name, column, columnPrefix),
Expand Down Expand Up @@ -206,6 +262,14 @@ func (numericField NumericField[TModel, TAttribute]) Set() NumericFieldSet[TMode
return NumericFieldSet[TModel, TAttribute]{field: numericField}
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (numericField NumericField[TModel, TAttribute]) Appearance(number uint) NumericField[TModel, TAttribute] {
return NumericField[TModel, TAttribute]{
UpdatableField: UpdatableField[TModel, TAttribute]{Field: numericField.Field.Appearance(number)},
}
}

type NullableNumericField[
TModel model.Model,
TAttribute int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64,
Expand All @@ -217,6 +281,16 @@ func (field NullableNumericField[TModel, TAttribute]) Set() NullableFieldSet[TMo
return NullableFieldSet[TModel, TAttribute]{FieldSet[TModel, TAttribute]{field: field.UpdatableField}}
}

// Appearance allows to choose which number of appearance use
// when field's model is joined more than once.
func (field NullableNumericField[TModel, TAttribute]) Appearance(number uint) NullableNumericField[TModel, TAttribute] {
return NullableNumericField[TModel, TAttribute]{
NumericField: NumericField[TModel, TAttribute]{
UpdatableField: UpdatableField[TModel, TAttribute]{Field: field.Field.Appearance(number)},
},
}
}

func NewNullableNumericField[
TModel model.Model,
TAttribute int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64,
Expand Down
11 changes: 1 addition & 10 deletions condition/field_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,10 @@ func (condition fieldCondition[TObject, TAtribute]) getSQL(query *GormQuery, tab
return sqlString, values, nil
}

func (condition *fieldCondition[TObject, TAtribute]) SelectJoin(operationNumber, joinNumber uint) DynamicCondition[TObject] {
dynamicOperator, isDynamic := condition.Operator.(DynamicOperator[TAtribute])
if isDynamic {
condition.Operator = dynamicOperator.SelectJoin(operationNumber, joinNumber)
}

return condition
}

func NewFieldCondition[TObject model.Model, TAttribute any](
fieldIdentifier Field[TObject, TAttribute],
operator Operator[TAttribute],
) DynamicCondition[TObject] {
) WhereCondition[TObject] {
return &fieldCondition[TObject, TAttribute]{
FieldIdentifier: fieldIdentifier,
Operator: operator,
Expand Down
40 changes: 20 additions & 20 deletions condition/field_is_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@ type DynamicFieldIs[TObject model.Model, TAttribute any] struct {
// - SQLite: https://www.sqlite.org/lang_expr.html

// EqualTo
func (is DynamicFieldIs[TObject, TAttribute]) Eq(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) Eq(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Eq[TAttribute](value))
}

// NotEqualTo
func (is DynamicFieldIs[TObject, TAttribute]) NotEq(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) NotEq(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, NotEq[TAttribute](value))
}

// LessThan
func (is DynamicFieldIs[TObject, TAttribute]) Lt(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) Lt(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Lt[TAttribute](value))
}

// LessThanOrEqualTo
func (is DynamicFieldIs[TObject, TAttribute]) LtOrEq(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) LtOrEq(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, LtOrEq[TAttribute](value))
}

// GreaterThan
func (is DynamicFieldIs[TObject, TAttribute]) Gt(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) Gt(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Gt[TAttribute](value))
}

// GreaterThanOrEqualTo
func (is DynamicFieldIs[TObject, TAttribute]) GtOrEq(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) GtOrEq(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, GtOrEq[TAttribute](value))
}

Expand All @@ -53,20 +53,20 @@ func (is DynamicFieldIs[TObject, TAttribute]) GtOrEq(value ValueOfType[TAttribut
// - SQLite: https://www.sqlite.org/lang_expr.html

// Equivalent to field1 < value < field2
func (is DynamicFieldIs[TObject, TAttribute]) Between(value1, value2 ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) Between(value1, value2 ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Between[TAttribute](value1, value2))
}

// Equivalent to NOT (field1 < value < field2)
func (is DynamicFieldIs[TObject, TAttribute]) NotBetween(value1, value2 ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) NotBetween(value1, value2 ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, NotBetween[TAttribute](value1, value2))
}

func (is DynamicFieldIs[TObject, TAttribute]) Distinct(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) Distinct(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, IsDistinct[TAttribute](value))
}

func (is DynamicFieldIs[TObject, TAttribute]) NotDistinct(value ValueOfType[TAttribute]) DynamicCondition[TObject] {
func (is DynamicFieldIs[TObject, TAttribute]) NotDistinct(value ValueOfType[TAttribute]) WhereCondition[TObject] {
return NewFieldCondition(is.field, IsNotDistinct[TAttribute](value))
}

Expand All @@ -82,32 +82,32 @@ type NumericDynamicFieldIs[TObject model.Model, TAttribute any] struct {
// - SQLite: https://www.sqlite.org/lang_expr.html

// EqualTo
func (is NumericDynamicFieldIs[TObject, TAttribute]) Eq(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) Eq(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Eq[TAttribute](value))
}

// NotEqualTo
func (is NumericDynamicFieldIs[TObject, TAttribute]) NotEq(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) NotEq(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, NotEq[TAttribute](value))
}

// LessThan
func (is NumericDynamicFieldIs[TObject, TAttribute]) Lt(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) Lt(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Lt[TAttribute](value))
}

// LessThanOrEqualTo
func (is NumericDynamicFieldIs[TObject, TAttribute]) LtOrEq(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) LtOrEq(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, LtOrEq[TAttribute](value))
}

// GreaterThan
func (is NumericDynamicFieldIs[TObject, TAttribute]) Gt(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) Gt(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Gt[TAttribute](value))
}

// GreaterThanOrEqualTo
func (is NumericDynamicFieldIs[TObject, TAttribute]) GtOrEq(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) GtOrEq(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, GtOrEq[TAttribute](value))
}

Expand All @@ -119,19 +119,19 @@ func (is NumericDynamicFieldIs[TObject, TAttribute]) GtOrEq(value ValueOfType[nu
// - SQLite: https://www.sqlite.org/lang_expr.html

// Equivalent to field1 < value < field2
func (is NumericDynamicFieldIs[TObject, TAttribute]) Between(value1, value2 ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) Between(value1, value2 ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, Between[TAttribute](value1, value2))
}

// Equivalent to NOT (field1 < value < field2)
func (is NumericDynamicFieldIs[TObject, TAttribute]) NotBetween(value1, value2 ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) NotBetween(value1, value2 ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, NotBetween[TAttribute](value1, value2))
}

func (is NumericDynamicFieldIs[TObject, TAttribute]) Distinct(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) Distinct(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, IsDistinct[TAttribute](value))
}

func (is NumericDynamicFieldIs[TObject, TAttribute]) NotDistinct(value ValueOfType[numeric]) DynamicCondition[TObject] {
func (is NumericDynamicFieldIs[TObject, TAttribute]) NotDistinct(value ValueOfType[numeric]) WhereCondition[TObject] {
return NewFieldCondition(is.field, IsNotDistinct[TAttribute](value))
}
Loading

0 comments on commit 8578021

Please sign in to comment.