fix: sqlite3 embed - use direct sql statements instead of asset paths
This commit is contained in:
parent
53d4a78adc
commit
b7e7e55916
@ -17,55 +17,99 @@ import (
|
||||
"github.com/johejo/golang-migrate-extra/source/iofs"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed ddl/*.sql
|
||||
var sqlite3DDLAssets embed.FS
|
||||
sqlite3DDLAssets embed.FS
|
||||
|
||||
//go:embed dml/*.sql
|
||||
var sqlite3DMLAssets embed.FS
|
||||
//go:embed dml/deleteDeviceByID.sql
|
||||
deleteDeviceByIDSQL string
|
||||
|
||||
const (
|
||||
sqlite3DDLAssetPath = "ddl"
|
||||
sqlite3DMLAssetPath = "dml"
|
||||
//go:embed dml/deleteDeviceByName.sql
|
||||
deleteDeviceByNameSQL string
|
||||
|
||||
deleteDeviceByIDSQLFile = sqlite3DMLAssetPath + "/deleteDeviceByID.sql"
|
||||
deleteDeviceByNameSQLFile = sqlite3DMLAssetPath + "/deleteDeviceByName.sql"
|
||||
//go:embed dml/deleteSensorByID.sql
|
||||
deleteSensorByIDSQL string
|
||||
|
||||
deleteSensorByIDSQLFile = sqlite3DMLAssetPath + "/deleteSensorByID.sql"
|
||||
deleteSensorByNameSQLFile = sqlite3DMLAssetPath + "/deleteSensorByName.sql"
|
||||
//go:embed dml/deleteSensorByName.sql
|
||||
deleteSensorByNameSQL string
|
||||
|
||||
insertDeviceSQLFile = sqlite3DMLAssetPath + "/insertDevice.sql"
|
||||
insertSensorSQLFile = sqlite3DMLAssetPath + "/insertSensor.sql"
|
||||
insertHumiditySQLFile = sqlite3DMLAssetPath + "/insertHumidity.sql"
|
||||
insertPressureSQLFile = sqlite3DMLAssetPath + "/insertPressure.sql"
|
||||
insertTemperatureSQLFile = sqlite3DMLAssetPath + "/insertTemperature.sql"
|
||||
//go:embed dml/insertDevice.sql
|
||||
insertDeviceSQL string
|
||||
|
||||
insertOrUpdateDeviceSQLFile = sqlite3DMLAssetPath + "/insertOrUpdateDevice.sql"
|
||||
insertOrUpdateSensorSQLFile = sqlite3DMLAssetPath + "/insertOrUpdateSensor.sql"
|
||||
insertOrUpdateHumiditySQLFile = sqlite3DMLAssetPath + "/insertOrUpdateHumidity.sql"
|
||||
insertOrUpdatePressureSQLFile = sqlite3DMLAssetPath + "/insertOrUpdatePressure.sql"
|
||||
insertOrUpdateTemperatureSQLFile = sqlite3DMLAssetPath + "/insertOrUpdateTemperature.sql"
|
||||
//go:embed dml/insertHumidity.sql
|
||||
insertHumiditySQL string
|
||||
|
||||
selectDeviceByIDSQLFile = sqlite3DMLAssetPath + "/selectDeviceByID.sql"
|
||||
selectDeviceByNameSQLFile = sqlite3DMLAssetPath + "/selectDeviceByName.sql"
|
||||
selectDevicesSQLFile = sqlite3DMLAssetPath + "/selectDevices.sql"
|
||||
//go:embed dml/insertOrUpdateDevice.sql
|
||||
insertOrUpdateDeviceSQL string
|
||||
|
||||
selectSensorByIDSQLFile = sqlite3DMLAssetPath + "/selectSensorByID.sql"
|
||||
selectSensorsSQLFile = sqlite3DMLAssetPath + "/selectSensors.sql"
|
||||
selectSensorsByDeviceIDSQLFile = sqlite3DMLAssetPath + "/selectSensorsByDeviceID.sql"
|
||||
selectSensorsByModelSQLFile = sqlite3DMLAssetPath + "/selectSensorsByModel.sql"
|
||||
selectSensorsByNameSQLFile = sqlite3DMLAssetPath + "/selectSensorsByName.sql"
|
||||
//go:embed dml/insertOrUpdateHumidity.sql
|
||||
insertOrUpdateHumiditySQL string
|
||||
|
||||
selectHumidityByIDSQLFile = sqlite3DMLAssetPath + "/selectHumidityByID.sql"
|
||||
selectHumiditiesSQLFile = sqlite3DMLAssetPath + "/selectHumidities.sql"
|
||||
//go:embed dml/insertOrUpdatePressure.sql
|
||||
insertOrUpdatePressureSQL string
|
||||
|
||||
selectPressureByIDSQLFile = sqlite3DMLAssetPath + "/selectPressureByID.sql"
|
||||
selectPressuresSQLFile = sqlite3DMLAssetPath + "/selectPressures.sql"
|
||||
//go:embed dml/insertOrUpdateSensor.sql
|
||||
insertOrUpdateSensorSQL string
|
||||
|
||||
selectTemperatureByIDSQLFile = sqlite3DMLAssetPath + "/selectTemperatureByID.sql"
|
||||
selectTemperaturesSQLFile = sqlite3DMLAssetPath + "/selectTemperatures.sql"
|
||||
//go:embed dml/insertOrUpdateTemperature.sql
|
||||
insertOrUpdateTemperatureSQL string
|
||||
|
||||
updateDeviceSQLFile = sqlite3DMLAssetPath + "/updateDevice.sql"
|
||||
updateSensorSQLFile = sqlite3DMLAssetPath + "/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 {
|
||||
@ -135,18 +179,13 @@ func (sqlite *SQLite) AddDevices(ctx context.Context, devices ...*types.Device)
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(insertDeviceSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", insertDeviceSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(insertDeviceSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -167,18 +206,13 @@ func (sqlite *SQLite) AddOrUpdateDevices(ctx context.Context, devices ...*types.
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(insertOrUpdateDeviceSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", insertOrUpdateDeviceSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(insertOrUpdateDeviceSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -215,13 +249,8 @@ func (sqlite *SQLite) AddMeasuredValues(ctx context.Context, measuredValues ...*
|
||||
defer tx.Rollback()
|
||||
|
||||
// General insert function
|
||||
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error {
|
||||
f, err := sqlite3DMLAssets.ReadFile(queryFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", deleteDeviceByIDSQLFile, 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)
|
||||
}
|
||||
@ -246,20 +275,20 @@ func (sqlite *SQLite) AddMeasuredValues(ctx context.Context, measuredValues ...*
|
||||
}
|
||||
|
||||
for measuredValueType, measuredValues := range splittedMeasuredValues {
|
||||
var queryFile string
|
||||
var query string
|
||||
|
||||
switch measuredValueType {
|
||||
case types.Humidity:
|
||||
queryFile = insertHumiditySQLFile
|
||||
query = insertHumiditySQL
|
||||
case types.Pressure:
|
||||
queryFile = insertPressureSQLFile
|
||||
query = insertPressureSQL
|
||||
case types.Temperature:
|
||||
queryFile = insertTemperatureSQLFile
|
||||
query = insertTemperatureSQL
|
||||
default:
|
||||
return fmt.Errorf("Measured value type %v not supported", measuredValueType)
|
||||
}
|
||||
|
||||
err := insert(tx, queryFile, measuredValues)
|
||||
err := insert(tx, query, measuredValues)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -289,13 +318,9 @@ func (sqlite *SQLite) AddOrUpdateMeasuredValues(ctx context.Context, measuredVal
|
||||
defer tx.Rollback()
|
||||
|
||||
// General insert function
|
||||
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error {
|
||||
f, err := sqlite3DMLAssets.ReadFile(queryFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", queryFile, err)
|
||||
}
|
||||
insert := func(tx *sql.Tx, query string, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
stmt, err := tx.Prepare(string(f))
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -324,11 +349,11 @@ func (sqlite *SQLite) AddOrUpdateMeasuredValues(ctx context.Context, measuredVal
|
||||
|
||||
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)
|
||||
}
|
||||
@ -347,18 +372,13 @@ func (sqlite *SQLite) AddSensors(ctx context.Context, sensors ...*types.Sensor)
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(insertSensorSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", insertSensorSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(insertSensorSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -393,18 +413,13 @@ func (sqlite *SQLite) AddOrUpdateSensors(ctx context.Context, sensors ...*types.
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(insertOrUpdateSensorSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", insertOrUpdateSensorSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(insertOrUpdateSensorSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -447,7 +462,7 @@ func (sqlite *SQLite) Migrate(ctx context.Context) error {
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
sourceDriver, err := iofs.New(sqlite3DDLAssets, sqlite3DDLAssetPath)
|
||||
sourceDriver, err := iofs.New(sqlite3DDLAssets, "ddl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -471,18 +486,13 @@ func (sqlite *SQLite) GetDeviceByID(ctx context.Context, id string) (*types.Devi
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectDeviceByIDSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectDeviceByIDSQLFile, err)
|
||||
}
|
||||
|
||||
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, string(f), id)
|
||||
devices, err := sqlite.selectDevices(tx, selectDeviceByIDSQL, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -504,18 +514,13 @@ func (sqlite *SQLite) GetDeviceByName(ctx context.Context, name string) (*types.
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectDeviceByNameSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectDeviceByNameSQLFile, err)
|
||||
}
|
||||
|
||||
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, string(f), name)
|
||||
devices, err := sqlite.selectDevices(tx, selectDeviceByNameSQL, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -537,18 +542,13 @@ func (sqlite *SQLite) GetDevices(ctx context.Context) ([]*types.Device, error) {
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectDevicesSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectDevicesSQLFile, err)
|
||||
}
|
||||
|
||||
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, string(f))
|
||||
devices, err := sqlite.selectDevices(tx, selectDevicesSQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -598,18 +598,13 @@ func (sqlite *SQLite) GetHumidityByID(ctx context.Context, id string) (*types.Me
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectHumidityByIDSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectHumidityByIDSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, string(f), id)
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, selectHumidityByIDSQL, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -635,18 +630,13 @@ func (sqlite *SQLite) GetHumidities(ctx context.Context) ([]*types.MeasuredValue
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectHumiditiesSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectHumiditiesSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, string(f))
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, selectHumiditiesSQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -703,18 +693,13 @@ func (sqlite *SQLite) GetPressureByID(ctx context.Context, id string) (*types.Me
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectPressureByIDSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectPressureByIDSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, string(f), id)
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, selectPressureByIDSQL, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -740,18 +725,13 @@ func (sqlite *SQLite) GetPressures(ctx context.Context) ([]*types.MeasuredValue,
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectPressuresSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", deleteDeviceByIDSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, string(f))
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, selectPressuresSQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -773,18 +753,13 @@ func (sqlite *SQLite) GetSensorByID(ctx context.Context, id string) (*types.Sens
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectSensorByIDSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorByIDSQLFile, err)
|
||||
}
|
||||
|
||||
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, string(f), id)
|
||||
sensors, err := sqlite.selectSensors(tx, selectSensorByIDSQL, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -806,18 +781,13 @@ func (sqlite *SQLite) GetSensors(ctx context.Context) ([]*types.Sensor, error) {
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectSensorsSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsSQLFile, err)
|
||||
}
|
||||
|
||||
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, string(f))
|
||||
sensors, err := sqlite.selectSensors(tx, selectSensorsSQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -835,11 +805,6 @@ func (sqlite *SQLite) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...st
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectSensorsByDeviceIDSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsByDeviceIDSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
@ -848,7 +813,7 @@ func (sqlite *SQLite) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...st
|
||||
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
for i := range deviceIDs {
|
||||
sensors, err := sqlite.selectSensors(tx, string(f), deviceIDs[i])
|
||||
sensors, err := sqlite.selectSensors(tx, selectSensorsByDeviceIDSQL, deviceIDs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -869,11 +834,6 @@ func (sqlite *SQLite) GetSensorsByModels(ctx context.Context, sensorModels ...st
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectSensorsByModelSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsByModelSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
@ -882,7 +842,7 @@ func (sqlite *SQLite) GetSensorsByModels(ctx context.Context, sensorModels ...st
|
||||
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
for i := range sensorModels {
|
||||
sensors, err := sqlite.selectSensors(tx, string(f), sensorModels[i])
|
||||
sensors, err := sqlite.selectSensors(tx, selectSensorsByModelSQL, sensorModels[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -903,11 +863,6 @@ func (sqlite *SQLite) GetSensorsByNames(ctx context.Context, sensorNames ...stri
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectSensorsByNameSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectSensorsByNameSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
@ -916,7 +871,7 @@ func (sqlite *SQLite) GetSensorsByNames(ctx context.Context, sensorNames ...stri
|
||||
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
for i := range sensorNames {
|
||||
sensors, err := sqlite.selectSensors(tx, string(f), sensorNames[i])
|
||||
sensors, err := sqlite.selectSensors(tx, selectSensorsByNameSQL, sensorNames[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -978,18 +933,13 @@ func (sqlite *SQLite) GetTemperatureByID(ctx context.Context, id string) (*types
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectTemperatureByIDSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectTemperatureByIDSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, string(f), id)
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, selectTemperatureByIDSQL, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -1015,18 +965,13 @@ func (sqlite *SQLite) GetTemperatures(ctx context.Context) ([]*types.MeasuredVal
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(selectTemperaturesSQLFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read file %v: %w", selectTemperaturesSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, string(f))
|
||||
measuredValues, err := sqlite.selectMeasuredValue(tx, selectTemperaturesSQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -1048,18 +993,13 @@ func (sqlite *SQLite) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...strin
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(deleteDeviceByIDSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", deleteDeviceByIDSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(deleteDeviceByIDSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -1080,18 +1020,13 @@ func (sqlite *SQLite) RemoveDevicesByNames(ctx context.Context, names ...string)
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(deleteDeviceByNameSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", deleteDeviceByNameSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(deleteDeviceByNameSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -1112,18 +1047,13 @@ func (sqlite *SQLite) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...strin
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(deleteSensorByIDSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", deleteSensorByIDSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(deleteSensorByIDSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -1144,18 +1074,13 @@ func (sqlite *SQLite) RemoveSensorsByNames(ctx context.Context, names ...string)
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(deleteSensorByNameSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", deleteSensorByNameSQLFile, err)
|
||||
}
|
||||
|
||||
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(string(f))
|
||||
stmt, err := tx.Prepare(deleteSensorByNameSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
@ -1176,18 +1101,13 @@ func (sqlite *SQLite) UpdateDevices(ctx context.Context, devices ...*types.Devic
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(updateDeviceSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", updateDeviceSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.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
|
||||
}
|
||||
@ -1214,18 +1134,13 @@ func (sqlite *SQLite) UpdateSensors(ctx context.Context, sensors ...*types.Senso
|
||||
sqlite.mutex.Lock()
|
||||
defer sqlite.mutex.Unlock()
|
||||
|
||||
f, err := sqlite3DMLAssets.ReadFile(updateSensorSQLFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read file %v: %w", updateSensorSQLFile, err)
|
||||
}
|
||||
|
||||
tx, err := sqlite.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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user