u
This commit is contained in:
1 parent
d03ac2b00a
commit
cf4291652a
43 files changed
+359
-6254
No files matched your search
@@ -1,56 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"cn/oneao/base-go/config"
|
||||
"cn/oneao/base-go/ent"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var Client *ent.Client
|
||||
|
||||
// InitDB 初始化 PostgreSQL 数据库连接(使用 sql.DB 配置连接池)
|
||||
func InitDB() {
|
||||
// 处理 SSLMode
|
||||
sslmode := "disable"
|
||||
if config.Db.SllMode {
|
||||
sslmode = "require"
|
||||
}
|
||||
|
||||
// 构建 PostgreSQL DSN
|
||||
dsn := fmt.Sprintf(
|
||||
"user=%s password=%s dbname=%s host=%s port=%d sslmode=%s TimeZone=%s",
|
||||
config.Db.User,
|
||||
config.Db.Password,
|
||||
config.Db.Dbname,
|
||||
config.Db.Host,
|
||||
config.Db.Port,
|
||||
sslmode,
|
||||
config.Db.TimeZone,
|
||||
)
|
||||
|
||||
// 使用 sql.Open 创建原生 *sql.DB 对象
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("❌ 数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
// 配置连接池
|
||||
db.SetMaxOpenConns(config.Db.MaxOpenConns)
|
||||
db.SetMaxIdleConns(config.Db.MaxIdleConns)
|
||||
db.SetConnMaxLifetime(config.Db.ConnMaxLifetime)
|
||||
|
||||
// 使用 sql.DB 创建 ent.Driver
|
||||
drv := entsql.OpenDB(dialect.Postgres, db)
|
||||
|
||||
// 创建 ent.Client
|
||||
client := ent.NewClient(ent.Driver(drv))
|
||||
|
||||
Client = client
|
||||
log.Println("✅ 数据库连接完成")
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// db/db.go
|
||||
package db
|
||||
|
||||
import (
|
||||
"cn/oneao/base-go/config"
|
||||
sqlcg "cn/oneao/base-go/db/sqlc"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *pgxpool.Pool
|
||||
DBClient *sqlcg.Queries
|
||||
)
|
||||
|
||||
func InitDB() error {
|
||||
cfg := config.Db
|
||||
|
||||
sslMode := "disable"
|
||||
if cfg.SllMode {
|
||||
sslMode = "require"
|
||||
}
|
||||
|
||||
// 构建 DSN
|
||||
dsn := fmt.Sprintf(
|
||||
"postgres://%s:%s@%s:%d/%s?sslmode=%s&TimeZone=%s",
|
||||
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Dbname, sslMode, cfg.TimeZone,
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to connect to database: %w", err)
|
||||
}
|
||||
|
||||
// 测试连接
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
return fmt.Errorf("ping database failed: %w", err)
|
||||
}
|
||||
|
||||
DB = pool
|
||||
|
||||
// 初始化 sqlc Queries,全局可用
|
||||
DBClient = sqlcg.New(DB)
|
||||
|
||||
// 可选:设置连接池参数
|
||||
pool.Config().MaxConns = int32(cfg.MaxOpenConns)
|
||||
pool.Config().MinConns = int32(cfg.MaxIdleConns)
|
||||
pool.Config().MaxConnLifetime = cfg.ConnMaxLifetime
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
-- name: CreateUser :execrows
|
||||
-- 插入用户并返回刚创建的记录
|
||||
INSERT INTO user_info (id, account, password)
|
||||
VALUES ($1, $2, $3);
|
||||
|
||||
-- name: GetUserById :one
|
||||
-- 根据 ID 查询单条用户记录
|
||||
SELECT *
|
||||
FROM user_info
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: GetUserByAccountAndPassword :one
|
||||
-- 根据账号和密码查询单条用户记录(登录用)
|
||||
SELECT *
|
||||
FROM user_info
|
||||
WHERE account = $1
|
||||
AND password = $2;
|
||||
|
||||
-- name: UpdateUserLoginLastTime :execrows
|
||||
-- 更新用户最后登录时间,并返回影响的行数
|
||||
UPDATE user_info
|
||||
SET last_login_time = now()
|
||||
WHERE id = $1;
|
||||
@@ -0,0 +1,42 @@
|
||||
CREATE TABLE user_info (
|
||||
id BIGINT PRIMARY KEY,
|
||||
seq_no SERIAL, -- 自增
|
||||
account VARCHAR(255) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) UNIQUE,
|
||||
username VARCHAR(50),
|
||||
gender INT DEFAULT 0,
|
||||
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
last_login_time TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE money_record(
|
||||
id BIGINT PRIMARY KEY,
|
||||
category_id BIGINT NOT NULL,
|
||||
user_id BIGINT NOT NULL,
|
||||
amount NUMERIC(12,2) NOT NULL,
|
||||
remark VARCHAR(255) DEFAULT '',
|
||||
record_time timestamp NOT NULL,
|
||||
create_time timestamp DEFAULT CURRENT_TIMESTAMP,
|
||||
update_time timestamp DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE money_sys_category(
|
||||
id BIGINT PRIMARY KEY ,
|
||||
name VARCHAR(255) NOT NULL ,
|
||||
icon VARCHAR(255) NOT NULL,
|
||||
type SMALLINT DEFAULT 0,
|
||||
sort_number INT DEFAULT 0,
|
||||
create_time timestamp DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE money_user_category(
|
||||
id BIGINT PRIMARY KEY ,
|
||||
user_id BIGINT NOT NULL,
|
||||
name VARCHAR(255) NOT NULL ,
|
||||
icon VARCHAR(255) NOT NULL,
|
||||
type SMALLINT DEFAULT 0,
|
||||
sort_number INT DEFAULT 0,
|
||||
create_time timestamp DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package sqlcg
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package sqlcg
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type MoneyRecord struct {
|
||||
ID int64
|
||||
CategoryID int64
|
||||
UserID int64
|
||||
Amount pgtype.Numeric
|
||||
Remark pgtype.Text
|
||||
RecordTime pgtype.Timestamp
|
||||
CreateTime pgtype.Timestamp
|
||||
UpdateTime pgtype.Timestamp
|
||||
}
|
||||
|
||||
type MoneySysCategory struct {
|
||||
ID int64
|
||||
Name string
|
||||
Icon string
|
||||
Type pgtype.Int2
|
||||
SortNumber pgtype.Int4
|
||||
CreateTime pgtype.Timestamp
|
||||
}
|
||||
|
||||
type MoneyUserCategory struct {
|
||||
ID int64
|
||||
UserID int64
|
||||
Name string
|
||||
Icon string
|
||||
Type pgtype.Int2
|
||||
SortNumber pgtype.Int4
|
||||
CreateTime pgtype.Timestamp
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
ID int64
|
||||
SeqNo pgtype.Int4
|
||||
Account string
|
||||
Password string
|
||||
Email pgtype.Text
|
||||
Username pgtype.Text
|
||||
Gender pgtype.Int4
|
||||
CreateTime pgtype.Timestamp
|
||||
UpdateTime pgtype.Timestamp
|
||||
LastLoginTime pgtype.Timestamp
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: user_info.sql
|
||||
|
||||
package sqlcg
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :execrows
|
||||
INSERT INTO user_info (id, account, password)
|
||||
VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
ID int64
|
||||
Account string
|
||||
Password string
|
||||
}
|
||||
|
||||
// 插入用户并返回刚创建的记录
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, createUser, arg.ID, arg.Account, arg.Password)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getUserByAccountAndPassword = `-- name: GetUserByAccountAndPassword :one
|
||||
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
|
||||
FROM user_info
|
||||
WHERE account = $1
|
||||
AND password = $2
|
||||
`
|
||||
|
||||
type GetUserByAccountAndPasswordParams struct {
|
||||
Account string
|
||||
Password string
|
||||
}
|
||||
|
||||
// 根据账号和密码查询单条用户记录(登录用)
|
||||
func (q *Queries) GetUserByAccountAndPassword(ctx context.Context, arg GetUserByAccountAndPasswordParams) (UserInfo, error) {
|
||||
row := q.db.QueryRow(ctx, getUserByAccountAndPassword, arg.Account, arg.Password)
|
||||
var i UserInfo
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SeqNo,
|
||||
&i.Account,
|
||||
&i.Password,
|
||||
&i.Email,
|
||||
&i.Username,
|
||||
&i.Gender,
|
||||
&i.CreateTime,
|
||||
&i.UpdateTime,
|
||||
&i.LastLoginTime,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserById = `-- name: GetUserById :one
|
||||
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
|
||||
FROM user_info
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
// 根据 ID 查询单条用户记录
|
||||
func (q *Queries) GetUserById(ctx context.Context, id int64) (UserInfo, error) {
|
||||
row := q.db.QueryRow(ctx, getUserById, id)
|
||||
var i UserInfo
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SeqNo,
|
||||
&i.Account,
|
||||
&i.Password,
|
||||
&i.Email,
|
||||
&i.Username,
|
||||
&i.Gender,
|
||||
&i.CreateTime,
|
||||
&i.UpdateTime,
|
||||
&i.LastLoginTime,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateUserLoginLastTime = `-- name: UpdateUserLoginLastTime :execrows
|
||||
UPDATE user_info
|
||||
SET last_login_time = now()
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
// 更新用户最后登录时间,并返回影响的行数
|
||||
func (q *Queries) UpdateUserLoginLastTime(ctx context.Context, id int64) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateUserLoginLastTime, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
Reference in new issue
Block a user