From d19e26d97100461df31029c52a28b7ca718fd3e7 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 25 Aug 2019 20:58:45 +0200 Subject: [PATCH] fix(pkg/db/postgres): error handling --- pkg/db/errors.go | 1 + pkg/db/postgres.go | 99 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/pkg/db/errors.go b/pkg/db/errors.go index 0b138d9..0b57bc9 100644 --- a/pkg/db/errors.go +++ b/pkg/db/errors.go @@ -6,6 +6,7 @@ import ( var ( 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") errorPrepareStatement = errors.New("Can not prepare sql statement") errorRollbackTransaction = errors.New("Can not rollback transaction") diff --git a/pkg/db/postgres.go b/pkg/db/postgres.go index ba80de1..37cecdc 100644 --- a/pkg/db/postgres.go +++ b/pkg/db/postgres.go @@ -18,7 +18,12 @@ func (p *Postgres) Close() 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) 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 { - 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) if err != nil { @@ -81,18 +91,41 @@ func (p *Postgres) DeleteMeasuredValues(ctx context.Context, measuredValues []*t 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 { switch measuredValueType { 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 } 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 } 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 } } @@ -102,8 +135,12 @@ func (p *Postgres) DeleteMeasuredValues(ctx context.Context, measuredValues []*t } func (p *Postgres) InsertDevices(ctx context.Context, devices []*types.Device) error { - - query := string(MustAsset("pkg/db/sql/psql/insertDevice.sql")) + asset := "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) 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 { - 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) 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 { - 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) 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 { - 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) 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 { - 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) 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) { - 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) 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) { - 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) 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) { - 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) if err != nil {