-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
50 lines (41 loc) · 1.2 KB
/
insert.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
package gosqle
import (
"fmt"
"io"
"github.com/dwethmar/gosqle/clauses"
"github.com/dwethmar/gosqle/clauses/values"
"github.com/dwethmar/gosqle/expressions"
"github.com/dwethmar/gosqle/statement"
)
// Insert is a wrapper for a insert query statement.
type Insert struct {
statement.Statement
}
// Values adds values to the insert query.
func (i *Insert) Values(arguments ...expressions.Expression) *Insert {
i.Statement.SetClause(values.New(arguments))
return i
}
// SetClause sets the clause for the query.
func (i *Insert) SetClause(c clauses.Clause) *Insert {
i.Statement.SetClause(c)
return i
}
// Write writes the insert query to the given writer.
// It also adds a semicolon to the end of the query.
func (i *Insert) Write(sw io.StringWriter) error {
if err := i.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
}
// NewInsert creates a new insert query.
func NewInsert(into string, columns ...string) *Insert {
return &Insert{
Statement: statement.NewInsert(into, columns),
}
}