mirror of https://github.com/go-gorm/gorm.git
Replace For with Locking
This commit is contained in:
parent
52b763aab3
commit
38d1cd2bf1
|
@ -1,9 +1,5 @@
|
|||
package clause
|
||||
|
||||
type For struct {
|
||||
Lockings []Locking
|
||||
}
|
||||
|
||||
type Locking struct {
|
||||
Strength string
|
||||
Table Table
|
||||
|
@ -11,38 +7,25 @@ type Locking struct {
|
|||
}
|
||||
|
||||
// Name where clause name
|
||||
func (f For) Name() string {
|
||||
func (locking Locking) Name() string {
|
||||
return "FOR"
|
||||
}
|
||||
|
||||
// Build build where clause
|
||||
func (f For) Build(builder Builder) {
|
||||
for idx, locking := range f.Lockings {
|
||||
if idx > 0 {
|
||||
builder.WriteByte(' ')
|
||||
}
|
||||
func (locking Locking) Build(builder Builder) {
|
||||
builder.WriteString(locking.Strength)
|
||||
if locking.Table.Name != "" {
|
||||
builder.WriteString(" OF ")
|
||||
builder.WriteQuoted(locking.Table)
|
||||
}
|
||||
|
||||
builder.WriteString("FOR ")
|
||||
builder.WriteString(locking.Strength)
|
||||
if locking.Table.Name != "" {
|
||||
builder.WriteString(" OF ")
|
||||
builder.WriteQuoted(locking.Table)
|
||||
}
|
||||
|
||||
if locking.Options != "" {
|
||||
builder.WriteByte(' ')
|
||||
builder.WriteString(locking.Options)
|
||||
}
|
||||
if locking.Options != "" {
|
||||
builder.WriteByte(' ')
|
||||
builder.WriteString(locking.Options)
|
||||
}
|
||||
}
|
||||
|
||||
// MergeClause merge order by clauses
|
||||
func (f For) MergeClause(clause *Clause) {
|
||||
clause.Name = ""
|
||||
|
||||
if v, ok := clause.Expression.(For); ok {
|
||||
f.Lockings = append(v.Lockings, f.Lockings...)
|
||||
}
|
||||
|
||||
clause.Expression = f
|
||||
func (locking Locking) MergeClause(clause *Clause) {
|
||||
clause.Expression = locking
|
||||
}
|
||||
|
|
|
@ -14,24 +14,16 @@ func TestFor(t *testing.T) {
|
|||
Vars []interface{}
|
||||
}{
|
||||
{
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.For{
|
||||
Lockings: []clause.Locking{{Strength: "UPDATE"}},
|
||||
}},
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: "UPDATE"}},
|
||||
"SELECT * FROM `users` FOR UPDATE", nil,
|
||||
},
|
||||
{
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.For{
|
||||
Lockings: []clause.Locking{{Strength: "UPDATE"}, {Strength: "SHARE", Table: clause.Table{Name: clause.CurrentTable}}},
|
||||
}},
|
||||
"SELECT * FROM `users` FOR UPDATE FOR SHARE OF `users`", nil,
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: "SHARE", Table: clause.Table{Name: clause.CurrentTable}}},
|
||||
"SELECT * FROM `users` FOR SHARE OF `users`", nil,
|
||||
},
|
||||
{
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.For{
|
||||
Lockings: []clause.Locking{{Strength: "UPDATE"}, {Strength: "SHARE", Table: clause.Table{Name: clause.CurrentTable}}},
|
||||
}, clause.For{
|
||||
Lockings: []clause.Locking{{Strength: "UPDATE", Options: "NOWAIT"}},
|
||||
}},
|
||||
"SELECT * FROM `users` FOR UPDATE FOR SHARE OF `users` FOR UPDATE NOWAIT", nil,
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: "UPDATE"}, clause.Locking{Strength: "UPDATE", Options: "NOWAIT"}},
|
||||
"SELECT * FROM `users` FOR UPDATE NOWAIT", nil,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue