From a2e66ce08b45f96f1c8048e69038cdca4b5718c8 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Fri, 6 Nov 2020 23:06:53 +0100 Subject: [PATCH] fix: sqlite and postgres, close rows and pass nothing instead nil --- pkg/repository/db/postgres.go | 6 +++--- pkg/repository/db/sqlite.go | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/repository/db/postgres.go b/pkg/repository/db/postgres.go index f554487..49424bd 100644 --- a/pkg/repository/db/postgres.go +++ b/pkg/repository/db/postgres.go @@ -401,7 +401,7 @@ func (postgres *Postgres) SelectHumidities(ctx context.Context) ([]*types.Measur return nil, err } - measuredValues, err := postgres.selectMeasuredValue(tx, query, nil) + measuredValues, err := postgres.selectMeasuredValue(tx, query) if err != nil { return nil, err } @@ -502,7 +502,7 @@ func (postgres *Postgres) SelectPressures(ctx context.Context) ([]*types.Measure return nil, err } - measuredValues, err := postgres.selectMeasuredValue(tx, query, nil) + measuredValues, err := postgres.selectMeasuredValue(tx, query) if err != nil { return nil, err } @@ -662,7 +662,7 @@ func (postgres *Postgres) SelectTemperatures(ctx context.Context) ([]*types.Meas return nil, err } - measuredValues, err := postgres.selectMeasuredValue(tx, query, nil) + measuredValues, err := postgres.selectMeasuredValue(tx, query) if err != nil { return nil, err } diff --git a/pkg/repository/db/sqlite.go b/pkg/repository/db/sqlite.go index bfd03bf..d2bf8d5 100644 --- a/pkg/repository/db/sqlite.go +++ b/pkg/repository/db/sqlite.go @@ -312,6 +312,7 @@ func (sqlite *SQLite) selectDevices(tx *sql.Tx, query string, args ...interface{ if err != nil { return nil, fmt.Errorf("Failed to query statement: %v", err) } + defer rows.Close() devices := make([]*types.Device, 0) for rows.Next() { @@ -379,7 +380,7 @@ func (sqlite *SQLite) SelectHumidities(ctx context.Context) ([]*types.MeasuredVa return nil, err } - measuredValues, err := sqlite.selectMeasuredValue(tx, query, nil) + measuredValues, err := sqlite.selectMeasuredValue(tx, query) if err != nil { return nil, err } @@ -409,6 +410,7 @@ func (sqlite *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...inte tx.Rollback() return nil, err } + defer rows.Close() measuredValues := make([]*types.MeasuredValue, 0) for rows.Next() { @@ -416,8 +418,8 @@ func (sqlite *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...inte err := rows.Scan( &measuredValue.ID, &measuredValue.Value, - &measuredValue.SensorID, &measuredValue.Date, + &measuredValue.SensorID, &measuredValue.CreationDate, &measuredValue.UpdateDate, ) @@ -480,7 +482,7 @@ func (sqlite *SQLite) SelectPressures(ctx context.Context) ([]*types.MeasuredVal return nil, err } - measuredValues, err := sqlite.selectMeasuredValue(tx, query, nil) + measuredValues, err := sqlite.selectMeasuredValue(tx, query) if err != nil { return nil, err } @@ -564,6 +566,7 @@ func (sqlite *SQLite) selectSensors(tx *sql.Tx, query string, args ...interface{ if err != nil { return nil, fmt.Errorf("Failed to query statement: %v", err) } + defer rows.Close() sensors := make([]*types.Sensor, 0) for rows.Next() { @@ -640,7 +643,7 @@ func (sqlite *SQLite) SelectTemperatures(ctx context.Context) ([]*types.Measured return nil, err } - measuredValues, err := sqlite.selectMeasuredValue(tx, query, nil) + measuredValues, err := sqlite.selectMeasuredValue(tx, query) if err != nil { return nil, err }