|
|
|
@ -28,7 +28,7 @@ func (postgres *Postgres) DeleteDevices(ctx context.Context, deviceIDs ...string
|
|
|
|
|
queryFile := "deleteDevice.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
@ -58,7 +58,7 @@ func (postgres *Postgres) DeleteSensors(ctx context.Context, sensorIDs ...string
|
|
|
|
|
queryFile := "deleteSensor.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
@ -88,7 +88,7 @@ func (postgres *Postgres) InsertDevices(ctx context.Context, devices ...*types.D
|
|
|
|
|
queryFile := "insertDevice.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
@ -103,7 +103,18 @@ func (postgres *Postgres) InsertDevices(ctx context.Context, devices ...*types.D
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
|
|
for _, device := range devices {
|
|
|
|
|
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
|
|
|
|
|
|
|
|
|
|
if device.CreationDate.Equal(time.Time{}) {
|
|
|
|
|
device.CreationDate = time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = stmt.Exec(
|
|
|
|
|
&device.ID,
|
|
|
|
|
&device.Name,
|
|
|
|
|
&device.Location,
|
|
|
|
|
&device.CreationDate,
|
|
|
|
|
&device.UpdateDate,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
|
|
|
@ -133,7 +144,7 @@ func (postgres *Postgres) InsertMeasuredValues(ctx context.Context, measuredValu
|
|
|
|
|
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error {
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
@ -143,6 +154,11 @@ func (postgres *Postgres) InsertMeasuredValues(ctx context.Context, measuredValu
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
|
|
|
|
|
|
if measuredValue.CreationDate.Equal(time.Time{}) {
|
|
|
|
|
measuredValue.CreationDate = time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := stmt.Exec(
|
|
|
|
|
&measuredValue.ID,
|
|
|
|
|
&measuredValue.Value,
|
|
|
|
@ -190,7 +206,7 @@ func (postgres *Postgres) InsertSensors(ctx context.Context, sensors ...*types.S
|
|
|
|
|
queryFile := "insertSensor.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
@ -205,6 +221,11 @@ func (postgres *Postgres) InsertSensors(ctx context.Context, sensors ...*types.S
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
|
|
|
|
|
|
if sensor.CreationDate.Equal(time.Time{}) {
|
|
|
|
|
sensor.CreationDate = time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = stmt.Exec(
|
|
|
|
|
&sensor.ID,
|
|
|
|
|
&sensor.Name,
|
|
|
|
@ -251,7 +272,7 @@ func (postgres *Postgres) SelectDevice(ctx context.Context, id string) (*types.D
|
|
|
|
|
queryFile := "selectDevice.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -281,7 +302,7 @@ func (postgres *Postgres) SelectDevices(ctx context.Context) ([]*types.Device, e
|
|
|
|
|
queryFile := "selectDevices.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -338,7 +359,7 @@ func (postgres *Postgres) SelectHumidity(ctx context.Context, id string) (*types
|
|
|
|
|
queryFile := "selectHumidity.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -372,7 +393,7 @@ func (postgres *Postgres) SelectHumidities(ctx context.Context) ([]*types.Measur
|
|
|
|
|
queryFile := "selectHumidities.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -439,7 +460,7 @@ func (postgres *Postgres) SelectPressure(ctx context.Context, id string) (*types
|
|
|
|
|
queryFile := "selectPressure.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -473,7 +494,7 @@ func (postgres *Postgres) SelectPressures(ctx context.Context) ([]*types.Measure
|
|
|
|
|
queryFile := "selectPressures.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -503,7 +524,7 @@ func (postgres *Postgres) SelectSensor(ctx context.Context, id string) (*types.S
|
|
|
|
|
queryFile := "selectSensor.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -533,7 +554,7 @@ func (postgres *Postgres) SelectSensors(ctx context.Context) ([]*types.Sensor, e
|
|
|
|
|
queryFile := "selectSensors.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -599,7 +620,7 @@ func (postgres *Postgres) SelectTemperature(ctx context.Context, id string) (*ty
|
|
|
|
|
queryFile := "selectTemperature.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -633,7 +654,7 @@ func (postgres *Postgres) SelectTemperatures(ctx context.Context) ([]*types.Meas
|
|
|
|
|
queryFile := "selectTemperatures.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
@ -663,7 +684,7 @@ func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.D
|
|
|
|
|
queryFile := "updateDevice.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
@ -703,7 +724,7 @@ func (postgres *Postgres) UpdateSensors(ctx context.Context, sensors ...*types.S
|
|
|
|
|
queryFile := "updateSensor.sql"
|
|
|
|
|
query, present := postgres.queries[queryFile]
|
|
|
|
|
if !present {
|
|
|
|
|
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
|
|
|
|
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
|