# README

go-utils Database

The go-utils Database package provides handy methods to read and write data from and to an database. The database uses PostgreSQL as management system.

Installation

To use the log package you must import the package.

import "github.com/eliona-smart-building-assistant/go-utils/db"

You have to define an environment variable named CONNECTION_STRING which defines the database that should be connected to. It is also possible to define INIT_CONNECTION_STRING which is used to create database objects like schemas or tables. If INIT_CONNECTION_STRING is not defined the CONNECTION_STRING is used as default.

export CONNECTION_STRING=postgres://user:secret@localhost
export INIT_CONNECTION_STRING=postgres://user:secret@localhost

Usage

After installation, you can access the database.

import "github.com/eliona-smart-building-assistant/go-utils/db"

SQL statements have to apply the guidelines of PostgreSQL. Placeholders are defined as $1, $2 and so on.

Reading data from database

For example, you can read temperature objects based on an SQL statement. You have to open a new connection and read the result through a channel.

type Temperature struct {
    Value int
    Unit  string
}

Note, that the fields of Temperature correspond to the selected values in the SQL statement. Here Value belongs to the constant numeric value 23 and Unit to constant string Celsius.

connection := db.NewConnection()
defer connection.Close(context.Background())
temperatures := make(chan Temperature)
go db.Query(connection, "select 23, 'Celsius'", temperatures)
for temperature := range temperatures {
    log.Debug("Temperature", "Temperature is: %d %s", temperature.Value, temperature.Unit)
}

To select single values only, you can use simple typed channels instead of structures.

connection := db.NewConnection()
defer connection.Close(context.Background())
values := make(chan int)
go db.Query(connection, "select 23", values)
for value := range values {
	log.Debug("Temperature", "Value is: %d", value)
}

Modifying data in database

For example, you can write a temperature in a database table named temperatures.

connection := db.NewConnection()
defer connection.Close(context.Background())
_ = db.Exec(connection, "insert into temperatures (value, unit) values ($1, $2)",
	23, "Celsius")

In the same way with db.Exec() you can update, delete and modifying data as you want.

Listen on database channels

You can listen for notifications on database channels. For example, a notification is triggered for changes on a database table with temperatures, so the database channel is named temperatures. The payload of the notification have to be a json string, e.g. {"Value": 30, "Unit": "Celsius"}. In sum the notification is triggered as pg_notify('temperatures', '{"Value": 30, "Unit": "Celsius"}').

You can use a go channel to read this into a corresponding structure. For example notification we can use the Temperature structure above. The notification payload will be mapped to this structure.

connection := db.NewConnection()
temperatures := make(chan Temperature)
go db.Listen(connection, "temperatures", temperatures)
for temperature := range temperatures {
    log.Debug("Temperature", "Temperature is: %d %s", temperature.Value, temperature.Unit)
}

# Functions

Begin returns a new transaction.
CloseDatabase closes the default database hold by this package.
ClosePool closes the default pool hold by this package.
ConnectionConfig returns the connection config defined by CONNECTION_STRING environment variable.
No description provided by the author
ConnectionString returns the connection string defined in the environment variable CONNECTION_STRING.
Database returns the configured database connection from CONNECTION_STRING.
DatabaseName returns the defined database name configured in CONNECTION_STRING.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Exec inserts a row using the given sql with arguments.
No description provided by the author
No description provided by the author
No description provided by the author
Hostname returns the defined hostname configured in CONNECTION_STRING.
InitConnectionConfig returns the connection config defined by INIT_CONNECTION_STRING environment variable.
No description provided by the author
InitConnectionString returns the connection string for init defined in the environment variable INIT_CONNECTION_STRING.
InitDatabaseName returns the defined database name configured in INIT_CONNECTION_STRING.
No description provided by the author
InitPassword returns the defined password configured in INIT_CONNECTION_STRING.
No description provided by the author
InitPort returns the defined port configured in INIT_CONNECTION_STRING.
InitUsername returns the defined username configured in INIT_CONNECTION_STRING.
No description provided by the author
Listen waits for notifications on database channel and writes the payload to the go channel.
No description provided by the author
ListenWithContext waits for notifications on database channel and writes the payload to the go channel.
No description provided by the author
NewConnection returns a new connection defined by CONNECTION_STRING environment variable.
NewConnectionWithContext returns a new connection defined by CONNECTION_STRING environment variable.
No description provided by the author
NewDatabase returns always a new database connection from CONNECTION_STRING.
NewInitConnection returns a new connection defined by INIT_CONNECTION_STRING environment variable.
NewInitConnectionWithContext returns a new connection defined by INIT_CONNECTION_STRING environment variable.
No description provided by the author
NewInitDatabase returns always a new database connection from CONNECTION_STRING.
No description provided by the author
No description provided by the author
Password returns the defined password configured in CONNECTION_STRING.
Pool returns the default pool hold by this package.
No description provided by the author
Port returns the defined port configured in CONNECTION_STRING.
Query gets values read from database into a channel.
QuerySingleRow returns the value if only a single row is queried.
No description provided by the author
No description provided by the author
Username returns the defined username configured in CONNECTION_STRING.

# Interfaces

The Connection interface allows mocking database connection for testing.