dbobj

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: BSD-2-Clause Imports: 8 Imported by: 0

README

dbobj GoDoc

A lightweight ORM using sql code generated from struct tags. No fancy relations, just plain old objects.

Documentation

Overview

Package dbobj provides a simple ORM-like framework for SQLite and derivatives

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")

	// ErrNilWritePointers is returned when a list handler returns a nil slice
	ErrNilWritePointers = errors.New("nil record dest members")
)

Functions

func Placeholders added in v0.0.3

func Placeholders(n int) string

Placeholders returns SQLite values placeholders

Types

type Common

type Common interface {
	Columns() []string
	Next() bool
	Scan(...interface{}) error
}

Common Rows object between rqlite and /pkg/database/sql

type DBList

type DBList interface {
	QueryString(extra string) string
	Receivers() []interface{}
}

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

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

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

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

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

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

	// Key returns the int64 id value of the object
	Key() int64

	// SetID updates the id of the object
	SetID(int64)

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

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

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

	// ModifiedBy returns the user id and timestamp of when the object was last modified
	ModifiedBy(int64, time.Time)
}

DBObject provides methods for object storage The functions are generated for each object annotated accordingly

type DBS

type DBS interface {
	// Query takes a SetHandler, which returns a slice of pointers to the members of a struct
	// It is incumbant upon the caller to build a slice and pass receivers to build a list
	Query(fn SetHandler, query string, args ...interface{}) error

	// Exec models the standard Golang exec but with direct values returned
	Exec(query string, args ...interface{}) (RowsAffected, LastInsertID int64, err error)
}

DBS is an abstracted database interface, intended to work with both rqlite and regular sql.DB connections

type DBU

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

DBU is a DataBaseUnit

func NewDBU

func NewDBU(file string, init bool, opener SQLDB) (*DBU, error)

NewDBU returns a new DBU

func (*DBU) Add

func (du *DBU) Add(o DBObject) error

Add new object to datastore

func (*DBU) Close added in v0.0.3

func (du *DBU) Close()

Close shuts down the database

func (*DBU) DB added in v0.0.3

func (du *DBU) DB() *sql.DB

DB returns the *sql.DB

func (*DBU) Delete

func (du *DBU) Delete(o DBObject) error

Delete object from datastore

func (*DBU) DeleteByID

func (du *DBU) DeleteByID(o DBObject, id interface{}) error

DeleteByID object from datastore by id

func (*DBU) Exec added in v0.0.4

func (du *DBU) Exec(query string, args ...interface{}) (rowsAffected, lastInsertID int64, err error)

func (*DBU) Find

func (du *DBU) Find(o DBObject, keys map[string]interface{}) error

Find loads an object matching the given keys

func (*DBU) FindBy

func (du *DBU) FindBy(o DBObject, key string, value interface{}) error

FindBy loads an object matching the given key/value

func (*DBU) FindByID

func (du *DBU) FindByID(o DBObject, value interface{}) error

FindByID loads an object based on a given ID

func (*DBU) FindSelf

func (du *DBU) FindSelf(o DBObject) error

FindSelf loads an object based on it's current ID

func (*DBU) InsertMany added in v0.0.2

func (du *DBU) InsertMany(query string, args ...[]interface{}) error

InsertMany inserts multiple records as a single transaction

func (*DBU) List

func (du *DBU) List(list DBList) error

List objects from datastore

func (*DBU) ListQuery

func (du *DBU) ListQuery(list DBList, extra string) error

ListQuery updates a list of objects TODO: handle args/vs no args for rqlite

func (*DBU) MakeList added in v0.0.7

func (du *DBU) MakeList(h ListHandler, query string, args ...interface{}) error

MakeList is an alternative list creation interface

func (*DBU) Query added in v0.0.5

func (du *DBU) Query(fn SetHandler, query string, args ...interface{}) error

Query satisfies DBS interface

func (*DBU) Replace

func (du *DBU) Replace(o DBObject) error

Replace will replace an existing object in datastore

func (*DBU) Save

func (du *DBU) Save(o DBObject) error

Save modified object in datastore

func (*DBU) SetLogger

func (du *DBU) SetLogger(logger *log.Logger)

SetLogger sets the logger for the db

type ListHandler added in v0.0.7

type ListHandler interface {
	Receivers() []interface{}
	Ready()
}

type SQLDB

type SQLDB func(string) (*sql.DB, error)

SQLDB is a common interface for opening an sql db

type SetHandler

type SetHandler func() []interface{}

SetHandler returns a slice of value pointer interfaces If there are no values to set it returns a nil instead

Directories

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

Jump to

Keyboard shortcuts

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