mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/schema: allow setting collation for string fields
Reviewed By: a8m Differential Revision: D17090481 fbshipit-source-id: a08768c9bac4318a91bc6a067c85d6eb022c3024
This commit is contained in:
committed by
Facebook Github Bot
parent
419753b33b
commit
4c1f28d58f
@@ -34,6 +34,7 @@ func ExampleUser() {
|
||||
SetAge(1).
|
||||
SetName("string").
|
||||
SetAddress("string").
|
||||
SetRole("string").
|
||||
SaveX(ctx)
|
||||
log.Println("user created:", u)
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ var (
|
||||
{Name: "age", Type: field.TypeInt32},
|
||||
{Name: "name", Type: field.TypeString, Size: 10},
|
||||
{Name: "address", Type: field.TypeString, Nullable: true},
|
||||
{Name: "role", Type: field.TypeString, Nullable: true},
|
||||
}
|
||||
// UsersTable holds the schema information for the "users" table.
|
||||
UsersTable = &schema.Table{
|
||||
|
||||
@@ -21,6 +21,7 @@ func (User) Fields() []ent.Field {
|
||||
field.Int32("age"),
|
||||
field.String("name").MaxLen(10),
|
||||
field.String("address").Optional(),
|
||||
field.String("role").Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ type User struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
// Address holds the value of the "address" field.
|
||||
Address string `json:"address,omitempty"`
|
||||
// Role holds the value of the "role" field.
|
||||
Role string `json:"role,omitempty"`
|
||||
}
|
||||
|
||||
// FromRows scans the sql response data into User.
|
||||
@@ -29,6 +31,7 @@ func (u *User) FromRows(rows *sql.Rows) error {
|
||||
Age sql.NullInt64
|
||||
Name sql.NullString
|
||||
Address sql.NullString
|
||||
Role sql.NullString
|
||||
}
|
||||
// the order here should be the same as in the `user.Columns`.
|
||||
if err := rows.Scan(
|
||||
@@ -36,6 +39,7 @@ func (u *User) FromRows(rows *sql.Rows) error {
|
||||
&vu.Age,
|
||||
&vu.Name,
|
||||
&vu.Address,
|
||||
&vu.Role,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -43,6 +47,7 @@ func (u *User) FromRows(rows *sql.Rows) error {
|
||||
u.Age = int32(vu.Age.Int64)
|
||||
u.Name = vu.Name.String
|
||||
u.Address = vu.Address.String
|
||||
u.Role = vu.Role.String
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -72,6 +77,7 @@ func (u *User) String() string {
|
||||
buf.WriteString(fmt.Sprintf(", age=%v", u.Age))
|
||||
buf.WriteString(fmt.Sprintf(", name=%v", u.Name))
|
||||
buf.WriteString(fmt.Sprintf(", address=%v", u.Address))
|
||||
buf.WriteString(fmt.Sprintf(", role=%v", u.Role))
|
||||
buf.WriteString(")")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ const (
|
||||
FieldName = "name"
|
||||
// FieldAddress holds the string denoting the address vertex property in the database.
|
||||
FieldAddress = "address"
|
||||
// FieldRole holds the string denoting the role vertex property in the database.
|
||||
FieldRole = "role"
|
||||
|
||||
// Table holds the table name of the user in the database.
|
||||
Table = "users"
|
||||
@@ -28,6 +30,7 @@ var Columns = []string{
|
||||
FieldAge,
|
||||
FieldName,
|
||||
FieldAddress,
|
||||
FieldRole,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -136,6 +136,15 @@ func Address(v string) predicate.User {
|
||||
)
|
||||
}
|
||||
|
||||
// Role applies equality check predicate on the "role" field. It's identical to RoleEQ.
|
||||
func Role(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// AgeEQ applies the EQ predicate on the "age" field.
|
||||
func AgeEQ(v int32) predicate.User {
|
||||
return predicate.User(
|
||||
@@ -520,6 +529,161 @@ func AddressContainsFold(v string) predicate.User {
|
||||
)
|
||||
}
|
||||
|
||||
// RoleEQ applies the EQ predicate on the "role" field.
|
||||
func RoleEQ(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleNEQ applies the NEQ predicate on the "role" field.
|
||||
func RoleNEQ(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.NEQ(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleGT applies the GT predicate on the "role" field.
|
||||
func RoleGT(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.GT(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleGTE applies the GTE predicate on the "role" field.
|
||||
func RoleGTE(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.GTE(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleLT applies the LT predicate on the "role" field.
|
||||
func RoleLT(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.LT(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleLTE applies the LTE predicate on the "role" field.
|
||||
func RoleLTE(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.LTE(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleIn applies the In predicate on the "role" field.
|
||||
func RoleIn(vs ...string) predicate.User {
|
||||
v := make([]interface{}, len(vs))
|
||||
for i := range v {
|
||||
v[i] = vs[i]
|
||||
}
|
||||
return predicate.User(
|
||||
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(vs) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
s.Where(sql.In(s.C(FieldRole), v...))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleNotIn applies the NotIn predicate on the "role" field.
|
||||
func RoleNotIn(vs ...string) predicate.User {
|
||||
v := make([]interface{}, len(vs))
|
||||
for i := range v {
|
||||
v[i] = vs[i]
|
||||
}
|
||||
return predicate.User(
|
||||
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(vs) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
s.Where(sql.NotIn(s.C(FieldRole), v...))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleContains applies the Contains predicate on the "role" field.
|
||||
func RoleContains(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.Contains(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleHasPrefix applies the HasPrefix predicate on the "role" field.
|
||||
func RoleHasPrefix(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.HasPrefix(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleHasSuffix applies the HasSuffix predicate on the "role" field.
|
||||
func RoleHasSuffix(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.HasSuffix(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleIsNil applies the IsNil predicate on the "role" field.
|
||||
func RoleIsNil() predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.IsNull(s.C(FieldRole)))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleNotNil applies the NotNil predicate on the "role" field.
|
||||
func RoleNotNil() predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.NotNull(s.C(FieldRole)))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleEqualFold applies the EqualFold predicate on the "role" field.
|
||||
func RoleEqualFold(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EqualFold(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleContainsFold applies the ContainsFold predicate on the "role" field.
|
||||
func RoleContainsFold(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.ContainsFold(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// And groups list of predicates with the AND operator between them.
|
||||
func And(predicates ...predicate.User) predicate.User {
|
||||
return predicate.User(
|
||||
|
||||
@@ -18,6 +18,7 @@ type UserCreate struct {
|
||||
age *int32
|
||||
name *string
|
||||
address *string
|
||||
role *string
|
||||
}
|
||||
|
||||
// SetAge sets the age field.
|
||||
@@ -46,6 +47,20 @@ func (uc *UserCreate) SetNillableAddress(s *string) *UserCreate {
|
||||
return uc
|
||||
}
|
||||
|
||||
// SetRole sets the role field.
|
||||
func (uc *UserCreate) SetRole(s string) *UserCreate {
|
||||
uc.role = &s
|
||||
return uc
|
||||
}
|
||||
|
||||
// SetNillableRole sets the role field if the given value is not nil.
|
||||
func (uc *UserCreate) SetNillableRole(s *string) *UserCreate {
|
||||
if s != nil {
|
||||
uc.SetRole(*s)
|
||||
}
|
||||
return uc
|
||||
}
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
if uc.age == nil {
|
||||
@@ -91,6 +106,10 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
builder.Set(user.FieldAddress, *uc.address)
|
||||
u.Address = *uc.address
|
||||
}
|
||||
if uc.role != nil {
|
||||
builder.Set(user.FieldRole, *uc.role)
|
||||
u.Role = *uc.role
|
||||
}
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
|
||||
@@ -18,6 +18,7 @@ type UserUpdate struct {
|
||||
age *int32
|
||||
name *string
|
||||
address *string
|
||||
role *string
|
||||
predicates []predicate.User
|
||||
}
|
||||
|
||||
@@ -53,6 +54,20 @@ func (uu *UserUpdate) SetNillableAddress(s *string) *UserUpdate {
|
||||
return uu
|
||||
}
|
||||
|
||||
// SetRole sets the role field.
|
||||
func (uu *UserUpdate) SetRole(s string) *UserUpdate {
|
||||
uu.role = &s
|
||||
return uu
|
||||
}
|
||||
|
||||
// SetNillableRole sets the role field if the given value is not nil.
|
||||
func (uu *UserUpdate) SetNillableRole(s *string) *UserUpdate {
|
||||
if s != nil {
|
||||
uu.SetRole(*s)
|
||||
}
|
||||
return uu
|
||||
}
|
||||
|
||||
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
||||
func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
||||
if uu.name != nil {
|
||||
@@ -129,6 +144,10 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
update = true
|
||||
builder.Set(user.FieldAddress, *uu.address)
|
||||
}
|
||||
if uu.role != nil {
|
||||
update = true
|
||||
builder.Set(user.FieldRole, *uu.role)
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
@@ -148,6 +167,7 @@ type UserUpdateOne struct {
|
||||
age *int32
|
||||
name *string
|
||||
address *string
|
||||
role *string
|
||||
}
|
||||
|
||||
// SetAge sets the age field.
|
||||
@@ -176,6 +196,20 @@ func (uuo *UserUpdateOne) SetNillableAddress(s *string) *UserUpdateOne {
|
||||
return uuo
|
||||
}
|
||||
|
||||
// SetRole sets the role field.
|
||||
func (uuo *UserUpdateOne) SetRole(s string) *UserUpdateOne {
|
||||
uuo.role = &s
|
||||
return uuo
|
||||
}
|
||||
|
||||
// SetNillableRole sets the role field if the given value is not nil.
|
||||
func (uuo *UserUpdateOne) SetNillableRole(s *string) *UserUpdateOne {
|
||||
if s != nil {
|
||||
uuo.SetRole(*s)
|
||||
}
|
||||
return uuo
|
||||
}
|
||||
|
||||
// Save executes the query and returns the updated entity.
|
||||
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
||||
if uuo.name != nil {
|
||||
@@ -258,6 +292,11 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
|
||||
builder.Set(user.FieldAddress, *uuo.address)
|
||||
u.Address = *uuo.address
|
||||
}
|
||||
if uuo.role != nil {
|
||||
update = true
|
||||
builder.Set(user.FieldRole, *uuo.role)
|
||||
u.Role = *uuo.role
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
|
||||
@@ -82,6 +82,7 @@ func ExampleUser() {
|
||||
SetPhone("string").
|
||||
SetBuffer([]byte{}).
|
||||
SetTitle("string").
|
||||
SetRole("string").
|
||||
SaveX(ctx)
|
||||
log.Println("user created:", u)
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ var (
|
||||
{Name: "phone", Type: field.TypeString},
|
||||
{Name: "buffer", Type: field.TypeBytes, Default: user.DefaultBuffer},
|
||||
{Name: "title", Type: field.TypeString, Default: user.DefaultTitle},
|
||||
{Name: "role", Type: field.TypeString, Nullable: true, Collation: "utf8mb4_general_ci"},
|
||||
}
|
||||
// UsersTable holds the schema information for the "users" table.
|
||||
UsersTable = &schema.Table{
|
||||
|
||||
@@ -31,6 +31,10 @@ func (User) Fields() []ent.Field {
|
||||
// all existing rows.
|
||||
field.String("title").
|
||||
Default("SWE"),
|
||||
// adding column collation
|
||||
field.String("role").
|
||||
Collation("utf8mb4_general_ci").
|
||||
Optional(),
|
||||
// deleting the `address` column.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ type User struct {
|
||||
Buffer []byte `json:"buffer,omitempty"`
|
||||
// Title holds the value of the "title" field.
|
||||
Title string `json:"title,omitempty"`
|
||||
// Role holds the value of the "role" field.
|
||||
Role string `json:"role,omitempty"`
|
||||
}
|
||||
|
||||
// FromRows scans the sql response data into User.
|
||||
@@ -35,6 +37,7 @@ func (u *User) FromRows(rows *sql.Rows) error {
|
||||
Phone sql.NullString
|
||||
Buffer []byte
|
||||
Title sql.NullString
|
||||
Role sql.NullString
|
||||
}
|
||||
// the order here should be the same as in the `user.Columns`.
|
||||
if err := rows.Scan(
|
||||
@@ -44,6 +47,7 @@ func (u *User) FromRows(rows *sql.Rows) error {
|
||||
&vu.Phone,
|
||||
&vu.Buffer,
|
||||
&vu.Title,
|
||||
&vu.Role,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -53,6 +57,7 @@ func (u *User) FromRows(rows *sql.Rows) error {
|
||||
u.Phone = vu.Phone.String
|
||||
u.Buffer = vu.Buffer
|
||||
u.Title = vu.Title.String
|
||||
u.Role = vu.Role.String
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -84,6 +89,7 @@ func (u *User) String() string {
|
||||
buf.WriteString(fmt.Sprintf(", phone=%v", u.Phone))
|
||||
buf.WriteString(fmt.Sprintf(", buffer=%v", u.Buffer))
|
||||
buf.WriteString(fmt.Sprintf(", title=%v", u.Title))
|
||||
buf.WriteString(fmt.Sprintf(", role=%v", u.Role))
|
||||
buf.WriteString(")")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ const (
|
||||
FieldBuffer = "buffer"
|
||||
// FieldTitle holds the string denoting the title vertex property in the database.
|
||||
FieldTitle = "title"
|
||||
// FieldRole holds the string denoting the role vertex property in the database.
|
||||
FieldRole = "role"
|
||||
|
||||
// Table holds the table name of the user in the database.
|
||||
Table = "users"
|
||||
@@ -34,6 +36,7 @@ var Columns = []string{
|
||||
FieldPhone,
|
||||
FieldBuffer,
|
||||
FieldTitle,
|
||||
FieldRole,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -154,6 +154,15 @@ func Title(v string) predicate.User {
|
||||
)
|
||||
}
|
||||
|
||||
// Role applies equality check predicate on the "role" field. It's identical to RoleEQ.
|
||||
func Role(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// AgeEQ applies the EQ predicate on the "age" field.
|
||||
func AgeEQ(v int) predicate.User {
|
||||
return predicate.User(
|
||||
@@ -749,6 +758,161 @@ func TitleContainsFold(v string) predicate.User {
|
||||
)
|
||||
}
|
||||
|
||||
// RoleEQ applies the EQ predicate on the "role" field.
|
||||
func RoleEQ(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleNEQ applies the NEQ predicate on the "role" field.
|
||||
func RoleNEQ(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.NEQ(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleGT applies the GT predicate on the "role" field.
|
||||
func RoleGT(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.GT(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleGTE applies the GTE predicate on the "role" field.
|
||||
func RoleGTE(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.GTE(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleLT applies the LT predicate on the "role" field.
|
||||
func RoleLT(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.LT(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleLTE applies the LTE predicate on the "role" field.
|
||||
func RoleLTE(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.LTE(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleIn applies the In predicate on the "role" field.
|
||||
func RoleIn(vs ...string) predicate.User {
|
||||
v := make([]interface{}, len(vs))
|
||||
for i := range v {
|
||||
v[i] = vs[i]
|
||||
}
|
||||
return predicate.User(
|
||||
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(vs) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
s.Where(sql.In(s.C(FieldRole), v...))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleNotIn applies the NotIn predicate on the "role" field.
|
||||
func RoleNotIn(vs ...string) predicate.User {
|
||||
v := make([]interface{}, len(vs))
|
||||
for i := range v {
|
||||
v[i] = vs[i]
|
||||
}
|
||||
return predicate.User(
|
||||
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(vs) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
s.Where(sql.NotIn(s.C(FieldRole), v...))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleContains applies the Contains predicate on the "role" field.
|
||||
func RoleContains(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.Contains(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleHasPrefix applies the HasPrefix predicate on the "role" field.
|
||||
func RoleHasPrefix(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.HasPrefix(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleHasSuffix applies the HasSuffix predicate on the "role" field.
|
||||
func RoleHasSuffix(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.HasSuffix(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleIsNil applies the IsNil predicate on the "role" field.
|
||||
func RoleIsNil() predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.IsNull(s.C(FieldRole)))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleNotNil applies the NotNil predicate on the "role" field.
|
||||
func RoleNotNil() predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.NotNull(s.C(FieldRole)))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleEqualFold applies the EqualFold predicate on the "role" field.
|
||||
func RoleEqualFold(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EqualFold(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// RoleContainsFold applies the ContainsFold predicate on the "role" field.
|
||||
func RoleContainsFold(v string) predicate.User {
|
||||
return predicate.User(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.ContainsFold(s.C(FieldRole), v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// And groups list of predicates with the AND operator between them.
|
||||
func And(predicates ...predicate.User) predicate.User {
|
||||
return predicate.User(
|
||||
|
||||
@@ -19,6 +19,7 @@ type UserCreate struct {
|
||||
phone *string
|
||||
buffer *[]byte
|
||||
title *string
|
||||
role *string
|
||||
}
|
||||
|
||||
// SetAge sets the age field.
|
||||
@@ -59,6 +60,20 @@ func (uc *UserCreate) SetNillableTitle(s *string) *UserCreate {
|
||||
return uc
|
||||
}
|
||||
|
||||
// SetRole sets the role field.
|
||||
func (uc *UserCreate) SetRole(s string) *UserCreate {
|
||||
uc.role = &s
|
||||
return uc
|
||||
}
|
||||
|
||||
// SetNillableRole sets the role field if the given value is not nil.
|
||||
func (uc *UserCreate) SetNillableRole(s *string) *UserCreate {
|
||||
if s != nil {
|
||||
uc.SetRole(*s)
|
||||
}
|
||||
return uc
|
||||
}
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
if uc.age == nil {
|
||||
@@ -120,6 +135,10 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
builder.Set(user.FieldTitle, *uc.title)
|
||||
u.Title = *uc.title
|
||||
}
|
||||
if uc.role != nil {
|
||||
builder.Set(user.FieldRole, *uc.role)
|
||||
u.Role = *uc.role
|
||||
}
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
|
||||
@@ -20,6 +20,7 @@ type UserUpdate struct {
|
||||
phone *string
|
||||
buffer *[]byte
|
||||
title *string
|
||||
role *string
|
||||
predicates []predicate.User
|
||||
}
|
||||
|
||||
@@ -67,6 +68,20 @@ func (uu *UserUpdate) SetNillableTitle(s *string) *UserUpdate {
|
||||
return uu
|
||||
}
|
||||
|
||||
// SetRole sets the role field.
|
||||
func (uu *UserUpdate) SetRole(s string) *UserUpdate {
|
||||
uu.role = &s
|
||||
return uu
|
||||
}
|
||||
|
||||
// SetNillableRole sets the role field if the given value is not nil.
|
||||
func (uu *UserUpdate) SetNillableRole(s *string) *UserUpdate {
|
||||
if s != nil {
|
||||
uu.SetRole(*s)
|
||||
}
|
||||
return uu
|
||||
}
|
||||
|
||||
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
||||
func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
||||
return uu.sqlSave(ctx)
|
||||
@@ -146,6 +161,10 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
update = true
|
||||
builder.Set(user.FieldTitle, *uu.title)
|
||||
}
|
||||
if uu.role != nil {
|
||||
update = true
|
||||
builder.Set(user.FieldRole, *uu.role)
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
@@ -167,6 +186,7 @@ type UserUpdateOne struct {
|
||||
phone *string
|
||||
buffer *[]byte
|
||||
title *string
|
||||
role *string
|
||||
}
|
||||
|
||||
// SetAge sets the age field.
|
||||
@@ -207,6 +227,20 @@ func (uuo *UserUpdateOne) SetNillableTitle(s *string) *UserUpdateOne {
|
||||
return uuo
|
||||
}
|
||||
|
||||
// SetRole sets the role field.
|
||||
func (uuo *UserUpdateOne) SetRole(s string) *UserUpdateOne {
|
||||
uuo.role = &s
|
||||
return uuo
|
||||
}
|
||||
|
||||
// SetNillableRole sets the role field if the given value is not nil.
|
||||
func (uuo *UserUpdateOne) SetNillableRole(s *string) *UserUpdateOne {
|
||||
if s != nil {
|
||||
uuo.SetRole(*s)
|
||||
}
|
||||
return uuo
|
||||
}
|
||||
|
||||
// Save executes the query and returns the updated entity.
|
||||
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
||||
return uuo.sqlSave(ctx)
|
||||
@@ -294,6 +328,11 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
|
||||
builder.Set(user.FieldTitle, *uuo.title)
|
||||
u.Title = *uuo.title
|
||||
}
|
||||
if uuo.role != nil {
|
||||
update = true
|
||||
builder.Set(user.FieldRole, *uuo.role)
|
||||
u.Role = *uuo.role
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
|
||||
@@ -12,9 +12,11 @@ import (
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/entc/integration/migrate/entv1"
|
||||
migratev1 "github.com/facebookincubator/ent/entc/integration/migrate/entv1/migrate"
|
||||
userv1 "github.com/facebookincubator/ent/entc/integration/migrate/entv1/user"
|
||||
"github.com/facebookincubator/ent/entc/integration/migrate/entv2"
|
||||
migratev2 "github.com/facebookincubator/ent/entc/integration/migrate/entv2/migrate"
|
||||
"github.com/facebookincubator/ent/entc/integration/migrate/entv2/user"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -52,6 +54,7 @@ func TestMySQL(t *testing.T) {
|
||||
idRange(t, clientv2.Pet.Create().SaveX(ctx).ID, 2<<32-1, 3<<32)
|
||||
|
||||
// sql specific predicates.
|
||||
Collation(t, clientv2)
|
||||
EqualFold(t, clientv2)
|
||||
ContainsFold(t, clientv2)
|
||||
})
|
||||
@@ -82,9 +85,15 @@ func TestSQLite(t *testing.T) {
|
||||
|
||||
func SanityV1(t *testing.T, client *entv1.Client) {
|
||||
ctx := context.Background()
|
||||
u := client.User.Create().SetAge(1).SetName("foo").SaveX(ctx)
|
||||
require.Equal(t, int32(1), u.Age)
|
||||
u := client.User.Create().SetAge(1).SetName("foo").SetRole("admin").SaveX(ctx)
|
||||
require.EqualValues(t, 1, u.Age)
|
||||
require.Equal(t, "foo", u.Name)
|
||||
require.Equal(t, "admin", u.Role)
|
||||
|
||||
u = client.User.Create().SetAge(5).SetName("bar").SetRole("Admin").SaveX(ctx)
|
||||
require.EqualValues(t, 5, u.Age)
|
||||
require.Equal(t, "bar", u.Name)
|
||||
require.Equal(t, "Admin", u.Role)
|
||||
|
||||
_, err := client.User.Create().SetAge(2).SetName("foobarbazqux").Save(ctx)
|
||||
require.Error(t, err, "name is limited to 10 chars")
|
||||
@@ -93,6 +102,14 @@ func SanityV1(t *testing.T, client *entv1.Client) {
|
||||
client.User.Create().SetAge(3).SetName("foo").SetAddress("tlv").SaveX(ctx)
|
||||
_, err = client.User.Create().SetAge(4).SetName("foo").SetAddress("tlv").Save(ctx)
|
||||
require.Error(t, err)
|
||||
|
||||
// default role collation is case sensitive
|
||||
c := client.User.Query().Where(userv1.Role("admin")).CountX(ctx)
|
||||
assert.Equal(t, 1, c)
|
||||
c = client.User.Query().Where(userv1.Role("Admin")).CountX(ctx)
|
||||
assert.Equal(t, 1, c)
|
||||
c = client.User.Query().Where(userv1.Role("ADMIN")).CountX(ctx)
|
||||
assert.Zero(t, c)
|
||||
}
|
||||
|
||||
func SanityV2(t *testing.T, client *entv2.Client) {
|
||||
@@ -120,6 +137,11 @@ func SanityV2(t *testing.T, client *entv2.Client) {
|
||||
)
|
||||
}
|
||||
|
||||
func Collation(t *testing.T, client *entv2.Client) {
|
||||
t.Log("testing case insensitive collation update on sql specific dialects")
|
||||
require.Equal(t, 2, client.User.Query().Where(user.Role("ADMIN")).CountX(context.Background()))
|
||||
}
|
||||
|
||||
func EqualFold(t *testing.T, client *entv2.Client) {
|
||||
ctx := context.Background()
|
||||
t.Log("testing equal-fold on sql specific dialects")
|
||||
|
||||
Reference in New Issue
Block a user