Skip to content

Commit 55e0038

Browse files
committed
Replace plan.ErrInsertIntoDuplicateColumn with sql.ErrColumnSpecifiedTwice, and add cast to MySqlError.
1 parent 7969b07 commit 55e0038

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

server/handler_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ func TestHandlerErrors(t *testing.T) {
261261
query: "INSERT INTO `test`.`no_such_table` (`id`, `v`) VALUES (1, 2)",
262262
expectedErrorCode: mysql.ERNoSuchTable,
263263
},
264+
{
265+
name: "insert into same column twice",
266+
handler: handler,
267+
conn: dummyConn,
268+
setup: []string{"CREATE TABLE `test_table` ( `id` INT NOT NULL PRIMARY KEY, `v` INT );"},
269+
query: "INSERT INTO `test`.`test_table` (`id`, `id`, `v`) VALUES (1, 2, 3)",
270+
expectedErrorCode: mysql.ERFieldSpecifiedTwice,
271+
},
264272
}
265273

266274
for _, test := range tests {

sql/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ var (
885885
ErrInsertIntoMismatchValueCount = errors.NewKind("number of values does not match number of columns provided")
886886

887887
ErrInvalidTypeForLimit = errors.NewKind("invalid limit. expected %T, found %T")
888+
889+
ErrColumnSpecifiedTwice = errors.NewKind("column '%v' specified twice")
888890
)
889891

890892
// CastSQLError returns a *mysql.SQLError with the error code and in some cases, also a SQL state, populated for the
@@ -953,6 +955,8 @@ func CastSQLError(err error) *mysql.SQLError {
953955
code = mysql.ERTruncatedWrongValueForField
954956
case ErrUnknownColumn.Is(err):
955957
code = mysql.ERBadFieldError
958+
case ErrColumnSpecifiedTwice.Is(err):
959+
code = mysql.ERFieldSpecifiedTwice
956960
case ErrLockDeadlock.Is(err):
957961
// ER_LOCK_DEADLOCK signals that the transaction was rolled back
958962
// due to a deadlock between concurrent transactions.

sql/plan/insert.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ var ErrReplaceIntoNotSupported = errors.NewKind("table doesn't support REPLACE I
3030
var ErrOnDuplicateKeyUpdateNotSupported = errors.NewKind("table doesn't support ON DUPLICATE KEY UPDATE")
3131
var ErrAutoIncrementNotSupported = errors.NewKind("table doesn't support AUTO_INCREMENT")
3232
var ErrInsertIntoUnsupportedValues = errors.NewKind("%T is unsupported for inserts")
33-
var ErrInsertIntoDuplicateColumn = errors.NewKind("duplicate column name %v")
3433
var ErrInsertIntoIncompatibleTypes = errors.NewKind("cannot convert type %s to %s")
3534

3635
// cc: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict

sql/planbuilder/dml_validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func validateColumns(tableName string, columnNames []string, dstSchema sql.Schem
111111
if _, exists := usedNames[columnName]; !exists {
112112
usedNames[columnName] = struct{}{}
113113
} else {
114-
return plan.ErrInsertIntoDuplicateColumn.New(columnName)
114+
return sql.ErrColumnSpecifiedTwice.New(columnName)
115115
}
116116
}
117117
return nil

0 commit comments

Comments
 (0)