|
|
@ -56,22 +56,28 @@ func (r *DeviceRepo) Find(ctx context.Context, id int) (*models.Device, error) { |
|
|
|
} |
|
|
|
|
|
|
|
func (r *DeviceRepo) FetchByReference(ctx context.Context, kind models.ReferenceKind, value string) ([]models.Device, error) { |
|
|
|
var err error |
|
|
|
records := make([]deviceRecord, 0, 8) |
|
|
|
q := sq.Select("device.*").From("device") |
|
|
|
|
|
|
|
switch kind { |
|
|
|
case models.RKDeviceID: |
|
|
|
err = r.DBX.SelectContext(ctx, &records, "SELECT * FROM device WHERE id=?", value) |
|
|
|
q = q.Where(sq.Eq{"id": strings.Split(value, ",")}) |
|
|
|
case models.RKBridgeID: |
|
|
|
err = r.DBX.SelectContext(ctx, &records, "SELECT * FROM device WHERE bridge_id=?", value) |
|
|
|
q = q.Where(sq.Eq{"bridge_id": strings.Split(value, ",")}) |
|
|
|
case models.RKTag: |
|
|
|
err = r.DBX.SelectContext(ctx, &records, "SELECT device.* FROM device JOIN device_tag dt ON device.id = dt.device_id WHERE dt.tag_name=?", value) |
|
|
|
q = q.Join("device_tag dt ON device.id=dt.device_id").Where(sq.Eq{"dt.tag_name": strings.Split(value, ",")}) |
|
|
|
case models.RKAll: |
|
|
|
err = r.DBX.SelectContext(ctx, &records, "SELECT device.* FROM device") |
|
|
|
default: |
|
|
|
log.Println("Unknown reference kind used for device fetch:", kind) |
|
|
|
return []models.Device{}, nil |
|
|
|
} |
|
|
|
|
|
|
|
query, args, err := q.ToSql() |
|
|
|
if err != nil { |
|
|
|
return nil, dbErr(err) |
|
|
|
} |
|
|
|
|
|
|
|
records := make([]deviceRecord, 0, 8) |
|
|
|
err = r.DBX.SelectContext(ctx, &records, query, args...) |
|
|
|
if err != nil { |
|
|
|
return nil, dbErr(err) |
|
|
|
} |
|
|
|