fix(pkg/db/postgres): error handling

This commit is contained in:
Markus Pesch 2019-08-25 20:58:45 +02:00
parent 60657b737d
commit d19e26d971
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 86 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import (
var ( var (
errorBeginTransaction = errors.New("Can not start new transaction") errorBeginTransaction = errors.New("Can not start new transaction")
errorGetAsset = errors.New("Can not get asset from go-bindata")
errorRowNotFound = errors.New("Can not find row by given ID") errorRowNotFound = errors.New("Can not find row by given ID")
errorPrepareStatement = errors.New("Can not prepare sql statement") errorPrepareStatement = errors.New("Can not prepare sql statement")
errorRollbackTransaction = errors.New("Can not rollback transaction") errorRollbackTransaction = errors.New("Can not rollback transaction")

View File

@ -18,7 +18,12 @@ func (p *Postgres) Close() error {
} }
func (p *Postgres) DeleteDevices(ctx context.Context, devices []*types.Device) error { func (p *Postgres) DeleteDevices(ctx context.Context, devices []*types.Device) error {
query := string(MustAsset("pkg/db/sql/psql/deleteDevice.sql")) asset := "pkg/db/sql/psql/deleteDevice.sql"
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -37,7 +42,12 @@ func (p *Postgres) DeleteDevices(ctx context.Context, devices []*types.Device) e
} }
func (p *Postgres) DeleteSensors(ctx context.Context, sensors []*types.Sensor) error { func (p *Postgres) DeleteSensors(ctx context.Context, sensors []*types.Sensor) error {
query := string(MustAsset("pkg/db/sql/psql/deleteSensor.sql")) asset := "pkg/db/sql/psql/deleteSensor.sql"
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -81,18 +91,41 @@ func (p *Postgres) DeleteMeasuredValues(ctx context.Context, measuredValues []*t
sortedMeasuredValueTypes[measuredValue.ValueType] = append(sortedMeasuredValueTypes[measuredValue.ValueType], measuredValue) sortedMeasuredValueTypes[measuredValue.ValueType] = append(sortedMeasuredValueTypes[measuredValue.ValueType], measuredValue)
} }
assetFunc := func(queryFile string) (string, error) {
queryBytes, err := Asset(queryFile)
if err != nil {
return "", fmt.Errorf("%v: %v", errorGetAsset, err)
}
return string(queryBytes), nil
}
for measuredValueType, sortedMeasuredValues := range sortedMeasuredValueTypes { for measuredValueType, sortedMeasuredValues := range sortedMeasuredValueTypes {
switch measuredValueType { switch measuredValueType {
case types.MeasuredValueTypeHumidity: case types.MeasuredValueTypeHumidity:
if err := deleteMeasuredValue(ctx, string(MustAsset("pkg/db/sql/psql/deleteHumidity.sql")), sortedMeasuredValues); err != nil { query, err := assetFunc("pkg/db/sql/psql/deleteHumidity.sql")
if err != nil {
return err
}
if err := deleteMeasuredValue(ctx, query, sortedMeasuredValues); err != nil {
return err return err
} }
case types.MeasuredValueTypePressure: case types.MeasuredValueTypePressure:
if err := deleteMeasuredValue(ctx, string(MustAsset("pkg/db/sql/psql/deletePressure.sql")), sortedMeasuredValues); err != nil { query, err := assetFunc("pkg/db/sql/psql/deletePressure.sql")
if err != nil {
return err
}
if err := deleteMeasuredValue(ctx, query, sortedMeasuredValues); err != nil {
return err return err
} }
case types.MeasuredValueTypeTemperature: case types.MeasuredValueTypeTemperature:
if err := deleteMeasuredValue(ctx, string(MustAsset("pkg/db/sql/psql/deleteTemperature.sql")), sortedMeasuredValues); err != nil { query, err := assetFunc("pkg/db/sql/psql/deleteTemperature.sql")
if err != nil {
return err
}
if err := deleteMeasuredValue(ctx, query, sortedMeasuredValues); err != nil {
return err return err
} }
} }
@ -102,8 +135,12 @@ func (p *Postgres) DeleteMeasuredValues(ctx context.Context, measuredValues []*t
} }
func (p *Postgres) InsertDevices(ctx context.Context, devices []*types.Device) error { func (p *Postgres) InsertDevices(ctx context.Context, devices []*types.Device) error {
asset := "pkg/db/sql/psql/insertDevice.sql"
query := string(MustAsset("pkg/db/sql/psql/insertDevice.sql")) queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -154,7 +191,12 @@ func (p *Postgres) InsertMeasuredValues(ctx context.Context, measuredValues []*t
func (p *Postgres) insertHumidity(ctx context.Context, measuredValues []*types.MeasuredValue) error { func (p *Postgres) insertHumidity(ctx context.Context, measuredValues []*types.MeasuredValue) error {
query := string(MustAsset("pkg/db/sql/psql/insertHumidity.sql")) asset := "pkg/db/sql/psql/insertHumidity.sql"
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -178,7 +220,12 @@ func (p *Postgres) insertHumidity(ctx context.Context, measuredValues []*types.M
func (p *Postgres) insertPressure(ctx context.Context, measuredValues []*types.MeasuredValue) error { func (p *Postgres) insertPressure(ctx context.Context, measuredValues []*types.MeasuredValue) error {
query := string(MustAsset("pkg/db/sql/psql/insertPressure.sql")) asset := "pkg/db/sql/psql/insertPressure.sql"
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -202,7 +249,12 @@ func (p *Postgres) insertPressure(ctx context.Context, measuredValues []*types.M
func (p *Postgres) insertTemperature(ctx context.Context, measuredValues []*types.MeasuredValue) error { func (p *Postgres) insertTemperature(ctx context.Context, measuredValues []*types.MeasuredValue) error {
query := string(MustAsset("pkg/db/sql/psql/insertTemperature.sql")) asset := "pkg/db/sql/psql/insertTemperature.sql"
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -226,7 +278,12 @@ func (p *Postgres) insertTemperature(ctx context.Context, measuredValues []*type
func (p *Postgres) InsertSensors(ctx context.Context, sensors []*types.Sensor) error { func (p *Postgres) InsertSensors(ctx context.Context, sensors []*types.Sensor) error {
query := string(MustAsset("pkg/db/sql/psql/insertSensor.sql")) asset := "pkg/db/sql/psql/insertSensor.sql"
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -245,7 +302,12 @@ func (p *Postgres) InsertSensors(ctx context.Context, sensors []*types.Sensor) e
} }
func (p *Postgres) SelectDeviceByID(ctx context.Context, id string) (*types.Device, error) { func (p *Postgres) SelectDeviceByID(ctx context.Context, id string) (*types.Device, error) {
query := string(MustAsset("pkg/db/sql/psql/selectDeviceByID.sql")) asset := "pkg/db/sql/psql/selectDeviceByID.sql"
queryBytes, err := Asset(asset)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -328,7 +390,12 @@ func (p *Postgres) SelectPressureByID(ctx context.Context, id string) (*types.Me
} }
func (p *Postgres) SelectSensorByID(ctx context.Context, id string) (*types.Sensor, error) { func (p *Postgres) SelectSensorByID(ctx context.Context, id string) (*types.Sensor, error) {
query := string(MustAsset("pkg/db/sql/psql/selectSensorByID.sql")) asset := "pkg/db/sql/psql/selectSensorByID.sql"
queryBytes, err := Asset(asset)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {
@ -374,7 +441,11 @@ func (p *Postgres) SelectTemperatureByID(ctx context.Context, id string) (*types
} }
func (p *Postgres) selectMeasuredValues(ctx context.Context, measuredValueType types.MeasuredValueType, queryFile string, queryArgs []interface{}) ([]*types.MeasuredValue, error) { func (p *Postgres) selectMeasuredValues(ctx context.Context, measuredValueType types.MeasuredValueType, queryFile string, queryArgs []interface{}) ([]*types.MeasuredValue, error) {
query := string(MustAsset(queryFile)) queryBytes, err := Asset(queryFile)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorGetAsset, err)
}
query := string(queryBytes)
stmt, err := p.dbo.PrepareContext(ctx, query) stmt, err := p.dbo.PrepareContext(ctx, query)
if err != nil { if err != nil {