-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
executable file
·490 lines (361 loc) · 25.6 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"fmt"
"github.com/dolthub/vitess/go/mysql"
"gopkg.in/src-d/go-errors.v1"
)
var (
// ErrSyntaxError is returned when a syntax error in vitess is encountered.
ErrSyntaxError = errors.NewKind("%s")
// ErrUnsupportedFeature is thrown when a feature is not already supported
ErrUnsupportedFeature = errors.NewKind("unsupported feature: %s")
// ErrInvalidSystemVariableValue is returned when a system variable is assigned a value that it does not accept.
ErrInvalidSystemVariableValue = errors.NewKind("Variable '%s' can't be set to the value of '%v'")
// ErrSystemVariableCodeFail is returned when failing to encode/decode a system variable.
ErrSystemVariableCodeFail = errors.NewKind("unable to encode/decode value '%v' for '%s'")
// ErrInvalidType is thrown when there is an unexpected type at some part of
// the execution tree.
ErrInvalidType = errors.NewKind("invalid type: %s")
// ErrTableAlreadyExists is thrown when someone tries to create a
// table with a name of an existing one
ErrTableAlreadyExists = errors.NewKind("table with name %s already exists")
// ErrTableNotFound is returned when the table is not available from the
// current scope.
ErrTableNotFound = errors.NewKind("table not found: %s")
// ErrColumnNotFound is thrown when a column named cannot be found in scope
ErrTableColumnNotFound = errors.NewKind("table %q does not have column %q")
// ErrColumnNotFound is returned when the column does not exist in any
// table in scope.
ErrColumnNotFound = errors.NewKind("column %q could not be found in any table in scope")
// ErrAmbiguousColumnName is returned when there is a column reference that
// is present in more than one table.
ErrAmbiguousColumnName = errors.NewKind("ambiguous column name %q, it's present in all these tables: %v")
// ErrAmbiguousColumnInOrderBy is returned when an order by column is ambiguous
ErrAmbiguousColumnInOrderBy = errors.NewKind("Column %q in order clause is ambiguous")
// ErrColumnExists is returned when an ALTER TABLE statement would create a duplicate column
ErrColumnExists = errors.NewKind("Column %q already exists")
// ErrUnexpectedRowLength is thrown when the obtained row has more columns than the schema
ErrUnexpectedRowLength = errors.NewKind("expected %d values, got %d")
// ErrInvalidChildrenNumber is returned when the WithChildren method of a
// node or expression is called with an invalid number of arguments.
ErrInvalidChildrenNumber = errors.NewKind("%T: invalid children number, got %d, expected %d")
// ErrInvalidChildType is returned when the WithChildren method of a
// node or expression is called with an invalid child type. This error is indicative of a bug.
ErrInvalidChildType = errors.NewKind("%T: invalid child type, got %T, expected %T")
// ErrInvalidJSONText is returned when a JSON string cannot be parsed or unmarshalled
ErrInvalidJSONText = errors.NewKind("Invalid JSON text: %s")
// ErrDeleteRowNotFound
ErrDeleteRowNotFound = errors.NewKind("row was not found when attempting to delete")
// ErrDuplicateAlias should be returned when a query contains a duplicate alias / table name.
ErrDuplicateAliasOrTable = errors.NewKind("Not unique table/alias: %s")
// ErrPrimaryKeyViolation is returned when a primary key constraint is violated
// This is meant to wrap a sql.UniqueKey error, which provides the key string
ErrPrimaryKeyViolation = errors.NewKind("duplicate primary key given")
// ErrUniqueKeyViolation is returned when a unique key constraint is violated
// This is meant to wrap a sql.UniqueKey error, which provides the key string
ErrUniqueKeyViolation = errors.NewKind("duplicate unique key given")
// ErrMisusedAlias is returned when a alias is defined and used in the same projection.
ErrMisusedAlias = errors.NewKind("column %q does not exist in scope, but there is an alias defined in" +
" this projection with that name. Aliases cannot be used in the same projection they're defined in")
// ErrInvalidAsOfExpression is returned when an expression for AS OF cannot be used
ErrInvalidAsOfExpression = errors.NewKind("expression %s cannot be used in AS OF")
// ErrIncompatibleDefaultType is returned when a provided default cannot be coerced into the type of the column
ErrIncompatibleDefaultType = errors.NewKind("incompatible type for default value")
// ErrInvalidTextBlobColumnDefault is returned when a column of type text/blob (or related) has a literal default set.
ErrInvalidTextBlobColumnDefault = errors.NewKind("text/blob types may only have expression default values")
// ErrInvalidColumnDefaultFunction is returned when an invalid function is used in a default value.
ErrInvalidColumnDefaultFunction = errors.NewKind("function `%s` on column `%s` is not valid for usage in a default value")
// ErrColumnDefaultDatetimeOnlyFunc is returned when a non datetime/timestamp column attempts to declare now/current_timestamp as a default value literal.
ErrColumnDefaultDatetimeOnlyFunc = errors.NewKind("only datetime/timestamp may declare default values of now()/current_timestamp() without surrounding parentheses")
// ErrColumnDefaultSubquery is returned when a default value contains a subquery.
ErrColumnDefaultSubquery = errors.NewKind("default value on column `%s` may not contain subqueries")
// ErrInvalidDefaultValueOrder is returned when a default value references a column that comes after it and contains a default expression.
ErrInvalidDefaultValueOrder = errors.NewKind(`default value of column "%s" cannot refer to a column defined after it if those columns have an expression default value`)
// ErrColumnDefaultReturnedNull is returned when a default expression evaluates to nil but the column is non-nullable.
ErrColumnDefaultReturnedNull = errors.NewKind(`default value attempted to return null but column is non-nullable`)
// ErrDropColumnReferencedInDefault is returned when a column cannot be dropped as it is referenced by another column's default value.
ErrDropColumnReferencedInDefault = errors.NewKind(`cannot drop column "%s" as default value of column "%s" references it`)
// ErrTriggersNotSupported is returned when attempting to create a trigger on a database that doesn't support them
ErrTriggersNotSupported = errors.NewKind(`database "%s" doesn't support triggers`)
// ErrTriggerCreateStatementInvalid is returned when a TriggerDatabase returns a CREATE TRIGGER statement that is invalid
ErrTriggerCreateStatementInvalid = errors.NewKind(`Invalid CREATE TRIGGER statement: %s`)
// ErrTriggerDoesNotExist is returned when a trigger does not exist.
ErrTriggerDoesNotExist = errors.NewKind(`trigger "%s" does not exist`)
// ErrTriggerTableInUse is returned when trigger execution calls for a table that invoked a trigger being updated by it
ErrTriggerTableInUse = errors.NewKind("Can't update table %s in stored function/trigger because it is already used by statement which invoked this stored function/trigger")
// ErrTriggerCannotBeDropped is returned when dropping a trigger would cause another trigger to reference a non-existent trigger.
ErrTriggerCannotBeDropped = errors.NewKind(`trigger "%s" cannot be dropped as it is referenced by trigger "%s"`)
// ErrStoredProceduresNotSupported is returned when attempting to create a stored procedure on a database that doesn't support them.
ErrStoredProceduresNotSupported = errors.NewKind(`database "%s" doesn't support stored procedures`)
// ErrTriggerDoesNotExist is returned when a stored procedure does not exist.
ErrStoredProcedureAlreadyExists = errors.NewKind(`stored procedure "%s" already exists`)
// ErrTriggerDoesNotExist is returned when a stored procedure does not exist.
ErrStoredProcedureDoesNotExist = errors.NewKind(`stored procedure "%s" does not exist`)
// ErrProcedureCreateStatementInvalid is returned when a StoredProcedureDatabase returns a CREATE PROCEDURE statement that is invalid.
ErrProcedureCreateStatementInvalid = errors.NewKind(`Invalid CREATE PROCEDURE statement: %s`)
// ErrProcedureDuplicateParameterName is returned when a stored procedure has two (or more) parameters with the same name.
ErrProcedureDuplicateParameterName = errors.NewKind("duplicate parameter name `%s` on stored procedure `%s`")
// ErrProcedureRecursiveCall is returned when a stored procedure has a CALL statement that refers to itself.
ErrProcedureRecursiveCall = errors.NewKind("recursive CALL on stored procedure `%s`")
// ErrProcedureInvalidBodyStatement is returned when a stored procedure has a statement that is invalid inside of procedures.
ErrProcedureInvalidBodyStatement = errors.NewKind("`%s` statements are invalid inside of stored procedures")
// ErrCallIncorrectParameterCount is returned when a CALL statement has the incorrect number of parameters.
ErrCallIncorrectParameterCount = errors.NewKind("`%s` expected `%d` parameters but got `%d`")
// ErrUnknownSystemVariable is returned when a query references a system variable that doesn't exist
ErrUnknownSystemVariable = errors.NewKind(`Unknown system variable '%s'`)
// ErrSystemVariableReadOnly is returned when attempting to set a value to a non-Dynamic system variable.
ErrSystemVariableReadOnly = errors.NewKind(`Variable '%s' is a read only variable`)
// ErrSystemVariableSessionOnly is returned when attempting to set a SESSION-only variable using SET GLOBAL.
ErrSystemVariableSessionOnly = errors.NewKind(`Variable '%s' is a SESSION variable and can't be used with SET GLOBAL`)
// ErrSystemVariableGlobalOnly is returned when attempting to set a GLOBAL-only variable using SET SESSION.
ErrSystemVariableGlobalOnly = errors.NewKind(`Variable '%s' is a GLOBAL variable and should be set with SET GLOBAL`)
// ErrUserVariableNoDefault is returned when attempting to set the default value on a user variable.
ErrUserVariableNoDefault = errors.NewKind(`User variable '%s' does not have a default value`)
// ErrInvalidUseOfOldNew is returned when a trigger attempts to make use of OLD or NEW references when they don't exist
ErrInvalidUseOfOldNew = errors.NewKind("There is no %s row in on %s trigger")
// ErrInvalidUpdateOfOldRow is returned when a trigger attempts to assign to an old row's value with SET
ErrInvalidUpdateOfOldRow = errors.NewKind("Updating of old row is not allowed in trigger")
// ErrInvalidUpdateInAfterTrigger is returned when a trigger attempts to assign to a new row in an AFTER trigger
ErrInvalidUpdateInAfterTrigger = errors.NewKind("Updating of new row is not allowed in after trigger")
// ErrUnboundPreparedStatementVariable is returned when a query is executed without a binding for one its variables.
ErrUnboundPreparedStatementVariable = errors.NewKind(`unbound variable "%s" in query`)
// ErrTruncateReferencedFromForeignKey is returned when a table is referenced in a foreign key and TRUNCATE is called on it.
ErrTruncateReferencedFromForeignKey = errors.NewKind("cannot truncate table %s as it is referenced in foreign key %s on table %s")
// ErrInvalidColTypeDefinition is returned when a column type-definition has argument violations.
ErrInvalidColTypeDefinition = errors.NewKind("column %s type definition is invalid: %s")
// ErrDatabaseExists is returned when CREATE DATABASE attempts to create a database that already exists.
ErrDatabaseExists = errors.NewKind("can't create database %s; database exists")
// ErrInvalidConstraintFunctionsNotSupported is returned when a CONSTRAINT CHECK is called with a sub-function expression.
ErrInvalidConstraintFunctionsNotSupported = errors.NewKind("Invalid constraint expression, functions not supported: %s")
// ErrInvalidConstraintSubqueryNotSupported is returned when a CONSTRAINT CHECK is called with a sub-query expression.
ErrInvalidConstraintSubqueryNotSupported = errors.NewKind("Invalid constraint expression, sub-queries not supported: %s")
ErrCheckConstraintViolatedFmtStr = "Check constraint %q violated"
// ErrCheckConstraintViolated is returned when a CONSTRAINT CHECK is called with a sub-query expression.
ErrCheckConstraintViolated = errors.NewKind(ErrCheckConstraintViolatedFmtStr)
// ErrColumnCountMismatch is returned when a view, derived table or common table expression has a declared column
// list with a different number of columns than the schema of the table.
ErrColumnCountMismatch = errors.NewKind("In definition of view, derived table or common table expression, SELECT list and column names list have different column counts")
// ErrUuidUnableToParse is returned when a UUID is unable to be parsed.
ErrUuidUnableToParse = errors.NewKind("unable to parse '%s' to UUID: %s")
// ErrLoadDataCannotOpen is returned when a LOAD DATA operation is unable to open the file specified.
ErrLoadDataCannotOpen = errors.NewKind("LOAD DATA is unable to open file: %s")
// ErrLoadDataCharacterLength is returned when a symbol is of the wrong character length for a LOAD DATA operation.
ErrLoadDataCharacterLength = errors.NewKind("%s must be 1 character long")
// ErrJSONObjectAggNullKey is returned when JSON_OBJECTAGG is run on a table with NULL keys
ErrJSONObjectAggNullKey = errors.NewKind("JSON documents may not contain NULL member names")
// ErrDeclareOrderInvalid is returned when a DECLARE statement is at an invalid location.
ErrDeclareOrderInvalid = errors.NewKind("DECLARE may only exist at the beginning of a BEGIN/END block")
// ErrDeclareConditionNotFound is returned when SIGNAL/RESIGNAL references a non-existent DECLARE CONDITION.
ErrDeclareConditionNotFound = errors.NewKind("condition %s does not exist")
// ErrDeclareConditionDuplicate is returned when a DECLARE CONDITION statement with the same name was declared in the current scope.
ErrDeclareConditionDuplicate = errors.NewKind("duplicate condition '%s'")
// ErrSignalOnlySqlState is returned when SIGNAL/RESIGNAL references a DECLARE CONDITION for a MySQL error code.
ErrSignalOnlySqlState = errors.NewKind("SIGNAL/RESIGNAL can only use a condition defined with SQLSTATE")
// ErrExpectedSingleRow is returned when a subquery executed in normal queries or aggregation function returns
// more than 1 row without an attached IN clause.
ErrExpectedSingleRow = errors.NewKind("the subquery returned more than 1 row")
// ErrUnknownConstraint is returned when a DROP CONSTRAINT statement refers to a constraint that doesn't exist
ErrUnknownConstraint = errors.NewKind("Constraint %q does not exist")
// ErrInsertIntoNonNullableDefaultNullColumn is returned when an INSERT excludes a field which is non-nullable and has no default/autoincrement.
ErrInsertIntoNonNullableDefaultNullColumn = errors.NewKind("Field '%s' doesn't have a default value")
// ErrAlterTableNotSupported is thrown when the table doesn't support ALTER TABLE statements
ErrAlterTableNotSupported = errors.NewKind("table %s cannot be altered")
// ErrPartitionNotFound is thrown when a partition key on a table is not found
ErrPartitionNotFound = errors.NewKind("partition not found %q")
// ErrInsertIntoNonNullableProvidedNull is called when a null value is inserted into a non-nullable column
ErrInsertIntoNonNullableProvidedNull = errors.NewKind("column name '%v' is non-nullable but attempted to set a value of null")
// ErrForeignKeyChildViolation is called when a rows is added but there is no parent row, and a foreign key constraint fails. Add the parent row first.
ErrForeignKeyChildViolation = errors.NewKind("cannot add or update a child row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
// ErrForeignKeyParentViolation is called when a parent row that is deleted has children, and a foreign key constraint fails. Delete the children first.
ErrForeignKeyParentViolation = errors.NewKind("cannot delete or update a parent row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
// ErrForeignKeyColumnCountMismatch is called when the declared column and referenced column counts do not match.
ErrForeignKeyColumnCountMismatch = errors.NewKind("the foreign key must reference an equivalent number of columns")
// ErrForeignKeyNotResolved is called when an add or update is attempted on a foreign key that has not been resolved yet.
ErrForeignKeyNotResolved = errors.NewKind("cannot add or update a child row: a foreign key constraint fails (`%s`, CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`))")
// ErrDuplicateEntry is returns when a duplicate entry is placed on an index such as a UNIQUE or a Primary Key.
ErrDuplicateEntry = errors.NewKind("Duplicate entry for key '%s'")
// ErrInvalidArgument is returned when an argument to a function is invalid.
ErrInvalidArgument = errors.NewKind("Incorrect arguments to %s")
// ErrSavepointDoesNotExist is returned when a RELEASE SAVEPOINT or ROLLBACK TO SAVEPOINT statement references a
// non-existent savepoint identifier
ErrSavepointDoesNotExist = errors.NewKind("SAVEPOINT %s does not exist")
// ErrTableCreatedNotFound is thrown when an integrator attempts to create a temporary tables without temporary table
// support.
ErrTemporaryTableNotSupported = errors.NewKind("database does not support temporary tables")
// ErrInvalidSyntax is returned for syntax errors that aren't picked up by the parser, e.g. the wrong type of
// expression used in part of statement.
ErrInvalidSyntax = errors.NewKind("Invalid syntax: %s")
// ErrTableCopyingNotSupported is returned when a table invokes the TableCopierDatabase interface's
// CopyTableData method without supporting the interface
ErrTableCopyingNotSupported = errors.NewKind("error: Table copying not supported")
// ErrMultiplePrimaryKeysDefined is returned when a table invokes CreatePrimaryKey with a primary key already
// defined.
ErrMultiplePrimaryKeysDefined = errors.NewKind("error: Multiple primary keys defined")
// ErrWrongAutoKey is returned when a table invokes DropPrimaryKey without first removing the auto increment property
// (if it exists) on it.
ErrWrongAutoKey = errors.NewKind("error: incorrect table definition: there can be only one auto column and it must be defined as a key")
// ErrKeyColumnDoesNotExist is returned when a table invoked CreatePrimaryKey with a non-existent column.
ErrKeyColumnDoesNotExist = errors.NewKind("error: key column '%s' doesn't exist in table")
// ErrCantDropFieldOrKey is returned when a table invokes DropPrimaryKey on a keyless table.
ErrCantDropFieldOrKey = errors.NewKind("error: can't drop '%s'; check that column/key exists")
// ErrCantDropIndex is return when a table can't drop an index due to a foreign key relationship.
ErrCantDropIndex = errors.NewKind("error: can't drop index '%s': needed in a foreign key constraint")
// ErrImmutableDatabaseProvider is returned when attempting to edit an immutable database databaseProvider.
ErrImmutableDatabaseProvider = errors.NewKind("error: can't modify database databaseProvider")
// ErrInvalidValue is returned when a given value does not match what is expected.
ErrInvalidValue = errors.NewKind(`error: '%v' is not a valid value for '%v'`)
// ErrInvalidValueType is returned when a given value's type does not match what is expected.
ErrInvalidValueType = errors.NewKind(`error: '%T' is not a valid value type for '%v'`)
// ErrFunctionNotFound is thrown when a function is not found
ErrFunctionNotFound = errors.NewKind("function: '%s' not found")
// ErrInvalidArgumentNumber is returned when the number of arguments to call a
// function is different from the function arity.
ErrInvalidArgumentNumber = errors.NewKind("function '%s' expected %v arguments, %v received")
// ErrDatabaseNotFound is thrown when a database is not found
ErrDatabaseNotFound = errors.NewKind("database not found: %s")
// ErrNoDatabaseSelected is thrown when a database is not selected and the query requires one
ErrNoDatabaseSelected = errors.NewKind("no database selected")
// ErrAsOfNotSupported is thrown when an AS OF query is run on a database that can't support it
ErrAsOfNotSupported = errors.NewKind("AS OF not supported for database %s")
// ErrIncompatibleAsOf is thrown when an AS OF clause is used in an incompatible manner, such as when using an AS OF
// expression with a view when the view definition has its own AS OF expressions.
ErrIncompatibleAsOf = errors.NewKind("incompatible use of AS OF: %s")
// ErrPidAlreadyUsed is returned when the pid is already registered.
ErrPidAlreadyUsed = errors.NewKind("pid %d is already in use")
// ErrInvalidOperandColumns is returned when the columns in the left
// operand and the elements of the right operand don't match. Also
// returned for invalid number of columns in projections, filters,
// joins, etc.
ErrInvalidOperandColumns = errors.NewKind("operand should have %d columns, but has %d")
// ErrReadOnlyTransaction is returned when a write query is executed in a READ ONLY transaction.
ErrReadOnlyTransaction = errors.NewKind("cannot execute statement in a READ ONLY transaction")
// ErrExistingView is returned when a CREATE VIEW statement uses a name that already exists
ErrExistingView = errors.NewKind("the view %s.%s already exists")
// ErrViewDoesNotExist is returned when a DROP VIEW statement drops a view that does not exist
ErrViewDoesNotExist = errors.NewKind("the view %s.%s does not exist")
// ErrSessionDoesNotSupportPersistence is thrown when a feature is not already supported
ErrSessionDoesNotSupportPersistence = errors.NewKind("session does not support persistence")
// ErrUnsupportedSyntax is returned when syntax that parses correctly is not supported
ErrUnsupportedSyntax = errors.NewKind("unsupported syntax: %s")
// ErrInvalidSQLValType is returned when a SQL value is of the incorrect type during parsing
ErrInvalidSQLValType = errors.NewKind("invalid SQLVal of type: %d")
// ErrInvalidIndexPrefix is returned when an index prefix is outside the accepted range
ErrInvalidIndexPrefix = errors.NewKind("invalid index prefix: %v")
// ErrUnknownIndexColumn is returned when a column in an index is not in the table
ErrUnknownIndexColumn = errors.NewKind("unknown column: '%s' in index '%s'")
// ErrInvalidAutoIncCols is returned when an auto_increment column cannot be applied
ErrInvalidAutoIncCols = errors.NewKind("there can be only one auto_increment column and it must be defined as a key")
// ErrUnknownConstraintDefinition is returned when an unknown constraint type is used
ErrUnknownConstraintDefinition = errors.NewKind("unknown constraint definition: %s, %T")
// ErrInvalidCheckConstraint is returned when a check constraint is defined incorrectly
ErrInvalidCheckConstraint = errors.NewKind("invalid constraint definition: %s")
)
func CastSQLError(err error) (*mysql.SQLError, error, bool) {
if err == nil {
return nil, nil, true
}
if mysqlErr, ok := err.(*mysql.SQLError); ok {
return mysqlErr, nil, false
}
var code int
var sqlState string = ""
if w, ok := err.(WrappedInsertError); ok {
return CastSQLError(w.Cause)
}
switch {
case ErrTableNotFound.Is(err):
code = mysql.ERNoSuchTable
case ErrDatabaseExists.Is(err):
code = mysql.ERDbCreateExists
case ErrExpectedSingleRow.Is(err):
code = mysql.ERSubqueryNo1Row
case ErrInvalidOperandColumns.Is(err):
code = mysql.EROperandColumns
case ErrInsertIntoNonNullableProvidedNull.Is(err):
code = mysql.ERBadNullError
case ErrPrimaryKeyViolation.Is(err):
code = mysql.ERDupEntry
case ErrUniqueKeyViolation.Is(err):
code = mysql.ERDupEntry
case ErrPartitionNotFound.Is(err):
code = 1526 // TODO: Needs to be added to vitess
case ErrForeignKeyChildViolation.Is(err):
code = mysql.ErNoReferencedRow2 // test with mysql returns 1452 vs 1216
case ErrForeignKeyParentViolation.Is(err):
code = mysql.ERRowIsReferenced2 // test with mysql returns 1451 vs 1215
case ErrDuplicateEntry.Is(err):
code = mysql.ERDupEntry
case ErrInvalidJSONText.Is(err):
code = 3141 // TODO: Needs to be added to vitess
case ErrMultiplePrimaryKeysDefined.Is(err):
code = mysql.ERMultiplePriKey
case ErrWrongAutoKey.Is(err):
code = mysql.ERWrongAutoKey
case ErrKeyColumnDoesNotExist.Is(err):
code = mysql.ERKeyColumnDoesNotExist
case ErrCantDropFieldOrKey.Is(err):
code = mysql.ERCantDropFieldOrKey
case ErrReadOnlyTransaction.Is(err):
code = 1792 // TODO: Needs to be added to vitess
case ErrCantDropIndex.Is(err):
code = 1553 // TODO: Needs to be added to vitess
default:
code = mysql.ERUnknownError
}
return mysql.NewSQLError(code, sqlState, err.Error()), err, false // return the original error as well
}
type UniqueKeyError struct {
keyStr string
IsPK bool
Existing Row
}
func NewUniqueKeyErr(keyStr string, isPK bool, existing Row) error {
ue := UniqueKeyError{
keyStr: keyStr,
IsPK: isPK,
Existing: existing,
}
if isPK {
return ErrPrimaryKeyViolation.Wrap(ue)
} else {
return ErrUniqueKeyViolation.Wrap(ue)
}
}
func (ue UniqueKeyError) Error() string {
return fmt.Sprintf("%s", ue.keyStr)
}
type WrappedInsertError struct {
OffendingRow Row
Cause error
}
func NewWrappedInsertError(r Row, err error) WrappedInsertError {
return WrappedInsertError{
OffendingRow: r,
Cause: err,
}
}
func (w WrappedInsertError) Error() string {
return w.Cause.Error()
}
type ErrInsertIgnore struct {
OffendingRow Row
}
func NewErrInsertIgnore(row Row) ErrInsertIgnore {
return ErrInsertIgnore{OffendingRow: row}
}
func (e ErrInsertIgnore) Error() string {
return "Insert ignore error shoudl never be printed"
}