Skip to content

Commit 405b719

Browse files
committed
chore: add README.zh.md
1 parent 1ad7669 commit 405b719

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed

README.zh.md

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
# MySQL 連線池 (Golang)
2+
> 一個適用於 Golang 支持鏈式呼叫的 MySQL 包裝器,具備讀寫分離、查詢建構器和自動日誌記錄功能,提供完整的連線管理。<br>
3+
> Node.js 版本可在[這裡](https://github.com/pardnchiu/node-mysql-pool)取得<br>
4+
> PHP 版本可在[這裡](https://github.com/pardnchiu/php-mysql-pool)取得
5+
6+
[![license](https://img.shields.io/github/license/pardnchiu/go-mysql-pool)](https://github.com/pardnchiu/go-mysql-pool/blob/main/LICENSE)
7+
[![version](https://img.shields.io/github/v/tag/pardnchiu/go-mysql-pool)](https://github.com/pardnchiu/go-mysql-pool/releases)
8+
9+
## 功能特色
10+
11+
- **讀寫分離**:支援讀寫連線池配置,提升資料庫效能
12+
- **查詢建構器**:鏈式語法的 SQL 查詢建構介面,防止 SQL 注入攻擊
13+
- **自動重連**:連線失敗時自動重新建立連線
14+
- **並發安全**:執行緒安全的連線管理,支援高並發存取
15+
- **慢查詢日誌**:自動記錄超過閾值的查詢,便於效能調優
16+
- **動態配置**:運行時動態調整連線池大小
17+
- **CRUD 操作**:完整的新增、查詢、更新、刪除操作支援
18+
- **記憶體效率**:基於連線池的資源管理,實現最佳效能
19+
20+
## 依賴套件
21+
22+
- [`github.com/go-sql-driver/mysql`](https://github.com/go-sql-driver/mysql)
23+
- [`github.com/pardnchiu/go-logger`](https://github.com/pardnchiu/go-logger)
24+
25+
## 使用方法
26+
27+
### 安裝
28+
```bash
29+
go get github.com/pardnchiu/golang-mysql-pool
30+
```
31+
32+
### 初始化
33+
```go
34+
package main
35+
36+
import (
37+
"fmt"
38+
"log"
39+
40+
mysqlPool "github.com/pardnchiu/golang-mysql-pool"
41+
)
42+
43+
func main() {
44+
// 建立配置
45+
config := mysqlPool.Config{
46+
Read: &mysqlPool.DBConfig{
47+
Host: "localhost",
48+
Port: 3306,
49+
User: "root",
50+
Password: "password",
51+
Charset: "utf8mb4",
52+
Connection: 10,
53+
},
54+
Write: &mysqlPool.DBConfig{
55+
Host: "localhost",
56+
Port: 3306,
57+
User: "root",
58+
Password: "password",
59+
Charset: "utf8mb4",
60+
Connection: 5,
61+
},
62+
Log: &mysqlPool.Log{
63+
Path: "./logs/mysql-pool",
64+
},
65+
}
66+
67+
// 初始化連線池
68+
pool, err := mysqlPool.New(config)
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
defer pool.Close()
73+
74+
// 插入資料
75+
userData := map[string]interface{}{
76+
"name": "John Doe",
77+
"email": "john@example.com",
78+
"age": 30,
79+
}
80+
81+
lastID, err := pool.Write.
82+
DB("myapp").
83+
Table("users").
84+
Insert(userData)
85+
if err != nil {
86+
log.Fatal(err)
87+
}
88+
89+
fmt.Printf("插入使用者 ID: %d\n", lastID)
90+
91+
// 查詢資料
92+
rows, err := pool.Read.
93+
DB("myapp").
94+
Table("users").
95+
Select("id", "name", "email").
96+
Where("age", ">", 18).
97+
OrderBy("created_at", "DESC").
98+
Limit(10).
99+
Get()
100+
if err != nil {
101+
log.Fatal(err)
102+
}
103+
defer rows.Close()
104+
105+
for rows.Next() {
106+
var id int
107+
var name, email string
108+
err := rows.Scan(&id, &name, &email)
109+
if err != nil {
110+
log.Fatal(err)
111+
}
112+
fmt.Printf("使用者: %s (%s)\n", name, email)
113+
}
114+
}
115+
```
116+
117+
### 配置說明
118+
119+
```go
120+
type Config struct {
121+
Read *DBConfig
122+
Write *DBConfig
123+
Log *Log
124+
}
125+
126+
type DBConfig struct {
127+
Host string // 資料庫主機位址
128+
Port int // 資料庫連接埠
129+
User string // 資料庫使用者名稱
130+
Password string // 資料庫密碼
131+
Charset string // 字元集 (預設: utf8mb4)
132+
Connection int // 最大連線數
133+
}
134+
135+
type Log struct {
136+
Path string // 日誌目錄路徑
137+
Stdout bool // 啟用控制台輸出
138+
MaxSize int64 // 檔案輪轉前的最大大小
139+
MaxBackup int // 保留的日誌檔案數量
140+
}
141+
```
142+
143+
## 支援的操作
144+
145+
### 查詢建構器
146+
147+
```go
148+
// 基本查詢
149+
rows, err := pool.Read.
150+
DB("database_name").
151+
Table("users").
152+
Select("id", "name", "email").
153+
Where("status", "active").
154+
Get()
155+
156+
// 複雜條件查詢
157+
rows, err := pool.Read.
158+
DB("database_name").
159+
Table("users").
160+
Select("*").
161+
Where("age", ">", 18).
162+
Where("status", "active").
163+
Where("name", "LIKE", "John").
164+
OrderBy("created_at", "DESC").
165+
Limit(10).
166+
Offset(20).
167+
Get()
168+
169+
// JOIN 查詢
170+
rows, err := pool.Read.
171+
DB("database_name").
172+
Table("users").
173+
Select("users.name", "profiles.bio").
174+
LeftJoin("profiles", "users.id", "profiles.user_id").
175+
Where("users.status", "active").
176+
Get()
177+
178+
// 計算總數
179+
rows, err := pool.Read.
180+
DB("database_name").
181+
Table("users").
182+
Select("id", "name").
183+
Where("status", "active").
184+
Total().
185+
Limit(10).
186+
Get()
187+
```
188+
189+
### CRUD 操作
190+
191+
```go
192+
// 插入資料
193+
data := map[string]interface{}{
194+
"name": "Jane Doe",
195+
"email": "jane@example.com",
196+
"age": 25,
197+
}
198+
199+
lastID, err := pool.Write.
200+
DB("database_name").
201+
Table("users").
202+
Insert(data)
203+
204+
// 更新資料
205+
updateData := map[string]interface{}{
206+
"age": 26,
207+
"status": "updated",
208+
}
209+
210+
result, err := pool.Write.
211+
DB("database_name").
212+
Table("users").
213+
Where("id", 1).
214+
Update(updateData)
215+
216+
// Upsert 操作
217+
data := map[string]interface{}{
218+
"email": "unique@example.com",
219+
"name": "New User",
220+
}
221+
222+
updateData := map[string]interface{}{
223+
"name": "Updated User",
224+
"last_login": "NOW()",
225+
}
226+
227+
lastID, err := pool.Write.
228+
DB("database_name").
229+
Table("users").
230+
Upsert(data, updateData)
231+
232+
// 遞增值
233+
result, err := pool.Write.
234+
DB("database_name").
235+
Table("users").
236+
Where("id", 1).
237+
Increase("view_count", 1).
238+
Update()
239+
```
240+
241+
### 直接 SQL 操作
242+
243+
```go
244+
// 直接查詢
245+
rows, err := pool.Read.Query("SELECT * FROM users WHERE age > ?", 18)
246+
247+
// 直接執行
248+
result, err := pool.Write.Exec("UPDATE users SET last_login = NOW() WHERE id = ?", userID)
249+
```
250+
251+
## 核心功能
252+
253+
### 連線池管理
254+
255+
- **New** - 建立新的連線池
256+
```go
257+
pool, err := mysqlPool.New(config)
258+
```
259+
- 初始化讀寫分離的連線池
260+
- 設定日誌系統
261+
- 驗證資料庫連線可用性
262+
263+
- **Close** - 關閉連線池
264+
```go
265+
err := pool.Close()
266+
```
267+
- 關閉所有連線
268+
- 等待進行中的查詢完成
269+
- 釋放系統資源
270+
271+
### 查詢建構
272+
273+
- **DB** - 指定資料庫
274+
```go
275+
builder := pool.Read.DB("database_name")
276+
```
277+
278+
- **Table** - 指定資料表
279+
```go
280+
builder := builder.Table("table_name")
281+
```
282+
283+
- **Select** - 選擇欄位
284+
```go
285+
builder := builder.Select("col1", "col2", "col3")
286+
```
287+
288+
- **Where** - 篩選條件
289+
```go
290+
builder := builder.Where("column", "value")
291+
builder := builder.Where("column", ">", "value")
292+
builder := builder.Where("column", "LIKE", "pattern")
293+
```
294+
295+
- **Join** - 資料表聯結
296+
```go
297+
builder := builder.LeftJoin("table2", "table1.id", "table2.foreign_id")
298+
builder := builder.RightJoin("table2", "table1.id", "table2.foreign_id")
299+
builder := builder.InnerJoin("table2", "table1.id", "table2.foreign_id")
300+
```
301+
302+
### 資料操作
303+
304+
- **Insert** - 插入資料
305+
```go
306+
lastID, err := builder.Insert(data)
307+
```
308+
309+
- **Update** - 更新資料
310+
```go
311+
result, err := builder.Update(data)
312+
```
313+
314+
- **Upsert** - 插入或更新
315+
```go
316+
lastID, err := builder.Upsert(insertData, updateData)
317+
```
318+
319+
## 授權條款
320+
321+
此原始碼專案採用 [MIT](https://github.com/pardnchiu/go-mysql-pool/blob/main/LICENSE) 授權條款。
322+
323+
## 作者
324+
325+
<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;">
326+
327+
<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4>
328+
329+
<a href="mailto:dev@pardn.io" target="_blank">
330+
<img src="https://pardn.io/image/email.svg" width="48" height="48">
331+
</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank">
332+
<img src="https://pardn.io/image/linkedin.svg" width="48" height="48">
333+
</a>
334+
335+
***
336+
337+
©️ 2025 [邱敬幃 Pardn Chiu](https://pardn.io)

0 commit comments

Comments
 (0)