WiP
This commit is contained in:
parent
f84dbe1ee0
commit
a6e4b77bbb
10 changed files with 186 additions and 2 deletions
4
dbconfig.yml
Normal file
4
dbconfig.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
development:
|
||||
dialect: sqlite3
|
||||
datasource: test.db
|
||||
dir: migrations/
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module git.katuwoss.dev/JustScreaMy/secret-santa
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.24
|
||||
|
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -0,0 +1,2 @@
|
|||
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
||||
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
31
internal/queries/db.go
Normal file
31
internal/queries/db.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package queries
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
12
internal/queries/models.go
Normal file
12
internal/queries/models.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package queries
|
||||
|
||||
type User struct {
|
||||
ID int64
|
||||
FirstName string
|
||||
LastName string
|
||||
Email string
|
||||
}
|
74
internal/queries/user.sql.go
Normal file
74
internal/queries/user.sql.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: user.sql
|
||||
|
||||
package queries
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :exec
|
||||
INSERT INTO users (id, email, first_name, last_name) VALUES (NULL, ?, ?, ?)
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Email string
|
||||
FirstName string
|
||||
LastName string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createUser, arg.Email, arg.FirstName, arg.LastName)
|
||||
return err
|
||||
}
|
||||
|
||||
const getUserId = `-- name: GetUserId :one
|
||||
SELECT id, first_name, last_name, email FROM users
|
||||
WHERE id = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserId(ctx context.Context, id int64) (User, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUserId, id)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FirstName,
|
||||
&i.LastName,
|
||||
&i.Email,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUsers = `-- name: GetUsers :many
|
||||
SELECT id, first_name, last_name, email FROM users
|
||||
`
|
||||
|
||||
func (q *Queries) GetUsers(ctx context.Context) ([]User, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getUsers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []User
|
||||
for rows.Next() {
|
||||
var i User
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.FirstName,
|
||||
&i.LastName,
|
||||
&i.Email,
|
||||
); 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
|
||||
}
|
35
main.go
35
main.go
|
@ -1,7 +1,38 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.katuwoss.dev/JustScreaMy/secret-santa/internal/queries"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello world!")
|
||||
ctx := context.Background()
|
||||
|
||||
dbConnection, err := sql.Open("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
db := queries.New(dbConnection)
|
||||
|
||||
if err := db.CreateUser(ctx, queries.CreateUserParams{
|
||||
Email: "jakub.kropacek@olc.cz",
|
||||
FirstName: "Jakub",
|
||||
LastName: "Kropáček",
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
users, err := db.GetUsers(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
fmt.Println(user)
|
||||
}
|
||||
}
|
||||
|
|
10
migrations/20241114201119-initial.sql
Normal file
10
migrations/20241114201119-initial.sql
Normal file
|
@ -0,0 +1,10 @@
|
|||
-- +migrate Up
|
||||
CREATE TABLE users (
|
||||
id INT PRIMARY KEY,
|
||||
first_name VARCHAR(64) NOT NULL,
|
||||
last_name VARCHAR(64) NOT NULL,
|
||||
email VARCHAR(128) NOT NULL
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
DROP TABLE IF EXISTS users;
|
9
queries/user.sql
Normal file
9
queries/user.sql
Normal file
|
@ -0,0 +1,9 @@
|
|||
-- name: GetUserId :one
|
||||
SELECT * FROM users
|
||||
WHERE id = ? LIMIT 1;
|
||||
|
||||
-- name: GetUsers :many
|
||||
SELECT * FROM users;
|
||||
|
||||
-- name: CreateUser :exec
|
||||
INSERT INTO users (id, email, first_name, last_name) VALUES (NULL, ?, ?, ?);
|
9
sqlc.yaml
Normal file
9
sqlc.yaml
Normal file
|
@ -0,0 +1,9 @@
|
|||
version: "2"
|
||||
sql:
|
||||
- engine: sqlite
|
||||
queries: queries/
|
||||
schema: migrations/
|
||||
gen:
|
||||
go:
|
||||
package: "queries"
|
||||
out: "internal/queries"
|
Loading…
Reference in a new issue