Replace For with Locking

This commit is contained in:
Jinzhu 2020-06-06 21:35:28 +08:00
parent 52b763aab3
commit 38d1cd2bf1
2 changed files with 17 additions and 42 deletions

View File

@ -1,9 +1,5 @@
package clause
type For struct {
Lockings []Locking
}
type Locking struct {
Strength string
Table Table
@ -11,18 +7,12 @@ 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(' ')
}
builder.WriteString("FOR ")
func (locking Locking) Build(builder Builder) {
builder.WriteString(locking.Strength)
if locking.Table.Name != "" {
builder.WriteString(" OF ")
@ -33,16 +23,9 @@ func (f For) Build(builder Builder) {
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
}

View File

@ -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,
},
}