-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_classifier_test.go
More file actions
44 lines (40 loc) · 1.6 KB
/
Copy pathsqlite_classifier_test.go
File metadata and controls
44 lines (40 loc) · 1.6 KB
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
package dalgo2sql
import (
"errors"
"modernc.org/sqlite"
)
// sqliteConstraintPrimaryKey and sqliteConstraintUnique are the extended
// SQLite result codes modernc.org/sqlite reports for a primary-key or
// unique-index constraint violation (see modernc.org/sqlite/lib:
// SQLITE_CONSTRAINT_PRIMARYKEY = 1555, SQLITE_CONSTRAINT_UNIQUE = 2067).
// They are hardcoded here rather than importing the generated
// modernc.org/sqlite/lib package for two integers.
const (
sqliteConstraintPrimaryKey = 1555
sqliteConstraintUnique = 2067
)
// sqliteIsAlreadyExists is a DbOptions.IsAlreadyExists classifier for
// modernc.org/sqlite, the driver dalgo2sql's own conformance test (see
// conformance_test.go) runs against.
//
// dalgo2sql carries no runtime (non-test) dependency on any driver's error
// type — detecting a duplicate-key violation is inherently driver-specific,
// which is exactly why DbOptions.IsAlreadyExists exists as a hook a wrapping
// adapter fills in. This function is that hook's minimal implementation for
// modernc.org/sqlite, kept test-only so the shared dalgotest conformance
// suite's unconditional "rejects an Insert over an existing key with
// record.IsAlreadyExists" check can be satisfied here, in the base package,
// without weakening or skipping that check. A production wrapping adapter
// (e.g. dalgo2sqlite) supplies its own equivalent.
func sqliteIsAlreadyExists(err error) bool {
var sqliteErr *sqlite.Error
if !errors.As(err, &sqliteErr) {
return false
}
switch sqliteErr.Code() {
case sqliteConstraintPrimaryKey, sqliteConstraintUnique:
return true
default:
return false
}
}