Documentation ¶
Overview ¶
Package db exports a DB interface that is implemented by multiple databases clients.
Index ¶
- type Bolt
- func (bdb *Bolt) Add(cmd *proto.BotCommand) error
- func (bdb *Bolt) Close() error
- func (bdb *Bolt) Connect() error
- func (bdb *Bolt) Get(cmd *proto.Command) (*proto.BotCommand, error)
- func (bdb *Bolt) GetAll() (*proto.BotCommands, error)
- func (bdb *Bolt) Remove(cmd *proto.Command) error
- func (bdb *Bolt) Update(cmd *proto.BotCommand) error
- type DB
- type Mem
- func (m Mem) Add(cmd *proto.BotCommand) error
- func (m Mem) Close() error
- func (m Mem) Connect() error
- func (m Mem) Get(cmd *proto.Command) (*proto.BotCommand, error)
- func (m Mem) GetAll() (*proto.BotCommands, error)
- func (m Mem) Remove(cmd *proto.Command) error
- func (m Mem) Update(cmd *proto.BotCommand) error
- type Postgres
- func (ps *Postgres) Add(cmd *proto.BotCommand) error
- func (ps *Postgres) Close() error
- func (ps *Postgres) Connect() error
- func (ps *Postgres) Get(cmd *proto.Command) (*proto.BotCommand, error)
- func (ps *Postgres) GetAll() (*proto.BotCommands, error)
- func (ps *Postgres) Remove(cmd *proto.Command) error
- func (ps *Postgres) Update(cmd *proto.BotCommand) error
- type SQLite
- func (sq *SQLite) Add(cmd *proto.BotCommand) error
- func (sq *SQLite) Close() error
- func (sq *SQLite) Connect() error
- func (sq *SQLite) Get(cmd *proto.Command) (*proto.BotCommand, error)
- func (sq *SQLite) GetAll() (*proto.BotCommands, error)
- func (sq *SQLite) Remove(cmd *proto.Command) error
- func (sq *SQLite) Update(cmd *proto.BotCommand) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bolt ¶
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 ¶
Close tries to close the connection to the BoltDB database. If fails it returns a non-nil error.
func (*Bolt) Connect ¶
Connect treis to connect to a BoltDB database. If it fails it returns a non-nil error.
func (*Bolt) Get ¶
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 ¶
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.
type Mem ¶
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) Get ¶
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.
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 ¶
Close tries to close the connection to the PostgreSQL database. If fails it returns a non-nil error.
func (*Postgres) Connect ¶
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 ¶
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.
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 ¶
Close tries to close the connection to the SQLite database. If it fails it returns a non-nil error.
func (*SQLite) Connect ¶
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 ¶
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.