sqlc
This commit is contained in:
parent
4d4d55904e
commit
29b35e931a
32
enshi_back/db/db.go
Normal file
32
enshi_back/db/db.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.27.0
|
||||||
|
|
||||||
|
package db_repo
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
28
enshi_back/db/migrations/migration1.sql
Normal file
28
enshi_back/db/migrations/migration1.sql
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
-- Add new schema named "public"
|
||||||
|
CREATE SCHEMA IF NOT EXISTS "public";
|
||||||
|
-- Set comment to schema: "public"
|
||||||
|
COMMENT ON SCHEMA "public" IS 'standard public schema';
|
||||||
|
-- Create "categories" table
|
||||||
|
CREATE TABLE "public"."categories" ("category_id" integer NOT NULL, "category_name" character varying(50) NOT NULL, PRIMARY KEY ("category_id"), CONSTRAINT "categories_category_name_key" UNIQUE ("category_name"));
|
||||||
|
-- Create "users" table
|
||||||
|
CREATE TABLE "public"."users" ("user_id" bigint NOT NULL, "username" character varying(50) NOT NULL, "email" character varying(100) NOT NULL, "password" character varying(255) NOT NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id"), CONSTRAINT "users_email_key" UNIQUE ("email"), CONSTRAINT "users_username_key" UNIQUE ("username"));
|
||||||
|
-- Create "blogs" table
|
||||||
|
CREATE TABLE "public"."blogs" ("blog_id" bigint NOT NULL, "user_id" bigint NOT NULL, "title" character varying(255) NULL, "description" text NULL, "category_id" integer NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("blog_id"), CONSTRAINT "blogs_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "public"."categories" ("category_id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "blogs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "posts" table
|
||||||
|
CREATE TABLE "public"."posts" ("post_id" bigint NOT NULL, "blog_id" bigint NULL, "user_id" bigint NULL, "title" character varying(255) NULL, "content" text NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("post_id"), CONSTRAINT "posts_blog_id_fkey" FOREIGN KEY ("blog_id") REFERENCES "public"."blogs" ("blog_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "posts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "bookmarks" table
|
||||||
|
CREATE TABLE "public"."bookmarks" ("user_id" bigint NOT NULL, "post_id" bigint NOT NULL, "bookmarked_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id", "post_id"), CONSTRAINT "bookmarks_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "bookmarks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "comments" table
|
||||||
|
CREATE TABLE "public"."comments" ("comment_id" bigint NOT NULL, "post_id" bigint NULL, "user_id" bigint NULL, "content" text NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("comment_id"), CONSTRAINT "comments_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "favorites" table
|
||||||
|
CREATE TABLE "public"."favorites" ("user_id" bigint NOT NULL, "blog_id" bigint NOT NULL, "favorited_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id", "blog_id"), CONSTRAINT "favorites_blog_id_fkey" FOREIGN KEY ("blog_id") REFERENCES "public"."blogs" ("blog_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "favorites_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "likes" table
|
||||||
|
CREATE TABLE "public"."likes" ("like_id" bigint NOT NULL, "user_id" bigint NULL, "comment_id" bigint NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("like_id"), CONSTRAINT "likes_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "public"."comments" ("comment_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "likes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "tags" table
|
||||||
|
CREATE TABLE "public"."tags" ("tag_id" integer NOT NULL, "tag_name" character varying(50) NOT NULL, PRIMARY KEY ("tag_id"), CONSTRAINT "tags_tag_name_key" UNIQUE ("tag_name"));
|
||||||
|
-- Create "post_tags" table
|
||||||
|
CREATE TABLE "public"."post_tags" ("post_id" bigint NOT NULL, "tag_id" integer NOT NULL, PRIMARY KEY ("post_id", "tag_id"), CONSTRAINT "post_tags_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "post_tags_tag_id_fkey" FOREIGN KEY ("tag_id") REFERENCES "public"."tags" ("tag_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
|
-- Create "post_votes" table
|
||||||
|
CREATE TABLE "public"."post_votes" ("post_id" bigint NOT NULL, "user_id" bigint NOT NULL, "vote" boolean NOT NULL, PRIMARY KEY ("post_id", "user_id"), CONSTRAINT "post_votes_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "post_votes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE NO ACTION);
|
||||||
|
-- Create "profiles" table
|
||||||
|
CREATE TABLE "public"."profiles" ("profile_id" bigint NOT NULL, "user_id" bigint NULL, "bio" text NULL, "avatar_url" character varying(255) NULL, "website_url" character varying(100) NULL, PRIMARY KEY ("profile_id"), CONSTRAINT "profiles_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
|
||||||
92
enshi_back/db/models.go
Normal file
92
enshi_back/db/models.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.27.0
|
||||||
|
|
||||||
|
package db_repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Blog struct {
|
||||||
|
BlogID int64 `json:"blog_id"`
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
|
Title pgtype.Text `json:"title"`
|
||||||
|
Description pgtype.Text `json:"description"`
|
||||||
|
CategoryID pgtype.Int4 `json:"category_id"`
|
||||||
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Bookmark struct {
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
|
PostID int64 `json:"post_id"`
|
||||||
|
BookmarkedAt pgtype.Timestamp `json:"bookmarked_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Category struct {
|
||||||
|
CategoryID int32 `json:"category_id"`
|
||||||
|
CategoryName string `json:"category_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Comment struct {
|
||||||
|
CommentID int64 `json:"comment_id"`
|
||||||
|
PostID pgtype.Int8 `json:"post_id"`
|
||||||
|
UserID pgtype.Int8 `json:"user_id"`
|
||||||
|
Content pgtype.Text `json:"content"`
|
||||||
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Favorite struct {
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
|
BlogID int64 `json:"blog_id"`
|
||||||
|
FavoritedAt pgtype.Timestamp `json:"favorited_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Like struct {
|
||||||
|
LikeID int64 `json:"like_id"`
|
||||||
|
UserID pgtype.Int8 `json:"user_id"`
|
||||||
|
CommentID pgtype.Int8 `json:"comment_id"`
|
||||||
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Post struct {
|
||||||
|
PostID int64 `json:"post_id"`
|
||||||
|
BlogID pgtype.Int8 `json:"blog_id"`
|
||||||
|
UserID pgtype.Int8 `json:"user_id"`
|
||||||
|
Title pgtype.Text `json:"title"`
|
||||||
|
Content pgtype.Text `json:"content"`
|
||||||
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||||
|
UpdatedAt pgtype.Timestamp `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PostTag struct {
|
||||||
|
PostID int64 `json:"post_id"`
|
||||||
|
TagID int32 `json:"tag_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PostVote struct {
|
||||||
|
PostID int64 `json:"post_id"`
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
|
Vote bool `json:"vote"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Profile struct {
|
||||||
|
ProfileID int64 `json:"profile_id"`
|
||||||
|
UserID pgtype.Int8 `json:"user_id"`
|
||||||
|
Bio pgtype.Text `json:"bio"`
|
||||||
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
||||||
|
WebsiteUrl pgtype.Text `json:"website_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
TagID int32 `json:"tag_id"`
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||||
|
}
|
||||||
4
enshi_back/db/queries/user_queries.sql
Normal file
4
enshi_back/db/queries/user_queries.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
-- name: GetAllUsers :many
|
||||||
|
SELECT * FROM users;
|
||||||
|
|
||||||
|
|
||||||
17
enshi_back/db/sqlc.yml
Normal file
17
enshi_back/db/sqlc.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
version: "2"
|
||||||
|
sql:
|
||||||
|
- engine: "postgresql"
|
||||||
|
schema: "./migrations"
|
||||||
|
queries: "./queries"
|
||||||
|
gen:
|
||||||
|
go:
|
||||||
|
emit_json_tags: true
|
||||||
|
package: "db_repo"
|
||||||
|
out: "."
|
||||||
|
sql_package: "pgx/v5"
|
||||||
|
overrides:
|
||||||
|
- db_type: "uuid"
|
||||||
|
go_type:
|
||||||
|
import: 'github.com/google/uuid'
|
||||||
|
type: 'UUID'
|
||||||
|
|
||||||
40
enshi_back/db/user_queries.sql.go
Normal file
40
enshi_back/db/user_queries.sql.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.27.0
|
||||||
|
// source: user_queries.sql
|
||||||
|
|
||||||
|
package db_repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
const getAllUsers = `-- name: GetAllUsers :many
|
||||||
|
SELECT user_id, username, email, password, created_at FROM users
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetAllUsers(ctx context.Context) ([]User, error) {
|
||||||
|
rows, err := q.db.Query(ctx, getAllUsers)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []User
|
||||||
|
for rows.Next() {
|
||||||
|
var i User
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.UserID,
|
||||||
|
&i.Username,
|
||||||
|
&i.Email,
|
||||||
|
&i.Password,
|
||||||
|
&i.CreatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
db_repo "enshi/db"
|
||||||
"enshi/utils"
|
"enshi/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@ -19,6 +21,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer utils.Dbx.Close()
|
defer utils.Dbx.Close()
|
||||||
|
defer utils.Dbx_connection.Close(context.Background())
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
if err := utils.SetupRotes(router); err != nil {
|
if err := utils.SetupRotes(router); err != nil {
|
||||||
@ -26,5 +29,20 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Transaction
|
||||||
|
tx, _ := utils.Dbx_connection.Begin(context.Background())
|
||||||
|
defer tx.Rollback(context.Background())
|
||||||
|
|
||||||
|
repo := db_repo.New(tx)
|
||||||
|
users, _ := repo.GetAllUsers(context.Background())
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
fmt.Printf("%v\n", user.Username)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(context.Background()); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Printf("Hey!, %v", "you")
|
fmt.Printf("Hey!, %v", "you")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pgx connection to database
|
// Pgx connection to database
|
||||||
var Dbx *pgxpool.Pool
|
var Dbx *pgxpool.Pool
|
||||||
|
var Dbx_connection *pgx.Conn
|
||||||
|
|
||||||
func SetupDatabase() error {
|
func SetupDatabase() error {
|
||||||
|
|
||||||
@ -29,6 +31,7 @@ func SetupDatabase() error {
|
|||||||
|
|
||||||
// Connecting to database
|
// Connecting to database
|
||||||
Dbx, err = pgxpool.New(context.Background(), url)
|
Dbx, err = pgxpool.New(context.Background(), url)
|
||||||
|
Dbx_connection, err = pgx.Connect(context.Background(), url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Unable to connect")
|
fmt.Println("Unable to connect")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user