Documentation
¶
Overview ¶
Package genji implements a document-oriented, embedded SQL database. Genji supports various engines that write data on-disk, like BoltDB or Badger, and in memory.
Example ¶
package main
import (
"fmt"
"github.com/tie/genji-release-test"
"github.com/tie/genji-release-test/document"
)
type User struct {
ID int64
Name string
Age uint32
Address struct {
City string
ZipCode string
}
}
func main() {
// Create a database instance, here we'll store everything in memory
db, err := genji.Open(":memory:")
if err != nil {
panic(err)
}
defer db.Close()
// Create a table. Genji tables are schemaless by default, you don't need to specify a schema.
err = db.Exec("CREATE TABLE user")
if err != nil {
panic(err)
}
// Create an index.
err = db.Exec("CREATE INDEX idx_user_name ON user (name)")
if err != nil {
panic(err)
}
// Insert some data
err = db.Exec("INSERT INTO user (id, name, age) VALUES (?, ?, ?)", 10, "foo", 15)
if err != nil {
panic(err)
}
// Insert some data using document notation
err = db.Exec(`INSERT INTO user VALUES {id: 12, "name": "bar", age: ?, address: {city: "Lyon", zipcode: "69001"}}`, 16)
if err != nil {
panic(err)
}
// Structs can be used to describe a document
err = db.Exec("INSERT INTO user VALUES ?, ?", &User{ID: 1, Name: "baz", Age: 100}, &User{ID: 2, Name: "bat"})
if err != nil {
panic(err)
}
// Query some documents
stream, err := db.Query("SELECT * FROM user WHERE id > ?", 1)
if err != nil {
panic(err)
}
// always close the result when you're done with it
defer stream.Close()
// Iterate over the results
err = stream.Iterate(func(d document.Document) error {
var u User
err = document.StructScan(d, &u)
if err != nil {
return err
}
fmt.Println(u)
return nil
})
if err != nil {
panic(err)
}
}
Output: {10 foo 15 { }} {12 bar 16 {Lyon 69001}} {2 bat 0 { }}
Index ¶
- type DB
- func (db *DB) Begin(writable bool) (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) Exec(q string, args ...interface{}) error
- func (db *DB) Query(q string, args ...interface{}) (*query.Result, error)
- func (db *DB) QueryDocument(q string, args ...interface{}) (document.Document, error)
- func (db *DB) Update(fn func(tx *Tx) error) error
- func (db *DB) View(fn func(tx *Tx) error) error
- func (db *DB) WithContext(ctx context.Context) *DB
- type Tx
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
DB represents a collection of tables stored in the underlying engine.
func Open ¶
Open creates a Genji database at the given path. If path is equal to ":memory:" it will open an in-memory database, otherwise it will create an on-disk database using the BoltDB engine.
func (*DB) Begin ¶
Begin starts a new transaction. The returned transaction must be closed either by calling Rollback or Commit.
func (*DB) Query ¶
Query the database and return the result. The returned result must always be closed after usage.
func (*DB) QueryDocument ¶
QueryDocument runs the query and returns the first document. If the query returns no error, QueryDocument returns database.ErrDocumentNotFound.
type Tx ¶
type Tx struct {
*database.Transaction
}
Tx represents a database transaction. It provides methods for managing the collection of tables and the transaction itself. Tx is either read-only or read/write. Read-only can be used to read tables and read/write can be used to read, create, delete and modify tables.
Example ¶
db, err := genji.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
panic(err)
}
defer tx.Rollback()
err = tx.Exec("CREATE TABLE IF NOT EXISTS user")
if err != nil {
log.Fatal(err)
}
err = tx.Exec("INSERT INTO user (id, name, age) VALUES (?, ?, ?)", 10, "foo", 15)
if err != nil {
log.Fatal(err)
}
d, err := tx.QueryDocument("SELECT id, name, age FROM user WHERE name = ?", "foo")
if err != nil {
panic(err)
}
var u User
err = document.StructScan(d, &u)
if err != nil {
panic(err)
}
fmt.Println(u)
var id uint64
var name string
var age uint8
err = document.Scan(d, &id, &name, &age)
if err != nil {
panic(err)
}
fmt.Println(id, name, age)
err = tx.Commit()
if err != nil {
panic(err)
}
Output: {10 foo 15 { }} 10 foo 15
Directories
¶
| Path | Synopsis |
|---|---|
|
Package binarysort provides types and functions to encode into naturally sorted binary representations.
|
Package binarysort provides types and functions to encode into naturally sorted binary representations. |
|
cmd
|
|
|
genji
module
|
|
|
Package database provides database primitives such as tables, transactions and indexes.
|
Package database provides database primitives such as tables, transactions and indexes. |
|
dev
|
|
|
gensqltest
command
|
|
|
Package document defines types to manipulate and compare documents and values.
|
Package document defines types to manipulate and compare documents and values. |
|
encoding
Package encoding defines types that deal with document encoding.
|
Package encoding defines types that deal with document encoding. |
|
encoding/encodingtest
Package encodingtest provides a test suite for testing codec implementations.
|
Package encodingtest provides a test suite for testing codec implementations. |
|
Package engine defines interfaces to be implemented by engines in order to be compatible with Genji.
|
Package engine defines interfaces to be implemented by engines in order to be compatible with Genji. |
|
boltengine
Package boltengine implements a BoltDB engine.
|
Package boltengine implements a BoltDB engine. |
|
enginetest
Package enginetest defines a list of tests that can be used to test a complete or partial engine implementation.
|
Package enginetest defines a list of tests that can be used to test a complete or partial engine implementation. |
|
badgerengine
module
|
|
|
glob
Package glob implements wildcard pattern matching algorithms for strings.
|
Package glob implements wildcard pattern matching algorithms for strings. |
|
sql
|
|