atmost working example
This commit is contained in:
32
internal/store/pg/queries/db.go
Normal file
32
internal/store/pg/queries/db.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.26.0
|
||||
|
||||
package queries
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
19
internal/store/pg/queries/models.go
Normal file
19
internal/store/pg/queries/models.go
Normal file
@ -0,0 +1,19 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.26.0
|
||||
|
||||
package queries
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Stat struct {
|
||||
DeviceID string
|
||||
IncTraffic int32
|
||||
OutTraffic int32
|
||||
IncRps int32
|
||||
ReadRps int32
|
||||
WriteRps int32
|
||||
UpdatedAt pgtype.Timestamp
|
||||
}
|
||||
90
internal/store/pg/queries/queries.sql.go
Normal file
90
internal/store/pg/queries/queries.sql.go
Normal file
@ -0,0 +1,90 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.26.0
|
||||
// source: queries.sql
|
||||
|
||||
package queries
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const listDeviceStats = `-- name: ListDeviceStats :many
|
||||
SELECT device_id, inc_traffic, out_traffic, inc_rps, read_rps, write_rps, updated_at FROM public.stats
|
||||
`
|
||||
|
||||
func (q *Queries) ListDeviceStats(ctx context.Context) ([]Stat, error) {
|
||||
rows, err := q.db.Query(ctx, listDeviceStats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Stat
|
||||
for rows.Next() {
|
||||
var i Stat
|
||||
if err := rows.Scan(
|
||||
&i.DeviceID,
|
||||
&i.IncTraffic,
|
||||
&i.OutTraffic,
|
||||
&i.IncRps,
|
||||
&i.ReadRps,
|
||||
&i.WriteRps,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const upsertDeviceMetrics = `-- name: UpsertDeviceMetrics :exec
|
||||
INSERT INTO public.stats(
|
||||
device_id,
|
||||
inc_traffic,
|
||||
out_traffic,
|
||||
inc_rps,
|
||||
write_rps,
|
||||
read_rps,
|
||||
updated_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
NOW()
|
||||
) ON CONFLICT(device_id) DO UPDATE SET
|
||||
device_id = EXCLUDED.device_id,
|
||||
inc_traffic = EXCLUDED.inc_traffic,
|
||||
out_traffic = EXCLUDED.out_traffic,
|
||||
inc_rps = EXCLUDED.inc_rps,
|
||||
write_rps = EXCLUDED.write_rps,
|
||||
read_rps = EXCLUDED.read_rps,
|
||||
updated_at = NOW()
|
||||
`
|
||||
|
||||
type UpsertDeviceMetricsParams struct {
|
||||
DeviceID string
|
||||
IncTraffic int32
|
||||
OutTraffic int32
|
||||
IncRps int32
|
||||
WriteRps int32
|
||||
ReadRps int32
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertDeviceMetrics(ctx context.Context, arg UpsertDeviceMetricsParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertDeviceMetrics,
|
||||
arg.DeviceID,
|
||||
arg.IncTraffic,
|
||||
arg.OutTraffic,
|
||||
arg.IncRps,
|
||||
arg.WriteRps,
|
||||
arg.ReadRps,
|
||||
)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user