Documentation
¶
Overview ¶
Package commitdb provides a Git-backed SQL database engine.
CommitDB stores data using Git as the underlying storage mechanism, making every transaction a Git commit. This provides built-in version control, history tracking, and the ability to restore to any point in time.
Quick Start ¶
Create an in-memory database:
p, _ := persistence.NewMemoryPersistence()
db := commitdb.Open(&p)
e := db.Engine(core.Identity{Name: "App", Email: "[email protected]"})
e.Execute("CREATE DATABASE mydb")
e.Execute("CREATE TABLE mydb.users (id INT PRIMARY KEY, name STRING)")
e.Execute("INSERT INTO mydb.users (id, name) VALUES (1, 'Alice')")
result, _ := e.Execute("SELECT * FROM mydb.users")
result.Display()
Supported SQL ¶
CommitDB supports a subset of SQL including:
- CREATE/DROP DATABASE
- CREATE/DROP TABLE
- CREATE/DROP INDEX
- INSERT, SELECT, UPDATE, DELETE
- WHERE with comparison operators
- ORDER BY, LIMIT, OFFSET
- Aggregate functions: SUM, AVG, MIN, MAX, COUNT
- GROUP BY, HAVING
- JOINs: INNER, LEFT, RIGHT
- DISTINCT
- Transactions: BEGIN, COMMIT, ROLLBACK
Example ¶
package main
import (
"fmt"
commitdb "github.com/nickyhof/CommitDB/v2"
"github.com/nickyhof/CommitDB/v2/core"
"github.com/nickyhof/CommitDB/v2/engine"
"github.com/nickyhof/CommitDB/v2/persistence"
)
func main() {
// Create an in-memory database
p, _ := persistence.NewMemoryPersistence()
instance := commitdb.Open(&p)
// Create an engine with identity (used for Git commit author)
e := instance.Engine(core.Identity{
Name: "Example",
Email: "[email protected]",
})
// Create a database and table
e.Execute("CREATE DATABASE myapp")
e.Execute("CREATE TABLE myapp.users (id INT PRIMARY KEY, name STRING)")
// Insert data
e.Execute("INSERT INTO myapp.users (id, name) VALUES (1, 'Alice')")
e.Execute("INSERT INTO myapp.users (id, name) VALUES (2, 'Bob')")
// Query data
result, _ := e.Execute("SELECT name FROM myapp.users ORDER BY id")
// Access result data
qr := result.(engine.QueryResult)
for _, row := range qr.Data {
fmt.Println(row[0])
}
}
Output: Alice Bob
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Instance ¶
type Instance struct {
Persistence *persistence.Persistence
}
func Open ¶
func Open(p *persistence.Persistence) *Instance
func (*Instance) Engine ¶
Example ¶
package main
import (
"fmt"
commitdb "github.com/nickyhof/CommitDB/v2"
"github.com/nickyhof/CommitDB/v2/core"
"github.com/nickyhof/CommitDB/v2/engine"
"github.com/nickyhof/CommitDB/v2/persistence"
)
func main() {
p, _ := persistence.NewMemoryPersistence()
instance := commitdb.Open(&p)
// Each engine has its own identity for Git commits
alice := instance.Engine(core.Identity{Name: "Alice", Email: "[email protected]"})
bob := instance.Engine(core.Identity{Name: "Bob", Email: "[email protected]"})
alice.Execute("CREATE DATABASE demo")
alice.Execute("CREATE TABLE demo.messages (id INT PRIMARY KEY, text STRING)")
alice.Execute("INSERT INTO demo.messages (id, text) VALUES (1, 'Hello from Alice')")
bob.Execute("INSERT INTO demo.messages (id, text) VALUES (2, 'Hello from Bob')")
result, _ := alice.Execute("SELECT text FROM demo.messages ORDER BY id")
qr := result.(engine.QueryResult)
for _, row := range qr.Data {
fmt.Println(row[0])
}
}
Output: Hello from Alice Hello from Bob
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
commitdb
command
|
|
|
Package core provides core types used throughout CommitDB.
|
Package core provides core types used throughout CommitDB. |
|
Package engine provides the SQL execution engine for CommitDB.
|
Package engine provides the SQL execution engine for CommitDB. |
|
internal
|
|
|
compare
Package compare provides shared value comparison utilities for query evaluation and indexing.
|
Package compare provides shared value comparison utilities for query evaluation and indexing. |
|
ops
Package ops provides high-level operations for working with CommitDB databases and tables.
|
Package ops provides high-level operations for working with CommitDB databases and tables. |
|
sql
Package sql provides SQL lexing and parsing for CommitDB.
|
Package sql provides SQL lexing and parsing for CommitDB. |
|
Package persistence provides the persistence layer for CommitDB.
|
Package persistence provides the persistence layer for CommitDB. |
Click to show internal directories.
Click to hide internal directories.