elephant

package module
v0.0.0-...-81f2ef4 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 11 Imported by: 1

README

Elephants never forget

Schema

The Elephant library will work only with structures which meet the following criteria:

  • It has a parameter of type string which has the tag db:"key" and is unique (will be used as primary key)
  • The structure can be marshalled to JSON
  • The struct name meets the following regular expression: [0-9A-Za-z_]{1,40}
  • All attributes to be saved must be public (first letter of the variable name must be uppercase)

This library will store every instance inside a table with the name of the structure. Each table will have two columns: the id (string) column and the value, which will be a JSON with, at most, 64 Kilobytes (defined by MaxStructLength)

Supported URIs, right now, follow this criteria:

  • sqlite3:path/to/file.db (if the file doesn't exist, it will be created)

Compiling and testing

go get -u .
go test -cover
go mod tidy

sqlite3 /tmp/foo.db
.tables
select * from structCheck;

Example usage

err := Initialize("sqlite3://example.db")
if err != nil {
    t.Error("Initialization failed", err)
}
defer Close()

Documentation

Index

Constants

View Source
const ContextSymbol = "."
View Source
const MaxStructLength = 65535 //64k

MaxStructLength defines how long can be a structure converted to JSON to be stored

Variables

This section is empty.

Functions

func Close

func Close()

Close should be called as a deferred method after Initialize

func Initialize

func Initialize(uri string) (err error)

Initialize requires a supported uri using one of the following supported formats

Types

type Elephant

type Elephant struct {
	Context string
	// contains filtered or unexported fields
}

Elephant provides db access to a concrete context

var MainContext *Elephant

MainContext is the default context

func GetElephant

func GetElephant(context string) (e *Elephant, err error)

GetElephant returns a valid elephant for the required context

func (*Elephant) Create

func (e *Elephant) Create(input interface{}) (string, error)

Create adds one element to the database If the key attribute value is empty (""), a new one will be assigned

func (*Elephant) Exists

func (e *Elephant) Exists(inputType reflect.Type, key string) bool

Exists check if one key is in use in the database

func (*Elephant) ExistsBy

func (e *Elephant) ExistsBy(inputType reflect.Type, attribute string, input interface{}) bool

ExistsBy gets one element from a specific type filtering by other attribute Returns true if found and false if parameters are incorrect or the element is not found

func (*Elephant) NextID

func (e *Elephant) NextID(inputType reflect.Type) string

NextID gives an empty id to create a new entry

func (*Elephant) Remove

func (e *Elephant) Remove(input interface{}) error

Remove deletes one element from the database Returns err if the object does not exist

func (*Elephant) RemoveByKey

func (e *Elephant) RemoveByKey(inputType reflect.Type, key string) error

RemoveByKey deletes one element from the database Returns err if the object does not exist

func (*Elephant) Retrieve

func (e *Elephant) Retrieve(inputType reflect.Type, key string) interface{}

Retrieve gets one element from a specific type filtering by key Returns the element if found and nil if not

func (*Elephant) RetrieveAll

func (e *Elephant) RetrieveAll(inputType reflect.Type) (map[string]interface{}, error)

RetrieveAll gets all elements with a specific type Returns a map with all elements. It will be empty if there are no elements

func (*Elephant) RetrieveBy

func (e *Elephant) RetrieveBy(inputType reflect.Type, attribute string, input interface{}) interface{}

RetrieveBy gets one element from a specific type filtering by other attribute Returns the element if found and nil if parameters are incorrect or the element is not found

func (*Elephant) Update

func (e *Elephant) Update(input interface{}) error

Update modifies an element on the database

Jump to

Keyboard shortcuts

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