diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index 777ad93c9..a401614f8 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -1596,6 +1596,148 @@ func TestQuery(t *testing.T) { return db.NewDelete().Model(&Model{}).WherePK().Returning("*") }, }, + { + id: 173, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewAddColumn(). + Model(&Model{}). + Comment("test"). + ColumnExpr("column_name VARCHAR(123)") + }, + }, + { + id: 174, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewDropColumn(). + Model(&Model{}). + Comment("test"). + Column("column_name") + }, + }, + { + id: 175, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewDelete(). + Model(&Model{}). + WherePK(). + Comment("test") + }, + }, + { + id: 176, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewCreateIndex(). + Model(&Model{}). + Unique(). + Index("index_name"). + Comment("test"). + Column("column_name") + }, + }, + { + id: 177, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewDropIndex(). + Model(&Model{}). + Comment("test"). + Index("index_name"). + Comment("test") + }, + }, + { + id: 178, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewInsert(). + Model(&Model{}). + Comment("test"). + Value("column_name", "value") + }, + }, + { + id: 179, + query: func(db *bun.DB) schema.QueryAppender { + type Model struct { + ID int64 `bun:",pk,autoincrement"` + Name string + Value string + } + + newModels := []*Model{ + {Name: "A", Value: "world"}, + {Name: "B", Value: "test"}, + } + + return db.NewMerge(). + Model(new(Model)). + With("_data", db.NewValues(&newModels)). + Using("_data"). + On("?TableAlias.name = _data.name"). + WhenUpdate("MATCHED", func(q *bun.UpdateQuery) *bun.UpdateQuery { + return q.Set("value = _data.value") + }). + WhenInsert("NOT MATCHED", func(q *bun.InsertQuery) *bun.InsertQuery { + return q.Value("name", "_data.name").Value("value", "_data.value") + }). + Comment("test"). + Returning("$action") + + }, + }, + { + id: 180, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewRaw("SELECT 1").Comment("test") + }, + }, + { + id: 181, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewSelect(). + Model(&Model{}). + Comment("test") + }, + }, + { + id: 182, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewCreateTable(). + Model(&Model{}). + Comment("test") + }, + }, + { + id: 183, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewDropTable(). + Model(&Model{}). + Comment("test") + }, + }, + { + id: 184, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewTruncateTable(). + Model(&Model{}). + Comment("test") + }, + }, + { + id: 185, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewUpdate(). + Model(&Model{}). + Comment("test"). + Set("name = ?", "new-name"). + Where("id = ?", 1) + }, + }, + { + id: 186, + query: func(db *bun.DB) schema.QueryAppender { + return db.NewValues(&[]Model{{1, "hello"}}). + Comment("test") + }, + }, } timeRE := regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-173 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-173 new file mode 100644 index 000000000..c34a43e69 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE `models` ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-174 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-174 new file mode 100644 index 000000000..87a608dff --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE `models` DROP COLUMN `column_name` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-175 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-175 new file mode 100644 index 000000000..167242f92 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM `models` WHERE (`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-176 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-176 new file mode 100644 index 000000000..678857ed9 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX `index_name` ON `models` (`column_name`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-177 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-178 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-178 new file mode 100644 index 000000000..3008a016e --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO `models` (`id`, `str`, `column_name`) VALUES (DEFAULT, '', value) RETURNING `id` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-179 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-179 new file mode 100644 index 000000000..1f669bf4f --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-179 @@ -0,0 +1 @@ +bun: merge not supported for current dialect diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-180 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-181 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-181 new file mode 100644 index 000000000..ff8eac338 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-181 @@ -0,0 +1 @@ +/* test */ SELECT `model`.`id`, `model`.`str` FROM `models` AS `model` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-182 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-182 new file mode 100644 index 000000000..b09a257fa --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE `models` (`id` BIGINT NOT NULL AUTO_INCREMENT, `str` VARCHAR(255), PRIMARY KEY (`id`)) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-183 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-183 new file mode 100644 index 000000000..bc85b0ff3 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE `models` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-184 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-184 new file mode 100644 index 000000000..453e07332 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-184 @@ -0,0 +1 @@ +/* test */ TRUNCATE TABLE `models` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-185 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-185 new file mode 100644 index 000000000..b12d24b77 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-185 @@ -0,0 +1 @@ +/* test */ UPDATE `models` AS `model` SET name = 'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-186 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-186 new file mode 100644 index 000000000..ff9452af2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-186 @@ -0,0 +1 @@ +/* test */ VALUES ROW(1, 'hello') diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-173 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-173 new file mode 100644 index 000000000..b390aea35 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-174 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-174 new file mode 100644 index 000000000..ea24fcda2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" DROP COLUMN "column_name" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-175 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-175 new file mode 100644 index 000000000..4871a5fe9 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM "models" WHERE ("id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-176 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-176 new file mode 100644 index 000000000..bdc09b6d5 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name") diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-177 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-178 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-178 new file mode 100644 index 000000000..fccd21ee1 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO "models" ("str", "column_name") OUTPUT INSERTED."id" VALUES (N'', value) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-179 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-179 new file mode 100644 index 000000000..178618d38 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-179 @@ -0,0 +1 @@ +/* test */ WITH "_data" AS (SELECT * FROM (VALUES (NULL, N'A', N'world'), (NULL, N'B', N'test')) AS t ("id", "name", "value")) MERGE "models" AS "model" USING _data ON "model".name = _data.name WHEN MATCHED THEN UPDATE SET value = _data.value WHEN NOT MATCHED THEN INSERT ("name", "value") VALUES (_data.name, _data.value) OUTPUT $action; diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-180 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-181 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-181 new file mode 100644 index 000000000..b3cef6088 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-181 @@ -0,0 +1 @@ +/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-182 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-182 new file mode 100644 index 000000000..cc9d4f6c6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE "models" ("id" BIGINT NOT NULL IDENTITY, "str" VARCHAR(255), PRIMARY KEY ("id")) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-183 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-183 new file mode 100644 index 000000000..cf503564a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE "models" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-184 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-184 new file mode 100644 index 000000000..dbfb31e8d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-184 @@ -0,0 +1 @@ +/* test */ DELETE FROM "models" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-185 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-185 new file mode 100644 index 000000000..b90915575 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-185 @@ -0,0 +1 @@ +/* test */ UPDATE "models" SET name = N'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-186 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-186 new file mode 100644 index 000000000..192158c40 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-186 @@ -0,0 +1 @@ +/* test */ VALUES (1, N'hello') diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-173 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-173 new file mode 100644 index 000000000..c34a43e69 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE `models` ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-174 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-174 new file mode 100644 index 000000000..87a608dff --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE `models` DROP COLUMN `column_name` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-175 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-175 new file mode 100644 index 000000000..167242f92 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM `models` WHERE (`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-176 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-176 new file mode 100644 index 000000000..678857ed9 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX `index_name` ON `models` (`column_name`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-177 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-178 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-178 new file mode 100644 index 000000000..35131417a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO `models` (`id`, `str`, `column_name`) VALUES (DEFAULT, '', value) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-179 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-179 new file mode 100644 index 000000000..1f669bf4f --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-179 @@ -0,0 +1 @@ +bun: merge not supported for current dialect diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-180 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-181 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-181 new file mode 100644 index 000000000..ff8eac338 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-181 @@ -0,0 +1 @@ +/* test */ SELECT `model`.`id`, `model`.`str` FROM `models` AS `model` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-182 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-182 new file mode 100644 index 000000000..b09a257fa --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE `models` (`id` BIGINT NOT NULL AUTO_INCREMENT, `str` VARCHAR(255), PRIMARY KEY (`id`)) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-183 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-183 new file mode 100644 index 000000000..bc85b0ff3 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE `models` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-184 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-184 new file mode 100644 index 000000000..453e07332 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-184 @@ -0,0 +1 @@ +/* test */ TRUNCATE TABLE `models` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-185 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-185 new file mode 100644 index 000000000..b12d24b77 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-185 @@ -0,0 +1 @@ +/* test */ UPDATE `models` AS `model` SET name = 'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-186 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-186 new file mode 100644 index 000000000..ff9452af2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-186 @@ -0,0 +1 @@ +/* test */ VALUES ROW(1, 'hello') diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-173 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-173 new file mode 100644 index 000000000..c34a43e69 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE `models` ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-174 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-174 new file mode 100644 index 000000000..87a608dff --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE `models` DROP COLUMN `column_name` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-175 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-175 new file mode 100644 index 000000000..cb15be3fc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM `models` AS `model` WHERE (`model`.`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-176 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-176 new file mode 100644 index 000000000..678857ed9 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX `index_name` ON `models` (`column_name`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-177 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-178 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-178 new file mode 100644 index 000000000..35131417a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO `models` (`id`, `str`, `column_name`) VALUES (DEFAULT, '', value) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-179 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-179 new file mode 100644 index 000000000..1f669bf4f --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-179 @@ -0,0 +1 @@ +bun: merge not supported for current dialect diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-180 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-181 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-181 new file mode 100644 index 000000000..ff8eac338 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-181 @@ -0,0 +1 @@ +/* test */ SELECT `model`.`id`, `model`.`str` FROM `models` AS `model` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-182 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-182 new file mode 100644 index 000000000..b09a257fa --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE `models` (`id` BIGINT NOT NULL AUTO_INCREMENT, `str` VARCHAR(255), PRIMARY KEY (`id`)) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-183 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-183 new file mode 100644 index 000000000..bc85b0ff3 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE `models` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-184 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-184 new file mode 100644 index 000000000..453e07332 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-184 @@ -0,0 +1 @@ +/* test */ TRUNCATE TABLE `models` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-185 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-185 new file mode 100644 index 000000000..b12d24b77 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-185 @@ -0,0 +1 @@ +/* test */ UPDATE `models` AS `model` SET name = 'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-186 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-186 new file mode 100644 index 000000000..ff9452af2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-186 @@ -0,0 +1 @@ +/* test */ VALUES ROW(1, 'hello') diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-173 b/internal/dbtest/testdata/snapshots/TestQuery-pg-173 new file mode 100644 index 000000000..b390aea35 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-174 b/internal/dbtest/testdata/snapshots/TestQuery-pg-174 new file mode 100644 index 000000000..ea24fcda2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" DROP COLUMN "column_name" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-175 b/internal/dbtest/testdata/snapshots/TestQuery-pg-175 new file mode 100644 index 000000000..a9250be4d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-176 b/internal/dbtest/testdata/snapshots/TestQuery-pg-176 new file mode 100644 index 000000000..bdc09b6d5 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name") diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-177 b/internal/dbtest/testdata/snapshots/TestQuery-pg-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-178 b/internal/dbtest/testdata/snapshots/TestQuery-pg-178 new file mode 100644 index 000000000..16cb9ff39 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO "models" ("id", "str", "column_name") VALUES (DEFAULT, '', value) RETURNING "id" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-179 b/internal/dbtest/testdata/snapshots/TestQuery-pg-179 new file mode 100644 index 000000000..5d24f3158 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-179 @@ -0,0 +1 @@ +/* test */ WITH "_data" ("id", "name", "value") AS (VALUES (NULL::BIGINT, 'A'::VARCHAR, 'world'::VARCHAR), (NULL::BIGINT, 'B'::VARCHAR, 'test'::VARCHAR)) MERGE INTO "models" AS "model" USING _data ON "model".name = _data.name WHEN MATCHED THEN UPDATE SET value = _data.value WHEN NOT MATCHED THEN INSERT ("id", "name", "value") VALUES (DEFAULT, _data.name, _data.value); diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-180 b/internal/dbtest/testdata/snapshots/TestQuery-pg-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-181 b/internal/dbtest/testdata/snapshots/TestQuery-pg-181 new file mode 100644 index 000000000..b3cef6088 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-181 @@ -0,0 +1 @@ +/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-182 b/internal/dbtest/testdata/snapshots/TestQuery-pg-182 new file mode 100644 index 000000000..85ad3769d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE "models" ("id" BIGSERIAL NOT NULL, "str" VARCHAR, PRIMARY KEY ("id")) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-183 b/internal/dbtest/testdata/snapshots/TestQuery-pg-183 new file mode 100644 index 000000000..cf503564a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE "models" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-184 b/internal/dbtest/testdata/snapshots/TestQuery-pg-184 new file mode 100644 index 000000000..25f91fd3c --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-184 @@ -0,0 +1 @@ +/* test */ TRUNCATE TABLE "models" RESTART IDENTITY diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-185 b/internal/dbtest/testdata/snapshots/TestQuery-pg-185 new file mode 100644 index 000000000..17ea651c6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-185 @@ -0,0 +1 @@ +/* test */ UPDATE "models" AS "model" SET name = 'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-186 b/internal/dbtest/testdata/snapshots/TestQuery-pg-186 new file mode 100644 index 000000000..0ef93e567 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-186 @@ -0,0 +1 @@ +/* test */ VALUES (1::BIGINT, 'hello'::VARCHAR) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-173 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-173 new file mode 100644 index 000000000..b390aea35 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-174 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-174 new file mode 100644 index 000000000..ea24fcda2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" DROP COLUMN "column_name" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-175 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-175 new file mode 100644 index 000000000..a9250be4d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-176 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-176 new file mode 100644 index 000000000..bdc09b6d5 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name") diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-177 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-178 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-178 new file mode 100644 index 000000000..16cb9ff39 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO "models" ("id", "str", "column_name") VALUES (DEFAULT, '', value) RETURNING "id" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-179 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-179 new file mode 100644 index 000000000..5d24f3158 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-179 @@ -0,0 +1 @@ +/* test */ WITH "_data" ("id", "name", "value") AS (VALUES (NULL::BIGINT, 'A'::VARCHAR, 'world'::VARCHAR), (NULL::BIGINT, 'B'::VARCHAR, 'test'::VARCHAR)) MERGE INTO "models" AS "model" USING _data ON "model".name = _data.name WHEN MATCHED THEN UPDATE SET value = _data.value WHEN NOT MATCHED THEN INSERT ("id", "name", "value") VALUES (DEFAULT, _data.name, _data.value); diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-180 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-181 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-181 new file mode 100644 index 000000000..b3cef6088 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-181 @@ -0,0 +1 @@ +/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-182 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-182 new file mode 100644 index 000000000..85ad3769d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE "models" ("id" BIGSERIAL NOT NULL, "str" VARCHAR, PRIMARY KEY ("id")) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-183 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-183 new file mode 100644 index 000000000..cf503564a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE "models" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-184 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-184 new file mode 100644 index 000000000..25f91fd3c --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-184 @@ -0,0 +1 @@ +/* test */ TRUNCATE TABLE "models" RESTART IDENTITY diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-185 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-185 new file mode 100644 index 000000000..17ea651c6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-185 @@ -0,0 +1 @@ +/* test */ UPDATE "models" AS "model" SET name = 'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-186 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-186 new file mode 100644 index 000000000..0ef93e567 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-186 @@ -0,0 +1 @@ +/* test */ VALUES (1::BIGINT, 'hello'::VARCHAR) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-173 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-173 new file mode 100644 index 000000000..b390aea35 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-173 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-174 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-174 new file mode 100644 index 000000000..ea24fcda2 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-174 @@ -0,0 +1 @@ +/* test */ ALTER TABLE "models" DROP COLUMN "column_name" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-175 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-175 new file mode 100644 index 000000000..a9250be4d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-175 @@ -0,0 +1 @@ +/* test */ DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-176 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-176 new file mode 100644 index 000000000..bdc09b6d5 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-176 @@ -0,0 +1 @@ +/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name") diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-177 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-177 new file mode 100644 index 000000000..8499c1dce --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-177 @@ -0,0 +1 @@ +/* test */ DROP INDEX index_name diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-178 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-178 new file mode 100644 index 000000000..a2d5a0490 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-178 @@ -0,0 +1 @@ +/* test */ INSERT INTO "models" ("str", "column_name") VALUES ('', value) RETURNING "id" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-179 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-179 new file mode 100644 index 000000000..1f669bf4f --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-179 @@ -0,0 +1 @@ +bun: merge not supported for current dialect diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-180 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-180 new file mode 100644 index 000000000..91b2f7636 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-180 @@ -0,0 +1 @@ +/* test */ SELECT 1 diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-181 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-181 new file mode 100644 index 000000000..b3cef6088 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-181 @@ -0,0 +1 @@ +/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-182 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-182 new file mode 100644 index 000000000..ad3341bfc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-182 @@ -0,0 +1 @@ +/* test */ CREATE TABLE "models" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "str" VARCHAR) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-183 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-183 new file mode 100644 index 000000000..cf503564a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-183 @@ -0,0 +1 @@ +/* test */ DROP TABLE "models" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-184 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-184 new file mode 100644 index 000000000..dbfb31e8d --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-184 @@ -0,0 +1 @@ +/* test */ DELETE FROM "models" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-185 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-185 new file mode 100644 index 000000000..17ea651c6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-185 @@ -0,0 +1 @@ +/* test */ UPDATE "models" AS "model" SET name = 'new-name' WHERE (id = 1) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-186 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-186 new file mode 100644 index 000000000..9f94e9abe --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-186 @@ -0,0 +1 @@ +/* test */ VALUES (1, 'hello') diff --git a/query_column_add.go b/query_column_add.go index 50576873c..48cb2542a 100644 --- a/query_column_add.go +++ b/query_column_add.go @@ -13,6 +13,7 @@ type AddColumnQuery struct { baseQuery ifNotExists bool + comment string } var _ Query = (*AddColumnQuery)(nil) @@ -85,6 +86,14 @@ func (q *AddColumnQuery) IfNotExists() *AddColumnQuery { //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *AddColumnQuery) Comment(comment string) *AddColumnQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *AddColumnQuery) Operation() string { return "ADD COLUMN" } @@ -93,6 +102,9 @@ func (q *AddColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte if q.err != nil { return nil, q.err } + + b = appendComment(b, q.comment) + if len(q.columns) != 1 { return nil, fmt.Errorf("bun: AddColumnQuery requires exactly one column") } diff --git a/query_column_drop.go b/query_column_drop.go index 24fc93cfd..986bffed3 100644 --- a/query_column_drop.go +++ b/query_column_drop.go @@ -11,6 +11,8 @@ import ( type DropColumnQuery struct { baseQuery + + comment string } var _ Query = (*DropColumnQuery)(nil) @@ -85,6 +87,14 @@ func (q *DropColumnQuery) ColumnExpr(query string, args ...interface{}) *DropCol //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *DropColumnQuery) Comment(comment string) *DropColumnQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *DropColumnQuery) Operation() string { return "DROP COLUMN" } @@ -93,6 +103,9 @@ func (q *DropColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byt if q.err != nil { return nil, q.err } + + b = appendComment(b, q.comment) + if len(q.columns) != 1 { return nil, fmt.Errorf("bun: DropColumnQuery requires exactly one column") } diff --git a/query_delete.go b/query_delete.go index 1235ba718..d2cf34bd5 100644 --- a/query_delete.go +++ b/query_delete.go @@ -15,6 +15,8 @@ type DeleteQuery struct { whereBaseQuery orderLimitOffsetQuery returningQuery + + comment string } var _ Query = (*DeleteQuery)(nil) @@ -174,6 +176,14 @@ func (q *DeleteQuery) Returning(query string, args ...interface{}) *DeleteQuery //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *DeleteQuery) Comment(comment string) *DeleteQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *DeleteQuery) Operation() string { return "DELETE" } @@ -183,6 +193,8 @@ func (q *DeleteQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e return nil, q.err } + b = appendComment(b, q.comment) + fmter = formatterWithModel(fmter, q) if q.isSoftDelete() { diff --git a/query_index_create.go b/query_index_create.go index 11824cfa4..ad1905cc3 100644 --- a/query_index_create.go +++ b/query_index_create.go @@ -20,6 +20,7 @@ type CreateIndexQuery struct { index schema.QueryWithArgs using schema.QueryWithArgs include []schema.QueryWithArgs + comment string } var _ Query = (*CreateIndexQuery)(nil) @@ -149,6 +150,14 @@ func (q *CreateIndexQuery) WhereOr(query string, args ...interface{}) *CreateInd //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *CreateIndexQuery) Comment(comment string) *CreateIndexQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *CreateIndexQuery) Operation() string { return "CREATE INDEX" } @@ -158,6 +167,8 @@ func (q *CreateIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []by return nil, q.err } + b = appendComment(b, q.comment) + b = append(b, "CREATE "...) if q.unique { diff --git a/query_index_drop.go b/query_index_drop.go index ae28e7956..a2a23fb8a 100644 --- a/query_index_drop.go +++ b/query_index_drop.go @@ -15,7 +15,8 @@ type DropIndexQuery struct { concurrently bool ifExists bool - index schema.QueryWithArgs + index schema.QueryWithArgs + comment string } var _ Query = (*DropIndexQuery)(nil) @@ -74,6 +75,14 @@ func (q *DropIndexQuery) Index(query string, args ...interface{}) *DropIndexQuer //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *DropIndexQuery) Comment(comment string) *DropIndexQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *DropIndexQuery) Operation() string { return "DROP INDEX" } @@ -83,6 +92,8 @@ func (q *DropIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte return nil, q.err } + b = appendComment(b, q.comment) + b = append(b, "DROP INDEX "...) if q.concurrently { diff --git a/query_insert.go b/query_insert.go index 8bec4ce26..63e84545a 100644 --- a/query_insert.go +++ b/query_insert.go @@ -22,6 +22,7 @@ type InsertQuery struct { ignore bool replace bool + comment string } var _ Query = (*InsertQuery)(nil) @@ -164,6 +165,14 @@ func (q *InsertQuery) Replace() *InsertQuery { //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *InsertQuery) Comment(comment string) *InsertQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *InsertQuery) Operation() string { return "INSERT" } @@ -173,6 +182,8 @@ func (q *InsertQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e return nil, q.err } + b = appendComment(b, q.comment) + fmter = formatterWithModel(fmter, q) b, err = q.appendWith(fmter, b) diff --git a/query_merge.go b/query_merge.go index 3c3f4f7f8..7dee02002 100644 --- a/query_merge.go +++ b/query_merge.go @@ -15,9 +15,10 @@ type MergeQuery struct { baseQuery returningQuery - using schema.QueryWithArgs - on schema.QueryWithArgs - when []schema.QueryAppender + using schema.QueryWithArgs + on schema.QueryWithArgs + when []schema.QueryAppender + comment string } var _ Query = (*MergeQuery)(nil) @@ -150,6 +151,14 @@ func (q *MergeQuery) When(expr string, args ...interface{}) *MergeQuery { //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *MergeQuery) Comment(comment string) *MergeQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *MergeQuery) Operation() string { return "MERGE" } @@ -159,6 +168,8 @@ func (q *MergeQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, er return nil, q.err } + b = appendComment(b, q.comment) + fmter = formatterWithModel(fmter, q) b, err = q.appendWith(fmter, b) diff --git a/query_raw.go b/query_raw.go index 1634d0e5b..8c3a6a7f8 100644 --- a/query_raw.go +++ b/query_raw.go @@ -10,8 +10,9 @@ import ( type RawQuery struct { baseQuery - query string - args []interface{} + query string + args []interface{} + comment string } // Deprecated: Use NewRaw instead. When add it to IDB, it conflicts with the sql.Conn#Raw @@ -56,6 +57,12 @@ func (q *RawQuery) Scan(ctx context.Context, dest ...interface{}) error { return err } +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *RawQuery) Comment(comment string) *RawQuery { + q.comment = comment + return q +} + func (q *RawQuery) scanOrExec( ctx context.Context, dest []interface{}, hasDest bool, ) (sql.Result, error) { @@ -90,6 +97,8 @@ func (q *RawQuery) scanOrExec( } func (q *RawQuery) AppendQuery(fmter schema.Formatter, b []byte) ([]byte, error) { + b = appendComment(b, q.comment) + return fmter.AppendQuery(b, q.query, q.args...), nil } diff --git a/query_select.go b/query_select.go index 2b0872ae0..238ce1dd5 100644 --- a/query_select.go +++ b/query_select.go @@ -31,7 +31,8 @@ type SelectQuery struct { having []schema.QueryWithArgs selFor schema.QueryWithArgs - union []union + union []union + comment string } var _ Query = (*SelectQuery)(nil) @@ -460,11 +461,21 @@ func (q *SelectQuery) selectJoins(ctx context.Context, joins []relationJoin) err //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *SelectQuery) Comment(comment string) *SelectQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *SelectQuery) Operation() string { return "SELECT" } func (q *SelectQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) { + b = appendComment(b, q.comment) + return q.appendQuery(fmter, b, false) } diff --git a/query_table_create.go b/query_table_create.go index aeb79cd37..2c7855e7a 100644 --- a/query_table_create.go +++ b/query_table_create.go @@ -32,6 +32,7 @@ type CreateTableQuery struct { fks []schema.QueryWithArgs partitionBy schema.QueryWithArgs tablespace schema.QueryWithArgs + comment string } var _ Query = (*CreateTableQuery)(nil) @@ -129,6 +130,14 @@ func (q *CreateTableQuery) WithForeignKeys() *CreateTableQuery { return q } +//------------------------------------------------------------------------------ + +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *CreateTableQuery) Comment(comment string) *CreateTableQuery { + q.comment = comment + return q +} + // ------------------------------------------------------------------------------ func (q *CreateTableQuery) Operation() string { @@ -139,6 +148,9 @@ func (q *CreateTableQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []by if q.err != nil { return nil, q.err } + + b = appendComment(b, q.comment) + if q.table == nil { return nil, errNilModel } diff --git a/query_table_drop.go b/query_table_drop.go index a92014515..01f000293 100644 --- a/query_table_drop.go +++ b/query_table_drop.go @@ -13,6 +13,7 @@ type DropTableQuery struct { cascadeQuery ifExists bool + comment string } var _ Query = (*DropTableQuery)(nil) @@ -80,6 +81,14 @@ func (q *DropTableQuery) Restrict() *DropTableQuery { //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *DropTableQuery) Comment(comment string) *DropTableQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *DropTableQuery) Operation() string { return "DROP TABLE" } @@ -89,6 +98,8 @@ func (q *DropTableQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte return nil, q.err } + b = appendComment(b, q.comment) + b = append(b, "DROP TABLE "...) if q.ifExists { b = append(b, "IF EXISTS "...) diff --git a/query_table_truncate.go b/query_table_truncate.go index 1db81fb53..7ee5d2a8d 100644 --- a/query_table_truncate.go +++ b/query_table_truncate.go @@ -14,6 +14,7 @@ type TruncateTableQuery struct { cascadeQuery continueIdentity bool + comment string } var _ Query = (*TruncateTableQuery)(nil) @@ -81,6 +82,14 @@ func (q *TruncateTableQuery) Restrict() *TruncateTableQuery { //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *TruncateTableQuery) Comment(comment string) *TruncateTableQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *TruncateTableQuery) Operation() string { return "TRUNCATE TABLE" } @@ -92,6 +101,8 @@ func (q *TruncateTableQuery) AppendQuery( return nil, q.err } + b = appendComment(b, q.comment) + if !fmter.HasFeature(feature.TableTruncate) { b = append(b, "DELETE FROM "...) diff --git a/query_update.go b/query_update.go index bb9264084..24a90d512 100644 --- a/query_update.go +++ b/query_update.go @@ -23,6 +23,7 @@ type UpdateQuery struct { joins []joinQuery omitZero bool + comment string } var _ Query = (*UpdateQuery)(nil) @@ -243,6 +244,14 @@ func (q *UpdateQuery) Returning(query string, args ...interface{}) *UpdateQuery //------------------------------------------------------------------------------ +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *UpdateQuery) Comment(comment string) *UpdateQuery { + q.comment = comment + return q +} + +//------------------------------------------------------------------------------ + func (q *UpdateQuery) Operation() string { return "UPDATE" } @@ -252,6 +261,8 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e return nil, q.err } + b = appendComment(b, q.comment) + fmter = formatterWithModel(fmter, q) b, err = q.appendWith(fmter, b) diff --git a/query_values.go b/query_values.go index 34deb1ee4..97fbc65fa 100644 --- a/query_values.go +++ b/query_values.go @@ -14,6 +14,7 @@ type ValuesQuery struct { customValueQuery withOrder bool + comment string } var ( @@ -64,6 +65,12 @@ func (q *ValuesQuery) WithOrder() *ValuesQuery { return q } +// Comment adds a comment to the query, wrapped by /* ... */. +func (q *ValuesQuery) Comment(comment string) *ValuesQuery { + q.comment = comment + return q +} + func (q *ValuesQuery) AppendNamedArg(fmter schema.Formatter, b []byte, name string) ([]byte, bool) { switch name { case "Columns": @@ -121,6 +128,8 @@ func (q *ValuesQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e return nil, errNilModel } + b = appendComment(b, q.comment) + fmter = formatterWithModel(fmter, q) if q.tableModel != nil { diff --git a/util.go b/util.go index 09ffbb996..ff24a5463 100644 --- a/util.go +++ b/util.go @@ -1,6 +1,10 @@ package bun -import "reflect" +import ( + "fmt" + "reflect" + "strings" +) func indirect(v reflect.Value) reflect.Value { switch v.Kind() { @@ -66,3 +70,13 @@ func sliceElemType(v reflect.Value) reflect.Type { } return indirectType(elemType) } + +// appendComment adds comment in the header of the query into buffer +func appendComment(b []byte, name string) []byte { + if name == "" { + return b + } + name = strings.ReplaceAll(name, `/*`, `/\*`) + name = strings.ReplaceAll(name, `*/`, `*\/`) + return append(b, fmt.Sprintf("/* %s */ ", name)...) +} diff --git a/util_test.go b/util_test.go new file mode 100644 index 000000000..9b2069830 --- /dev/null +++ b/util_test.go @@ -0,0 +1,41 @@ +package bun + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_appendComment(t *testing.T) { + t.Run("ordinary comment", func(t *testing.T) { + var res []byte + c := "comment" + + s := appendComment(res, c) + require.Equal(t, "/* comment */ ", string(s)) + }) + + t.Run("only open sequence", func(t *testing.T) { + var res []byte + c := "/* comment" + + s := appendComment(res, c) + require.Equal(t, "/* /\\* comment */ ", string(s)) + }) + + t.Run("only close sequence", func(t *testing.T) { + var res []byte + c := "comment */" + + s := appendComment(res, c) + require.Equal(t, "/* comment *\\/ */ ", string(s)) + }) + + t.Run("open and close sequences", func(t *testing.T) { + var res []byte + c := "/* comment */" + + s := appendComment(res, c) + require.Equal(t, "/* /\\* comment *\\/ */ ", string(s)) + }) +}