embedded

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: Apache-2.0 Imports: 22 Imported by: 2

README

Dolt Database Driver

This package provides a database/sql compatible driver for embedding Dolt inside a Go application. It allows you to access local Dolt databases via the file system, akin to SQLite, without running a Dolt server process.

For details of the database/sql package see this tutorial. Below I will cover things that are specific to using dolt with this database driver.

Ways to Set up a Dolt Database

Create a directory to house one or more dolt databases. Once this is created we'll create a directory for our first database using the dolt cli

mkdir dbs
mkdir dbs/testdb
cd dbs/testdb
dolt init

Alternatively you could clone a database from dolthub or a different remote:

mkdir dbs
cd dbs
dolt clone <REMOTE URL>

Finally, you can create the dbs directory as shown above and then create the database in code using a SQL CREATE TABLE statement

Connecting to the Database

First we'll import the dolt driver so that it will be registered

_ "github.com/dolthub/driver"

Then we will open a connection to the database:

db, err := sql.Open("dolt", "file:///path/to/dbs?commitname=Your%20Name&commitemail=your@email.com&database=databasename")

Now you can use your db as you would normally, however you have access to all of dolt's special features as well.

Dolt Data Source Names

The Dolt driver requires a DSN containing the directory where your databases live, and the name and email that are used in the commit log.

commitname - The name of the committer seen in the dolt commit log
commitemail - The email of the committer seen in the dolt commit log
database - The initial database to connect to
multistatements - If set to true, allows multiple statements in one query
clientfoundrows - If set to true, returns the number of matching rows instead of the number of changed rows in UPDATE queries
Example DSN

file:///path/to/dbs?commitname=Your%20Name&commitemail=your@email.com&database=databasename

Multi-Statement Support

If you pass the multistatements=true parameter in the DSN, you can execute multiple statements in one query. The returned rows allow you to iterate over the returned result sets by using the NextResultSet method, just like you can with the MySQL driver.

rows, err := db.Query("SELECT * from someTable; SELECT * from anotherTable;")
// If an error is returned, it means it came from the first statement
if err != nil {
	panic(err)
}

for rows.Next() {
	// process the first result set
}

if rows.NextResultSet() {
    for rows.Next() {
        // process the second result set
    }
} else {
	// If NextResultSet returns false when there were more statements, it means there was an error,
	// which you can access through rows.Err()
	panic(rows.Err())
}

Documentation

Index

Constants

View Source
const (
	DoltDriverName = "dolt"

	CommitNameParam      = "commitname"
	CommitEmailParam     = "commitemail"
	DatabaseParam        = "database"
	MultiStatementsParam = "multistatements"
	ClientFoundRowsParam = "clientfoundrows"
)

Variables

This section is empty.

Functions

func LoadMultiEnvFromDir

func LoadMultiEnvFromDir(
	ctx context.Context,
	cfg config.ReadWriteConfig,
	fs filesys.Filesys,
	path, version string,
) (*env.MultiRepoEnv, error)

LoadMultiEnvFromDir looks at each subfolder of the given path as a Dolt repository and attempts to return a MultiRepoEnv with initialized environments for each of those subfolder data repositories. subfolders whose name starts with '.' are skipped.

Types

type DoltConn

type DoltConn struct {
	DataSource *DoltDataSource
	// contains filtered or unexported fields
}

DoltConn is a driver.Conn implementation that represents a connection to a dolt database located on the filesystem

func (*DoltConn) Begin deprecated

func (d *DoltConn) Begin() (driver.Tx, error)

Begin starts and returns a new transaction.

Deprecated: Use BeginTx instead

func (*DoltConn) BeginTx

func (d *DoltConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx starts and returns a new transaction. If the context is canceled by the user the sql package will call Tx.Rollback before discarding and closing the connection.

func (*DoltConn) Close

func (d *DoltConn) Close() error

Close releases the resources held by the DoltConn instance

func (*DoltConn) Prepare

func (d *DoltConn) Prepare(query string) (driver.Stmt, error)

Prepare packages up |query| as a *doltStmt so it can be executed. If multistatements mode has been enabled, then a *doltMultiStmt will be returned, capable of executing multiple statements.

type DoltDataSource

type DoltDataSource struct {
	Directory string
	Params    map[string][]string
}

DoltDataSource provides access to the data provided by the connection string

func ParseDataSource

func ParseDataSource(dataSource string) (*DoltDataSource, error)

ParseDataSource takes the connection string and parses out the parameters and the local filesys directory where the dolt database lives

func (*DoltDataSource) ParamIsTrue

func (ds *DoltDataSource) ParamIsTrue(paramName string) bool

type QuerySplitter

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

func NewQuerySplitter

func NewQuerySplitter(str string) *QuerySplitter

func (*QuerySplitter) HasMore

func (qs *QuerySplitter) HasMore() bool

func (*QuerySplitter) Next

func (qs *QuerySplitter) Next() (string, error)

type RuneStack

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

RuneStack is a simple stack of runes

func NewByteStack

func NewByteStack() *RuneStack

NewByteStack returns a new RuneStack object

func (*RuneStack) Peek

func (bs *RuneStack) Peek() rune

Peek returns the value at the top of the stack

func (*RuneStack) Pop

func (bs *RuneStack) Pop() rune

Pop takes the top value of the top of the stack and returns it

func (*RuneStack) Push

func (bs *RuneStack) Push(b rune)

Push pushes a new rune on the stack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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