-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
78 lines (63 loc) · 1.8 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gosqle
import (
"fmt"
"io"
"github.com/dwethmar/gosqle/clauses"
"github.com/dwethmar/gosqle/clauses/join"
"github.com/dwethmar/gosqle/clauses/orderby"
"github.com/dwethmar/gosqle/clauses/set"
"github.com/dwethmar/gosqle/clauses/where"
"github.com/dwethmar/gosqle/logic"
"github.com/dwethmar/gosqle/statement"
)
// Update is a wrapper for a update query statement.
type Update struct {
statement.Statement
}
// From adds a from clause to the select statement.
func (u *Update) Join(j ...join.Options) *Update {
if len(j) == 0 {
return u
}
return u.SetClause(join.New(j))
}
// Set adds a set clause to the select statement.
func (u *Update) Set(changes ...set.Change) *Update {
return u.SetClause(set.New(changes))
}
// Where adds a where clause to the select statement.
func (u *Update) Where(conditions ...logic.Logic) *Update {
if len(conditions) == 0 {
return u
}
return u.SetClause(where.New(conditions))
}
// OrderBy adds a order by clause to the select statement.
func (u *Update) OrderBy(sorting ...orderby.Sort) *Update {
if len(sorting) == 0 {
return u
}
return u.SetClause(orderby.New(sorting))
}
// SetClause sets the clause for the select statement.
func (u *Update) SetClause(c clauses.Clause) *Update {
u.Statement.SetClause(c)
return u
}
// WriteTo writes the select statement to the given writer.
func (u *Update) Write(sw io.StringWriter) error {
if err := u.Statement.Write(sw); err != nil {
return fmt.Errorf("failed to write insert statement: %v", err)
}
// Add a semicolon to the end of the query.
if _, err := sw.WriteString(";"); err != nil {
return fmt.Errorf("failed to write semicolon: %v", err)
}
return nil
}
// NewUpdate creates a new update query.
func NewUpdate(table string) *Update {
return &Update{
Statement: statement.NewUpdate(table),
}
}