Skip to content

Commit bee6d75

Browse files
committed
Add and implement QueryBuilder.InsertSelectStatement()
1 parent 045b496 commit bee6d75

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

database/query_builder.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
type QueryBuilder interface {
1212
InsertStatement(stmt InsertStatement) string
1313

14+
InsertSelectStatement(stmt InsertSelectStatement) string
15+
1416
SelectStatement(stmt SelectStatement) string
1517

1618
BuildColumns(entity Entity, columns []string, excludedColumns []string) []string
@@ -43,6 +45,22 @@ func (qb *queryBuilder) InsertStatement(stmt InsertStatement) string {
4345
)
4446
}
4547

48+
func (qb *queryBuilder) InsertSelectStatement(stmt InsertSelectStatement) string {
49+
selectStmt := qb.SelectStatement(stmt.Select())
50+
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludedColumns())
51+
into := stmt.Table()
52+
if into == "" {
53+
into = TableName(stmt.Entity())
54+
}
55+
56+
return fmt.Sprintf(
57+
`INSERT INTO "%s" ("%s") %s`,
58+
into,
59+
strings.Join(columns, `", "`),
60+
selectStmt,
61+
)
62+
}
63+
4664
func (qb *queryBuilder) SelectStatement(stmt SelectStatement) string {
4765
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludeColumns())
4866
from := stmt.Table()

0 commit comments

Comments
 (0)