Skip to content

Commit 5b61801

Browse files
committed
add: sqlite3 support
1 parent 500d633 commit 5b61801

6 files changed

Lines changed: 24 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
/mkblog.pid
1111
/nohup.out
1212
/test_add_article.sh
13-
/app.log
13+
/app.log
14+
/app.db

config.yaml.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
database:
2-
host: localhost # 数据库地址
2+
host: data/app.db # 数据库地址,如果启用sqlite3,只需要在 host 填写路径,其他不用管
33
port: 3306 # 数据库端口
44
user: root # 数据库用户
55
password: root # 数据库密码
66
name: mkblog # 数据库名称
7-
kind: mysql # 数据库类型,支持mysql和postgres
7+
kind: sqlite3 # 数据库类型,支持mysql和postgres以及sqlite3
88

99
tls:
1010
enabled: false # 是否启用TLS

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1414
gorm.io/driver/mysql v1.6.0
1515
gorm.io/driver/postgres v1.6.0
16+
gorm.io/driver/sqlite v1.6.0
1617
gorm.io/gorm v1.30.2
1718
)
1819

@@ -51,6 +52,7 @@ require (
5152
github.com/kr/text v0.2.0 // indirect
5253
github.com/leodido/go-urn v1.4.0 // indirect
5354
github.com/mattn/go-isatty v0.0.20 // indirect
55+
github.com/mattn/go-sqlite3 v1.14.22 // indirect
5456
github.com/miekg/dns v1.1.69 // indirect
5557
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5658
github.com/modern-go/reflect2 v1.0.2 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
153153
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
154154
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
155155
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
156+
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
157+
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
156158
github.com/miekg/dns v1.1.69 h1:Kb7Y/1Jo+SG+a2GtfoFUfDkG//csdRPwRLkCsxDG9Sc=
157159
github.com/miekg/dns v1.1.69/go.mod h1:7OyjD9nEba5OkqQ/hB4fy3PIoxafSZJtducccIelz3g=
158160
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -374,6 +376,8 @@ gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
374376
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
375377
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
376378
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
379+
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
380+
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
377381
gorm.io/gorm v1.30.2 h1:f7bevlVoVe4Byu3pmbWPVHnPsLoWaMjEb7/clyr9Ivs=
378382
gorm.io/gorm v1.30.2/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
379383
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pkg/database/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func Init() error {
4040
break
4141
}
4242
time.Sleep(time.Duration(i<<2) * time.Microsecond) // 指数退避
43+
slog.Warn("failed to connect database", "dsn", dsn)
4344
if i == retryTimes-1 {
4445
return err
4546
}

pkg/database/utils.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"fmt"
55
"log/slog"
66
"mkBlog/config"
7+
"strings"
78
"time"
89

910
"gorm.io/driver/mysql"
1011
"gorm.io/driver/postgres"
12+
"gorm.io/driver/sqlite"
1113
"gorm.io/gorm"
1214
)
1315

1416
func getDSN() string {
15-
switch config.Cfg.Database.Kind {
17+
switch strings.ToLower(config.Cfg.Database.Kind) {
1618
case "mysql":
1719
return config.Cfg.Database.User + ":" + config.Cfg.Database.Password +
1820
"@tcp(" + config.Cfg.Database.Host + ":" + config.Cfg.Database.Port + ")/" +
@@ -24,12 +26,14 @@ func getDSN() string {
2426
" password=" + config.Cfg.Database.Password +
2527
" dbname=" + config.Cfg.Database.Name +
2628
" sslmode=disable TimeZone=UTC"
29+
case "sqlite3":
30+
return config.Cfg.Database.Host
2731
}
2832
return ""
2933
}
3034

3135
func openDatabase(dsn string) (*gorm.DB, error) {
32-
switch config.Cfg.Database.Kind {
36+
switch strings.ToLower(config.Cfg.Database.Kind) {
3337
case "mysql":
3438
return gorm.Open(mysql.Open(dsn), &gorm.Config{
3539
NowFunc: func() time.Time { return time.Now().UTC() },
@@ -38,12 +42,16 @@ func openDatabase(dsn string) (*gorm.DB, error) {
3842
return gorm.Open(postgres.Open(dsn), &gorm.Config{
3943
NowFunc: func() time.Time { return time.Now().UTC() },
4044
})
45+
case "sqlite3":
46+
return gorm.Open(sqlite.Open(dsn), &gorm.Config{
47+
NowFunc: func() time.Time { return time.Now().UTC() },
48+
})
4149
}
4250
return nil, fmt.Errorf("unsupported database kind: %s", config.Cfg.Database.Kind)
4351
}
4452

4553
func createFullTextIndex() {
46-
switch config.Cfg.Database.Kind {
54+
switch strings.ToLower(config.Cfg.Database.Kind) {
4755
case "mysql":
4856
db.Exec(createMySQLFullTextIndexSQL)
4957
case "postgres":
@@ -54,5 +62,7 @@ func createFullTextIndex() {
5462
db.Exec(createPostgresChineseDictionarySQL)
5563
db.Exec(createPostgresDictionaryMappingSQL)
5664
db.Exec(createPostgresFullTextIndexSQL)
65+
case "sqlite3":
66+
return
5767
}
5868
}

0 commit comments

Comments
 (0)