rqlobj

package module
v0.0.0-...-13e7ea3 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: BSD-2-Clause Imports: 10 Imported by: 0

README

rqlobj GoDocBuild Status

An Object Relational Manager (ORM) for rqlite

Because rqlite does not provide the standard *sql.DB connection, it will not work with existing ORMs, which all wrap around that interface.

This project allows for using struct tags to annotate your structs that need persistence. The associated dbgen command is used to generate data handlers for these struts to enable CRUD and list operations against objects without writing any SQL.

The goals of this project are to provide object mapping, and it does not include data migrations.

Documentation

Overview

Package rqlobj provides basic object relational mapping against an sql(ite) database

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoKeyField is returned for tables without primary key identified
	ErrNoKeyField = errors.New("table has no key field")

	// ErrKeyMissing is returned when key value is not set
	ErrKeyMissing = errors.New("key is not set")

	// ErrNotFound is returned when query returns no rows
	ErrNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type DBList

type DBList interface {
	// SQLGet is the query string to retrieve the list
	SQLGet(extra string) string

	// SQLResults takes a Scan function
	SQLResults(func(...interface{}) error) error
}

DBList is the interface for a list of db objects

type DBObject

type DBObject interface {
	// TableName is the name of the sql table
	TableName() string

	// Primary returns the primary key and a bool,
	// set true, if the key is valid
	Primary() (int64, bool)

	// SetPrimary updates the primary key
	SetPrimary(int64)

	// KeyNames are the struct names of the
	// primary id fields
	KeyNames() []string

	// KeyValues returns the value(s) of the object key(s)
	KeyValues() []interface{}

	// KeyFields are the names of the table fields
	// comprising the primary id
	KeyFields() []string

	// Elements returns the struct element names
	Elements() []string

	// SelectFields returns the comma separated
	// list of fields to be selected
	SelectFields() string

	// InsertFields returns the comma separated
	// list of fields to be inserted
	InsertFields() string

	// InsertValues returns the values of the object to be inserted
	InsertValues() []interface{}

	// UpdateValues returns the values of the object to be updated
	UpdateValues() []interface{}

	// Receivers  returns a slice of pointers to values
	// for the db Scan function
	Receivers() []interface{}

	// SQLCreate returns the string to create a table
	// representing the struct
	SQLCreate() string
}

DBObject interface provides methods for object storage in an sql database. The functions are generated for each relevant struct type that are annotated accordingly TODO: consider namespacing these functions to avoid collision

type RDB

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

RDB is a database handler that works with DBOject variables

func NewRqlite

func NewRqlite(host string, logger, trace io.Writer) (RDB, error)

NewRqlite returns a RDB connected to a rqlite cluster

func (RDB) Add

func (db RDB) Add(o DBObject) error

Add new object to datastore

func (RDB) Debug

func (db RDB) Debug(enable bool)

Debug sets database debugging on/off

func (RDB) Delete

func (db RDB) Delete(o DBObject) error

Delete object from datastore

func (RDB) DeleteAll

func (db RDB) DeleteAll(o DBObject) error

DeleteAll deletes all objects of that type from the datastore

func (RDB) DeleteByID

func (db RDB) DeleteByID(o DBObject, id int64) error

DeleteByID object from datastore by id

func (RDB) List

func (db RDB) List(list DBList) error

List objects from datastore

func (RDB) ListQuery

func (db RDB) ListQuery(list DBList, where string) error

ListQuery updates a list of objects

func (RDB) Load

func (db RDB) Load(o DBObject, keys map[string]interface{}) error

Load loads an object matching the given keys

func (RDB) LoadBy

func (db RDB) LoadBy(o DBObject, key string, value interface{}) error

LoadBy loads an object matching the given key/value

func (RDB) LoadByID

func (db RDB) LoadByID(o DBObject, id int64) error

LoadByID loads an object based on a given int64 primary ID

func (RDB) LoadSelf

func (db RDB) LoadSelf(o DBObject) error

LoadSelf loads an object based on it's current ID

func (RDB) SetLogger

func (db RDB) SetLogger(w io.Writer)

SetLogger sets the logger for the db

func (RDB) Update

func (db RDB) Update(o DBObject) error

Update saves a modified object in the datastore

func (RDB) Write

func (db RDB) Write(queries ...string) ([]gorqlite.WriteResult, error)

Write will process a batch of queries and return a batch of results

Directories

Path Synopsis
dbgen is a tool to automate the creation of CRUD methods (create/update/delete) that satisfy the DBObject interface in github.com/paulstuart/rqlobj.
dbgen is a tool to automate the creation of CRUD methods (create/update/delete) that satisfy the DBObject interface in github.com/paulstuart/rqlobj.

Jump to

Keyboard shortcuts

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