memdb

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 10 Imported by: 0

README

Go memdb SQLite VFS

This package implements the "memdb" SQLite VFS in pure Go.

It has some benefits over the C version:

  • the memory backing the database needs not be contiguous,
  • the database can grow/shrink incrementally without copying,
  • reader-writer concurrency is slightly improved.

Documentation

Overview

Package memdb implements the "memdb" SQLite VFS.

The "memdb" vfs.VFS allows the same in-memory database to be shared among multiple database connections in the same process, as long as the database name begins with "/".

Importing package memdb registers the VFS:

import _ "github.com/ncruces/go-sqlite3/vfs/memdb"
Example
package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "embed"

	_ "github.com/ncruces/go-sqlite3/driver"
	_ "github.com/ncruces/go-sqlite3/embed"
	"github.com/ncruces/go-sqlite3/vfs/memdb"
)

//go:embed testdata/test.db
var testDB []byte

func main() {
	memdb.Create("test.db", testDB)

	db, err := sql.Open("sqlite3", "file:/test.db?vfs=memdb")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	_, err = db.Exec(`INSERT INTO users (id, name) VALUES (3, 'rust')`)
	if err != nil {
		log.Fatal(err)
	}

	rows, err := db.Query(`SELECT id, name FROM users`)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	for rows.Next() {
		var id, name string
		err = rows.Scan(&id, &name)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s %s\n", id, name)
	}
}
Output:

0 go
1 zig
2 whatever
3 rust

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(name string)

Delete deletes a shared memory database.

func TestDB

func TestDB(tb testing.TB, params ...url.Values) string

TestDB creates an empty shared memory database for the test to use. The database is automatically deleted when the test and all its subtests complete. Each subsequent call to TestDB returns a unique database.

Types

type MemDB

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

MemDB is a no-export memDB struct dump helper. Note: This struct does not provide concurrency safety, and you must manage concurrent access yourself.

func Create

func Create(name string, data []byte) *MemDB

Create creates a shared memory database, using data as its initial contents. The new database takes ownership of data, and the caller should not use data after this call.

func (*MemDB) Dump added in v1.0.2

func (m *MemDB) Dump() io.ReadSeekCloser

type MemDBDumper added in v1.0.2

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

func (*MemDBDumper) Close added in v1.0.2

func (m *MemDBDumper) Close() error

func (*MemDBDumper) Read added in v1.0.2

func (m *MemDBDumper) Read(p []byte) (n int, err error)

func (*MemDBDumper) Seek added in v1.0.2

func (m *MemDBDumper) Seek(offset int64, whence int) (int64, error)

Jump to

Keyboard shortcuts

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