simplehstore

package module
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2021 License: BSD-3-Clause Imports: 13 Imported by: 2

README

simplehstore

Build GoDoc License Go Report Card

Easy way to use a PostgreSQL database (and the HSTORE feature) from Go.

Online API Documentation

godoc.org

Features and limitations

  • Requires PostgreSQL 9.1 or later.
  • Requires Go 1.10 or later.
  • Supports simple use of lists, hashmaps, sets and key/values.
  • Deals mainly with strings.
  • Uses the pq package.
  • Modeled after simpleredis.
  • Uses SQL queries with HSTORE for the KeyValue and HashMap types.
  • Uses regular SQL for the List and Set types.

Sample usage

package main

import (
    "log"

    db "github.com/xyproto/simplehstore"
)

func main() {
    // Check if the local db 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:5432/db")

    // Connect to a different db host/port, with a username and password
    // host := db.NewHost("username:password@server/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 PostgreSQL server must be up and running locally for go test to work, and a database named test must exist.

License, author and version

  • License: BSD-3
  • Author: Alexander F. Rødseth <xyproto@archlinux.org>
  • Version: 2.11.0 (also tagged as 1.2.11)

Documentation

Overview

Package simplehstore offers a simple way to use a PostgreSQL database with HSTORE. The database back end is interchangeable with Redis (xyproto/simpleredis), BoltDB (xyproto/simplebolt) and MariaDB/MySQL (xyproto/simplemaria) by using the interfaces in the xyproto/pinterface package.

Index

Constants

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

Variables

View Source
var (

	// ErrNoAvailableValues is used as an error if an SQL query returns no values
	ErrNoAvailableValues = errors.New("no available values")
	// ErrTooFewResults is used as an error if an SQL query returns too few results
	ErrTooFewResults = errors.New("too few results")
)
View Source
var Verbose = false

Verbose can be set to true when testing, for more information

Functions

func Decode

func Decode(code *string) error

Decode decompresses and decodes an encoded string to an UTF-8 string.

func Encode

func Encode(value *string) error

Encode compresses and encodes a given string in order to safely handle *any* UTF-8 characters.

func SetColumnNames

func SetColumnNames(list, set, hashMapOwner, keyValuePrefix string)

SetColumnNames can be used to change the column names and prefixes that are used in the PostgreSQL tables. The default values are: "a_list", "a_set", "owner" and "a_kv_".

func TestConnection

func TestConnection() (err error)

TestConnection checks if the local database server is up and running

func TestConnectionHost

func TestConnectionHost(connectionString string) error

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

func TestConnectionHostWithDSN

func TestConnectionHostWithDSN(connectionString string) error

TestConnectionHostWithDSN checks if a given database server is up and running.

Types

type HashMap

type HashMap dbDatastructure

HashMap is a hash map with a name, key and value, stored in PostgreSQL Useful when storing several keys and values for a specific username, for instance.

func NewHashMap

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

NewHashMap creates a new HashMap struct

func (*HashMap) All

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

All returns all owners for all hash map elements

func (*HashMap) AllWhere

func (h *HashMap) AllWhere(key, value string) ([]string, error)

AllWhere returns all owner ID's that has a property where key == value

func (*HashMap) Clear

func (h *HashMap) Clear() error

Clear the contents

func (*HashMap) Count

func (h *HashMap) Count() (int, error)

Count counts the number of owners for hash map elements

func (*HashMap) CreateIndexTable

func (h *HashMap) CreateIndexTable() error

CreateIndexTable creates an INDEX table for this hash map, that may speed up lookups

func (*HashMap) Del

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

Del removes an element (for instance a user)

func (*HashMap) DelKey

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

DelKey removes a key of an owner in a hashmap (for instance the email field for a user)

func (*HashMap) Exists

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

Exists checks 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)

GetAll is deprecated in favor of All

func (*HashMap) Has

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

Has checks if a given owner + key exists in the hash map

func (*HashMap) JSON

func (h *HashMap) JSON(owner string) (string, error)

JSON returns the first found hstore value for the given key as a JSON string

func (*HashMap) Keys

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

Keys returns all keys for a given owner

func (*HashMap) Remove

func (h *HashMap) Remove() error

Remove this hashmap

func (*HashMap) RemoveIndexTable

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

RemoveIndexTable removes the INDEX table for this hash map

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

func (*HashMap) SetMap

func (h *HashMap) SetMap(owner string, items map[string]string) error

SetMap is like Set, except that it can set many key+value pairs with one SQL query, and it does not check if each key exists first.

type Host

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

Host represents a PostgreSQL database

func New

func New() *Host

New sets up a connection to the default (local) database host

func NewHost

func NewHost(connectionString string) *Host

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

func NewHost2

func NewHost2(connectionString string) (*Host, error)

NewHost2 sets up a new database connection. connectionString may be on the form "username:password@host:port/database". An error may be returned.

func NewHostWithDSN

func NewHostWithDSN(connectionString string, dbname string) *Host

NewHostWithDSN creates a new database connection with a valid DSN.

func NewHostWithDSN2

func NewHostWithDSN2(connectionString string, dbname string) (*Host, error)

NewHostWithDSN2 creates a new database connection with a valid DSN. An error may be returned.

func (*Host) Close

func (host *Host) Close()

Close the connection

func (*Host) Database

func (host *Host) Database() *sql.DB

Database returns the underlying *sql.DB database struct

func (*Host) Ping

func (host *Host) Ping() error

Ping the host

func (*Host) SelectDatabase

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

SelectDatabase sets a different database name and creates the database if needed.

func (*Host) SetRawUTF8

func (host *Host) SetRawUTF8(enabled bool)

SetRawUTF8 can be used to select if the UTF-8 data be unprocessed, and not hex encoded and compressed. Unprocessed UTF-8 may be slightly faster, but malformed UTF-8 strings can potentially cause problems. Encoding the strings before sending them to PostgreSQL is the default. Choose the setting that best suits your situation.

type KeyValue

type KeyValue dbDatastructure

KeyValue is a hash map with a key and a value, stored in PostgreSQL

func NewKeyValue

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

NewKeyValue creates a new KeyValue struct, for storing key/value pairs.

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

Clear this key/value

func (*KeyValue) CreateIndexTable

func (kv *KeyValue) CreateIndexTable() error

CreateIndexTable creates an INDEX table for this key/value, that may speed up lookups

func (*KeyValue) Del

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

Del removes the given 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)

Inc increases the value of a key and returns the new value. Returns "1" if no previous value is found.

func (*KeyValue) Remove

func (kv *KeyValue) Remove() error

Remove this key/value

func (*KeyValue) RemoveIndexTable

func (kv *KeyValue) RemoveIndexTable() error

RemoveIndexTable removes the INDEX table for this key/value

func (*KeyValue) Set

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

Set a key and value

type List

type List dbDatastructure

List is a list of strings, stored in PostgreSQL

func NewList

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

NewList creates 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)

All retrieves 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)

GetAll is deprecated in favor of All

func (*List) GetLast

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

GetLast is deprecated in favor of Last

func (*List) GetLastN

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

GetLastN is deprecated in favor of LastN

func (*List) Last

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

Last retrieves the last element of a list

func (*List) LastN

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

LastN retrieves the N last elements of a list. If there are too few available elements, the values that were found are returned, together with a TooFewElementsError.

func (*List) Remove

func (l *List) Remove() error

Remove this list

func (*List) RemoveByIndex

func (l *List) RemoveByIndex(index int) error

RemoveByIndex can remove the Nth item, in the same order as returned by All()

type PostgresCreator

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

PostgresCreator is a general struct to create datatypes with. The main purpose is to implement pinterface.ICreator.

func NewCreator

func NewCreator(host *Host) *PostgresCreator

NewCreator can be used to create a new PostgresCreator. The main purpose is to implement pinterface.ICreator.

func (*PostgresCreator) NewHashMap

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

NewHashMap can be used to create a new pinterface.IHashMap. The main purpose is to implement pinterface.ICreator.

func (*PostgresCreator) NewKeyValue

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

NewKeyValue can be used to create a new pinterface.IKeyValue. The main purpose is to implement pinterface.ICreator.

func (*PostgresCreator) NewList

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

NewList can be used to create a new pinterface.IList. The main purpose is to implement pinterface.ICreator.

func (*PostgresCreator) NewSet

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

NewSet can be used to create a new pinterface.ISet. The main purpose is to implement pinterface.ICreator.

type Set

type Set dbDatastructure

Set is a set of strings, stored in PostgreSQL

func NewSet

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

NewSet creates 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)

All returns all elements in the set

func (*Set) Clear

func (s *Set) Clear() error

Clear the list contents

func (*Set) Del

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

Del removes an element from the set

func (*Set) GetAll

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

GetAll is deprecated in favor of All

func (*Set) Has

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

Has checks if the given value is in the set

func (*Set) Remove

func (s *Set) Remove() error

Remove this set

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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