From a48ae72b4bae26021de76d3cd346ad2415e4bafc Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Tue, 6 Apr 2021 19:19:08 +0200 Subject: [PATCH] fix: postgres embed - use direct sql statements instead of asset paths --- pkg/repository/postgres/postgres.go | 318 ++++++++++------------------ 1 file changed, 116 insertions(+), 202 deletions(-) diff --git a/pkg/repository/postgres/postgres.go b/pkg/repository/postgres/postgres.go index c959e13..f9ffda9 100644 --- a/pkg/repository/postgres/postgres.go +++ b/pkg/repository/postgres/postgres.go @@ -15,55 +15,99 @@ import ( "github.com/johejo/golang-migrate-extra/source/iofs" ) -//go:embed ddl/*.sql -var postgresDDLAssets embed.FS +var ( + //go:embed ddl/*.sql + postgresDDLAssets embed.FS -//go:embed dml/*.sql -var postgresDMLAssets embed.FS + //go:embed dml/deleteDeviceByID.sql + deleteDeviceByIDSQL string -const ( - postgresDDLAssetPath = "ddl" - postgresDMLAssetPath = "dml" + //go:embed dml/deleteDeviceByName.sql + deleteDeviceByNameSQL string - deleteDeviceByIDSQLFile = postgresDMLAssetPath + "/deleteDeviceByID.sql" - deleteDeviceByNameSQLFile = postgresDMLAssetPath + "/deleteDeviceByName.sql" + //go:embed dml/deleteSensorByID.sql + deleteSensorByIDSQL string - deleteSensorByIDSQLFile = postgresDMLAssetPath + "/deleteSensorByID.sql" - deleteSensorByNameSQLFile = postgresDMLAssetPath + "/deleteSensorByName.sql" + //go:embed dml/deleteSensorByName.sql + deleteSensorByNameSQL string - insertDeviceSQLFile = postgresDMLAssetPath + "/insertDevice.sql" - insertSensorSQLFile = postgresDMLAssetPath + "/insertSensor.sql" - insertHumiditySQLFile = postgresDMLAssetPath + "/insertHumidity.sql" - insertPressureSQLFile = postgresDMLAssetPath + "/insertPressure.sql" - insertTemperatureSQLFile = postgresDMLAssetPath + "/insertTemperature.sql" + //go:embed dml/insertDevice.sql + insertDeviceSQL string - insertOrUpdateDeviceSQLFile = postgresDMLAssetPath + "/insertOrUpdateDevice.sql" - insertOrUpdateSensorSQLFile = postgresDMLAssetPath + "/insertOrUpdateSensor.sql" - insertOrUpdateHumiditySQLFile = postgresDMLAssetPath + "/insertOrUpdateHumidity.sql" - insertOrUpdatePressureSQLFile = postgresDMLAssetPath + "/insertOrUpdatePressure.sql" - insertOrUpdateTemperatureSQLFile = postgresDMLAssetPath + "/insertOrUpdateTemperature.sql" + //go:embed dml/insertHumidity.sql + insertHumiditySQL string - selectDeviceByIDSQLFile = postgresDMLAssetPath + "/selectDeviceByID.sql" - selectDeviceByNameSQLFile = postgresDMLAssetPath + "/selectDeviceByName.sql" - selectDevicesSQLFile = postgresDMLAssetPath + "/selectDevices.sql" + //go:embed dml/insertOrUpdateDevice.sql + insertOrUpdateDeviceSQL string - selectSensorByIDSQLFile = postgresDMLAssetPath + "/selectSensorByID.sql" - selectSensorsSQLFile = postgresDMLAssetPath + "/selectSensors.sql" - selectSensorsByDeviceIDSQLFile = postgresDMLAssetPath + "/selectSensorsByDeviceID.sql" - selectSensorsByModelSQLFile = postgresDMLAssetPath + "/selectSensorsByModel.sql" - selectSensorsByNameSQLFile = postgresDMLAssetPath + "/selectSensorsByName.sql" + //go:embed dml/insertOrUpdateHumidity.sql + insertOrUpdateHumiditySQL string - selectHumidityByIDSQLFile = postgresDMLAssetPath + "/selectHumidityByID.sql" - selectHumiditiesSQLFile = postgresDMLAssetPath + "/selectHumidities.sql" + //go:embed dml/insertOrUpdatePressure.sql + insertOrUpdatePressureSQL string - selectPressureByIDSQLFile = postgresDMLAssetPath + "/selectPressureByID.sql" - selectPressuresSQLFile = postgresDMLAssetPath + "/selectPressures.sql" + //go:embed dml/insertOrUpdateSensor.sql + insertOrUpdateSensorSQL string - selectTemperatureByIDSQLFile = postgresDMLAssetPath + "/selectTemperatureByID.sql" - selectTemperaturesSQLFile = postgresDMLAssetPath + "/selectTemperatures.sql" + //go:embed dml/insertOrUpdateTemperature.sql + insertOrUpdateTemperatureSQL string - updateDeviceSQLFile = postgresDMLAssetPath + "/updateDevice.sql" - updateSensorSQLFile = postgresDMLAssetPath + "/updateSensor.sql" + //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 { @@ -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 { - f, err := postgresDMLAssets.ReadFile(insertDeviceSQLFile) - if err != nil { - return fmt.Errorf("Failed to read file %v: %w", insertDeviceSQLFile, err) - } - - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(insertDeviceSQL) if err != nil { 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 { - 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(insertOrUpdateDeviceSQL) if err != nil { return fmt.Errorf("Failed to prepare statement: %w", err) } @@ -218,13 +252,8 @@ func (postgres *Postgres) AddMeasuredValues(ctx context.Context, measuredValues defer tx.Rollback() // General insert function - insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error { - f, err := postgresDMLAssets.ReadFile(queryFile) - if err != nil { - return fmt.Errorf("Failed to read file %v: %w", queryFile, err) - } - - stmt, err := tx.Prepare(string(f)) + 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) } @@ -258,11 +287,11 @@ func (postgres *Postgres) AddMeasuredValues(ctx context.Context, measuredValues switch measuredValueType { case types.Humidity: - queryFile = insertHumiditySQLFile + queryFile = insertHumiditySQL case types.Pressure: - queryFile = insertPressureSQLFile + queryFile = insertPressureSQL case types.Temperature: - queryFile = insertTemperatureSQLFile + queryFile = insertTemperatureSQL default: 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() // General insert function - insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error { - f, err := postgresDMLAssets.ReadFile(queryFile) - if err != nil { - return fmt.Errorf("Failed to read file %v: %w", queryFile, err) - } - - stmt, err := tx.Prepare(string(f)) + 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) } @@ -334,11 +358,11 @@ func (postgres *Postgres) AddOrUpdateMeasuredValues(ctx context.Context, measure switch measuredValueType { case types.Humidity: - queryFile = insertOrUpdateHumiditySQLFile + queryFile = insertOrUpdateHumiditySQL case types.Pressure: - queryFile = insertOrUpdatePressureSQLFile + queryFile = insertOrUpdatePressureSQL case types.Temperature: - queryFile = insertOrUpdateTemperatureSQLFile + queryFile = insertOrUpdateTemperatureSQL default: 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 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(insertSensorSQL) if err != nil { 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 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(insertOrUpdateSensorSQL) if err != nil { 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 func (postgres *Postgres) Migrate(ctx context.Context) error { - sourceDriver, err := iofs.New(postgresDDLAssets, postgresDDLAssetPath) + sourceDriver, err := iofs.New(postgresDDLAssets, "ddl") if err != nil { return err } @@ -477,18 +491,13 @@ func (postgres *Postgres) Migrate(ctx context.Context) error { // GetDevice from database 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}) if err != nil { return nil, fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - devices, err := postgres.selectDevices(tx, string(f), id) + devices, err := postgres.selectDevices(tx, selectDeviceByIDSQL, id) if err != nil { return nil, err } @@ -507,18 +516,13 @@ func (postgres *Postgres) GetDeviceByID(ctx context.Context, id string) (*types. // GetDevice from database 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}) if err != nil { return nil, fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - devices, err := postgres.selectDevices(tx, string(f), name) + devices, err := postgres.selectDevices(tx, selectDeviceByNameSQL, name) if err != nil { return nil, err } @@ -537,18 +541,13 @@ func (postgres *Postgres) GetDeviceByName(ctx context.Context, name string) (*ty // GetDevices from the database 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}) if err != nil { return nil, fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - devices, err := postgres.selectDevices(tx, string(f)) + devices, err := postgres.selectDevices(tx, selectDevicesSQL) if err != nil { return nil, err } @@ -594,18 +593,13 @@ func (postgres *Postgres) selectDevices(tx *sql.Tx, query string, args ...interf // GetHumidity returns humidity from the database 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}) if err != nil { return nil, err } defer tx.Rollback() - measuredValues, err := postgres.selectMeasuredValue(tx, string(f), id) + measuredValues, err := postgres.selectMeasuredValue(tx, selectHumidityByIDSQL, id) if err != nil { return nil, err } @@ -628,18 +622,13 @@ func (postgres *Postgres) GetHumidityByID(ctx context.Context, id string) (*type // GetHumidities returns humidities from the database 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}) if err != nil { return nil, err } defer tx.Rollback() - measuredValues, err := postgres.selectMeasuredValue(tx, string(f)) + measuredValues, err := postgres.selectMeasuredValue(tx, selectHumiditiesSQL) if err != nil { return nil, err } @@ -692,18 +681,13 @@ func (postgres *Postgres) selectMeasuredValue(tx *sql.Tx, query string, args ... // GetPressure returns pressure from the database 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}) if err != nil { return nil, err } defer tx.Rollback() - measuredValues, err := postgres.selectMeasuredValue(tx, string(f), id) + measuredValues, err := postgres.selectMeasuredValue(tx, selectPressureByIDSQL, id) if err != nil { return nil, err } @@ -726,18 +710,13 @@ func (postgres *Postgres) GetPressureByID(ctx context.Context, id string) (*type // GetPressures returns pressure from the database 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}) if err != nil { return nil, err } defer tx.Rollback() - measuredValues, err := postgres.selectMeasuredValue(tx, string(f)) + measuredValues, err := postgres.selectMeasuredValue(tx, selectPressuresSQL) if err != nil { return nil, err } @@ -756,18 +735,13 @@ func (postgres *Postgres) GetPressures(ctx context.Context) ([]*types.MeasuredVa // GetSensor from database 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}) if err != nil { return nil, fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - sensors, err := postgres.selectSensors(tx, string(f), id) + sensors, err := postgres.selectSensors(tx, selectSensorByIDSQL, id) if err != nil { return nil, err } @@ -786,18 +760,13 @@ func (postgres *Postgres) GetSensorByID(ctx context.Context, id string) (*types. // GetSensors from the database 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}) if err != nil { return nil, fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - sensors, err := postgres.selectSensors(tx, string(f)) + sensors, err := postgres.selectSensors(tx, selectSensorsSQL) if err != nil { return nil, err } @@ -812,11 +781,6 @@ func (postgres *Postgres) GetSensors(ctx context.Context) ([]*types.Sensor, erro // GetSensorsByModels from the database 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}) if err != nil { 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) for i := range deviceIDs { - sensors, err := postgres.selectSensors(tx, string(f), deviceIDs[i]) + sensors, err := postgres.selectSensors(tx, selectSensorsByDeviceIDSQL, deviceIDs[i]) if err != nil { return nil, err } @@ -843,11 +807,6 @@ func (postgres *Postgres) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs . // GetSensorsByModel from the database 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}) if err != nil { 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) for i := range sensorModels { - sensors, err := postgres.selectSensors(tx, string(f), sensorModels[i]) + sensors, err := postgres.selectSensors(tx, selectSensorsByModelSQL, sensorModels[i]) if err != nil { return nil, err } @@ -873,11 +832,6 @@ func (postgres *Postgres) GetSensorsByModels(ctx context.Context, sensorModels . // GetSensorsByModel from the database 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}) if err != nil { 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) for i := range sensorNames { - sensors, err := postgres.selectSensors(tx, string(f), sensorNames[i]) + sensors, err := postgres.selectSensors(tx, selectSensorsByNameSQL, sensorNames[i]) if err != nil { return nil, err } @@ -944,18 +898,13 @@ func (postgres *Postgres) selectSensors(tx *sql.Tx, query string, args ...interf // GetTemperature returns temperatures from the database 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}) if err != nil { return nil, err } defer tx.Rollback() - measuredValues, err := postgres.selectMeasuredValue(tx, string(f), id) + measuredValues, err := postgres.selectMeasuredValue(tx, selectTemperatureByIDSQL, id) if err != nil { return nil, err } @@ -978,18 +927,13 @@ func (postgres *Postgres) GetTemperatureByID(ctx context.Context, id string) (*t // GetTemperatures returns temperatures from the database 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}) if err != nil { return nil, err } defer tx.Rollback() - measuredValues, err := postgres.selectMeasuredValue(tx, string(f)) + measuredValues, err := postgres.selectMeasuredValue(tx, selectTemperaturesSQL) if err != nil { return nil, err } @@ -1008,18 +952,13 @@ func (postgres *Postgres) GetTemperatures(ctx context.Context) ([]*types.Measure // RemoveDevices from the database 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(deleteDeviceByIDSQL) if err != nil { 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 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(deleteDeviceByNameSQL) if err != nil { 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 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(deleteSensorByIDSQL) if err != nil { 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 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}) if err != nil { return fmt.Errorf("Failed to begin new transaction: %v", err) } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(deleteSensorByNameSQL) if err != nil { 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 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}) if err != nil { return err } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(updateDeviceSQL) if err != nil { return err } @@ -1162,18 +1081,13 @@ func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.D // UpdateSensors updates a sensor in the database 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}) if err != nil { return err } defer tx.Rollback() - stmt, err := tx.Prepare(string(f)) + stmt, err := tx.Prepare(updateSensorSQL) if err != nil { return err }