diff --git a/dialect/sql/schema/mysql.go b/dialect/sql/schema/mysql.go index 0641c7a68..1a706db04 100644 --- a/dialect/sql/schema/mysql.go +++ b/dialect/sql/schema/mysql.go @@ -250,9 +250,14 @@ func (d *MySQL) cType(c *Column) (t string) { if size == 0 { size = d.defaultSize(c) } - if size <= math.MaxUint16 { + switch { + case c.typ == "tinytext", c.typ == "text": + t = c.typ + case size <= math.MaxUint16: t = fmt.Sprintf("varchar(%d)", size) - } else { + case size == 1<<24-1: + t = "mediumtext" + default: t = "longtext" } case field.TypeFloat32, field.TypeFloat64: diff --git a/dialect/sql/schema/mysql_test.go b/dialect/sql/schema/mysql_test.go index 6746a421e..c6c0c2350 100644 --- a/dialect/sql/schema/mysql_test.go +++ b/dialect/sql/schema/mysql_test.go @@ -250,6 +250,7 @@ func TestMySQL_Create(t *testing.T) { {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "name", Type: field.TypeString, Nullable: true}, {Name: "text", Type: field.TypeString, Nullable: true, Size: math.MaxInt32}, + {Name: "mediumtext", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{dialect.MySQL: "mediumtext"}}, {Name: "uuid", Type: field.TypeUUID, Nullable: true}, {Name: "date", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{dialect.MySQL: "date"}}, {Name: "age", Type: field.TypeInt}, @@ -278,6 +279,7 @@ func TestMySQL_Create(t *testing.T) { AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "", nil, nil). AddRow("name", "varchar(255)", "YES", "YES", "NULL", "", "", "", nil, nil). AddRow("text", "longtext", "YES", "YES", "NULL", "", "", "", nil, nil). + AddRow("mediumtext", "mediumtext", "YES", "YES", "NULL", "", "", "", nil, nil). AddRow("uuid", "char(36)", "YES", "YES", "NULL", "", "", "utf8mb4_bin", nil, nil). AddRow("date", "date", "YES", "YES", "NULL", "", "", "", nil, nil). // 8.0.19: new int column type formats diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index 921741d9a..3aacf7444 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -101,6 +101,7 @@ var schemaGraph = func() *sqlgraph.Schema { fieldtype.FieldState: {Type: field.TypeEnum, Column: fieldtype.FieldState}, fieldtype.FieldOptionalFloat: {Type: field.TypeFloat64, Column: fieldtype.FieldOptionalFloat}, fieldtype.FieldOptionalFloat32: {Type: field.TypeFloat32, Column: fieldtype.FieldOptionalFloat32}, + fieldtype.FieldText: {Type: field.TypeString, Column: fieldtype.FieldText}, fieldtype.FieldDatetime: {Type: field.TypeTime, Column: fieldtype.FieldDatetime}, fieldtype.FieldDecimal: {Type: field.TypeFloat64, Column: fieldtype.FieldDecimal}, fieldtype.FieldLinkOther: {Type: field.TypeOther, Column: fieldtype.FieldLinkOther}, @@ -955,6 +956,11 @@ func (f *FieldTypeFilter) WhereOptionalFloat32(p entql.Float32P) { f.Where(p.Field(fieldtype.FieldOptionalFloat32)) } +// WhereText applies the entql string predicate on the text field. +func (f *FieldTypeFilter) WhereText(p entql.StringP) { + f.Where(p.Field(fieldtype.FieldText)) +} + // WhereDatetime applies the entql time.Time predicate on the datetime field. func (f *FieldTypeFilter) WhereDatetime(p entql.TimeP) { f.Where(p.Field(fieldtype.FieldDatetime)) diff --git a/entc/integration/ent/fieldtype.go b/entc/integration/ent/fieldtype.go index 5acc6f02d..22b7ffacf 100644 --- a/entc/integration/ent/fieldtype.go +++ b/entc/integration/ent/fieldtype.go @@ -74,6 +74,8 @@ type FieldType struct { OptionalFloat float64 `json:"optional_float,omitempty"` // OptionalFloat32 holds the value of the "optional_float32" field. OptionalFloat32 float32 `json:"optional_float32,omitempty"` + // Text holds the value of the "text" field. + Text string `json:"text,omitempty"` // Datetime holds the value of the "datetime" field. Datetime time.Time `json:"datetime,omitempty"` // Decimal holds the value of the "decimal" field. @@ -196,7 +198,7 @@ func (*FieldType) scanValues(columns []string) ([]interface{}, error) { values[i] = new(sql.NullFloat64) case fieldtype.FieldID, fieldtype.FieldInt, fieldtype.FieldInt8, fieldtype.FieldInt16, fieldtype.FieldInt32, fieldtype.FieldInt64, fieldtype.FieldOptionalInt, fieldtype.FieldOptionalInt8, fieldtype.FieldOptionalInt16, fieldtype.FieldOptionalInt32, fieldtype.FieldOptionalInt64, fieldtype.FieldNillableInt, fieldtype.FieldNillableInt8, fieldtype.FieldNillableInt16, fieldtype.FieldNillableInt32, fieldtype.FieldNillableInt64, fieldtype.FieldValidateOptionalInt32, fieldtype.FieldOptionalUint, fieldtype.FieldOptionalUint8, fieldtype.FieldOptionalUint16, fieldtype.FieldOptionalUint32, fieldtype.FieldOptionalUint64, fieldtype.FieldDuration, fieldtype.FieldNullInt64, fieldtype.FieldSchemaInt, fieldtype.FieldSchemaInt8, fieldtype.FieldSchemaInt64: values[i] = new(sql.NullInt64) - case fieldtype.FieldState, fieldtype.FieldPassword, fieldtype.FieldDir, fieldtype.FieldNdir, fieldtype.FieldStr, fieldtype.FieldNullStr, fieldtype.FieldRole: + case fieldtype.FieldState, fieldtype.FieldText, fieldtype.FieldPassword, fieldtype.FieldDir, fieldtype.FieldNdir, fieldtype.FieldStr, fieldtype.FieldNullStr, fieldtype.FieldRole: values[i] = new(sql.NullString) case fieldtype.FieldDatetime, fieldtype.FieldDeletedAt: values[i] = new(sql.NullTime) @@ -374,6 +376,12 @@ func (ft *FieldType) assignValues(columns []string, values []interface{}) error } else if value.Valid { ft.OptionalFloat32 = float32(value.Float64) } + case fieldtype.FieldText: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field text", values[i]) + } else if value.Valid { + ft.Text = value.String + } case fieldtype.FieldDatetime: if value, ok := values[i].(*sql.NullTime); !ok { return fmt.Errorf("unexpected type %T for field datetime", values[i]) @@ -713,6 +721,8 @@ func (ft *FieldType) String() string { builder.WriteString(fmt.Sprintf("%v", ft.OptionalFloat)) builder.WriteString(", optional_float32=") builder.WriteString(fmt.Sprintf("%v", ft.OptionalFloat32)) + builder.WriteString(", text=") + builder.WriteString(ft.Text) builder.WriteString(", datetime=") builder.WriteString(ft.Datetime.Format(time.ANSIC)) builder.WriteString(", decimal=") diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index a54ae0773..fd110a95e 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -70,6 +70,8 @@ const ( FieldOptionalFloat = "optional_float" // FieldOptionalFloat32 holds the string denoting the optional_float32 field in the database. FieldOptionalFloat32 = "optional_float32" + // FieldText holds the string denoting the text field in the database. + FieldText = "text" // FieldDatetime holds the string denoting the datetime field in the database. FieldDatetime = "datetime" // FieldDecimal holds the string denoting the decimal field in the database. @@ -181,6 +183,7 @@ var Columns = []string{ FieldState, FieldOptionalFloat, FieldOptionalFloat32, + FieldText, FieldDatetime, FieldDecimal, FieldLinkOther, diff --git a/entc/integration/ent/fieldtype/where.go b/entc/integration/ent/fieldtype/where.go index 9fb6ab1f5..f36187c50 100644 --- a/entc/integration/ent/fieldtype/where.go +++ b/entc/integration/ent/fieldtype/where.go @@ -262,6 +262,13 @@ func OptionalFloat32(v float32) predicate.FieldType { }) } +// Text applies equality check predicate on the "text" field. It's identical to TextEQ. +func Text(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldText), v)) + }) +} + // Datetime applies equality check predicate on the "datetime" field. It's identical to DatetimeEQ. func Datetime(v time.Time) predicate.FieldType { return predicate.FieldType(func(s *sql.Selector) { @@ -2594,6 +2601,131 @@ func OptionalFloat32NotNil() predicate.FieldType { }) } +// TextEQ applies the EQ predicate on the "text" field. +func TextEQ(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldText), v)) + }) +} + +// TextNEQ applies the NEQ predicate on the "text" field. +func TextNEQ(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldText), v)) + }) +} + +// TextIn applies the In predicate on the "text" field. +func TextIn(vs ...string) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.In(s.C(FieldText), v...)) + }) +} + +// TextNotIn applies the NotIn predicate on the "text" field. +func TextNotIn(vs ...string) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.NotIn(s.C(FieldText), v...)) + }) +} + +// TextGT applies the GT predicate on the "text" field. +func TextGT(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldText), v)) + }) +} + +// TextGTE applies the GTE predicate on the "text" field. +func TextGTE(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldText), v)) + }) +} + +// TextLT applies the LT predicate on the "text" field. +func TextLT(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldText), v)) + }) +} + +// TextLTE applies the LTE predicate on the "text" field. +func TextLTE(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldText), v)) + }) +} + +// TextContains applies the Contains predicate on the "text" field. +func TextContains(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.Contains(s.C(FieldText), v)) + }) +} + +// TextHasPrefix applies the HasPrefix predicate on the "text" field. +func TextHasPrefix(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.HasPrefix(s.C(FieldText), v)) + }) +} + +// TextHasSuffix applies the HasSuffix predicate on the "text" field. +func TextHasSuffix(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.HasSuffix(s.C(FieldText), v)) + }) +} + +// TextIsNil applies the IsNil predicate on the "text" field. +func TextIsNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldText))) + }) +} + +// TextNotNil applies the NotNil predicate on the "text" field. +func TextNotNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldText))) + }) +} + +// TextEqualFold applies the EqualFold predicate on the "text" field. +func TextEqualFold(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EqualFold(s.C(FieldText), v)) + }) +} + +// TextContainsFold applies the ContainsFold predicate on the "text" field. +func TextContainsFold(v string) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.ContainsFold(s.C(FieldText), v)) + }) +} + // DatetimeEQ applies the EQ predicate on the "datetime" field. func DatetimeEQ(v time.Time) predicate.FieldType { return predicate.FieldType(func(s *sql.Selector) { diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index c76605805..17a5f1c52 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -327,6 +327,20 @@ func (ftc *FieldTypeCreate) SetNillableOptionalFloat32(f *float32) *FieldTypeCre return ftc } +// SetText sets the "text" field. +func (ftc *FieldTypeCreate) SetText(s string) *FieldTypeCreate { + ftc.mutation.SetText(s) + return ftc +} + +// SetNillableText sets the "text" field if the given value is not nil. +func (ftc *FieldTypeCreate) SetNillableText(s *string) *FieldTypeCreate { + if s != nil { + ftc.SetText(*s) + } + return ftc +} + // SetDatetime sets the "datetime" field. func (ftc *FieldTypeCreate) SetDatetime(t time.Time) *FieldTypeCreate { ftc.mutation.SetDatetime(t) @@ -1169,6 +1183,14 @@ func (ftc *FieldTypeCreate) createSpec() (*FieldType, *sqlgraph.CreateSpec) { }) _node.OptionalFloat32 = value } + if value, ok := ftc.mutation.Text(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: fieldtype.FieldText, + }) + _node.Text = value + } if value, ok := ftc.mutation.Datetime(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, @@ -2083,6 +2105,24 @@ func (u *FieldTypeUpsert) ClearOptionalFloat32() *FieldTypeUpsert { return u } +// SetText sets the "text" field. +func (u *FieldTypeUpsert) SetText(v string) *FieldTypeUpsert { + u.Set(fieldtype.FieldText, v) + return u +} + +// UpdateText sets the "text" field to the value that was provided on create. +func (u *FieldTypeUpsert) UpdateText() *FieldTypeUpsert { + u.SetExcluded(fieldtype.FieldText) + return u +} + +// ClearText clears the value of the "text" field. +func (u *FieldTypeUpsert) ClearText() *FieldTypeUpsert { + u.SetNull(fieldtype.FieldText) + return u +} + // SetDatetime sets the "datetime" field. func (u *FieldTypeUpsert) SetDatetime(v time.Time) *FieldTypeUpsert { u.Set(fieldtype.FieldDatetime, v) @@ -3493,6 +3533,27 @@ func (u *FieldTypeUpsertOne) ClearOptionalFloat32() *FieldTypeUpsertOne { }) } +// SetText sets the "text" field. +func (u *FieldTypeUpsertOne) SetText(v string) *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.SetText(v) + }) +} + +// UpdateText sets the "text" field to the value that was provided on create. +func (u *FieldTypeUpsertOne) UpdateText() *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.UpdateText() + }) +} + +// ClearText clears the value of the "text" field. +func (u *FieldTypeUpsertOne) ClearText() *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.ClearText() + }) +} + // SetDatetime sets the "datetime" field. func (u *FieldTypeUpsertOne) SetDatetime(v time.Time) *FieldTypeUpsertOne { return u.Update(func(s *FieldTypeUpsert) { @@ -5188,6 +5249,27 @@ func (u *FieldTypeUpsertBulk) ClearOptionalFloat32() *FieldTypeUpsertBulk { }) } +// SetText sets the "text" field. +func (u *FieldTypeUpsertBulk) SetText(v string) *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.SetText(v) + }) +} + +// UpdateText sets the "text" field to the value that was provided on create. +func (u *FieldTypeUpsertBulk) UpdateText() *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.UpdateText() + }) +} + +// ClearText clears the value of the "text" field. +func (u *FieldTypeUpsertBulk) ClearText() *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.ClearText() + }) +} + // SetDatetime sets the "datetime" field. func (u *FieldTypeUpsertBulk) SetDatetime(v time.Time) *FieldTypeUpsertBulk { return u.Update(func(s *FieldTypeUpsert) { diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index 62ceb292c..347aed59e 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -608,6 +608,26 @@ func (ftu *FieldTypeUpdate) ClearOptionalFloat32() *FieldTypeUpdate { return ftu } +// SetText sets the "text" field. +func (ftu *FieldTypeUpdate) SetText(s string) *FieldTypeUpdate { + ftu.mutation.SetText(s) + return ftu +} + +// SetNillableText sets the "text" field if the given value is not nil. +func (ftu *FieldTypeUpdate) SetNillableText(s *string) *FieldTypeUpdate { + if s != nil { + ftu.SetText(*s) + } + return ftu +} + +// ClearText clears the value of the "text" field. +func (ftu *FieldTypeUpdate) ClearText() *FieldTypeUpdate { + ftu.mutation.ClearText() + return ftu +} + // SetDatetime sets the "datetime" field. func (ftu *FieldTypeUpdate) SetDatetime(t time.Time) *FieldTypeUpdate { ftu.mutation.SetDatetime(t) @@ -1887,6 +1907,19 @@ func (ftu *FieldTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: fieldtype.FieldOptionalFloat32, }) } + if value, ok := ftu.mutation.Text(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: fieldtype.FieldText, + }) + } + if ftu.mutation.TextCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Column: fieldtype.FieldText, + }) + } if value, ok := ftu.mutation.Datetime(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, @@ -3023,6 +3056,26 @@ func (ftuo *FieldTypeUpdateOne) ClearOptionalFloat32() *FieldTypeUpdateOne { return ftuo } +// SetText sets the "text" field. +func (ftuo *FieldTypeUpdateOne) SetText(s string) *FieldTypeUpdateOne { + ftuo.mutation.SetText(s) + return ftuo +} + +// SetNillableText sets the "text" field if the given value is not nil. +func (ftuo *FieldTypeUpdateOne) SetNillableText(s *string) *FieldTypeUpdateOne { + if s != nil { + ftuo.SetText(*s) + } + return ftuo +} + +// ClearText clears the value of the "text" field. +func (ftuo *FieldTypeUpdateOne) ClearText() *FieldTypeUpdateOne { + ftuo.mutation.ClearText() + return ftuo +} + // SetDatetime sets the "datetime" field. func (ftuo *FieldTypeUpdateOne) SetDatetime(t time.Time) *FieldTypeUpdateOne { ftuo.mutation.SetDatetime(t) @@ -4326,6 +4379,19 @@ func (ftuo *FieldTypeUpdateOne) sqlSave(ctx context.Context) (_node *FieldType, Column: fieldtype.FieldOptionalFloat32, }) } + if value, ok := ftuo.mutation.Text(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: fieldtype.FieldText, + }) + } + if ftuo.mutation.TextCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Column: fieldtype.FieldText, + }) + } if value, ok := ftuo.mutation.Datetime(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index bb0701899..039001fad 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -94,6 +94,7 @@ var ( {Name: "state", Type: field.TypeEnum, Nullable: true, Enums: []string{"on", "off"}}, {Name: "optional_float", Type: field.TypeFloat64, Nullable: true}, {Name: "optional_float32", Type: field.TypeFloat32, Nullable: true}, + {Name: "text", Type: field.TypeString, Nullable: true, Size: 2147483647, SchemaType: map[string]string{"mysql": "mediumtext"}}, {Name: "datetime", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"mysql": "datetime", "postgres": "date"}}, {Name: "decimal", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"mysql": "decimal(6,2)", "postgres": "numeric"}}, {Name: "link_other", Type: field.TypeOther, Nullable: true, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "varchar", "sqlite3": "varchar(255)"}}, @@ -144,7 +145,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "field_types_files_field", - Columns: []*schema.Column{FieldTypesColumns[65]}, + Columns: []*schema.Column{FieldTypesColumns[66]}, RefColumns: []*schema.Column{FilesColumns[0]}, OnDelete: schema.SetNull, }, diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 786c431d0..e70e38190 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -1396,6 +1396,7 @@ type FieldTypeMutation struct { addoptional_float *float64 optional_float32 *float32 addoptional_float32 *float32 + text *string datetime *time.Time decimal *float64 adddecimal *float64 @@ -3137,6 +3138,55 @@ func (m *FieldTypeMutation) ResetOptionalFloat32() { delete(m.clearedFields, fieldtype.FieldOptionalFloat32) } +// SetText sets the "text" field. +func (m *FieldTypeMutation) SetText(s string) { + m.text = &s +} + +// Text returns the value of the "text" field in the mutation. +func (m *FieldTypeMutation) Text() (r string, exists bool) { + v := m.text + if v == nil { + return + } + return *v, true +} + +// OldText returns the old "text" field's value of the FieldType entity. +// If the FieldType object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *FieldTypeMutation) OldText(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldText is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldText requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldText: %w", err) + } + return oldValue.Text, nil +} + +// ClearText clears the value of the "text" field. +func (m *FieldTypeMutation) ClearText() { + m.text = nil + m.clearedFields[fieldtype.FieldText] = struct{}{} +} + +// TextCleared returns if the "text" field was cleared in this mutation. +func (m *FieldTypeMutation) TextCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldText] + return ok +} + +// ResetText resets all changes to the "text" field. +func (m *FieldTypeMutation) ResetText() { + m.text = nil + delete(m.clearedFields, fieldtype.FieldText) +} + // SetDatetime sets the "datetime" field. func (m *FieldTypeMutation) SetDatetime(t time.Time) { m.datetime = &t @@ -5219,7 +5269,7 @@ func (m *FieldTypeMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 64) + fields := make([]string, 0, 65) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -5292,6 +5342,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.optional_float32 != nil { fields = append(fields, fieldtype.FieldOptionalFloat32) } + if m.text != nil { + fields = append(fields, fieldtype.FieldText) + } if m.datetime != nil { fields = append(fields, fieldtype.FieldDatetime) } @@ -5468,6 +5521,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.OptionalFloat() case fieldtype.FieldOptionalFloat32: return m.OptionalFloat32() + case fieldtype.FieldText: + return m.Text() case fieldtype.FieldDatetime: return m.Datetime() case fieldtype.FieldDecimal: @@ -5605,6 +5660,8 @@ func (m *FieldTypeMutation) OldField(ctx context.Context, name string) (ent.Valu return m.OldOptionalFloat(ctx) case fieldtype.FieldOptionalFloat32: return m.OldOptionalFloat32(ctx) + case fieldtype.FieldText: + return m.OldText(ctx) case fieldtype.FieldDatetime: return m.OldDatetime(ctx) case fieldtype.FieldDecimal: @@ -5862,6 +5919,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetOptionalFloat32(v) return nil + case fieldtype.FieldText: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetText(v) + return nil case fieldtype.FieldDatetime: v, ok := value.(time.Time) if !ok { @@ -6604,6 +6668,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldOptionalFloat32) { fields = append(fields, fieldtype.FieldOptionalFloat32) } + if m.FieldCleared(fieldtype.FieldText) { + fields = append(fields, fieldtype.FieldText) + } if m.FieldCleared(fieldtype.FieldDatetime) { fields = append(fields, fieldtype.FieldDatetime) } @@ -6780,6 +6847,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldOptionalFloat32: m.ClearOptionalFloat32() return nil + case fieldtype.FieldText: + m.ClearText() + return nil case fieldtype.FieldDatetime: m.ClearDatetime() return nil @@ -6965,6 +7035,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldOptionalFloat32: m.ResetOptionalFloat32() return nil + case fieldtype.FieldText: + m.ResetText() + return nil case fieldtype.FieldDatetime: m.ResetDatetime() return nil diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index f7a8625ed..b9a979af1 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -66,43 +66,43 @@ func init() { // fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error) // fieldtypeDescLinkOther is the schema descriptor for link_other field. - fieldtypeDescLinkOther := fieldtypeFields[26].Descriptor() + fieldtypeDescLinkOther := fieldtypeFields[27].Descriptor() // fieldtype.DefaultLinkOther holds the default value on creation for the link_other field. fieldtype.DefaultLinkOther = fieldtypeDescLinkOther.Default.(*schema.Link) // fieldtypeDescLinkOtherFunc is the schema descriptor for link_other_func field. - fieldtypeDescLinkOtherFunc := fieldtypeFields[27].Descriptor() + fieldtypeDescLinkOtherFunc := fieldtypeFields[28].Descriptor() // fieldtype.DefaultLinkOtherFunc holds the default value on creation for the link_other_func field. fieldtype.DefaultLinkOtherFunc = fieldtypeDescLinkOtherFunc.Default.(func() *schema.Link) // fieldtypeDescMAC is the schema descriptor for mac field. - fieldtypeDescMAC := fieldtypeFields[28].Descriptor() + fieldtypeDescMAC := fieldtypeFields[29].Descriptor() // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. fieldtype.MACValidator = fieldtypeDescMAC.Validators[0].(func(string) error) // fieldtypeDescDuration is the schema descriptor for duration field. - fieldtypeDescDuration := fieldtypeFields[32].Descriptor() + fieldtypeDescDuration := fieldtypeFields[33].Descriptor() // fieldtype.UpdateDefaultDuration holds the default value on update for the duration field. fieldtype.UpdateDefaultDuration = fieldtypeDescDuration.UpdateDefault.(func() time.Duration) // fieldtypeDescDir is the schema descriptor for dir field. - fieldtypeDescDir := fieldtypeFields[33].Descriptor() + fieldtypeDescDir := fieldtypeFields[34].Descriptor() // fieldtype.DefaultDir holds the default value on creation for the dir field. fieldtype.DefaultDir = fieldtypeDescDir.Default.(func() http.Dir) // fieldtypeDescNdir is the schema descriptor for ndir field. - fieldtypeDescNdir := fieldtypeFields[34].Descriptor() + fieldtypeDescNdir := fieldtypeFields[35].Descriptor() // fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save. fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error) // fieldtypeDescStr is the schema descriptor for str field. - fieldtypeDescStr := fieldtypeFields[35].Descriptor() + fieldtypeDescStr := fieldtypeFields[36].Descriptor() // fieldtype.DefaultStr holds the default value on creation for the str field. fieldtype.DefaultStr = fieldtypeDescStr.Default.(func() sql.NullString) // fieldtypeDescNullStr is the schema descriptor for null_str field. - fieldtypeDescNullStr := fieldtypeFields[36].Descriptor() + fieldtypeDescNullStr := fieldtypeFields[37].Descriptor() // fieldtype.DefaultNullStr holds the default value on creation for the null_str field. fieldtype.DefaultNullStr = fieldtypeDescNullStr.Default.(func() *sql.NullString) // fieldtypeDescLink is the schema descriptor for link field. - fieldtypeDescLink := fieldtypeFields[37].Descriptor() + fieldtypeDescLink := fieldtypeFields[38].Descriptor() // fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save. fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error) // fieldtypeDescRawData is the schema descriptor for raw_data field. - fieldtypeDescRawData := fieldtypeFields[43].Descriptor() + fieldtypeDescRawData := fieldtypeFields[44].Descriptor() // fieldtype.RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. fieldtype.RawDataValidator = func() func([]byte) error { validators := fieldtypeDescRawData.Validators @@ -120,21 +120,21 @@ func init() { } }() // fieldtypeDescIP is the schema descriptor for ip field. - fieldtypeDescIP := fieldtypeFields[45].Descriptor() + fieldtypeDescIP := fieldtypeFields[46].Descriptor() // fieldtype.DefaultIP holds the default value on creation for the ip field. fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtype.IPValidator is a validator for the "ip" field. It is called by the builders before save. fieldtype.IPValidator = fieldtypeDescIP.Validators[0].(func([]byte) error) // fieldtypeDescPair is the schema descriptor for pair field. - fieldtypeDescPair := fieldtypeFields[58].Descriptor() + fieldtypeDescPair := fieldtypeFields[59].Descriptor() // fieldtype.DefaultPair holds the default value on creation for the pair field. fieldtype.DefaultPair = fieldtypeDescPair.Default.(func() schema.Pair) // fieldtypeDescVstring is the schema descriptor for vstring field. - fieldtypeDescVstring := fieldtypeFields[60].Descriptor() + fieldtypeDescVstring := fieldtypeFields[61].Descriptor() // fieldtype.DefaultVstring holds the default value on creation for the vstring field. fieldtype.DefaultVstring = fieldtypeDescVstring.Default.(func() schema.VString) // fieldtypeDescTriple is the schema descriptor for triple field. - fieldtypeDescTriple := fieldtypeFields[61].Descriptor() + fieldtypeDescTriple := fieldtypeFields[62].Descriptor() // fieldtype.DefaultTriple holds the default value on creation for the triple field. fieldtype.DefaultTriple = fieldtypeDescTriple.Default.(func() schema.Triple) fileFields := schema.File{}.Fields() diff --git a/entc/integration/ent/schema/fieldtype.go b/entc/integration/ent/schema/fieldtype.go index 0a0a45f6b..eae23a8ad 100644 --- a/entc/integration/ent/schema/fieldtype.go +++ b/entc/integration/ent/schema/fieldtype.go @@ -94,6 +94,11 @@ func (FieldType) Fields() []ent.Field { //nolint:funlen // ---------------------------------------------------------------------------- // Dialect-specific types + field.Text("text"). + Optional(). + SchemaType(map[string]string{ + dialect.MySQL: "mediumtext", + }), field.Time("datetime"). Optional(). SchemaType(map[string]string{ diff --git a/entc/integration/gremlin/ent/fieldtype.go b/entc/integration/gremlin/ent/fieldtype.go index 5fe7ce5a0..4b178176b 100644 --- a/entc/integration/gremlin/ent/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype.go @@ -74,6 +74,8 @@ type FieldType struct { OptionalFloat float64 `json:"optional_float,omitempty"` // OptionalFloat32 holds the value of the "optional_float32" field. OptionalFloat32 float32 `json:"optional_float32,omitempty"` + // Text holds the value of the "text" field. + Text string `json:"text,omitempty"` // Datetime holds the value of the "datetime" field. Datetime time.Time `json:"datetime,omitempty"` // Decimal holds the value of the "decimal" field. @@ -188,6 +190,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { State fieldtype.State `json:"state,omitempty"` OptionalFloat float64 `json:"optional_float,omitempty"` OptionalFloat32 float32 `json:"optional_float32,omitempty"` + Text string `json:"text,omitempty"` Datetime int64 `json:"datetime,omitempty"` Decimal float64 `json:"decimal,omitempty"` LinkOther *schema.Link `json:"link_other,omitempty"` @@ -257,6 +260,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { ft.State = scanft.State ft.OptionalFloat = scanft.OptionalFloat ft.OptionalFloat32 = scanft.OptionalFloat32 + ft.Text = scanft.Text ft.Datetime = time.Unix(0, scanft.Datetime) ft.Decimal = scanft.Decimal ft.LinkOther = scanft.LinkOther @@ -381,6 +385,8 @@ func (ft *FieldType) String() string { builder.WriteString(fmt.Sprintf("%v", ft.OptionalFloat)) builder.WriteString(", optional_float32=") builder.WriteString(fmt.Sprintf("%v", ft.OptionalFloat32)) + builder.WriteString(", text=") + builder.WriteString(ft.Text) builder.WriteString(", datetime=") builder.WriteString(ft.Datetime.Format(time.ANSIC)) builder.WriteString(", decimal=") @@ -513,6 +519,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { State fieldtype.State `json:"state,omitempty"` OptionalFloat float64 `json:"optional_float,omitempty"` OptionalFloat32 float32 `json:"optional_float32,omitempty"` + Text string `json:"text,omitempty"` Datetime int64 `json:"datetime,omitempty"` Decimal float64 `json:"decimal,omitempty"` LinkOther *schema.Link `json:"link_other,omitempty"` @@ -584,6 +591,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { State: v.State, OptionalFloat: v.OptionalFloat, OptionalFloat32: v.OptionalFloat32, + Text: v.Text, Datetime: time.Unix(0, v.Datetime), Decimal: v.Decimal, LinkOther: v.LinkOther, diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index 8c49a0cd5..267998d85 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -70,6 +70,8 @@ const ( FieldOptionalFloat = "optional_float" // FieldOptionalFloat32 holds the string denoting the optional_float32 field in the database. FieldOptionalFloat32 = "optional_float32" + // FieldText holds the string denoting the text field in the database. + FieldText = "text" // FieldDatetime holds the string denoting the datetime field in the database. FieldDatetime = "datetime" // FieldDecimal holds the string denoting the decimal field in the database. diff --git a/entc/integration/gremlin/ent/fieldtype/where.go b/entc/integration/gremlin/ent/fieldtype/where.go index 039b309bf..cf227777d 100644 --- a/entc/integration/gremlin/ent/fieldtype/where.go +++ b/entc/integration/gremlin/ent/fieldtype/where.go @@ -253,6 +253,13 @@ func OptionalFloat32(v float32) predicate.FieldType { }) } +// Text applies equality check predicate on the "text" field. It's identical to TextEQ. +func Text(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.EQ(v)) + }) +} + // Datetime applies equality check predicate on the "datetime" field. It's identical to DatetimeEQ. func Datetime(v time.Time) predicate.FieldType { return predicate.FieldType(func(t *dsl.Traversal) { @@ -2297,6 +2304,105 @@ func OptionalFloat32NotNil() predicate.FieldType { }) } +// TextEQ applies the EQ predicate on the "text" field. +func TextEQ(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.EQ(v)) + }) +} + +// TextNEQ applies the NEQ predicate on the "text" field. +func TextNEQ(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.NEQ(v)) + }) +} + +// TextIn applies the In predicate on the "text" field. +func TextIn(vs ...string) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.Within(v...)) + }) +} + +// TextNotIn applies the NotIn predicate on the "text" field. +func TextNotIn(vs ...string) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.Without(v...)) + }) +} + +// TextGT applies the GT predicate on the "text" field. +func TextGT(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.GT(v)) + }) +} + +// TextGTE applies the GTE predicate on the "text" field. +func TextGTE(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.GTE(v)) + }) +} + +// TextLT applies the LT predicate on the "text" field. +func TextLT(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.LT(v)) + }) +} + +// TextLTE applies the LTE predicate on the "text" field. +func TextLTE(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.LTE(v)) + }) +} + +// TextContains applies the Contains predicate on the "text" field. +func TextContains(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.Containing(v)) + }) +} + +// TextHasPrefix applies the HasPrefix predicate on the "text" field. +func TextHasPrefix(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.StartingWith(v)) + }) +} + +// TextHasSuffix applies the HasSuffix predicate on the "text" field. +func TextHasSuffix(v string) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldText, p.EndingWith(v)) + }) +} + +// TextIsNil applies the IsNil predicate on the "text" field. +func TextIsNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldText) + }) +} + +// TextNotNil applies the NotNil predicate on the "text" field. +func TextNotNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldText) + }) +} + // DatetimeEQ applies the EQ predicate on the "datetime" field. func DatetimeEQ(v time.Time) predicate.FieldType { return predicate.FieldType(func(t *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index 4421387c5..ed4107fbb 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -327,6 +327,20 @@ func (ftc *FieldTypeCreate) SetNillableOptionalFloat32(f *float32) *FieldTypeCre return ftc } +// SetText sets the "text" field. +func (ftc *FieldTypeCreate) SetText(s string) *FieldTypeCreate { + ftc.mutation.SetText(s) + return ftc +} + +// SetNillableText sets the "text" field if the given value is not nil. +func (ftc *FieldTypeCreate) SetNillableText(s *string) *FieldTypeCreate { + if s != nil { + ftc.SetText(*s) + } + return ftc +} + // SetDatetime sets the "datetime" field. func (ftc *FieldTypeCreate) SetDatetime(t time.Time) *FieldTypeCreate { ftc.mutation.SetDatetime(t) @@ -1042,6 +1056,9 @@ func (ftc *FieldTypeCreate) gremlin() *dsl.Traversal { if value, ok := ftc.mutation.OptionalFloat32(); ok { v.Property(dsl.Single, fieldtype.FieldOptionalFloat32, value) } + if value, ok := ftc.mutation.Text(); ok { + v.Property(dsl.Single, fieldtype.FieldText, value) + } if value, ok := ftc.mutation.Datetime(); ok { v.Property(dsl.Single, fieldtype.FieldDatetime, value) } diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index 5dca2bdfa..e95a502f2 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -610,6 +610,26 @@ func (ftu *FieldTypeUpdate) ClearOptionalFloat32() *FieldTypeUpdate { return ftu } +// SetText sets the "text" field. +func (ftu *FieldTypeUpdate) SetText(s string) *FieldTypeUpdate { + ftu.mutation.SetText(s) + return ftu +} + +// SetNillableText sets the "text" field if the given value is not nil. +func (ftu *FieldTypeUpdate) SetNillableText(s *string) *FieldTypeUpdate { + if s != nil { + ftu.SetText(*s) + } + return ftu +} + +// ClearText clears the value of the "text" field. +func (ftu *FieldTypeUpdate) ClearText() *FieldTypeUpdate { + ftu.mutation.ClearText() + return ftu +} + // SetDatetime sets the "datetime" field. func (ftu *FieldTypeUpdate) SetDatetime(t time.Time) *FieldTypeUpdate { ftu.mutation.SetDatetime(t) @@ -1589,6 +1609,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if value, ok := ftu.mutation.AddedOptionalFloat32(); ok { v.Property(dsl.Single, fieldtype.FieldOptionalFloat32, __.Union(__.Values(fieldtype.FieldOptionalFloat32), __.Constant(value)).Sum()) } + if value, ok := ftu.mutation.Text(); ok { + v.Property(dsl.Single, fieldtype.FieldText, value) + } if value, ok := ftu.mutation.Datetime(); ok { v.Property(dsl.Single, fieldtype.FieldDatetime, value) } @@ -1791,6 +1814,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if ftu.mutation.OptionalFloat32Cleared() { properties = append(properties, fieldtype.FieldOptionalFloat32) } + if ftu.mutation.TextCleared() { + properties = append(properties, fieldtype.FieldText) + } if ftu.mutation.DatetimeCleared() { properties = append(properties, fieldtype.FieldDatetime) } @@ -2483,6 +2509,26 @@ func (ftuo *FieldTypeUpdateOne) ClearOptionalFloat32() *FieldTypeUpdateOne { return ftuo } +// SetText sets the "text" field. +func (ftuo *FieldTypeUpdateOne) SetText(s string) *FieldTypeUpdateOne { + ftuo.mutation.SetText(s) + return ftuo +} + +// SetNillableText sets the "text" field if the given value is not nil. +func (ftuo *FieldTypeUpdateOne) SetNillableText(s *string) *FieldTypeUpdateOne { + if s != nil { + ftuo.SetText(*s) + } + return ftuo +} + +// ClearText clears the value of the "text" field. +func (ftuo *FieldTypeUpdateOne) ClearText() *FieldTypeUpdateOne { + ftuo.mutation.ClearText() + return ftuo +} + // SetDatetime sets the "datetime" field. func (ftuo *FieldTypeUpdateOne) SetDatetime(t time.Time) *FieldTypeUpdateOne { ftuo.mutation.SetDatetime(t) @@ -3474,6 +3520,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := ftuo.mutation.AddedOptionalFloat32(); ok { v.Property(dsl.Single, fieldtype.FieldOptionalFloat32, __.Union(__.Values(fieldtype.FieldOptionalFloat32), __.Constant(value)).Sum()) } + if value, ok := ftuo.mutation.Text(); ok { + v.Property(dsl.Single, fieldtype.FieldText, value) + } if value, ok := ftuo.mutation.Datetime(); ok { v.Property(dsl.Single, fieldtype.FieldDatetime, value) } @@ -3676,6 +3725,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if ftuo.mutation.OptionalFloat32Cleared() { properties = append(properties, fieldtype.FieldOptionalFloat32) } + if ftuo.mutation.TextCleared() { + properties = append(properties, fieldtype.FieldText) + } if ftuo.mutation.DatetimeCleared() { properties = append(properties, fieldtype.FieldDatetime) } diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 9a5cc66d3..25a40c5a8 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -1396,6 +1396,7 @@ type FieldTypeMutation struct { addoptional_float *float64 optional_float32 *float32 addoptional_float32 *float32 + text *string datetime *time.Time decimal *float64 adddecimal *float64 @@ -3137,6 +3138,55 @@ func (m *FieldTypeMutation) ResetOptionalFloat32() { delete(m.clearedFields, fieldtype.FieldOptionalFloat32) } +// SetText sets the "text" field. +func (m *FieldTypeMutation) SetText(s string) { + m.text = &s +} + +// Text returns the value of the "text" field in the mutation. +func (m *FieldTypeMutation) Text() (r string, exists bool) { + v := m.text + if v == nil { + return + } + return *v, true +} + +// OldText returns the old "text" field's value of the FieldType entity. +// If the FieldType object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *FieldTypeMutation) OldText(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldText is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldText requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldText: %w", err) + } + return oldValue.Text, nil +} + +// ClearText clears the value of the "text" field. +func (m *FieldTypeMutation) ClearText() { + m.text = nil + m.clearedFields[fieldtype.FieldText] = struct{}{} +} + +// TextCleared returns if the "text" field was cleared in this mutation. +func (m *FieldTypeMutation) TextCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldText] + return ok +} + +// ResetText resets all changes to the "text" field. +func (m *FieldTypeMutation) ResetText() { + m.text = nil + delete(m.clearedFields, fieldtype.FieldText) +} + // SetDatetime sets the "datetime" field. func (m *FieldTypeMutation) SetDatetime(t time.Time) { m.datetime = &t @@ -5219,7 +5269,7 @@ func (m *FieldTypeMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 64) + fields := make([]string, 0, 65) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -5292,6 +5342,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.optional_float32 != nil { fields = append(fields, fieldtype.FieldOptionalFloat32) } + if m.text != nil { + fields = append(fields, fieldtype.FieldText) + } if m.datetime != nil { fields = append(fields, fieldtype.FieldDatetime) } @@ -5468,6 +5521,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.OptionalFloat() case fieldtype.FieldOptionalFloat32: return m.OptionalFloat32() + case fieldtype.FieldText: + return m.Text() case fieldtype.FieldDatetime: return m.Datetime() case fieldtype.FieldDecimal: @@ -5605,6 +5660,8 @@ func (m *FieldTypeMutation) OldField(ctx context.Context, name string) (ent.Valu return m.OldOptionalFloat(ctx) case fieldtype.FieldOptionalFloat32: return m.OldOptionalFloat32(ctx) + case fieldtype.FieldText: + return m.OldText(ctx) case fieldtype.FieldDatetime: return m.OldDatetime(ctx) case fieldtype.FieldDecimal: @@ -5862,6 +5919,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetOptionalFloat32(v) return nil + case fieldtype.FieldText: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetText(v) + return nil case fieldtype.FieldDatetime: v, ok := value.(time.Time) if !ok { @@ -6604,6 +6668,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldOptionalFloat32) { fields = append(fields, fieldtype.FieldOptionalFloat32) } + if m.FieldCleared(fieldtype.FieldText) { + fields = append(fields, fieldtype.FieldText) + } if m.FieldCleared(fieldtype.FieldDatetime) { fields = append(fields, fieldtype.FieldDatetime) } @@ -6780,6 +6847,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldOptionalFloat32: m.ClearOptionalFloat32() return nil + case fieldtype.FieldText: + m.ClearText() + return nil case fieldtype.FieldDatetime: m.ClearDatetime() return nil @@ -6965,6 +7035,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldOptionalFloat32: m.ResetOptionalFloat32() return nil + case fieldtype.FieldText: + m.ResetText() + return nil case fieldtype.FieldDatetime: m.ResetDatetime() return nil diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 91957b87c..8b0852cdb 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -66,43 +66,43 @@ func init() { // fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error) // fieldtypeDescLinkOther is the schema descriptor for link_other field. - fieldtypeDescLinkOther := fieldtypeFields[26].Descriptor() + fieldtypeDescLinkOther := fieldtypeFields[27].Descriptor() // fieldtype.DefaultLinkOther holds the default value on creation for the link_other field. fieldtype.DefaultLinkOther = fieldtypeDescLinkOther.Default.(*schema.Link) // fieldtypeDescLinkOtherFunc is the schema descriptor for link_other_func field. - fieldtypeDescLinkOtherFunc := fieldtypeFields[27].Descriptor() + fieldtypeDescLinkOtherFunc := fieldtypeFields[28].Descriptor() // fieldtype.DefaultLinkOtherFunc holds the default value on creation for the link_other_func field. fieldtype.DefaultLinkOtherFunc = fieldtypeDescLinkOtherFunc.Default.(func() *schema.Link) // fieldtypeDescMAC is the schema descriptor for mac field. - fieldtypeDescMAC := fieldtypeFields[28].Descriptor() + fieldtypeDescMAC := fieldtypeFields[29].Descriptor() // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. fieldtype.MACValidator = fieldtypeDescMAC.Validators[0].(func(string) error) // fieldtypeDescDuration is the schema descriptor for duration field. - fieldtypeDescDuration := fieldtypeFields[32].Descriptor() + fieldtypeDescDuration := fieldtypeFields[33].Descriptor() // fieldtype.UpdateDefaultDuration holds the default value on update for the duration field. fieldtype.UpdateDefaultDuration = fieldtypeDescDuration.UpdateDefault.(func() time.Duration) // fieldtypeDescDir is the schema descriptor for dir field. - fieldtypeDescDir := fieldtypeFields[33].Descriptor() + fieldtypeDescDir := fieldtypeFields[34].Descriptor() // fieldtype.DefaultDir holds the default value on creation for the dir field. fieldtype.DefaultDir = fieldtypeDescDir.Default.(func() http.Dir) // fieldtypeDescNdir is the schema descriptor for ndir field. - fieldtypeDescNdir := fieldtypeFields[34].Descriptor() + fieldtypeDescNdir := fieldtypeFields[35].Descriptor() // fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save. fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error) // fieldtypeDescStr is the schema descriptor for str field. - fieldtypeDescStr := fieldtypeFields[35].Descriptor() + fieldtypeDescStr := fieldtypeFields[36].Descriptor() // fieldtype.DefaultStr holds the default value on creation for the str field. fieldtype.DefaultStr = fieldtypeDescStr.Default.(func() sql.NullString) // fieldtypeDescNullStr is the schema descriptor for null_str field. - fieldtypeDescNullStr := fieldtypeFields[36].Descriptor() + fieldtypeDescNullStr := fieldtypeFields[37].Descriptor() // fieldtype.DefaultNullStr holds the default value on creation for the null_str field. fieldtype.DefaultNullStr = fieldtypeDescNullStr.Default.(func() *sql.NullString) // fieldtypeDescLink is the schema descriptor for link field. - fieldtypeDescLink := fieldtypeFields[37].Descriptor() + fieldtypeDescLink := fieldtypeFields[38].Descriptor() // fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save. fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error) // fieldtypeDescRawData is the schema descriptor for raw_data field. - fieldtypeDescRawData := fieldtypeFields[43].Descriptor() + fieldtypeDescRawData := fieldtypeFields[44].Descriptor() // fieldtype.RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. fieldtype.RawDataValidator = func() func([]byte) error { validators := fieldtypeDescRawData.Validators @@ -120,21 +120,21 @@ func init() { } }() // fieldtypeDescIP is the schema descriptor for ip field. - fieldtypeDescIP := fieldtypeFields[45].Descriptor() + fieldtypeDescIP := fieldtypeFields[46].Descriptor() // fieldtype.DefaultIP holds the default value on creation for the ip field. fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtype.IPValidator is a validator for the "ip" field. It is called by the builders before save. fieldtype.IPValidator = fieldtypeDescIP.Validators[0].(func([]byte) error) // fieldtypeDescPair is the schema descriptor for pair field. - fieldtypeDescPair := fieldtypeFields[58].Descriptor() + fieldtypeDescPair := fieldtypeFields[59].Descriptor() // fieldtype.DefaultPair holds the default value on creation for the pair field. fieldtype.DefaultPair = fieldtypeDescPair.Default.(func() schema.Pair) // fieldtypeDescVstring is the schema descriptor for vstring field. - fieldtypeDescVstring := fieldtypeFields[60].Descriptor() + fieldtypeDescVstring := fieldtypeFields[61].Descriptor() // fieldtype.DefaultVstring holds the default value on creation for the vstring field. fieldtype.DefaultVstring = fieldtypeDescVstring.Default.(func() schema.VString) // fieldtypeDescTriple is the schema descriptor for triple field. - fieldtypeDescTriple := fieldtypeFields[61].Descriptor() + fieldtypeDescTriple := fieldtypeFields[62].Descriptor() // fieldtype.DefaultTriple holds the default value on creation for the triple field. fieldtype.DefaultTriple = fieldtypeDescTriple.Default.(func() schema.Triple) fileFields := schema.File{}.Fields()