grafana/pkg/util/sqlite/sqlite_cgo.go
Serge Zaitsev 174e924e15
Chore: Disable cgo by default for local builds (#111807)
* disable cgo by default for local builds, also set cgo variable in either case

* actually do not set the default value

* disable cgo for darwin, display sqlite driver in logs

* fix linter warning, although I do not fully agree with it
2025-09-30 23:06:40 +02: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 {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Error()
}
return err.Error()
}
func DriverType() string {
return "mattn/go-sqlite3 (CGO enabled)"
}