Skip to content

Commit b0e3835

Browse files
committed
Make InsertSelect a standalone statement
1 parent 8f65e96 commit b0e3835

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

database/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ func BuildInsertIgnoreStatement(db *DB, stmt InsertStatement) (string, error) {
906906
return NewQueryBuilder(db.DriverName()).InsertIgnoreStatement(stmt)
907907
}
908908

909-
func BuildInsertSelectStatement(db *DB, stmt InsertSelectStatement) string {
909+
func BuildInsertSelectStatement(db *DB, stmt InsertSelectStatement) (string, error) {
910910
return NewQueryBuilder(db.DriverName()).InsertSelectStatement(stmt)
911911
}
912912

database/insert.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,68 @@ func (i *insertStatement) apply(opts *insertOptions) {
8686

8787
// InsertSelectStatement is the interface for building INSERT SELECT statements.
8888
type InsertSelectStatement interface {
89-
InsertStatement
89+
// Into sets the table name for the INSERT SELECT statement.
90+
// Overrides the table name provided by the entity.
91+
Into(table string) InsertSelectStatement
92+
93+
// SetColumns sets the columns to be inserted.
94+
SetColumns(columns ...string) InsertSelectStatement
95+
96+
// SetExcludedColumns sets the columns to be excluded from the INSERT SELECT statement.
97+
// Excludes also columns set by SetColumns.
98+
SetExcludedColumns(columns ...string) InsertSelectStatement
9099

91100
// SetSelect sets the SELECT statement for the INSERT SELECT statement.
92101
SetSelect(stmt SelectStatement) InsertSelectStatement
93102

103+
// Entity returns the entity associated with the INSERT SELECT statement.
104+
Entity() Entity
105+
106+
// Table returns the table name for the INSERT SELECT statement.
107+
Table() string
108+
109+
// Columns returns the columns to be inserted.
110+
Columns() []string
111+
112+
// ExcludedColumns returns the columns to be excluded from the INSERT statement.
113+
ExcludedColumns() []string
114+
94115
// Select returns the SELECT statement for the INSERT SELECT statement.
95116
Select() SelectStatement
96117
}
97118

98-
// NewInsertSelect returns a new insertSelectStatement for the given entity.
99-
func NewInsertSelect(entity Entity) InsertSelectStatement {
119+
// NewInsertSelectStatement returns a new insertSelectStatement for the given entity.
120+
func NewInsertSelectStatement(entity Entity) InsertSelectStatement {
100121
return &insertSelectStatement{
101-
insertStatement: insertStatement{
102-
entity: entity,
103-
},
122+
entity: entity,
104123
}
105124
}
106125

107126
// insertSelectStatement is the default implementation of the InsertSelectStatement interface.
108127
type insertSelectStatement struct {
109-
insertStatement
110-
selectStmt SelectStatement
128+
entity Entity
129+
table string
130+
columns []string
131+
excludedColumns []string
132+
selectStmt SelectStatement
133+
}
134+
135+
func (i *insertSelectStatement) Into(table string) InsertSelectStatement {
136+
i.table = table
137+
138+
return i
139+
}
140+
141+
func (i *insertSelectStatement) SetColumns(columns ...string) InsertSelectStatement {
142+
i.columns = columns
143+
144+
return i
145+
}
146+
147+
func (i *insertSelectStatement) SetExcludedColumns(columns ...string) InsertSelectStatement {
148+
i.excludedColumns = columns
149+
150+
return i
111151
}
112152

113153
func (i *insertSelectStatement) SetSelect(stmt SelectStatement) InsertSelectStatement {
@@ -116,6 +156,22 @@ func (i *insertSelectStatement) SetSelect(stmt SelectStatement) InsertSelectStat
116156
return i
117157
}
118158

159+
func (i *insertSelectStatement) Entity() Entity {
160+
return i.entity
161+
}
162+
163+
func (i *insertSelectStatement) Table() string {
164+
return i.table
165+
}
166+
167+
func (i *insertSelectStatement) Columns() []string {
168+
return i.columns
169+
}
170+
171+
func (i *insertSelectStatement) ExcludedColumns() []string {
172+
return i.excludedColumns
173+
}
174+
119175
func (i *insertSelectStatement) Select() SelectStatement {
120176
return i.selectStmt
121177
}

database/query_builder.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type QueryBuilder interface {
2222

2323
InsertIgnoreStatement(stmt InsertStatement) (string, error)
2424

25-
InsertSelectStatement(stmt InsertSelectStatement) string
25+
InsertSelectStatement(stmt InsertSelectStatement) (string, error)
2626

2727
SelectStatement(stmt SelectStatement) string
2828

@@ -139,9 +139,15 @@ func (qb *queryBuilder) InsertIgnoreStatement(stmt InsertStatement) (string, err
139139
}
140140
}
141141

142-
func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) string {
143-
selectStmt := qb.SelectStatement(stmt.Select())
142+
func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) (string, error) {
144143
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludedColumns())
144+
145+
sel := stmt.Select()
146+
if sel == nil {
147+
return "", fmt.Errorf("%w: %s", ErrMissingStatementPart, "select statement")
148+
}
149+
selectStmt := qb.SelectStatement(sel)
150+
145151
into := stmt.Table()
146152
if into == "" {
147153
into = TableName(stmt.Entity())
@@ -152,15 +158,17 @@ func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) string
152158
into,
153159
strings.Join(columns, `", "`),
154160
selectStmt,
155-
)
161+
), nil
156162
}
157163

158164
func (qb *queryBuilder) SelectStatement(stmt SelectStatement) string {
159165
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludedColumns())
166+
160167
from := stmt.Table()
161168
if from == "" {
162169
from = TableName(stmt.Entity())
163170
}
171+
164172
where := stmt.Where()
165173
if where != "" {
166174
where = fmt.Sprintf(" WHERE %s", where)

0 commit comments

Comments
 (0)