mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql: use raw NULL value in statements rather than passing nil arguments
This commit is contained in:
committed by
Ariel Mashraki
parent
5c2013b5fd
commit
94b19ac973
@@ -3266,6 +3266,9 @@ type (
|
||||
// Arg appends an input argument to the builder.
|
||||
func (b *Builder) Arg(a interface{}) *Builder {
|
||||
switch a := a.(type) {
|
||||
case nil:
|
||||
b.WriteString("NULL")
|
||||
return b
|
||||
case *raw:
|
||||
b.WriteString(a.s)
|
||||
return b
|
||||
@@ -3278,7 +3281,7 @@ func (b *Builder) Arg(a interface{}) *Builder {
|
||||
// Default placeholder param (MySQL and SQLite).
|
||||
param := "?"
|
||||
if b.postgres() {
|
||||
// PostgreSQL arguments are referenced using the syntax $n.
|
||||
// Postgres' arguments are referenced using the syntax $n.
|
||||
// $1 refers to the 1st argument, $2 to the 2nd, and so on.
|
||||
param = "$" + strconv.Itoa(b.total)
|
||||
}
|
||||
|
||||
@@ -1950,8 +1950,8 @@ func TestInsert_OnConflict(t *testing.T) {
|
||||
|
||||
query, args = Dialect(dialect.MySQL).
|
||||
Insert("users").
|
||||
Columns("name").
|
||||
Values("Mashraki").
|
||||
Columns("name", "rank").
|
||||
Values("Mashraki", nil).
|
||||
OnConflict(
|
||||
ResolveWithNewValues(),
|
||||
ResolveWith(func(s *UpdateSet) {
|
||||
@@ -1959,8 +1959,23 @@ func TestInsert_OnConflict(t *testing.T) {
|
||||
}),
|
||||
).
|
||||
Query()
|
||||
require.Equal(t, "INSERT INTO `users` (`name`) VALUES (?) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `id` = LAST_INSERT_ID(`id`)", query)
|
||||
require.Equal(t, "INSERT INTO `users` (`name`, `rank`) VALUES (?, NULL) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `rank` = VALUES(`rank`), `id` = LAST_INSERT_ID(`id`)", query)
|
||||
require.Equal(t, []interface{}{"Mashraki"}, args)
|
||||
|
||||
query, args = Dialect(dialect.MySQL).
|
||||
Insert("users").
|
||||
Columns("name", "rank").
|
||||
Values("Ariel", 10).
|
||||
Values("Mashraki", nil).
|
||||
OnConflict(
|
||||
ResolveWithNewValues(),
|
||||
ResolveWith(func(s *UpdateSet) {
|
||||
s.Set("id", Expr("LAST_INSERT_ID(`id`)"))
|
||||
}),
|
||||
).
|
||||
Query()
|
||||
require.Equal(t, "INSERT INTO `users` (`name`, `rank`) VALUES (?, ?), (?, NULL) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `rank` = VALUES(`rank`), `id` = LAST_INSERT_ID(`id`)", query)
|
||||
require.Equal(t, []interface{}{"Ariel", 10, "Mashraki"}, args)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1366,8 +1366,8 @@ func TestBatchCreate(t *testing.T) {
|
||||
},
|
||||
expect: func(m sqlmock.Sqlmock) {
|
||||
// Insert nodes with FKs.
|
||||
m.ExpectExec(escape("INSERT INTO `users` (`active`, `age`, `best_friend_id`, `name`, `workplace_id`) VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)")).
|
||||
WithArgs(false, 32, 3, "a8m", 2, nil, 30, 4, "nati", 2).
|
||||
m.ExpectExec(escape("INSERT INTO `users` (`active`, `age`, `best_friend_id`, `name`, `workplace_id`) VALUES (?, ?, ?, ?, ?), (NULL, ?, ?, ?, ?)")).
|
||||
WithArgs(false, 32, 3, "a8m", 2, 30, 4, "nati", 2).
|
||||
WillReturnResult(sqlmock.NewResult(10, 2))
|
||||
},
|
||||
},
|
||||
@@ -1450,8 +1450,8 @@ func TestBatchCreate(t *testing.T) {
|
||||
expect: func(m sqlmock.Sqlmock) {
|
||||
m.ExpectBegin()
|
||||
// Insert nodes with FKs.
|
||||
m.ExpectExec(escape("INSERT INTO `users` (`active`, `age`, `name`, `workplace_id`) VALUES (?, ?, ?, ?), (?, ?, ?, ?)")).
|
||||
WithArgs(false, 32, "a8m", 2, nil, 30, "nati", nil).
|
||||
m.ExpectExec(escape("INSERT INTO `users` (`active`, `age`, `name`, `workplace_id`) VALUES (?, ?, ?, ?), (NULL, ?, ?, NULL)")).
|
||||
WithArgs(false, 32, "a8m", 2, 30, "nati").
|
||||
WillReturnResult(sqlmock.NewResult(10, 2))
|
||||
// Insert M2M inverse-edges.
|
||||
m.ExpectExec(escape("INSERT INTO `group_users` (`group_id`, `user_id`) VALUES (?, ?), (?, ?)")).
|
||||
|
||||
Reference in New Issue
Block a user