From 38d1cd2bf182f55f28a8c909395ceaa2019d8b99 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 6 Jun 2020 21:35:28 +0800 Subject: [PATCH] Replace For with Locking --- clause/locking.go | 41 ++++++++++++----------------------------- clause/locking_test.go | 18 +++++------------- 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/clause/locking.go b/clause/locking.go index 3be1063b..290aac92 100644 --- a/clause/locking.go +++ b/clause/locking.go @@ -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 } diff --git a/clause/locking_test.go b/clause/locking_test.go index 6f507692..5ca30ef0 100644 --- a/clause/locking_test.go +++ b/clause/locking_test.go @@ -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, }, }