You've already forked gun-manager-backend
commit
This commit is contained in:
@@ -3,17 +3,19 @@ package sql
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
sqlc "git.siteworxpro.com/gun-manager/sql/db"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type SqlDb struct {
|
||||
type Db struct {
|
||||
db *sql.DB
|
||||
created bool
|
||||
Queries *sqlc.Queries
|
||||
}
|
||||
|
||||
var dbConnection SqlDb
|
||||
var dbConnection Db
|
||||
|
||||
func NewDb(file string) (*SqlDb, error) {
|
||||
func NewDb(file string) (*Db, error) {
|
||||
if dbConnection.created {
|
||||
return &dbConnection, nil
|
||||
}
|
||||
@@ -31,9 +33,11 @@ func NewDb(file string) (*SqlDb, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbConnection.Queries = sqlc.New(db)
|
||||
|
||||
return &dbConnection, nil
|
||||
}
|
||||
|
||||
func GetDb() *SqlDb {
|
||||
func GetDb() *Db {
|
||||
return &dbConnection
|
||||
}
|
||||
|
||||
31
sql/db/db.go
Normal file
31
sql/db/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.20.0
|
||||
|
||||
package db
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
27
sql/db/models.go
Normal file
27
sql/db/models.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.20.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Gun struct {
|
||||
ID int64 `json:"id"`
|
||||
Make sql.NullString `json:"make"`
|
||||
Model sql.NullString `json:"model"`
|
||||
SerialNumber sql.NullString `json:"serial_number"`
|
||||
PurchaseAmount sql.NullInt64 `json:"purchase_amount"`
|
||||
ValueAmount sql.NullInt64 `json:"value_amount"`
|
||||
DatePurchased sql.NullString `json:"date_purchased"`
|
||||
Notes sql.NullString `json:"notes"`
|
||||
}
|
||||
|
||||
type Photo struct {
|
||||
ID int64 `json:"id"`
|
||||
GunID sql.NullInt64 `json:"gun_id"`
|
||||
Photo []byte `json:"photo"`
|
||||
Filename sql.NullString `json:"filename"`
|
||||
}
|
||||
148
sql/db/query.sql.go
Normal file
148
sql/db/query.sql.go
Normal file
@@ -0,0 +1,148 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.20.0
|
||||
// source: query.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const getAllGuns = `-- name: GetAllGuns :many
|
||||
SELECT id, make, model, value_amount
|
||||
from guns
|
||||
order by id desc
|
||||
`
|
||||
|
||||
type GetAllGunsRow struct {
|
||||
ID int64 `json:"id"`
|
||||
Make sql.NullString `json:"make"`
|
||||
Model sql.NullString `json:"model"`
|
||||
ValueAmount sql.NullInt64 `json:"value_amount"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetAllGuns(ctx context.Context) ([]GetAllGunsRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getAllGuns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetAllGunsRow
|
||||
for rows.Next() {
|
||||
var i GetAllGunsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Make,
|
||||
&i.Model,
|
||||
&i.ValueAmount,
|
||||
); 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
|
||||
}
|
||||
|
||||
const getGunById = `-- name: GetGunById :one
|
||||
SELECT guns.id as id, make, model, serial_number, purchase_amount, value_amount, date_purchased, notes
|
||||
from guns
|
||||
where guns.id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetGunById(ctx context.Context, id int64) (Gun, error) {
|
||||
row := q.db.QueryRowContext(ctx, getGunById, id)
|
||||
var i Gun
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Make,
|
||||
&i.Model,
|
||||
&i.SerialNumber,
|
||||
&i.PurchaseAmount,
|
||||
&i.ValueAmount,
|
||||
&i.DatePurchased,
|
||||
&i.Notes,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getGunPhotoData = `-- name: GetGunPhotoData :one
|
||||
select photo from photos where id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetGunPhotoData(ctx context.Context, id int64) ([]byte, error) {
|
||||
row := q.db.QueryRowContext(ctx, getGunPhotoData, id)
|
||||
var photo []byte
|
||||
err := row.Scan(&photo)
|
||||
return photo, err
|
||||
}
|
||||
|
||||
const getGunPhotos = `-- name: GetGunPhotos :many
|
||||
SELECT id, filename from photos where gun_id = ?
|
||||
`
|
||||
|
||||
type GetGunPhotosRow struct {
|
||||
ID int64 `json:"id"`
|
||||
Filename sql.NullString `json:"filename"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetGunPhotos(ctx context.Context, gunID sql.NullInt64) ([]GetGunPhotosRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getGunPhotos, gunID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetGunPhotosRow
|
||||
for rows.Next() {
|
||||
var i GetGunPhotosRow
|
||||
if err := rows.Scan(&i.ID, &i.Filename); 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
|
||||
}
|
||||
|
||||
const insertGun = `-- name: InsertGun :one
|
||||
INSERT into guns
|
||||
(make, model, serial_number, purchase_amount, value_amount, date_purchased, notes) VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?) returning id
|
||||
`
|
||||
|
||||
type InsertGunParams struct {
|
||||
Make sql.NullString `json:"make"`
|
||||
Model sql.NullString `json:"model"`
|
||||
SerialNumber sql.NullString `json:"serial_number"`
|
||||
PurchaseAmount sql.NullInt64 `json:"purchase_amount"`
|
||||
ValueAmount sql.NullInt64 `json:"value_amount"`
|
||||
DatePurchased sql.NullString `json:"date_purchased"`
|
||||
Notes sql.NullString `json:"notes"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertGun(ctx context.Context, arg InsertGunParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertGun,
|
||||
arg.Make,
|
||||
arg.Model,
|
||||
arg.SerialNumber,
|
||||
arg.PurchaseAmount,
|
||||
arg.ValueAmount,
|
||||
arg.DatePurchased,
|
||||
arg.Notes,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
70
sql/guns.go
70
sql/guns.go
@@ -1,70 +0,0 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
func (dbClient *SqlDb) AllGuns() []Gun {
|
||||
|
||||
var guns []Gun
|
||||
|
||||
rows, _ := dbClient.db.Query("select guns.*, p.id, p.gun_id, p.filename from guns join photos p on guns.id = p.gun_id order by guns.id")
|
||||
defer func(rows *sql.Rows) {
|
||||
_ = rows.Close()
|
||||
}(rows)
|
||||
|
||||
for rows.Next() {
|
||||
i := Gun{}
|
||||
p := Photo{}
|
||||
err := rows.Scan(
|
||||
&i.Id,
|
||||
&i.Make,
|
||||
&i.Model,
|
||||
&i.SerialNumber,
|
||||
&i.PurchaseAmount,
|
||||
&i.ValueAmount,
|
||||
&i.DatePurchased,
|
||||
&i.Notes,
|
||||
&p.Id,
|
||||
&p.GunId,
|
||||
&p.FileName,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
index := -1
|
||||
for gunIndex, gun := range guns {
|
||||
if gun.Id == i.Id {
|
||||
index = gunIndex
|
||||
continue
|
||||
}
|
||||
|
||||
index = -1
|
||||
}
|
||||
|
||||
if index == -1 {
|
||||
guns = append(guns, i)
|
||||
index = len(guns) - 1
|
||||
}
|
||||
|
||||
guns[index].Photos = append(guns[index].Photos, p)
|
||||
}
|
||||
|
||||
return guns
|
||||
}
|
||||
|
||||
func (dbClient *SqlDb) GunById(id uint) (*Gun, error) {
|
||||
var gun Gun
|
||||
|
||||
row, _ := dbClient.db.Query("select guns.*, p.id, p.gun_id, p.filename from guns join photos p on guns.id = p.gun_id where guns.id = ?", id)
|
||||
|
||||
err := row.Scan(gun)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &gun, nil
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package sql
|
||||
|
||||
func (dbClient *SqlDb) GetPhoto(id uint) string {
|
||||
var photo string
|
||||
|
||||
row := dbClient.db.QueryRow("SELECT photo from photos where id = ?", id)
|
||||
|
||||
err := row.Scan(&photo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return photo
|
||||
}
|
||||
21
sql/query.sql
Normal file
21
sql/query.sql
Normal file
@@ -0,0 +1,21 @@
|
||||
-- name: GetGunById :one
|
||||
SELECT guns.id as id, make, model, serial_number, purchase_amount, value_amount, date_purchased, notes
|
||||
from guns
|
||||
where guns.id = ?;
|
||||
|
||||
-- name: GetGunPhotos :many
|
||||
SELECT id, filename from photos where gun_id = ?;
|
||||
|
||||
-- name: GetGunPhotoData :one
|
||||
select photo from photos where id = ?;
|
||||
|
||||
-- name: GetAllGuns :many
|
||||
SELECT id, make, model, value_amount
|
||||
from guns
|
||||
order by id desc;
|
||||
|
||||
-- name: InsertGun :one
|
||||
INSERT into guns
|
||||
(make, model, serial_number, purchase_amount, value_amount, date_purchased, notes) VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?) returning id
|
||||
|
||||
24
sql/schema.sql
Normal file
24
sql/schema.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
create table guns
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
make text,
|
||||
model text,
|
||||
serial_number text,
|
||||
purchase_amount integer,
|
||||
value_amount integer,
|
||||
date_purchased text,
|
||||
notes text
|
||||
);
|
||||
|
||||
create table photos
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
gun_id integer,
|
||||
photo blob,
|
||||
filename text
|
||||
);
|
||||
|
||||
10
sql/sqlc.yaml
Normal file
10
sql/sqlc.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
version: 2
|
||||
sql:
|
||||
- engine: "sqlite"
|
||||
schema: "schema.sql"
|
||||
queries: "query.sql"
|
||||
gen:
|
||||
go:
|
||||
package: "db"
|
||||
out: "db"
|
||||
emit_json_tags: true
|
||||
22
sql/types.go
22
sql/types.go
@@ -1,22 +0,0 @@
|
||||
package sql
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type Gun struct {
|
||||
Id uint `json:"id"`
|
||||
Make string `json:"make"`
|
||||
Model string `json:"model"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
PurchaseAmount uint `json:"purchase_amount"`
|
||||
ValueAmount uint `json:"value_amount"`
|
||||
DatePurchased sql.NullString `json:"date_purchased"`
|
||||
Notes sql.NullString `json:"notes"`
|
||||
Photos []Photo `json:"photos"`
|
||||
}
|
||||
|
||||
type Photo struct {
|
||||
Id uint `json:"id"`
|
||||
GunId uint `json:"gun_id"`
|
||||
Photo string `json:"photo"`
|
||||
FileName string `json:"file_name"`
|
||||
}
|
||||
Reference in New Issue
Block a user