diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index 509727f43..8547b86dc 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -564,6 +564,9 @@ func TestQuery(t *testing.T) { } return db.NewInsert().Model(&Model{ID: 123, Time: time.Unix(0, 0)}) }, + func(db *bun.DB) schema.QueryAppender { + return db.NewInsert().ColumnExpr("id, name").Table("dest").Table("src") + }, } timeRE := regexp.MustCompile(`'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+(\+\d{2}:\d{2})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-89 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-89 new file mode 100644 index 000000000..24ec27922 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-89 @@ -0,0 +1 @@ +INSERT INTO `dest` (id, name) SELECT id, name FROM `src` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-89 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-89 new file mode 100644 index 000000000..24ec27922 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-89 @@ -0,0 +1 @@ +INSERT INTO `dest` (id, name) SELECT id, name FROM `src` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-89 b/internal/dbtest/testdata/snapshots/TestQuery-pg-89 new file mode 100644 index 000000000..05a5ce328 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-89 @@ -0,0 +1 @@ +INSERT INTO "dest" (id, name) SELECT id, name FROM "src" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-89 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-89 new file mode 100644 index 000000000..05a5ce328 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-89 @@ -0,0 +1 @@ +INSERT INTO "dest" (id, name) SELECT id, name FROM "src" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-89 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-89 new file mode 100644 index 000000000..05a5ce328 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-89 @@ -0,0 +1 @@ +INSERT INTO "dest" (id, name) SELECT id, name FROM "src" diff --git a/query_insert.go b/query_insert.go index e56089d24..79d9a4309 100644 --- a/query_insert.go +++ b/query_insert.go @@ -84,6 +84,11 @@ func (q *InsertQuery) Column(columns ...string) *InsertQuery { return q } +func (q *InsertQuery) ColumnExpr(query string, args ...interface{}) *InsertQuery { + q.addColumn(schema.SafeQuery(query, args)) + return q +} + func (q *InsertQuery) ExcludeColumn(columns ...string) *InsertQuery { q.excludeColumn(columns) return q @@ -209,7 +214,18 @@ func (q *InsertQuery) appendColumnsValues( b = append(b, ")"...) } - b = append(b, " SELECT * FROM "...) + b = append(b, " SELECT "...) + + if q.columns != nil { + b, err = q.appendColumns(fmter, b) + if err != nil { + return nil, err + } + } else { + b = append(b, "*"...) + } + + b = append(b, " FROM "...) b, err = q.appendOtherTables(fmter, b) if err != nil { return nil, err