Skip to content

Commit 148319b

Browse files
committed
Make InsertSelect a standalone statement
1 parent 893d4d9 commit 148319b

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
@@ -880,7 +880,7 @@ func BuildInsertIgnoreStatement(db *DB, stmt InsertStatement) (string, error) {
880880
return NewQueryBuilder(db.DriverName()).InsertIgnoreStatement(stmt)
881881
}
882882

883-
func BuildInsertSelectStatement(db *DB, stmt InsertSelectStatement) string {
883+
func BuildInsertSelectStatement(db *DB, stmt InsertSelectStatement) (string, error) {
884884
return NewQueryBuilder(db.DriverName()).InsertSelectStatement(stmt)
885885
}
886886

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
@@ -23,7 +23,7 @@ type QueryBuilder interface {
2323

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

26-
InsertSelectStatement(stmt InsertSelectStatement) string
26+
InsertSelectStatement(stmt InsertSelectStatement) (string, error)
2727

2828
SelectStatement(stmt SelectStatement) string
2929

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

143-
func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) string {
144-
selectStmt := qb.SelectStatement(stmt.Select())
143+
func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) (string, error) {
145144
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludedColumns())
145+
146+
sel := stmt.Select()
147+
if sel == nil {
148+
return "", fmt.Errorf("%w: %s", ErrMissingStatementPart, "select statement")
149+
}
150+
selectStmt := qb.SelectStatement(sel)
151+
146152
into := stmt.Table()
147153
if into == "" {
148154
into = TableName(stmt.Entity())
@@ -153,15 +159,17 @@ func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) string
153159
into,
154160
strings.Join(columns, `", "`),
155161
selectStmt,
156-
)
162+
), nil
157163
}
158164

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

0 commit comments

Comments
 (0)