sqlite

package
v0.8.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 13 Imported by: 0

README

SQLite

This is a no CGO sqlite driver for Goent ORM based on https://pkg.go.dev/modernc.org/sqlite.

Features

  • 🪝 Connection Hook
  • 🧪 In Memory Database

Usage

Basic
package main

import (
	"github.com/azhai/goent"
	"github.com/azhai/goent/drivers/sqlite"
)

type User struct {
	ID    int
	Name  string
	Email string `goe:"unique"`
}

type Database struct {
	User *User
	*goent.DB
}

func main() {
	logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
	cfg := sqlite.NewConfig(sqlite.Config{
		Logger: logger, IncludeArguments: true,
	})
	db, err := goent.Open[Database](sqlite.Open("goent.db", cfg))
}
Connection Hook
package main

import (
	"context"
	"os"
	"path/filepath"

	"github.com/azhai/goent"
	"github.com/azhai/goent/drivers/sqlite"
)

type User struct {
	ID    int
	Name  string
	Email string `goe:"unique"`
}

type Database struct {
	User *User
	*goent.DB
}

func main() {
	db, err := goent.Open[Database](sqlite.Open(filepath.Join(os.TempDir(), "goent.db"), sqlite.NewConfig(
		sqlite.Config{
			ConnectionHook: func(conn sqlite.ExecQuerierContext, dsn string) error {
                initSql := "PRAGMA foreign_keys = ON;"
                _, _ = conn.ExecContext(context.Background(), initSql, nil)
                return nil
			},
		},
	)))
}
In Memory Database
package main

import (
	"github.com/azhai/goent"
	"github.com/azhai/goent/drivers/sqlite"
)

type User struct {
	ID    int
	Name  string
	Email string `goe:"unique"`
}

type Database struct {
	User *User
	*goent.DB
}

func main() {
	cfg := sqlite.NewConfig(sqlite.Config{})
	db, err := goent.Open[Database](sqlite.OpenInMemory(cfg))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConfig

func NewConfig(c Config) config

Types

type Config

type Config struct {
	Logger           model.Logger
	IncludeArguments bool          // include all arguments used on query
	QueryThreshold   time.Duration // query threshold to warning on slow queries

	MigratePath    string         // output sql file, if defined the driver will not auto apply the migration.
	ConnectionHook ConnectionHook // ConnectionHook is called after each connection is opened.
}

Config contains SQLite driver configuration options.

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection represents a SQLite database connection.

func (Connection) ExecContext

func (c Connection) ExecContext(ctx context.Context, query *model.Query) error

func (Connection) QueryContext

func (c Connection) QueryContext(ctx context.Context, query *model.Query) (model.Rows, error)

func (Connection) QueryRowContext

func (c Connection) QueryRowContext(ctx context.Context, query *model.Query) model.Row

type ConnectionHook

type ConnectionHook func(
	conn ExecQuerierContext,
	dsn string,
) error

ConnectionHook is a callback function called after each connection is opened.

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver implements the SQLite database driver.

func Open

func Open(dsn string, c config) (driver *Driver)

Open opens a sqlite connection. By default uses "PRAGMA foreign_keys = ON;" and "PRAGMA busy_timeout = 5000;".

func OpenDSN

func OpenDSN(dsn string) (driver *Driver)

func OpenInMemory

func OpenInMemory(c config) (driver *Driver)

OpenInMemory opens a in memory database.

func (*Driver) AddLogger

func (dr *Driver) AddLogger(logger model.Logger, err error) error

func (*Driver) Close

func (dr *Driver) Close() error

func (*Driver) DropColumn

func (dr *Driver) DropColumn(schema, table, column string) error

func (*Driver) DropTable

func (dr *Driver) DropTable(schema, table string) error

func (*Driver) ErrorTranslator

func (dr *Driver) ErrorTranslator() func(err error) error

func (*Driver) FormatTableName

func (dr *Driver) FormatTableName(schema, table string) string

func (*Driver) GetDatabaseConfig

func (dr *Driver) GetDatabaseConfig() *model.DatabaseConfig

func (*Driver) Init

func (dr *Driver) Init() error

func (*Driver) KeywordHandler

func (dr *Driver) KeywordHandler(s string) string

func (*Driver) MigrateContext

func (dr *Driver) MigrateContext(ctx context.Context, migrator *model.Migrator) error

func (*Driver) Name

func (dr *Driver) Name() string

func (*Driver) NewConnection

func (dr *Driver) NewConnection() model.Connection

func (*Driver) NewTransaction

func (dr *Driver) NewTransaction(ctx context.Context, opts *sql.TxOptions) (model.Transaction, error)

func (*Driver) RenameColumn

func (dr *Driver) RenameColumn(schema, table, oldColumn, newColumn string) error

func (*Driver) RenameTable

func (dr *Driver) RenameTable(schema, table, newTable string) error

func (*Driver) Stats

func (dr *Driver) Stats() sql.DBStats

func (*Driver) SupportsReturning

func (dr *Driver) SupportsReturning() bool

type ExecQuerierContext

type ExecQuerierContext interface {
	driver.ExecerContext
	driver.QueryerContext
}

ExecQuerierContext combines ExecerContext and QueryerContext interfaces.

type SavePoint

type SavePoint struct {
	// contains filtered or unexported fields
}

SavePoint represents a transaction savepoint for partial rollbacks.

func (SavePoint) Commit

func (s SavePoint) Commit() error

func (SavePoint) Rollback

func (s SavePoint) Rollback() error

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction represents a SQLite database transaction.

func (Transaction) Commit

func (t Transaction) Commit() error

func (Transaction) ExecContext

func (t Transaction) ExecContext(ctx context.Context, query *model.Query) error

func (Transaction) QueryContext

func (t Transaction) QueryContext(ctx context.Context, query *model.Query) (model.Rows, error)

func (Transaction) QueryRowContext

func (t Transaction) QueryRowContext(ctx context.Context, query *model.Query) model.Row

func (Transaction) Rollback

func (t Transaction) Rollback() error

func (Transaction) SavePoint

func (t Transaction) SavePoint() (model.SavePoint, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL