glebarez / sqlite

The pure-Go SQLite driver for GORM
MIT License
592 stars 39 forks source link
driver golang gorm gorm-driver gorm-orm sqlite sqlite3

badge badge
Hits

Pure-Go SQLite driver for GORM

Pure-go (without cgo) implementation of SQLite driver for GORM

This driver has SQLite embedded, you don't need to install one separately.

Usage

import (
  "github.com/glebarez/sqlite"
  "gorm.io/gorm"
)

db, err := gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{})

In-memory DB example

db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})

Foreign-key constraint activation

Foreign-key constraint is disabled by default in SQLite. To activate it, use connection URL parameter:

db, err := gorm.Open(sqlite.Open(":memory:?_pragma=foreign_keys(1)"), &gorm.Config{})

More info: https://www.sqlite.org/foreignkeys.html

FAQ

How is this better than standard GORM SQLite driver?

The standard GORM driver for SQLite has one major drawback: it is based on a Go-bindings of SQLite C-source (this is called cgo). This fact imposes following restrictions on Go developers:

Instead, this driver is based on pure-Go implementation of SQLite (https://gitlab.com/cznic/sqlite), which is basically an original SQLite C-source AST, translated into Go! So, you may be sure you're using the original SQLite implementation under the hood.

Is this tested good ?

Yes, The CI pipeline of this driver employs whole test base of GORM, which includes more than 12k tests (see badge on the page-top). Testing is run against latest major releases of Go:

In following environments:

Is it fast?

Well, it's slower than CGo implementation, but not terribly. See the bechmark of underlying pure-Go driver vs CGo implementation.

Included features