Database package, schema and queries
This commit is contained in:
31
internal/database/db.go
Normal file
31
internal/database/db.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DBTX interface {
|
||||||
|
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||||
|
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||||
|
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||||
|
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(db DBTX) *Queries {
|
||||||
|
return &Queries{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Queries struct {
|
||||||
|
db DBTX
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||||
|
return &Queries{
|
||||||
|
db: tx,
|
||||||
|
}
|
||||||
|
}
|
||||||
18
internal/database/models.go
Normal file
18
internal/database/models.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FtsTransaction struct {
|
||||||
|
TransactionID int32
|
||||||
|
Description string
|
||||||
|
Amount string
|
||||||
|
Category string
|
||||||
|
TransactionDate time.Time
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
110
internal/database/transaction.sql.go
Normal file
110
internal/database/transaction.sql.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: transaction.sql
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createTransaction = `-- name: CreateTransaction :one
|
||||||
|
INSERT INTO fts.transactions (
|
||||||
|
description, amount, category, transaction_date
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4
|
||||||
|
)
|
||||||
|
RETURNING transaction_id, description, amount, category, transaction_date, created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateTransactionParams struct {
|
||||||
|
Description string
|
||||||
|
Amount string
|
||||||
|
Category string
|
||||||
|
TransactionDate time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (FtsTransaction, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, createTransaction,
|
||||||
|
arg.Description,
|
||||||
|
arg.Amount,
|
||||||
|
arg.Category,
|
||||||
|
arg.TransactionDate,
|
||||||
|
)
|
||||||
|
var i FtsTransaction
|
||||||
|
err := row.Scan(
|
||||||
|
&i.TransactionID,
|
||||||
|
&i.Description,
|
||||||
|
&i.Amount,
|
||||||
|
&i.Category,
|
||||||
|
&i.TransactionDate,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteTransaction = `-- name: DeleteTransaction :exec
|
||||||
|
DELETE FROM fts.transactions
|
||||||
|
WHERE transaction_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteTransaction(ctx context.Context, transactionID int32) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteTransaction, transactionID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getTransaction = `-- name: GetTransaction :one
|
||||||
|
SELECT transaction_id, description, amount, category, transaction_date, created_at FROM fts.transactions
|
||||||
|
WHERE transaction_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetTransaction(ctx context.Context, transactionID int32) (FtsTransaction, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getTransaction, transactionID)
|
||||||
|
var i FtsTransaction
|
||||||
|
err := row.Scan(
|
||||||
|
&i.TransactionID,
|
||||||
|
&i.Description,
|
||||||
|
&i.Amount,
|
||||||
|
&i.Category,
|
||||||
|
&i.TransactionDate,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listTransactions = `-- name: ListTransactions :many
|
||||||
|
SELECT transaction_id, description, amount, category, transaction_date, created_at FROM fts.transactions
|
||||||
|
ORDER BY transaction_date DESC, transaction_id DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListTransactions(ctx context.Context) ([]FtsTransaction, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listTransactions)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []FtsTransaction
|
||||||
|
for rows.Next() {
|
||||||
|
var i FtsTransaction
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.TransactionID,
|
||||||
|
&i.Description,
|
||||||
|
&i.Amount,
|
||||||
|
&i.Category,
|
||||||
|
&i.TransactionDate,
|
||||||
|
&i.CreatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
19
sql/queries/transaction.sql
Normal file
19
sql/queries/transaction.sql
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
-- name: CreateTransaction :one
|
||||||
|
INSERT INTO fts.transactions (
|
||||||
|
description, amount, category, transaction_date
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ListTransactions :many
|
||||||
|
SELECT * FROM fts.transactions
|
||||||
|
ORDER BY transaction_date DESC, transaction_id DESC;
|
||||||
|
|
||||||
|
-- name: GetTransaction :one
|
||||||
|
SELECT * FROM fts.transactions
|
||||||
|
WHERE transaction_id = $1;
|
||||||
|
|
||||||
|
-- name: DeleteTransaction :exec
|
||||||
|
DELETE FROM fts.transactions
|
||||||
|
WHERE transaction_id = $1;
|
||||||
18
sql/schema/001_transaction.sql
Normal file
18
sql/schema/001_transaction.sql
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
-- +goose Up
|
||||||
|
CREATE SCHEMA IF NOT EXISTS fts;
|
||||||
|
|
||||||
|
CREATE TABLE fts.transactions (
|
||||||
|
transaction_id SERIAL PRIMARY KEY,
|
||||||
|
description TEXT NOT NULL,
|
||||||
|
amount NUMERIC(12, 2) NOT NULL,
|
||||||
|
category VARCHAR(50) NOT NULL,
|
||||||
|
transaction_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Index for querying transactions by date
|
||||||
|
CREATE INDEX idx_transactions_date ON fts.transactions(transaction_date);
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP INDEX IF EXISTS fts.idx_transactions_date;
|
||||||
|
DROP TABLE IF EXISTS fts.transactions;
|
||||||
Reference in New Issue
Block a user