2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
2018-05-15 17:13:09 +02:00
|
|
|
"database/sql"
|
2019-04-16 14:58:56 +02:00
|
|
|
"encoding/json"
|
2019-05-30 15:33:49 +02:00
|
|
|
"fmt"
|
2018-05-15 17:13:09 +02:00
|
|
|
|
2019-05-16 13:35:13 +02:00
|
|
|
"github.com/gofrs/uuid"
|
2018-11-28 15:58:48 +01:00
|
|
|
"github.com/lib/pq" // required for DB access
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
)
|
|
|
|
|
2019-05-30 15:33:49 +02:00
|
|
|
const (
|
|
|
|
errDuplicate = "unique_violation"
|
|
|
|
errFK = "foreign_key_violation"
|
|
|
|
errInvalid = "invalid_text_representation"
|
|
|
|
errTruncation = "string_data_right_truncation"
|
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
var _ things.ThingRepository = (*thingRepository)(nil)
|
|
|
|
|
|
|
|
type thingRepository struct {
|
2019-10-07 05:32:09 -06:00
|
|
|
db Database
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewThingRepository instantiates a PostgreSQL implementation of thing
|
|
|
|
// repository.
|
2019-10-07 05:32:09 -06:00
|
|
|
func NewThingRepository(db Database) things.ThingRepository {
|
2019-05-10 18:55:13 +02:00
|
|
|
return &thingRepository{
|
|
|
|
db: db,
|
|
|
|
}
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (tr thingRepository) Save(ctx context.Context, thing things.Thing) (string, error) {
|
2019-04-20 14:09:11 +02:00
|
|
|
q := `INSERT INTO things (id, owner, name, key, metadata)
|
2019-07-18 15:01:09 +02:00
|
|
|
VALUES (:id, :owner, :name, :key, :metadata);`
|
2018-05-16 14:28:41 +02:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
dbth, err := toDBThing(thing)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2018-11-28 15:58:48 +01:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
_, err = tr.db.NamedExecContext(ctx, q, dbth)
|
2018-11-28 15:58:48 +01:00
|
|
|
if err != nil {
|
|
|
|
pqErr, ok := err.(*pq.Error)
|
2019-04-25 14:37:51 +02:00
|
|
|
if ok {
|
|
|
|
switch pqErr.Code.Name() {
|
2019-05-30 15:33:49 +02:00
|
|
|
case errInvalid, errTruncation:
|
2019-04-25 14:37:51 +02:00
|
|
|
return "", things.ErrMalformedEntity
|
|
|
|
case errDuplicate:
|
|
|
|
return "", things.ErrConflict
|
|
|
|
}
|
2018-11-28 15:58:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
return "", err
|
2018-05-16 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
return dbth.ID, nil
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 05:59:54 -06:00
|
|
|
func (tr thingRepository) BulkSave(ctx context.Context, ths []things.Thing) ([]things.Thing, error) {
|
|
|
|
tx, err := tr.db.BeginTxx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
q := `INSERT INTO things (id, owner, name, key, metadata)
|
|
|
|
VALUES (:id, :owner, :name, :key, :metadata);`
|
|
|
|
|
|
|
|
for _, thing := range ths {
|
|
|
|
dbth, err := toDBThing(thing)
|
|
|
|
if err != nil {
|
|
|
|
return []things.Thing{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.NamedExecContext(ctx, q, dbth)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
pqErr, ok := err.(*pq.Error)
|
|
|
|
if ok {
|
|
|
|
switch pqErr.Code.Name() {
|
|
|
|
case errInvalid, errTruncation:
|
|
|
|
return []things.Thing{}, things.ErrMalformedEntity
|
|
|
|
case errDuplicate:
|
|
|
|
return []things.Thing{}, things.ErrConflict
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return []things.Thing{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
return []things.Thing{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ths, nil
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) Update(ctx context.Context, thing things.Thing) error {
|
2019-04-16 14:58:56 +02:00
|
|
|
q := `UPDATE things SET name = :name, metadata = :metadata WHERE owner = :owner AND id = :id;`
|
2018-05-15 17:13:09 +02:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
dbth, err := toDBThing(thing)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-28 15:58:48 +01:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
res, err := tr.db.NamedExecContext(ctx, q, dbth)
|
2018-05-15 17:13:09 +02:00
|
|
|
if err != nil {
|
2018-11-28 15:58:48 +01:00
|
|
|
pqErr, ok := err.(*pq.Error)
|
2019-05-30 15:33:49 +02:00
|
|
|
if ok {
|
|
|
|
switch pqErr.Code.Name() {
|
|
|
|
case errInvalid, errTruncation:
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
2018-11-28 15:58:48 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnt == 0 {
|
|
|
|
return things.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) UpdateKey(ctx context.Context, owner, id, key string) error {
|
2019-04-25 14:37:51 +02:00
|
|
|
q := `UPDATE things SET key = :key WHERE owner = :owner AND id = :id;`
|
2019-10-07 05:32:09 -06:00
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
dbth := dbThing{
|
|
|
|
ID: id,
|
|
|
|
Owner: owner,
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
res, err := tr.db.NamedExecContext(ctx, q, dbth)
|
2019-04-25 14:37:51 +02:00
|
|
|
if err != nil {
|
|
|
|
pqErr, ok := err.(*pq.Error)
|
|
|
|
if ok {
|
|
|
|
switch pqErr.Code.Name() {
|
|
|
|
case errInvalid:
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
case errDuplicate:
|
|
|
|
return things.ErrConflict
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnt == 0 {
|
|
|
|
return things.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) RetrieveByID(ctx context.Context, owner, id string) (things.Thing, error) {
|
2019-04-20 14:09:11 +02:00
|
|
|
q := `SELECT name, key, metadata FROM things WHERE id = $1 AND owner = $2;`
|
2018-05-15 17:13:09 +02:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
dbth := dbThing{
|
|
|
|
ID: id,
|
|
|
|
Owner: owner,
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
if err := tr.db.QueryRowxContext(ctx, q, id, owner).StructScan(&dbth); err != nil {
|
2018-05-15 17:13:09 +02:00
|
|
|
empty := things.Thing{}
|
2019-03-06 14:10:01 +01:00
|
|
|
|
|
|
|
pqErr, ok := err.(*pq.Error)
|
|
|
|
if err == sql.ErrNoRows || ok && errInvalid == pqErr.Code.Name() {
|
2018-05-15 17:13:09 +02:00
|
|
|
return empty, things.ErrNotFound
|
|
|
|
}
|
2019-03-06 14:10:01 +01:00
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
return empty, err
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
return toThing(dbth)
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) RetrieveByKey(ctx context.Context, key string) (string, error) {
|
2019-04-16 14:58:56 +02:00
|
|
|
q := `SELECT id FROM things WHERE key = $1;`
|
2019-10-07 05:32:09 -06:00
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
var id string
|
2019-10-07 05:32:09 -06:00
|
|
|
if err := tr.db.QueryRowxContext(ctx, q, key).Scan(&id); err != nil {
|
2018-05-17 20:17:02 +02:00
|
|
|
if err == sql.ErrNoRows {
|
2018-12-05 13:09:25 +01:00
|
|
|
return "", things.ErrNotFound
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
2018-12-05 13:09:25 +01:00
|
|
|
return "", err
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) RetrieveAll(ctx context.Context, owner string, offset, limit uint64, name string, metadata things.Metadata) (things.ThingsPage, error) {
|
2019-10-01 14:12:52 +02:00
|
|
|
nq, name := getNameQuery(name)
|
|
|
|
m, mq, err := getMetadataQuery(metadata)
|
2019-09-17 13:46:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return things.ThingsPage{}, err
|
|
|
|
}
|
2019-05-30 15:33:49 +02:00
|
|
|
|
|
|
|
q := fmt.Sprintf(`SELECT id, name, key, metadata FROM things
|
2019-10-01 14:12:52 +02:00
|
|
|
WHERE owner = :owner %s%s ORDER BY id LIMIT :limit OFFSET :offset;`, mq, nq)
|
2018-05-15 17:13:09 +02:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
params := map[string]interface{}{
|
2019-09-17 13:46:24 +00:00
|
|
|
"owner": owner,
|
|
|
|
"limit": limit,
|
|
|
|
"offset": offset,
|
|
|
|
"name": name,
|
|
|
|
"metadata": m,
|
2019-04-16 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
rows, err := tr.db.NamedQueryContext(ctx, q, params)
|
2018-05-15 17:13:09 +02:00
|
|
|
if err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
items := []things.Thing{}
|
2018-05-15 17:13:09 +02:00
|
|
|
for rows.Next() {
|
2019-04-16 14:58:56 +02:00
|
|
|
dbth := dbThing{Owner: owner}
|
|
|
|
if err := rows.StructScan(&dbth); err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
2019-04-16 14:58:56 +02:00
|
|
|
|
|
|
|
th, err := toThing(dbth)
|
|
|
|
if err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2019-04-16 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, th)
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 15:33:49 +02:00
|
|
|
cq := ""
|
|
|
|
if name != "" {
|
2019-06-24 14:03:48 +02:00
|
|
|
cq = `AND LOWER(name) LIKE $2`
|
2019-05-30 15:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
q = fmt.Sprintf(`SELECT COUNT(*) FROM things WHERE owner = $1 %s;`, cq)
|
2019-01-08 11:53:24 +01:00
|
|
|
|
2019-05-30 15:33:49 +02:00
|
|
|
total := uint64(0)
|
|
|
|
switch name {
|
|
|
|
case "":
|
2019-10-07 05:32:09 -06:00
|
|
|
if err := tr.db.GetContext(ctx, &total, q, owner); err != nil {
|
2019-05-30 15:33:49 +02:00
|
|
|
return things.ThingsPage{}, err
|
|
|
|
}
|
|
|
|
default:
|
2019-10-07 05:32:09 -06:00
|
|
|
if err := tr.db.GetContext(ctx, &total, q, owner, name); err != nil {
|
2019-05-30 15:33:49 +02:00
|
|
|
return things.ThingsPage{}, err
|
|
|
|
}
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
page := things.ThingsPage{
|
|
|
|
Things: items,
|
|
|
|
PageMetadata: things.PageMetadata{
|
|
|
|
Total: total,
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:55:13 +02:00
|
|
|
return page, nil
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) RetrieveByChannel(ctx context.Context, owner, channel string, offset, limit uint64) (things.ThingsPage, error) {
|
2019-05-10 18:55:13 +02:00
|
|
|
// Verify if UUID format is valid to avoid internal Postgres error
|
|
|
|
if _, err := uuid.FromString(channel); err != nil {
|
|
|
|
return things.ThingsPage{}, things.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2019-04-20 14:09:11 +02:00
|
|
|
q := `SELECT id, name, key, metadata
|
2019-01-08 11:53:24 +01:00
|
|
|
FROM things th
|
|
|
|
INNER JOIN connections co
|
|
|
|
ON th.id = co.thing_id
|
2019-04-16 14:58:56 +02:00
|
|
|
WHERE th.owner = :owner AND co.channel_id = :channel
|
2019-01-08 11:53:24 +01:00
|
|
|
ORDER BY th.id
|
2019-04-16 14:58:56 +02:00
|
|
|
LIMIT :limit
|
|
|
|
OFFSET :offset;`
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"owner": owner,
|
|
|
|
"channel": channel,
|
|
|
|
"limit": limit,
|
|
|
|
"offset": offset,
|
|
|
|
}
|
2019-01-08 11:53:24 +01:00
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
rows, err := tr.db.NamedQueryContext(ctx, q, params)
|
2019-01-08 11:53:24 +01:00
|
|
|
if err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
items := []things.Thing{}
|
2019-01-08 11:53:24 +01:00
|
|
|
for rows.Next() {
|
2019-04-16 14:58:56 +02:00
|
|
|
dbth := dbThing{Owner: owner}
|
|
|
|
if err := rows.StructScan(&dbth); err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2019-04-16 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
th, err := toThing(dbth)
|
|
|
|
if err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
2019-04-16 14:58:56 +02:00
|
|
|
|
|
|
|
items = append(items, th)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
q = `SELECT COUNT(*)
|
|
|
|
FROM things th
|
|
|
|
INNER JOIN connections co
|
|
|
|
ON th.id = co.thing_id
|
2019-04-16 14:58:56 +02:00
|
|
|
WHERE th.owner = $1 AND co.channel_id = $2;`
|
2019-01-08 11:53:24 +01:00
|
|
|
|
|
|
|
var total uint64
|
2019-10-07 05:32:09 -06:00
|
|
|
if err := tr.db.GetContext(ctx, &total, q, owner, channel); err != nil {
|
2019-05-10 18:55:13 +02:00
|
|
|
return things.ThingsPage{}, err
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return things.ThingsPage{
|
|
|
|
Things: items,
|
|
|
|
PageMetadata: things.PageMetadata{
|
|
|
|
Total: total,
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
},
|
2019-05-10 18:55:13 +02:00
|
|
|
}, nil
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
func (tr thingRepository) Remove(ctx context.Context, owner, id string) error {
|
2019-04-16 14:58:56 +02:00
|
|
|
dbth := dbThing{
|
|
|
|
ID: id,
|
|
|
|
Owner: owner,
|
|
|
|
}
|
|
|
|
q := `DELETE FROM things WHERE id = :id AND owner = :owner;`
|
2019-10-07 05:32:09 -06:00
|
|
|
tr.db.NamedExecContext(ctx, q, dbth)
|
2018-05-15 17:13:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-16 14:58:56 +02:00
|
|
|
|
|
|
|
type dbThing struct {
|
|
|
|
ID string `db:"id"`
|
|
|
|
Owner string `db:"owner"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
Key string `db:"key"`
|
2019-10-01 14:12:52 +02:00
|
|
|
Metadata []byte `db:"metadata"`
|
2019-04-16 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func toDBThing(th things.Thing) (dbThing, error) {
|
2019-10-01 14:12:52 +02:00
|
|
|
data := []byte("{}")
|
|
|
|
if len(th.Metadata) > 0 {
|
|
|
|
b, err := json.Marshal(th.Metadata)
|
|
|
|
if err != nil {
|
|
|
|
return dbThing{}, err
|
|
|
|
}
|
|
|
|
data = b
|
2019-04-16 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return dbThing{
|
|
|
|
ID: th.ID,
|
|
|
|
Owner: th.Owner,
|
|
|
|
Name: th.Name,
|
|
|
|
Key: th.Key,
|
2019-10-01 14:12:52 +02:00
|
|
|
Metadata: data,
|
2019-04-16 14:58:56 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toThing(dbth dbThing) (things.Thing, error) {
|
|
|
|
var metadata map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(dbth.Metadata), &metadata); err != nil {
|
|
|
|
return things.Thing{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return things.Thing{
|
|
|
|
ID: dbth.ID,
|
|
|
|
Owner: dbth.Owner,
|
|
|
|
Name: dbth.Name,
|
|
|
|
Key: dbth.Key,
|
|
|
|
Metadata: metadata,
|
|
|
|
}, nil
|
|
|
|
}
|