simplemaria

package module
v0.0.0-...-2041381 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 12 Imported by: 0

README

SimpleMaria

Build Status GoDoc

An easy way to use a MariaDB/MySQL database from Go.

Online API Documentation

godoc.org

Features and limitations

  • Supports simple use of lists, hashmaps, sets and key/values.
  • Deals mainly with strings.
  • Uses the mysql package.
  • Modeled after simpleredis.
  • The hash maps behaves like hash maps, but are not backed by actual hashmaps, unlike with simpleredis. This is for keeping compatibility with simpleredis. If performance when scaling up is a concern, simpleredis backed by redis might be a better choice.
  • MariaDB/MySQL normally has issues with variable size UTF-8 strings, even for for some combinations of characters. This package avoids these problems by compressing and hex encoding the data before storing in the database. This may slow down or speed up the time it takes to access the data, depending on your setup, but it's a safe way to encode any string. This behavior is optional and can be disabled with host.SetRawUTF8(true), (to just use utf8mb4).

Sample usage

package main

import (
	"log"

	"github.com/xyproto/simplemaria"
)

func main() {
	// Check if the simplemaria service is up
	if err := db.TestConnection(); err != nil {
		log.Fatalln("Could not connect to local database. Is the service up and running?")
	}

	// Create a Host, connect to the local db server
	host := db.New()

	// Connecting to a different host/port
	//host := db.NewHost("server:3306/db")

	// Connect to a different db host/port, with a username and password
	// host := db.NewHost("username:password@server:port/db")

	// Close the connection when the function returns
	defer host.Close()

	// Create a list named "greetings"
	list, err := db.NewList(host, "greetings")
	if err != nil {
		log.Fatalln("Could not create list!")
	}

	// Add "hello" to the list, check if there are errors
	if list.Add("hello") != nil {
		log.Fatalln("Could not add an item to list!")
	}

	// Get the last item of the list
	if item, err := list.GetLast(); err != nil {
		log.Fatalln("Could not fetch the last item from the list!")
	} else {
		log.Println("The value of the stored item is:", item)
	}

	// Remove the list
	if list.Remove() != nil {
		log.Fatalln("Could not remove the list!")
	}
}

Testing

A MariaDB/MySQL Database must be up and running locally for go test to work.

Version, license and author

Documentation

Overview

Package simplemaria offers a simple way to use a MySQL/MariaDB database. This database backend is interchangeable with xyproto/simpleredis and xyproto/simplebolt, since they all use xyproto/pinterface.

Index

Constants

View Source
const (
	// Version number. Stable API within major version numbers.
	Version = 3.2
)

Variables

View Source
var Verbose = false

Functions

func Decode

func Decode(code *string) error

Dehex and decompress the given string

func Encode

func Encode(value *string) error

MariaDB/MySQL does not handle some characters well. Compressing and hex encoding the value is one of many possible ways to avoid this. Using BLOB fields and different datatypes is another.

func TestConnection

func TestConnection() (err error)

Test if the local database server is up and running.

func TestConnectionHost

func TestConnectionHost(connectionString string) (err error)

Test if a given database server is up and running. connectionString may be on the form "username:password@host:port/database".

func TestConnectionHostWithDSN

func TestConnectionHostWithDSN(connectionString string) (err error)

Test if a given database server is up and running.

Types

type HashMap

type HashMap dbDatastructure

func NewHashMap

func NewHashMap(host *Host, name string) (*HashMap, error)

Create a new hashmap

func (*HashMap) All

func (h *HashMap) All() ([]string, error)

Get all owners (not keys, not values) for all hash elements

func (*HashMap) Clear

func (h *HashMap) Clear() error

Clear the contents

func (*HashMap) Del

func (h *HashMap) Del(owner string) error

Remove an element (for instance a user)

func (*HashMap) DelKey

func (h *HashMap) DelKey(owner, key string) error

Remove a key for an entry in a hashmap (for instance the email field for a user)

func (*HashMap) Exists

func (h *HashMap) Exists(owner string) (bool, error)

Check if a given owner exists as a hash map at all

func (*HashMap) Get

func (h *HashMap) Get(owner, key string) (string, error)

Get a value from a hashmap given the element id (for instance a user id) and the key (for instance "password").

func (*HashMap) GetAll

func (h *HashMap) GetAll() ([]string, error)

Deprecated, please use .All() instead

func (*HashMap) Has

func (h *HashMap) Has(owner, key string) (bool, error)

Check if a given owner + key is in the hash map

func (*HashMap) Keys

func (h *HashMap) Keys(owner string) ([]string, error)

Get all keys for a given owner

func (*HashMap) Remove

func (h *HashMap) Remove() error

Remove this hashmap

func (*HashMap) Set

func (h *HashMap) Set(owner, key, value string) error

Set a value in a hashmap given the element id (for instance a user id) and the key (for instance "password")

type Host

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

Host represents a specific database at a database host

func New

func New() *Host

The default database connection

func NewHost

func NewHost(connectionString string) *Host

Create a new database connection. connectionString may be on the form "username:password@host:port/database".

func NewHostWithDSN

func NewHostWithDSN(connectionString string, dbname string) *Host

Create a new database connection with a valid DSN.

func (*Host) Close

func (host *Host) Close()

Close the connection

func (*Host) Ping

func (host *Host) Ping() error

Ping the host

func (*Host) SelectDatabase

func (host *Host) SelectDatabase(dbname string) error

Select a different database. Create the database if needed.

func (*Host) SetRawUTF8

func (host *Host) SetRawUTF8(enabled bool)

Should the UTF-8 data be raw, and not hex encoded and compressed?

type KeyValue

type KeyValue dbDatastructure

func NewKeyValue

func NewKeyValue(host *Host, name string) (*KeyValue, error)

Create a new key/value

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

Clear this key/value

func (*KeyValue) Del

func (kv *KeyValue) Del(key string) error

Remove a key

func (*KeyValue) Get

func (kv *KeyValue) Get(key string) (string, error)

Get a value given a key

func (*KeyValue) Inc

func (kv *KeyValue) Inc(key string) (string, error)

Increase the value of a key, returns the new value Returns an empty string if there were errors, or "0" if the key does not already exist.

func (*KeyValue) Remove

func (kv *KeyValue) Remove() error

Remove this key/value

func (*KeyValue) Set

func (kv *KeyValue) Set(key, value string) error

Set a key and value

type List

type List dbDatastructure

func NewList

func NewList(host *Host, name string) (*List, error)

Create a new list. Lists are ordered.

func (*List) Add

func (l *List) Add(value string) error

Add an element to the list

func (*List) All

func (l *List) All() ([]string, error)

Get all elements of a list

func (*List) Clear

func (l *List) Clear() error

Clear the list contents

func (*List) GetAll

func (l *List) GetAll() ([]string, error)

Deprecated, please use .All() instead

func (*List) GetLast

func (l *List) GetLast() (string, error)

Deprecated, please use .Last() instead

func (*List) GetLastN

func (l *List) GetLastN(n int) ([]string, error)

Deprecated, please use .LastN(n) instead

func (*List) Last

func (l *List) Last() (string, error)

Get the last element of a list

func (*List) LastN

func (l *List) LastN(n int) ([]string, error)

Get the last N elements of a list

func (*List) Remove

func (l *List) Remove() error

Remove this list

type MariaCreator

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

func NewCreator

func NewCreator(host *Host) *MariaCreator

func (*MariaCreator) NewHashMap

func (m *MariaCreator) NewHashMap(id string) (pinterface.IHashMap, error)

func (*MariaCreator) NewKeyValue

func (m *MariaCreator) NewKeyValue(id string) (pinterface.IKeyValue, error)

func (*MariaCreator) NewList

func (m *MariaCreator) NewList(id string) (pinterface.IList, error)

func (*MariaCreator) NewSet

func (m *MariaCreator) NewSet(id string) (pinterface.ISet, error)

type Set

type Set dbDatastructure

func NewSet

func NewSet(host *Host, name string) (*Set, error)

Create a new set

func (*Set) Add

func (s *Set) Add(value string) error

Add an element to the set

func (*Set) All

func (s *Set) All() ([]string, error)

Get all elements of the set

func (*Set) Clear

func (s *Set) Clear() error

Clear the list contents

func (*Set) Del

func (s *Set) Del(value string) error

Remove an element from the set

func (*Set) GetAll

func (s *Set) GetAll() ([]string, error)

Deprecated, please use .All() instead

func (*Set) Has

func (s *Set) Has(value string) (bool, error)

Check if a given value is in the set

func (*Set) Remove

func (s *Set) Remove() error

Remove this set

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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