fix: postgres embed - use direct sql statements instead of asset paths

This commit is contained in:
Markus Pesch 2021-04-06 19:19:08 +02:00
parent b7e7e55916
commit a48ae72b4b
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -15,55 +15,99 @@ import (
"github.com/johejo/golang-migrate-extra/source/iofs" "github.com/johejo/golang-migrate-extra/source/iofs"
) )
var (
//go:embed ddl/*.sql //go:embed ddl/*.sql
var postgresDDLAssets embed.FS postgresDDLAssets embed.FS
//go:embed dml/*.sql //go:embed dml/deleteDeviceByID.sql
var postgresDMLAssets embed.FS deleteDeviceByIDSQL string
const ( //go:embed dml/deleteDeviceByName.sql
postgresDDLAssetPath = "ddl" deleteDeviceByNameSQL string
postgresDMLAssetPath = "dml"
deleteDeviceByIDSQLFile = postgresDMLAssetPath + "/deleteDeviceByID.sql" //go:embed dml/deleteSensorByID.sql
deleteDeviceByNameSQLFile = postgresDMLAssetPath + "/deleteDeviceByName.sql" deleteSensorByIDSQL string
deleteSensorByIDSQLFile = postgresDMLAssetPath + "/deleteSensorByID.sql" //go:embed dml/deleteSensorByName.sql
deleteSensorByNameSQLFile = postgresDMLAssetPath + "/deleteSensorByName.sql" deleteSensorByNameSQL string
insertDeviceSQLFile = postgresDMLAssetPath + "/insertDevice.sql" //go:embed dml/insertDevice.sql
insertSensorSQLFile = postgresDMLAssetPath + "/insertSensor.sql" insertDeviceSQL string
insertHumiditySQLFile = postgresDMLAssetPath + "/insertHumidity.sql"
insertPressureSQLFile = postgresDMLAssetPath + "/insertPressure.sql"
insertTemperatureSQLFile = postgresDMLAssetPath + "/insertTemperature.sql"
insertOrUpdateDeviceSQLFile = postgresDMLAssetPath + "/insertOrUpdateDevice.sql" //go:embed dml/insertHumidity.sql
insertOrUpdateSensorSQLFile = postgresDMLAssetPath + "/insertOrUpdateSensor.sql" insertHumiditySQL string
insertOrUpdateHumiditySQLFile = postgresDMLAssetPath + "/insertOrUpdateHumidity.sql"
insertOrUpdatePressureSQLFile = postgresDMLAssetPath + "/insertOrUpdatePressure.sql"
insertOrUpdateTemperatureSQLFile = postgresDMLAssetPath + "/insertOrUpdateTemperature.sql"
selectDeviceByIDSQLFile = postgresDMLAssetPath + "/selectDeviceByID.sql" //go:embed dml/insertOrUpdateDevice.sql
selectDeviceByNameSQLFile = postgresDMLAssetPath + "/selectDeviceByName.sql" insertOrUpdateDeviceSQL string
selectDevicesSQLFile = postgresDMLAssetPath + "/selectDevices.sql"
selectSensorByIDSQLFile = postgresDMLAssetPath + "/selectSensorByID.sql" //go:embed dml/insertOrUpdateHumidity.sql
selectSensorsSQLFile = postgresDMLAssetPath + "/selectSensors.sql" insertOrUpdateHumiditySQL string
selectSensorsByDeviceIDSQLFile = postgresDMLAssetPath + "/selectSensorsByDeviceID.sql"
selectSensorsByModelSQLFile = postgresDMLAssetPath + "/selectSensorsByModel.sql"
selectSensorsByNameSQLFile = postgresDMLAssetPath + "/selectSensorsByName.sql"
selectHumidityByIDSQLFile = postgresDMLAssetPath + "/selectHumidityByID.sql" //go:embed dml/insertOrUpdatePressure.sql
selectHumiditiesSQLFile = postgresDMLAssetPath + "/selectHumidities.sql" insertOrUpdatePressureSQL string
selectPressureByIDSQLFile = postgresDMLAssetPath + "/selectPressureByID.sql" //go:embed dml/insertOrUpdateSensor.sql
selectPressuresSQLFile = postgresDMLAssetPath + "/selectPressures.sql" insertOrUpdateSensorSQL string
selectTemperatureByIDSQLFile = postgresDMLAssetPath + "/selectTemperatureByID.sql" //go:embed dml/insertOrUpdateTemperature.sql
selectTemperaturesSQLFile = postgresDMLAssetPath + "/selectTemperatures.sql" insertOrUpdateTemperatureSQL string
updateDeviceSQLFile = postgresDMLAssetPath + "/updateDevice.sql" //go:embed dml/insertPressure.sql
updateSensorSQLFile = postgresDMLAssetPath + "/updateSensor.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 { type Opts struct {
@ -129,12 +173,7 @@ func (postgres *Postgres) AddDevices(ctx context.Context, devices ...*types.Devi
} }
func (postgres *Postgres) insertDevices(tx *sql.Tx, devices ...*types.Device) error { func (postgres *Postgres) insertDevices(tx *sql.Tx, devices ...*types.Device) error {
f, err := postgresDMLAssets.ReadFile(insertDeviceSQLFile) stmt, err := tx.Prepare(insertDeviceSQL)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", insertDeviceSQLFile, err)
}
stmt, err := tx.Prepare(string(f))
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -162,18 +201,13 @@ func (postgres *Postgres) insertDevices(tx *sql.Tx, devices ...*types.Device) er
} }
func (postgres *Postgres) AddOrUpdateDevices(ctx context.Context, devices ...*types.Device) error { func (postgres *Postgres) AddOrUpdateDevices(ctx context.Context, devices ...*types.Device) error {
f, err := postgresDMLAssets.ReadFile(insertOrUpdateDeviceSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", insertOrUpdateDeviceSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(insertOrUpdateDeviceSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %w", err) return fmt.Errorf("Failed to prepare statement: %w", err)
} }
@ -218,13 +252,8 @@ func (postgres *Postgres) AddMeasuredValues(ctx context.Context, measuredValues
defer tx.Rollback() defer tx.Rollback()
// General insert function // General insert function
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error { insert := func(tx *sql.Tx, query string, measuredValues []*types.MeasuredValue) error {
f, err := postgresDMLAssets.ReadFile(queryFile) stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", queryFile, err)
}
stmt, err := tx.Prepare(string(f))
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -258,11 +287,11 @@ func (postgres *Postgres) AddMeasuredValues(ctx context.Context, measuredValues
switch measuredValueType { switch measuredValueType {
case types.Humidity: case types.Humidity:
queryFile = insertHumiditySQLFile queryFile = insertHumiditySQL
case types.Pressure: case types.Pressure:
queryFile = insertPressureSQLFile queryFile = insertPressureSQL
case types.Temperature: case types.Temperature:
queryFile = insertTemperatureSQLFile queryFile = insertTemperatureSQL
default: default:
return fmt.Errorf("Measured value type %v not supported", measuredValueType) return fmt.Errorf("Measured value type %v not supported", measuredValueType)
} }
@ -294,13 +323,8 @@ func (postgres *Postgres) AddOrUpdateMeasuredValues(ctx context.Context, measure
defer tx.Rollback() defer tx.Rollback()
// General insert function // General insert function
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error { insert := func(tx *sql.Tx, query string, measuredValues []*types.MeasuredValue) error {
f, err := postgresDMLAssets.ReadFile(queryFile) stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", queryFile, err)
}
stmt, err := tx.Prepare(string(f))
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -334,11 +358,11 @@ func (postgres *Postgres) AddOrUpdateMeasuredValues(ctx context.Context, measure
switch measuredValueType { switch measuredValueType {
case types.Humidity: case types.Humidity:
queryFile = insertOrUpdateHumiditySQLFile queryFile = insertOrUpdateHumiditySQL
case types.Pressure: case types.Pressure:
queryFile = insertOrUpdatePressureSQLFile queryFile = insertOrUpdatePressureSQL
case types.Temperature: case types.Temperature:
queryFile = insertOrUpdateTemperatureSQLFile queryFile = insertOrUpdateTemperatureSQL
default: default:
return fmt.Errorf("Measured value type %v not supported", measuredValueType) return fmt.Errorf("Measured value type %v not supported", measuredValueType)
} }
@ -354,18 +378,13 @@ func (postgres *Postgres) AddOrUpdateMeasuredValues(ctx context.Context, measure
// AddSensors into the database // AddSensors into the database
func (postgres *Postgres) AddSensors(ctx context.Context, sensors ...*types.Sensor) error { func (postgres *Postgres) AddSensors(ctx context.Context, sensors ...*types.Sensor) error {
f, err := postgresDMLAssets.ReadFile(insertSensorSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", insertSensorSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(insertSensorSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -402,18 +421,13 @@ func (postgres *Postgres) AddSensors(ctx context.Context, sensors ...*types.Sens
// AddOrUpdateSensors into the database // AddOrUpdateSensors into the database
func (postgres *Postgres) AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sensor) error { func (postgres *Postgres) AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
f, err := postgresDMLAssets.ReadFile(insertOrUpdateSensorSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", insertOrUpdateSensorSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(insertOrUpdateSensorSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -456,7 +470,7 @@ func (postgres *Postgres) Close() error {
// Migrate creates all required tables if not exist // Migrate creates all required tables if not exist
func (postgres *Postgres) Migrate(ctx context.Context) error { func (postgres *Postgres) Migrate(ctx context.Context) error {
sourceDriver, err := iofs.New(postgresDDLAssets, postgresDDLAssetPath) sourceDriver, err := iofs.New(postgresDDLAssets, "ddl")
if err != nil { if err != nil {
return err return err
} }
@ -477,18 +491,13 @@ func (postgres *Postgres) Migrate(ctx context.Context) error {
// GetDevice from database // GetDevice from database
func (postgres *Postgres) GetDeviceByID(ctx context.Context, id string) (*types.Device, error) { func (postgres *Postgres) GetDeviceByID(ctx context.Context, id string) (*types.Device, error) {
f, err := postgresDMLAssets.ReadFile(selectDeviceByIDSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectDeviceByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
devices, err := postgres.selectDevices(tx, string(f), id) devices, err := postgres.selectDevices(tx, selectDeviceByIDSQL, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -507,18 +516,13 @@ func (postgres *Postgres) GetDeviceByID(ctx context.Context, id string) (*types.
// GetDevice from database // GetDevice from database
func (postgres *Postgres) GetDeviceByName(ctx context.Context, name string) (*types.Device, error) { func (postgres *Postgres) GetDeviceByName(ctx context.Context, name string) (*types.Device, error) {
f, err := postgresDMLAssets.ReadFile(selectDeviceByNameSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectDeviceByNameSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
devices, err := postgres.selectDevices(tx, string(f), name) devices, err := postgres.selectDevices(tx, selectDeviceByNameSQL, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -537,18 +541,13 @@ func (postgres *Postgres) GetDeviceByName(ctx context.Context, name string) (*ty
// GetDevices from the database // GetDevices from the database
func (postgres *Postgres) GetDevices(ctx context.Context) ([]*types.Device, error) { func (postgres *Postgres) GetDevices(ctx context.Context) ([]*types.Device, error) {
f, err := postgresDMLAssets.ReadFile(selectDevicesSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectDevicesSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
devices, err := postgres.selectDevices(tx, string(f)) devices, err := postgres.selectDevices(tx, selectDevicesSQL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -594,18 +593,13 @@ func (postgres *Postgres) selectDevices(tx *sql.Tx, query string, args ...interf
// GetHumidity returns humidity from the database // GetHumidity returns humidity from the database
func (postgres *Postgres) GetHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error) { func (postgres *Postgres) GetHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
f, err := postgresDMLAssets.ReadFile(selectHumidityByIDSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectHumidityByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
measuredValues, err := postgres.selectMeasuredValue(tx, string(f), id) measuredValues, err := postgres.selectMeasuredValue(tx, selectHumidityByIDSQL, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -628,18 +622,13 @@ func (postgres *Postgres) GetHumidityByID(ctx context.Context, id string) (*type
// GetHumidities returns humidities from the database // GetHumidities returns humidities from the database
func (postgres *Postgres) GetHumidities(ctx context.Context) ([]*types.MeasuredValue, error) { func (postgres *Postgres) GetHumidities(ctx context.Context) ([]*types.MeasuredValue, error) {
f, err := postgresDMLAssets.ReadFile(selectHumiditiesSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectHumiditiesSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
measuredValues, err := postgres.selectMeasuredValue(tx, string(f)) measuredValues, err := postgres.selectMeasuredValue(tx, selectHumiditiesSQL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -692,18 +681,13 @@ func (postgres *Postgres) selectMeasuredValue(tx *sql.Tx, query string, args ...
// GetPressure returns pressure from the database // GetPressure returns pressure from the database
func (postgres *Postgres) GetPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error) { func (postgres *Postgres) GetPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
f, err := postgresDMLAssets.ReadFile(selectPressureByIDSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectPressureByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
measuredValues, err := postgres.selectMeasuredValue(tx, string(f), id) measuredValues, err := postgres.selectMeasuredValue(tx, selectPressureByIDSQL, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -726,18 +710,13 @@ func (postgres *Postgres) GetPressureByID(ctx context.Context, id string) (*type
// GetPressures returns pressure from the database // GetPressures returns pressure from the database
func (postgres *Postgres) GetPressures(ctx context.Context) ([]*types.MeasuredValue, error) { func (postgres *Postgres) GetPressures(ctx context.Context) ([]*types.MeasuredValue, error) {
f, err := postgresDMLAssets.ReadFile(selectPressuresSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectPressuresSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
measuredValues, err := postgres.selectMeasuredValue(tx, string(f)) measuredValues, err := postgres.selectMeasuredValue(tx, selectPressuresSQL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -756,18 +735,13 @@ func (postgres *Postgres) GetPressures(ctx context.Context) ([]*types.MeasuredVa
// GetSensor from database // GetSensor from database
func (postgres *Postgres) GetSensorByID(ctx context.Context, id string) (*types.Sensor, error) { func (postgres *Postgres) GetSensorByID(ctx context.Context, id string) (*types.Sensor, error) {
f, err := postgresDMLAssets.ReadFile(selectSensorByIDSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
sensors, err := postgres.selectSensors(tx, string(f), id) sensors, err := postgres.selectSensors(tx, selectSensorByIDSQL, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -786,18 +760,13 @@ func (postgres *Postgres) GetSensorByID(ctx context.Context, id string) (*types.
// GetSensors from the database // GetSensors from the database
func (postgres *Postgres) GetSensors(ctx context.Context) ([]*types.Sensor, error) { func (postgres *Postgres) GetSensors(ctx context.Context) ([]*types.Sensor, error) {
f, err := postgresDMLAssets.ReadFile(selectSensorsSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
sensors, err := postgres.selectSensors(tx, string(f)) sensors, err := postgres.selectSensors(tx, selectSensorsSQL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -812,11 +781,6 @@ func (postgres *Postgres) GetSensors(ctx context.Context) ([]*types.Sensor, erro
// GetSensorsByModels from the database // GetSensorsByModels from the database
func (postgres *Postgres) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string) ([]*types.Sensor, error) { func (postgres *Postgres) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string) ([]*types.Sensor, error) {
f, err := postgresDMLAssets.ReadFile(selectSensorsByDeviceIDSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsByDeviceIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
@ -825,7 +789,7 @@ func (postgres *Postgres) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs .
cachedSensors := make([]*types.Sensor, 0) cachedSensors := make([]*types.Sensor, 0)
for i := range deviceIDs { for i := range deviceIDs {
sensors, err := postgres.selectSensors(tx, string(f), deviceIDs[i]) sensors, err := postgres.selectSensors(tx, selectSensorsByDeviceIDSQL, deviceIDs[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -843,11 +807,6 @@ func (postgres *Postgres) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs .
// GetSensorsByModel from the database // GetSensorsByModel from the database
func (postgres *Postgres) GetSensorsByModels(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error) { func (postgres *Postgres) GetSensorsByModels(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error) {
f, err := postgresDMLAssets.ReadFile(selectSensorsByModelSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsByModelSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
@ -855,7 +814,7 @@ func (postgres *Postgres) GetSensorsByModels(ctx context.Context, sensorModels .
cachedSensors := make([]*types.Sensor, 0) cachedSensors := make([]*types.Sensor, 0)
for i := range sensorModels { for i := range sensorModels {
sensors, err := postgres.selectSensors(tx, string(f), sensorModels[i]) sensors, err := postgres.selectSensors(tx, selectSensorsByModelSQL, sensorModels[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -873,11 +832,6 @@ func (postgres *Postgres) GetSensorsByModels(ctx context.Context, sensorModels .
// GetSensorsByModel from the database // GetSensorsByModel from the database
func (postgres *Postgres) GetSensorsByNames(ctx context.Context, sensorNames ...string) ([]*types.Sensor, error) { func (postgres *Postgres) GetSensorsByNames(ctx context.Context, sensorNames ...string) ([]*types.Sensor, error) {
f, err := postgresDMLAssets.ReadFile(selectSensorsByNameSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsByNameSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err) return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
@ -886,7 +840,7 @@ func (postgres *Postgres) GetSensorsByNames(ctx context.Context, sensorNames ...
cachedSensors := make([]*types.Sensor, 0) cachedSensors := make([]*types.Sensor, 0)
for i := range sensorNames { for i := range sensorNames {
sensors, err := postgres.selectSensors(tx, string(f), sensorNames[i]) sensors, err := postgres.selectSensors(tx, selectSensorsByNameSQL, sensorNames[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -944,18 +898,13 @@ func (postgres *Postgres) selectSensors(tx *sql.Tx, query string, args ...interf
// GetTemperature returns temperatures from the database // GetTemperature returns temperatures from the database
func (postgres *Postgres) GetTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error) { func (postgres *Postgres) GetTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
f, err := postgresDMLAssets.ReadFile(selectTemperatureByIDSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectTemperatureByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
measuredValues, err := postgres.selectMeasuredValue(tx, string(f), id) measuredValues, err := postgres.selectMeasuredValue(tx, selectTemperatureByIDSQL, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -978,18 +927,13 @@ func (postgres *Postgres) GetTemperatureByID(ctx context.Context, id string) (*t
// GetTemperatures returns temperatures from the database // GetTemperatures returns temperatures from the database
func (postgres *Postgres) GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, error) { func (postgres *Postgres) GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, error) {
f, err := postgresDMLAssets.ReadFile(selectTemperaturesSQLFile)
if err != nil {
return nil, fmt.Errorf("Failed to read file %v: %w", selectTemperaturesSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
measuredValues, err := postgres.selectMeasuredValue(tx, string(f)) measuredValues, err := postgres.selectMeasuredValue(tx, selectTemperaturesSQL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1008,18 +952,13 @@ func (postgres *Postgres) GetTemperatures(ctx context.Context) ([]*types.Measure
// RemoveDevices from the database // RemoveDevices from the database
func (postgres *Postgres) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) error { func (postgres *Postgres) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) error {
f, err := postgresDMLAssets.ReadFile(deleteDeviceByIDSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", deleteDeviceByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(deleteDeviceByIDSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -1037,18 +976,13 @@ func (postgres *Postgres) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...s
// RemoveDevices from the database // RemoveDevices from the database
func (postgres *Postgres) RemoveDevicesByNames(ctx context.Context, names ...string) error { func (postgres *Postgres) RemoveDevicesByNames(ctx context.Context, names ...string) error {
f, err := postgresDMLAssets.ReadFile(deleteDeviceByNameSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", deleteDeviceByNameSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(deleteDeviceByNameSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -1066,18 +1000,13 @@ func (postgres *Postgres) RemoveDevicesByNames(ctx context.Context, names ...str
// RemoveSensors from the database // RemoveSensors from the database
func (postgres *Postgres) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) error { func (postgres *Postgres) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) error {
f, err := postgresDMLAssets.ReadFile(deleteSensorByIDSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", deleteSensorByIDSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(deleteSensorByIDSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -1095,18 +1024,13 @@ func (postgres *Postgres) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...s
// RemoveSensors from the database // RemoveSensors from the database
func (postgres *Postgres) RemoveSensorsByNames(ctx context.Context, sensorIDs ...string) error { func (postgres *Postgres) RemoveSensorsByNames(ctx context.Context, sensorIDs ...string) error {
f, err := postgresDMLAssets.ReadFile(deleteSensorByNameSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", deleteSensorByNameSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err) return fmt.Errorf("Failed to begin new transaction: %v", err)
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(deleteSensorByNameSQL)
if err != nil { if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err) return fmt.Errorf("Failed to prepare statement: %v", err)
} }
@ -1124,18 +1048,13 @@ func (postgres *Postgres) RemoveSensorsByNames(ctx context.Context, sensorIDs ..
// UpdateDevices updates a device in the database // UpdateDevices updates a device in the database
func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.Device) error { func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.Device) error {
f, err := postgresDMLAssets.ReadFile(updateDeviceSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", updateDeviceSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return err return err
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(updateDeviceSQL)
if err != nil { if err != nil {
return err return err
} }
@ -1162,18 +1081,13 @@ func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.D
// UpdateSensors updates a sensor in the database // UpdateSensors updates a sensor in the database
func (postgres *Postgres) UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error { func (postgres *Postgres) UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
f, err := postgresDMLAssets.ReadFile(updateSensorSQLFile)
if err != nil {
return fmt.Errorf("Failed to read file %v: %w", updateSensorSQLFile, err)
}
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false}) tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil { if err != nil {
return err return err
} }
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(string(f)) stmt, err := tx.Prepare(updateSensorSQL)
if err != nil { if err != nil {
return err return err
} }