db

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2017 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package db exposes the database interface to rscs. This package can be used in conjunction with the rscs daemon or integrated directly into a client program.

Example
rscsDB, newErr := NewRscsDB("file::memory:?mode=memory&cache=shared")
if newErr != nil {
	log.Fatalf("fail on tmpfile new:%s", newErr.Error())
}
createErr := rscsDB.CreateTable()
if createErr != nil {
	log.Fatalf("fail on create table:%s", createErr.Error())
}
dupcreateErr := rscsDB.CreateTable()
if dupcreateErr == nil {
	log.Fatalf("fail on duplicate create table:%s", dupcreateErr.Error())
}
dropErr := rscsDB.DropTable()
if dropErr != nil {
	log.Fatalf("fail on drop table:%s", dropErr.Error())
}
recreateErr := rscsDB.CreateTable()
if recreateErr != nil {
	log.Fatalf("fail on recreate table:%s", recreateErr.Error())
}

var rowCount int
var insertErr, updateErr, deleteErr error

testKey := "my-test-key"
testValue := "my-test-value"

rowCount, insertErr = rscsDB.Insert(testKey, testValue)
if insertErr != nil {
	log.Fatalf("insert fail:%s", insertErr.Error())
}
if rowCount != 1 {
	log.Fatalf("insert rowcount:%d", rowCount)
}

value, found, getErr := rscsDB.Get(testKey)
if getErr != nil {
	log.Fatalf("get fail:%s", getErr.Error())
}
if !found {
	log.Fatalf("%s not found", testKey)
}
if value != testValue {
	log.Fatalf("%s and %s not equal", value, testValue)
}

rowCount, updateErr = rscsDB.Update(testKey, "my-new-val")
if updateErr != nil {
	log.Fatalf("update fail:%s", updateErr.Error())
}
if rowCount != 1 {
	log.Fatalf("update rowcount:%d", rowCount)
}

rowCount, deleteErr = rscsDB.Delete(testKey)
if deleteErr != nil {
	log.Fatalf("delete fail:%s", deleteErr.Error())
}
if rowCount != 1 {
	log.Fatalf("delete rowcount:%d", rowCount)
}
Output:

Index

Examples

Constants

View Source
const (
	// KVTableName is the KV table name.
	KVTableName = "kv"
	// KVPrimaryKeyColumn is the KV primary key.
	KVPrimaryKeyColumn = "key"
	// KVValueColumn is the KV value.
	KVValueColumn = "value"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type RscsDB

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

RscsDB contains the state values for communicating with the underlying sqlite file.

func NewRscsDB

func NewRscsDB(sqliteDBFile string) (*RscsDB, error)

NewRscsDB initializes a new RscsDB instance.

func (*RscsDB) CreateTable

func (r *RscsDB) CreateTable() error

CreateTable will create a new kv table. You will need to DROP independently if needed.

func (*RscsDB) DBFileName

func (r *RscsDB) DBFileName() string

DBFileName returns the db used.

func (*RscsDB) Delete

func (r *RscsDB) Delete(key string) (int, error)

Delete will delete a row with ID key.

func (*RscsDB) DropTable

func (r *RscsDB) DropTable() error

DropTable will drop the kv table.

func (*RscsDB) Get

func (r *RscsDB) Get(key string) (string, bool, error)

Get returns the value string for the key string. The second return value is a 'found' flag that easily distinguishes a db error case from that of no matching row.

func (*RscsDB) Insert

func (r *RscsDB) Insert(key, value string) (int, error)

Insert will insert a new key/value pair.

func (*RscsDB) Update

func (r *RscsDB) Update(key, value string) (int, error)

Update will give a row a new value.

Jump to

Keyboard shortcuts

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