PKGBUILD/pkg/repository/sqlite3/sqlite.go

1172 lines
27 KiB
Go
Raw Normal View History

package sqlite3
import (
"context"
"database/sql"
"embed"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
"git.cryptic.systems/volker.raschek/go-logger"
"github.com/golang-migrate/migrate/v4"
"github.com/johejo/golang-migrate-extra/source/iofs"
)
var (
//go:embed ddl/*.sql
2021-04-06 17:40:56 +00:00
ddlAssets embed.FS
//go:embed dml/deleteDeviceByID.sql
deleteDeviceByIDSQL string
//go:embed dml/deleteDeviceByName.sql
deleteDeviceByNameSQL string
//go:embed dml/deleteSensorByID.sql
deleteSensorByIDSQL string
//go:embed dml/deleteSensorByName.sql
deleteSensorByNameSQL string
//go:embed dml/insertDevice.sql
insertDeviceSQL string
//go:embed dml/insertHumidity.sql
insertHumiditySQL string
//go:embed dml/insertOrUpdateDevice.sql
insertOrUpdateDeviceSQL string
//go:embed dml/insertOrUpdateHumidity.sql
insertOrUpdateHumiditySQL string
//go:embed dml/insertOrUpdatePressure.sql
insertOrUpdatePressureSQL string
//go:embed dml/insertOrUpdateSensor.sql
insertOrUpdateSensorSQL string
//go:embed dml/insertOrUpdateTemperature.sql
insertOrUpdateTemperatureSQL string
//go:embed dml/insertPressure.sql
insertPressureSQL string
//go:embed dml/insertSensor.sql
insertSensorSQL string
//go:embed dml/insertTemperature.sql
insertTemperatureSQL string
//go:embed dml/selectDeviceByID.sql
selectDeviceByIDSQL string
//go:embed dml/selectDeviceByName.sql
selectDeviceByNameSQL string
//go:embed dml/selectDevices.sql
selectDevicesSQL string
//go:embed dml/selectHumidities.sql
selectHumiditiesSQL string
//go:embed dml/selectHumidityByID.sql
selectHumidityByIDSQL string
//go:embed dml/selectPressureByID.sql
selectPressureByIDSQL string
//go:embed dml/selectPressures.sql
selectPressuresSQL string
//go:embed dml/selectSensorByID.sql
selectSensorByIDSQL string
//go:embed dml/selectSensors.sql
selectSensorsSQL string
//go:embed dml/selectSensorsByDeviceID.sql
selectSensorsByDeviceIDSQL string
//go:embed dml/selectSensorsByModel.sql
selectSensorsByModelSQL string
//go:embed dml/selectSensorsByName.sql
selectSensorsByNameSQL string
//go:embed dml/selectTemperatureByID.sql
selectTemperatureByIDSQL string
//go:embed dml/selectTemperatures.sql
selectTemperaturesSQL string
//go:embed dml/updateDevice.sql
updateDeviceSQL string
//go:embed dml/updateSensor.sql
updateSensorSQL string
)
type Opts struct {
DatabaseURL *url.URL
Logger logger.Logger
}
func (o *Opts) Validate() error {
for k, v := range map[string]interface{}{
"DatabaseURL": o.DatabaseURL,
"Logger": o.Logger,
} {
if v == nil {
return fmt.Errorf("%v does not be empty or nil", k)
}
}
if o.DatabaseURL.Scheme != "sqlite3" {
return fmt.Errorf("Unsupported database scheme %v for database backend sqlite3", o.DatabaseURL.Scheme)
}
return nil
}
func New(opts Opts) (*SQLite, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
// Create directory if not exist
if _, err := os.Stat(filepath.Dir(opts.DatabaseURL.Path)); os.IsNotExist(err) {
err := os.MkdirAll(filepath.Dir(opts.DatabaseURL.Path), 0755)
if err != nil {
return nil, err
}
}
// enable foreign keys
values := opts.DatabaseURL.Query()
values.Set("_foreign_keys", "on")
customRawURL := fmt.Sprintf("file://%v?%v", opts.DatabaseURL.Path, values.Encode())
sqlDBO, err := sql.Open(opts.DatabaseURL.Scheme, customRawURL)
if err != nil {
return nil, err
}
return &SQLite{
databaseURL: opts.DatabaseURL,
dbo: sqlDBO,
flogger: opts.Logger,
mutex: new(sync.Mutex),
}, nil
}
// SQLite implementation
type SQLite struct {
databaseURL *url.URL
dbo *sql.DB
flogger logger.Logger
mutex *sync.Mutex
}
2021-03-21 21:30:50 +00:00
// AddDevices into the database
func (sqlite *SQLite) AddDevices(ctx context.Context, devices ...*types.Device) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(insertDeviceSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
2021-03-21 21:30:50 +00:00
for _, device := range devices {
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
if err != nil {
2021-03-21 21:30:50 +00:00
return fmt.Errorf("Failed to execute statement: %v", err)
}
}
return tx.Commit()
}
2021-03-21 21:30:50 +00:00
// AddOrUpdateDevices into the database
func (sqlite *SQLite) AddOrUpdateDevices(ctx context.Context, devices ...*types.Device) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(insertOrUpdateDeviceSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, device := range devices {
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
}
return tx.Commit()
}
// AddMeasuredValues into the database
func (sqlite *SQLite) AddMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
splittedMeasuredValues := make(map[types.MeasuredValueType][]*types.MeasuredValue, 0)
for _, measuredValue := range measuredValues {
if _, ok := splittedMeasuredValues[measuredValue.ValueType]; !ok {
splittedMeasuredValues[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
}
splittedMeasuredValues[measuredValue.ValueType] = append(splittedMeasuredValues[measuredValue.ValueType], measuredValue)
}
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
// General insert function
insert := func(tx *sql.Tx, query string, measuredValues []*types.MeasuredValue) error {
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, measuredValue := range measuredValues {
_, err := stmt.Exec(
&measuredValue.ID,
&measuredValue.Value,
&measuredValue.Date,
&measuredValue.SensorID,
&measuredValue.CreationDate,
&measuredValue.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
}
return nil
}
for measuredValueType, measuredValues := range splittedMeasuredValues {
var query string
switch measuredValueType {
case types.Humidity:
query = insertHumiditySQL
case types.Pressure:
query = insertPressureSQL
case types.Temperature:
query = insertTemperatureSQL
default:
return fmt.Errorf("Measured value type %v not supported", measuredValueType)
}
err := insert(tx, query, measuredValues)
if err != nil {
return err
}
}
return tx.Commit()
}
// AddOrUpdateMeasuredValues into the database
func (sqlite *SQLite) AddOrUpdateMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
splittedMeasuredValues := make(map[types.MeasuredValueType][]*types.MeasuredValue, 0)
for _, measuredValue := range measuredValues {
if _, ok := splittedMeasuredValues[measuredValue.ValueType]; !ok {
splittedMeasuredValues[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
}
splittedMeasuredValues[measuredValue.ValueType] = append(splittedMeasuredValues[measuredValue.ValueType], measuredValue)
}
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
// General insert function
insert := func(tx *sql.Tx, query string, measuredValues []*types.MeasuredValue) error {
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, measuredValue := range measuredValues {
_, err := stmt.Exec(
&measuredValue.ID,
&measuredValue.Value,
&measuredValue.Date,
&measuredValue.SensorID,
&measuredValue.CreationDate,
&measuredValue.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
}
return nil
}
for measuredValueType, measuredValues := range splittedMeasuredValues {
var queryFile string
switch measuredValueType {
case types.Humidity:
queryFile = insertOrUpdateHumiditySQL
case types.Pressure:
queryFile = insertOrUpdatePressureSQL
case types.Temperature:
queryFile = insertOrUpdateTemperatureSQL
default:
return fmt.Errorf("Measured value type %v not supported", measuredValueType)
}
err := insert(tx, queryFile, measuredValues)
if err != nil {
return err
}
}
return tx.Commit()
}
2021-03-21 21:30:50 +00:00
// AddSensors into the database
func (sqlite *SQLite) AddSensors(ctx context.Context, sensors ...*types.Sensor) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(insertSensorSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, sensor := range sensors {
_, err = stmt.Exec(
&sensor.ID,
&sensor.Name,
&sensor.Location,
&sensor.WireID,
&sensor.I2CBus,
&sensor.I2CAddress,
&sensor.GPIONumber,
&sensor.Model,
&sensor.Enabled,
&sensor.TickDuration,
&sensor.DeviceID,
&sensor.CreationDate,
&sensor.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
}
return tx.Commit()
}
2021-03-21 21:30:50 +00:00
// AddOrUpdateSensors into the database
func (sqlite *SQLite) AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(insertOrUpdateSensorSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, sensor := range sensors {
_, err = stmt.Exec(
&sensor.ID,
&sensor.Name,
&sensor.Location,
&sensor.WireID,
&sensor.I2CBus,
&sensor.I2CAddress,
&sensor.GPIONumber,
&sensor.Model,
&sensor.Enabled,
&sensor.TickDuration,
&sensor.DeviceID,
&sensor.CreationDate,
&sensor.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
}
return tx.Commit()
}
2021-03-21 21:30:50 +00:00
// Close closes the database and prevents new queries from starting. Close then
// waits for all queries that have started processing on the server to finish.
func (sqlite *SQLite) Close() error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
return sqlite.dbo.Close()
}
// Migrate creates all required tables if not exist
func (sqlite *SQLite) Migrate(ctx context.Context) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
2021-04-06 17:40:56 +00:00
sourceDriver, err := iofs.New(ddlAssets, "ddl")
if err != nil {
return err
}
m, err := migrate.NewWithSourceInstance("iofs", sourceDriver, sqlite.databaseURL.String())
if err != nil {
return err
}
err = m.Up()
switch {
case errors.Is(err, migrate.ErrNoChange):
return nil
default:
return err
}
}
// GetDevice from database
func (sqlite *SQLite) GetDeviceByID(ctx context.Context, id string) (*types.Device, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
devices, err := sqlite.selectDevices(tx, selectDeviceByIDSQL, id)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
if len(devices) == 0 {
return nil, nil
}
return devices[0], nil
}
// GetDevice from database
func (sqlite *SQLite) GetDeviceByName(ctx context.Context, name string) (*types.Device, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
devices, err := sqlite.selectDevices(tx, selectDeviceByNameSQL, name)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
if len(devices) == 0 {
return nil, nil
}
return devices[0], nil
}
// GetDevices from the database
func (sqlite *SQLite) GetDevices(ctx context.Context) ([]*types.Device, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
devices, err := sqlite.selectDevices(tx, selectDevicesSQL)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
}
return devices, nil
}
func (sqlite *SQLite) selectDevices(tx *sql.Tx, query string, args ...interface{}) ([]*types.Device, error) {
stmt, err := tx.Prepare(query)
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
rows, err := stmt.Query(args...)
if err != nil {
return nil, fmt.Errorf("Failed to query statement: %v", err)
}
defer rows.Close()
devices := make([]*types.Device, 0)
for rows.Next() {
device := new(types.Device)
err = rows.Scan(
&device.ID,
&device.Name,
&device.Location,
&device.CreationDate,
&device.UpdateDate,
)
if err != nil {
return nil, fmt.Errorf("Failed to scan row: %v", err)
}
devices = append(devices, device)
}
return devices, nil
}
// GetHumidity returns humidity from the database
func (sqlite *SQLite) GetHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
measuredValues, err := sqlite.selectMeasuredValue(tx, selectHumidityByIDSQL, id)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
if measuredValues == nil {
return nil, nil
}
for _, measuredValue := range measuredValues {
measuredValue.ValueType = types.Humidity
}
return measuredValues[0], nil
}
// GetHumidities returns humidities from the database
func (sqlite *SQLite) GetHumidities(ctx context.Context) ([]*types.MeasuredValue, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
measuredValues, err := sqlite.selectMeasuredValue(tx, selectHumiditiesSQL)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
for _, measuredValue := range measuredValues {
measuredValue.ValueType = types.Humidity
}
return measuredValues, nil
}
func (sqlite *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...interface{}) ([]*types.MeasuredValue, error) {
stmt, err := tx.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(args...)
if err != nil {
return nil, err
}
defer rows.Close()
measuredValues := make([]*types.MeasuredValue, 0)
for rows.Next() {
measuredValue := new(types.MeasuredValue)
err := rows.Scan(
&measuredValue.ID,
&measuredValue.Value,
&measuredValue.Date,
&measuredValue.SensorID,
&measuredValue.CreationDate,
&measuredValue.UpdateDate,
)
if err != nil {
return nil, err
}
measuredValues = append(measuredValues, measuredValue)
}
return measuredValues, nil
}
// GetPressure returns pressure from the database
func (sqlite *SQLite) GetPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
measuredValues, err := sqlite.selectMeasuredValue(tx, selectPressureByIDSQL, id)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
if measuredValues == nil {
return nil, nil
}
for _, measuredValue := range measuredValues {
measuredValue.ValueType = types.Pressure
}
return measuredValues[0], nil
}
// GetPressures returns pressure from the database
func (sqlite *SQLite) GetPressures(ctx context.Context) ([]*types.MeasuredValue, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
measuredValues, err := sqlite.selectMeasuredValue(tx, selectPressuresSQL)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
for _, measuredValue := range measuredValues {
measuredValue.ValueType = types.Pressure
}
return measuredValues, nil
}
// GetSensor from database
func (sqlite *SQLite) GetSensorByID(ctx context.Context, id string) (*types.Sensor, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
sensors, err := sqlite.selectSensors(tx, selectSensorByIDSQL, id)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
}
if len(sensors) == 0 {
return nil, nil
}
return sensors[0], nil
}
// GetSensors from the database
func (sqlite *SQLite) GetSensors(ctx context.Context) ([]*types.Sensor, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
sensors, err := sqlite.selectSensors(tx, selectSensorsSQL)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
}
return sensors, nil
}
// GetSensorsByModels from the database
func (sqlite *SQLite) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string) ([]*types.Sensor, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
cachedSensors := make([]*types.Sensor, 0)
for i := range deviceIDs {
sensors, err := sqlite.selectSensors(tx, selectSensorsByDeviceIDSQL, deviceIDs[i])
if err != nil {
return nil, err
}
cachedSensors = append(cachedSensors, sensors...)
}
err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
}
return cachedSensors, nil
}
// GetSensorsByModels from the database
func (sqlite *SQLite) GetSensorsByModels(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
cachedSensors := make([]*types.Sensor, 0)
for i := range sensorModels {
sensors, err := sqlite.selectSensors(tx, selectSensorsByModelSQL, sensorModels[i])
if err != nil {
return nil, err
}
cachedSensors = append(cachedSensors, sensors...)
}
err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
}
return cachedSensors, nil
}
// GetSensorsByModels from the database
func (sqlite *SQLite) GetSensorsByNames(ctx context.Context, sensorNames ...string) ([]*types.Sensor, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
cachedSensors := make([]*types.Sensor, 0)
for i := range sensorNames {
sensors, err := sqlite.selectSensors(tx, selectSensorsByNameSQL, sensorNames[i])
if err != nil {
return nil, err
}
cachedSensors = append(cachedSensors, sensors...)
}
err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
}
return cachedSensors, nil
}
func (sqlite *SQLite) selectSensors(tx *sql.Tx, query string, args ...interface{}) ([]*types.Sensor, error) {
stmt, err := tx.Prepare(query)
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
rows, err := stmt.Query(args...)
if err != nil {
return nil, fmt.Errorf("Failed to query statement: %v", err)
}
defer rows.Close()
sensors := make([]*types.Sensor, 0)
for rows.Next() {
sensor := new(types.Sensor)
err = rows.Scan(
&sensor.ID,
&sensor.Name,
&sensor.Location,
&sensor.WireID,
&sensor.I2CBus,
&sensor.I2CAddress,
&sensor.GPIONumber,
&sensor.Model,
&sensor.Enabled,
&sensor.TickDuration,
&sensor.DeviceID,
&sensor.CreationDate,
&sensor.UpdateDate,
)
if err != nil {
return nil, fmt.Errorf("Failed to scan row: %v", err)
}
sensors = append(sensors, sensor)
}
return sensors, nil
}
// GetTemperature returns temperatures from the database
func (sqlite *SQLite) GetTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
measuredValues, err := sqlite.selectMeasuredValue(tx, selectTemperatureByIDSQL, id)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
if measuredValues == nil {
return nil, nil
}
for _, measuredValue := range measuredValues {
measuredValue.ValueType = types.Temperature
}
return measuredValues[0], nil
}
// GetTemperatures returns temperatures from the database
func (sqlite *SQLite) GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, error) {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
measuredValues, err := sqlite.selectMeasuredValue(tx, selectTemperaturesSQL)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
for _, measuredValue := range measuredValues {
measuredValue.ValueType = types.Temperature
}
return measuredValues, nil
}
2021-03-21 21:30:50 +00:00
// RemoveDevices from the database
func (sqlite *SQLite) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(deleteDeviceByIDSQL)
2021-03-21 21:30:50 +00:00
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, deviceID := range deviceIDs {
_, err = stmt.Exec(deviceID)
if err != nil {
return err
}
}
return tx.Commit()
}
// RemoveDevices from the database
func (sqlite *SQLite) RemoveDevicesByNames(ctx context.Context, names ...string) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(deleteDeviceByNameSQL)
2021-03-21 21:30:50 +00:00
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, deviceID := range names {
_, err = stmt.Exec(deviceID)
if err != nil {
return err
}
}
return tx.Commit()
}
// RemoveSensors from the database
func (sqlite *SQLite) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(deleteSensorByIDSQL)
2021-03-21 21:30:50 +00:00
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, sensorID := range sensorIDs {
_, err = stmt.Exec(sensorID)
if err != nil {
return err
}
}
return tx.Commit()
}
// RemoveSensors from the database
func (sqlite *SQLite) RemoveSensorsByNames(ctx context.Context, names ...string) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
stmt, err := tx.Prepare(deleteSensorByNameSQL)
2021-03-21 21:30:50 +00:00
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
for _, sensorID := range names {
_, err = stmt.Exec(sensorID)
if err != nil {
return err
}
}
return tx.Commit()
}
// UpdateDevices updates a device in the database
func (sqlite *SQLite) UpdateDevices(ctx context.Context, devices ...*types.Device) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return err
}
defer tx.Rollback()
stmt, err := tx.Prepare(updateDeviceSQL)
if err != nil {
return err
}
defer stmt.Close()
for _, device := range devices {
_, err := stmt.Exec(
&device.Name,
&device.Location,
&device.CreationDate,
&device.UpdateDate,
&device.ID,
)
if err != nil {
return err
}
}
return tx.Commit()
}
// UpdateSensors updates a sensor in the database
func (sqlite *SQLite) UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
sqlite.mutex.Lock()
defer sqlite.mutex.Unlock()
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return err
}
defer tx.Rollback()
stmt, err := tx.Prepare(updateSensorSQL)
if err != nil {
return err
}
defer stmt.Close()
for _, sensor := range sensors {
_, err := stmt.Exec(
&sensor.Name,
&sensor.Location,
&sensor.WireID,
&sensor.I2CBus,
&sensor.I2CAddress,
&sensor.GPIONumber,
&sensor.Model,
&sensor.Enabled,
&sensor.TickDuration,
&sensor.DeviceID,
&sensor.CreationDate,
&sensor.UpdateDate,
&sensor.ID,
)
if err != nil {
return err
}
}
return tx.Commit()
}