db

package
v0.0.0-...-40a7325 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package db exports a DB interface that is implemented by multiple databases clients.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bolt

type Bolt struct {
	Path string
	Col  string
	// contains filtered or unexported fields
}

Bolt wraps a bolt.DB client and satisfies the DB interface.

func (*Bolt) Add

func (bdb *Bolt) Add(cmd *proto.BotCommand) error

Add receives a *proto.BotCommand and adds it to the bucket designated. If something goes wrong it returns a non-nil error.

func (*Bolt) Close

func (bdb *Bolt) Close() error

Close tries to close the connection to the BoltDB database. If fails it returns a non-nil error.

func (*Bolt) Connect

func (bdb *Bolt) Connect() error

Connect treis to connect to a BoltDB database. If it fails it returns a non-nil error.

func (*Bolt) Get

func (bdb *Bolt) Get(cmd *proto.Command) (*proto.BotCommand, error)

Get receives a *proto.Command and returns the respective *proto.BotCommand if exists in the designated bucket. If not it returns a non-nil error.

func (*Bolt) GetAll

func (bdb *Bolt) GetAll() (*proto.BotCommands, error)

GetAll ranges over all the entries of the designated bucket and returns a *proto.BotCommands with all the *proto.BotCommand found. If something goes wrong it returns a non-nil error.

func (*Bolt) Remove

func (bdb *Bolt) Remove(cmd *proto.Command) error

Remove removes a *proto.BotCommand from the designated bucket. It returns a non-nil error if something goes wrong.

func (*Bolt) Update

func (bdb *Bolt) Update(cmd *proto.BotCommand) error

Update updates the Response of an existing *proto.BotCommand with he Response of the received *proto.BotCommand. If the *proto.BotCommand didn't exists it adds it to the bucket due to how BoltDB databases work. If something goes wrong it returns a non-nil error.

type DB

type DB interface {
	Connect() error
	Add(cmd *proto.BotCommand) error
	Get(cmd *proto.Command) (*proto.BotCommand, error)
	GetAll() (*proto.BotCommands, error)
	Remove(cmd *proto.Command) error
	Update(cmd *proto.BotCommand) error
	Close() error
}

DB represents a database client with basic CRUD methods as basic methods to connect and disconnect from the database itself.

func Create

func Create(env string) DB

Create follows the Factory pattern to return a DB depending on the received parameter.

type Mem

type Mem map[string]string

Mem is a mocked-up database for testing.

func (Mem) Add

func (m Mem) Add(cmd *proto.BotCommand) error

Add receives a *proto.BotCommand and adds it to the map using the Command as key and the Response as a value.

func (Mem) Close

func (m Mem) Close() error

Close deletes all the keys from the map.

func (Mem) Connect

func (m Mem) Connect() error

Connect simulates a connection with a database.

func (Mem) Get

func (m Mem) Get(cmd *proto.Command) (*proto.BotCommand, error)

Get receives a *proto.Command and returns if exists the respective *proto.BotCommand.

func (Mem) GetAll

func (m Mem) GetAll() (*proto.BotCommands, error)

GetAll ranges over the map and returns a *proto.BotCommands with all the *proto.BotCommand found.

func (Mem) Remove

func (m Mem) Remove(cmd *proto.Command) error

Remove removes a *proto.BotCommand from the map.

func (Mem) Update

func (m Mem) Update(cmd *proto.BotCommand) error

Update updates the Response of an existing *proto.BotCommand with the Response of the received *proto.BotCommand. If the *proto.BotCommand didn't exists it adds it.

type Postgres

type Postgres struct {
	Host     string
	Port     string
	User     string
	Password string
	DB       string
	Table    string

	MaxConns        int
	MaxConnLifetime time.Duration
	// contains filtered or unexported fields
}

Postgres wraps a sql.DB client for PostgreSQL and satisfies the DB interface.

func (*Postgres) Add

func (ps *Postgres) Add(cmd *proto.BotCommand) error

Add receives a *proto.BotCommand and adds it to the table designated. If something goes wrong executing the SQL statement it returns a non-nil error.

func (*Postgres) Close

func (ps *Postgres) Close() error

Close tries to close the connection to the PostgreSQL database. If fails it returns a non-nil error.

func (*Postgres) Connect

func (ps *Postgres) Connect() error

Connect tries to connect to a PostgreSQL database. If it fails it returns a non-nil error. It also tries to create a table for the commands if not exist.

func (*Postgres) Get

func (ps *Postgres) Get(cmd *proto.Command) (*proto.BotCommand, error)

Get receives a *proto.Command and returns the respective *proto.BotCommand if exists in the designated table. If not or there is any problem while executing the SQL statement it returns a non-nil error.

func (*Postgres) GetAll

func (ps *Postgres) GetAll() (*proto.BotCommands, error)

GetAll ranges over all the entries of the designated table for the commands and returns a *proto.BotCommands with all the *proto.BotCommand found. If something goes wrong while executing the SQL statement or while getting some command it returns a non-nil error.

func (*Postgres) Remove

func (ps *Postgres) Remove(cmd *proto.Command) error

Remove removes a *proto.BotCommand from the designated table. It returns a non-nil error if there is some problem while executing the SQL statement or deleting the command.

func (*Postgres) Update

func (ps *Postgres) Update(cmd *proto.BotCommand) error

Update updates the Response of an existing *proto.BotCommand with the Response of the received *proto.BotCommand. If there is any error while executing the SQL statement it returns a non-nil error.

type SQLite

type SQLite struct {
	Path  string
	Table string

	MaxConns        int
	MaxConnLifetime time.Duration
	// contains filtered or unexported fields
}

SQLite wraps a sql.DB client for SQLite to satisfy the DB interface.

func (*SQLite) Add

func (sq *SQLite) Add(cmd *proto.BotCommand) error

Add receives a *proto.BotCommand and adds it to the table designated. If something goes wrong while executing the SQL statement it returns a non-nil error.

func (*SQLite) Close

func (sq *SQLite) Close() error

Close tries to close the connection to the SQLite database. If it fails it returns a non-nil error.

func (*SQLite) Connect

func (sq *SQLite) Connect() error

Connect tries to connect to a SQLite database. If it fails it returns a non-nil error. It also tries to create a table for the commands if not exists.

func (*SQLite) Get

func (sq *SQLite) Get(cmd *proto.Command) (*proto.BotCommand, error)

Get reveives a *proto.Command and returns the respective *proto.BotCommand if exists in the designated table. If not exists or there is any problem while executing the SQL statement it returns a non-nil error.

func (*SQLite) GetAll

func (sq *SQLite) GetAll() (*proto.BotCommands, error)

GetAll ranges over all the entries of the designated table for the commands and returns a *proto.BotCommand with all the *proto.BotCommands found. If something goes wrong while executing the SQL statement or while getting some *proto.BotCommand it returns a non-nil error.

func (*SQLite) Remove

func (sq *SQLite) Remove(cmd *proto.Command) error

Remove removes the received *proto.BotCommand from the designated table. It returns a non-nil error if something goes wrong while executing the SQL statement.

func (*SQLite) Update

func (sq *SQLite) Update(cmd *proto.BotCommand) error

Update updates the *proto.Response of an existing *proto.BotCommand with the *proto.Response of the received *proto.BotCommand. If something goes wrong while executing the SQL statement it returns a non-nil error.

Jump to

Keyboard shortcuts

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