memdb

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 6 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 Create

func Create(name string, data []byte)

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 Delete

func Delete(name string)

Delete deletes a shared memory database.

Types

This section is empty.

Jump to

Keyboard shortcuts

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