Skip to content

Commit 26afbfe

Browse files
committed
feat: add sqlite support(#75)
Signed-off-by: closetool <[email protected]>
1 parent d4ad5d9 commit 26afbfe

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
.glide/
1515

1616
.idea/
17-
*.iml
17+
*.iml
18+
19+
casbin.db

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ script:
1313

1414
services:
1515
- mysql
16-
- postgresql
16+
- postgresql

adapter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/jackc/pgconn"
2626
"gorm.io/driver/mysql"
2727
"gorm.io/driver/postgres"
28+
"gorm.io/driver/sqlite"
2829
"gorm.io/driver/sqlserver"
2930
"gorm.io/gorm"
3031
)
@@ -210,10 +211,10 @@ func openDBConnection(driverName, dataSourceName string) (*gorm.DB, error) {
210211
db, err = gorm.Open(postgres.Open(dataSourceName), &gorm.Config{})
211212
} else if driverName == "mysql" {
212213
db, err = gorm.Open(mysql.Open(dataSourceName), &gorm.Config{})
213-
//} else if driverName == "sqlite3" {
214-
// db, err = gorm.Open(sqlite.Open(dataSourceName), &gorm.Config{})
215214
} else if driverName == "sqlserver" {
216215
db, err = gorm.Open(sqlserver.Open(dataSourceName), &gorm.Config{})
216+
} else if driverName == "sqlite3" {
217+
db, err = gorm.Open(sqlite.Open(dataSourceName), &gorm.Config{})
217218
} else {
218219
return nil, errors.New("database dialect is not supported")
219220
}

adapter_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package gormadapter
1616

1717
import (
1818
"github.com/jackc/pgconn"
19+
"gorm.io/driver/sqlite"
1920
"log"
2021
"testing"
2122

@@ -269,6 +270,10 @@ func TestAdapters(t *testing.T) {
269270
testAutoSave(t, a)
270271
testSaveLoad(t, a)
271272

273+
a = initAdapter(t, "sqlite3", "casbin.db")
274+
testAutoSave(t, a)
275+
testSaveLoad(t, a)
276+
272277
db, err := gorm.Open(mysql.Open("root:@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{})
273278
if err != nil {
274279
panic(err)
@@ -291,6 +296,17 @@ func TestAdapters(t *testing.T) {
291296
a = initAdapterWithGormInstance(t, db)
292297
testFilteredPolicy(t, a)
293298

299+
db, err = gorm.Open(sqlite.Open("casbin.db"), &gorm.Config{})
300+
if err != nil {
301+
panic(err)
302+
}
303+
a = initAdapterWithGormInstance(t, db)
304+
testAutoSave(t, a)
305+
testSaveLoad(t, a)
306+
307+
a = initAdapterWithGormInstance(t, db)
308+
testFilteredPolicy(t, a)
309+
294310
db, err = gorm.Open(mysql.Open("root:@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{})
295311
if err != nil {
296312
panic(err)
@@ -313,6 +329,20 @@ func TestAdapters(t *testing.T) {
313329
a = initAdapterWithGormInstanceByName(t, db, "casbin_rule")
314330
testFilteredPolicy(t, a)
315331

332+
db, err = gorm.Open(sqlite.Open("casbin.db"), &gorm.Config{})
333+
if err != nil {
334+
panic(err)
335+
}
336+
a = initAdapterWithGormInstanceByName(t, db, "casbin_rule")
337+
testAutoSave(t, a)
338+
testSaveLoad(t, a)
339+
340+
a = initAdapterWithGormInstanceByName(t, db, "casbin_rule")
341+
testFilteredPolicy(t, a)
342+
316343
a = initAdapter(t, "mysql", "root:@tcp(127.0.0.1:3306)/", "casbin", "casbin_rule")
317344
testUpdatePolicy(t, a)
345+
346+
a = initAdapter(t, "sqlite3", "casbin.db")
347+
testUpdatePolicy(t, a)
318348
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/stretchr/testify v1.6.1
1111
gorm.io/driver/mysql v1.0.1
1212
gorm.io/driver/postgres v1.0.1
13+
gorm.io/driver/sqlite v1.1.4
1314
gorm.io/driver/sqlserver v1.0.4
1415
gorm.io/gorm v1.20.7
1516
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
101101
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
102102
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
103103
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
104+
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
105+
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
104106
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
105107
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
106108
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -188,6 +190,8 @@ gorm.io/driver/mysql v1.0.1 h1:omJoilUzyrAp0xNoio88lGJCroGdIOen9hq2A/+3ifw=
188190
gorm.io/driver/mysql v1.0.1/go.mod h1:KtqSthtg55lFp3S5kUXqlGaelnWpKitn4k1xZTnoiPw=
189191
gorm.io/driver/postgres v1.0.1 h1:jRfDNUxpxNrea/97kbcscAQGmiks4UCKAYXsvh4rhOQ=
190192
gorm.io/driver/postgres v1.0.1/go.mod h1:pv4dVhHvEVrP7k/UYqdBIllbdbpB5VTz89X1O0uOrCA=
193+
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
194+
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
191195
gorm.io/driver/sqlserver v1.0.4 h1:V15fszi0XAo7fbx3/cF50ngshDSN4QT0MXpWTylyPTY=
192196
gorm.io/driver/sqlserver v1.0.4/go.mod h1:ciEo5btfITTBCj9BkoUVDvgQbUdLWQNqdFY5OGuGnRg=
193197
gorm.io/gorm v1.9.19/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=

0 commit comments

Comments
 (0)