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 ¶
- Constants
- type RscsDB
- func (r *RscsDB) CreateTable() error
- func (r *RscsDB) DBFileName() string
- func (r *RscsDB) Delete(key string) (int, error)
- func (r *RscsDB) DropTable() error
- func (r *RscsDB) Get(key string) (string, bool, error)
- func (r *RscsDB) Insert(key, value string) (int, error)
- func (r *RscsDB) Update(key, value string) (int, error)
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 (*RscsDB) CreateTable ¶
CreateTable will create a new kv table. You will need to DROP independently if needed.
func (*RscsDB) Get ¶
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.
Click to show internal directories.
Click to hide internal directories.