Files
grafana/pkg/util/sqlite/sqlite_cgo.go
Serge Zaitsev 6b1143565a Chore: Make cgo optional (for sqlite) (#108756)
* make cgo optional for sqlite

* update go.mod; check error code differently

* reduce api surface even more

* move test errors into sqlite package

* add a comment
2025-07-31 09:25:19 +00:00

47 lines
1.2 KiB
Go

//go:build cgo
package sqlite
import (
"errors"
"github.com/mattn/go-sqlite3"
)
type Driver = sqlite3.SQLiteDriver
// The errors below are used in tests to simulate specific SQLite errors. It's a temporary solution
// until we rewrite the tests not to depend on the sqlite3 package internals directly.
var (
TestErrUniqueConstraintViolation = sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintUnique}
TestErrBusy = sqlite3.Error{Code: sqlite3.ErrBusy}
TestErrLocked = sqlite3.Error{Code: sqlite3.ErrLocked}
)
func IsBusyOrLocked(err error) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Code == sqlite3.ErrLocked || sqliteErr.Code == sqlite3.ErrBusy
}
return false
}
func IsUniqueConstraintViolation(err error) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique || sqliteErr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey
}
return false
}
func ErrorMessage(err error) string {
if err == nil {
return ""
}
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Error()
}
return err.Error()
}