@@ -16,6 +16,7 @@ package xormadapter
1616
1717import (
1818 "errors"
19+ "log"
1920 "runtime"
2021 "strings"
2122
@@ -25,22 +26,25 @@ import (
2526 "xorm.io/xorm"
2627)
2728
29+ // TableName if tableName=="" , adapter will use default tablename "casbin_rule".
2830func (the * CasbinRule ) TableName () string {
2931 if len (the .tableName ) == 0 {
3032 return "casbin_rule"
3133 }
3234 return the .tableName
3335}
3436
37+ // CasbinRule .
3538type CasbinRule struct {
36- PType string `xorm:"varchar(100) index not null default ''"`
37- V0 string `xorm:"varchar(100) index not null default ''"`
38- V1 string `xorm:"varchar(100) index not null default ''"`
39- V2 string `xorm:"varchar(100) index not null default ''"`
40- V3 string `xorm:"varchar(100) index not null default ''"`
41- V4 string `xorm:"varchar(100) index not null default ''"`
42- V5 string `xorm:"varchar(100) index not null default ''"`
43- tableName string `xorm:"-" json:"-"`
39+ PType string `xorm:"varchar(100) index not null default ''"`
40+ V0 string `xorm:"varchar(100) index not null default ''"`
41+ V1 string `xorm:"varchar(100) index not null default ''"`
42+ V2 string `xorm:"varchar(100) index not null default ''"`
43+ V3 string `xorm:"varchar(100) index not null default ''"`
44+ V4 string `xorm:"varchar(100) index not null default ''"`
45+ V5 string `xorm:"varchar(100) index not null default ''"`
46+
47+ tableName string `xorm:"-"`
4448}
4549
4650// Adapter represents the Xorm adapter for policy storage.
@@ -53,6 +57,7 @@ type Adapter struct {
5357 tableName string
5458}
5559
60+ // Filter .
5661type Filter struct {
5762 PType []string
5863 V0 []string
@@ -65,9 +70,13 @@ type Filter struct {
6570
6671// finalizer is the destructor for Adapter.
6772func finalizer (a * Adapter ) {
73+ if a .engine == nil {
74+ return
75+ }
76+
6877 err := a .engine .Close ()
6978 if err != nil {
70- panic ( err )
79+ log . Printf ( "close xorm adapter engine failed, err: %v" , err )
7180 }
7281}
7382
@@ -102,6 +111,7 @@ func NewAdapter(driverName string, dataSourceName string, dbSpecified ...bool) (
102111 return a , nil
103112}
104113
114+ // NewAdapterWithTableName .
105115func NewAdapterWithTableName (driverName string , dataSourceName string , tableName string , dbSpecified ... bool ) (* Adapter , error ) {
106116 a := & Adapter {
107117 driverName : driverName ,
@@ -129,6 +139,7 @@ func NewAdapterWithTableName(driverName string, dataSourceName string, tableName
129139 return a , nil
130140}
131141
142+ // NewAdapterByEngine .
132143func NewAdapterByEngine (engine * xorm.Engine ) (* Adapter , error ) {
133144 a := & Adapter {
134145 engine : engine ,
@@ -142,6 +153,7 @@ func NewAdapterByEngine(engine *xorm.Engine) (*Adapter, error) {
142153 return a , nil
143154}
144155
156+ // NewAdapterByEngineWithTableName .
145157func NewAdapterByEngineWithTableName (engine * xorm.Engine , tableName string ) (* Adapter , error ) {
146158 a := & Adapter {
147159 engine : engine ,
@@ -218,16 +230,6 @@ func (a *Adapter) open() error {
218230 return a .createTable ()
219231}
220232
221- func (a * Adapter ) close () error {
222- err := a .engine .Close ()
223- if err != nil {
224- return err
225- }
226-
227- a .engine = nil
228- return nil
229- }
230-
231233func (a * Adapter ) createTable () error {
232234 return a .engine .Sync2 (& CasbinRule {tableName : a .tableName })
233235}
@@ -259,7 +261,8 @@ func loadPolicyLine(line *CasbinRule, model model.Model) {
259261
260262// LoadPolicy loads policy from database.
261263func (a * Adapter ) LoadPolicy (model model.Model ) error {
262- var lines []* CasbinRule
264+ lines := make ([]* CasbinRule , 0 , 64 )
265+
263266 if err := a .engine .Find (& lines ); err != nil {
264267 return err
265268 }
@@ -271,8 +274,8 @@ func (a *Adapter) LoadPolicy(model model.Model) error {
271274 return nil
272275}
273276
274- func (a * Adapter ) savePolicyLine (ptype string , rule []string ) * CasbinRule {
275- line := & CasbinRule {PType : ptype , tableName : a .tableName }
277+ func (a * Adapter ) genPolicyLine (ptype string , rule []string ) * CasbinRule {
278+ line := CasbinRule {PType : ptype , tableName : a .tableName }
276279
277280 l := len (rule )
278281 if l > 0 {
@@ -294,7 +297,7 @@ func (a *Adapter) savePolicyLine(ptype string, rule []string) *CasbinRule {
294297 line .V5 = rule [5 ]
295298 }
296299
297- return line
300+ return & line
298301}
299302
300303// SavePolicy saves policy to database.
@@ -308,39 +311,40 @@ func (a *Adapter) SavePolicy(model model.Model) error {
308311 return err
309312 }
310313
311- var lines []* CasbinRule
314+ lines := make ( []* CasbinRule , 0 , 64 )
312315
313316 for ptype , ast := range model ["p" ] {
314317 for _ , rule := range ast .Policy {
315- line := a .savePolicyLine (ptype , rule )
318+ line := a .genPolicyLine (ptype , rule )
316319 lines = append (lines , line )
317320 }
318321 }
319322
320323 for ptype , ast := range model ["g" ] {
321324 for _ , rule := range ast .Policy {
322- line := a .savePolicyLine (ptype , rule )
325+ line := a .genPolicyLine (ptype , rule )
323326 lines = append (lines , line )
324327 }
325328 }
326329
327330 _ , err = a .engine .Insert (& lines )
331+
328332 return err
329333}
330334
331335// AddPolicy adds a policy rule to the storage.
332336func (a * Adapter ) AddPolicy (sec string , ptype string , rule []string ) error {
333- line := a .savePolicyLine (ptype , rule )
334- _ , err := a .engine .Insert (line )
337+ line := a .genPolicyLine (ptype , rule )
338+ _ , err := a .engine .InsertOne (line )
335339 return err
336340}
337341
338342// AddPolicies adds multiple policy rule to the storage.
339343func (a * Adapter ) AddPolicies (sec string , ptype string , rules [][]string ) error {
340344 _ , err := a .engine .Transaction (func (tx * xorm.Session ) (interface {}, error ) {
341345 for _ , rule := range rules {
342- line := a .savePolicyLine (ptype , rule )
343- _ , err := tx .Insert (line )
346+ line := a .genPolicyLine (ptype , rule )
347+ _ , err := tx .InsertOne (line )
344348 if err != nil {
345349 return nil , err
346350 }
@@ -352,16 +356,16 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
352356
353357// RemovePolicy removes a policy rule from the storage.
354358func (a * Adapter ) RemovePolicy (sec string , ptype string , rule []string ) error {
355- line := a .savePolicyLine (ptype , rule )
359+ line := a .genPolicyLine (ptype , rule )
356360 _ , err := a .engine .Delete (line )
357361 return err
358362}
359363
360- // ReovRemovePolicies removes multiple policy rule from the storage.
364+ // RemovePolicies removes multiple policy rule from the storage.
361365func (a * Adapter ) RemovePolicies (sec string , ptype string , rules [][]string ) error {
362366 _ , err := a .engine .Transaction (func (tx * xorm.Session ) (interface {}, error ) {
363367 for _ , rule := range rules {
364- line := a .savePolicyLine (ptype , rule )
368+ line := a .genPolicyLine (ptype , rule )
365369 _ , err := tx .Delete (line )
366370 if err != nil {
367371 return nil , nil
@@ -374,7 +378,7 @@ func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) err
374378
375379// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
376380func (a * Adapter ) RemoveFilteredPolicy (sec string , ptype string , fieldIndex int , fieldValues ... string ) error {
377- line := & CasbinRule {PType : ptype , tableName : a .tableName }
381+ line := CasbinRule {PType : ptype , tableName : a .tableName }
378382
379383 idx := fieldIndex + len (fieldValues )
380384 if fieldIndex <= 0 && idx > 0 {
@@ -396,18 +400,18 @@ func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int,
396400 line .V5 = fieldValues [5 - fieldIndex ]
397401 }
398402
399- _ , err := a .engine .Delete (line )
403+ _ , err := a .engine .Delete (& line )
400404 return err
401405}
402406
403407// LoadFilteredPolicy loads only policy rules that match the filter.
404408func (a * Adapter ) LoadFilteredPolicy (model model.Model , filter interface {}) error {
405- var lines []* CasbinRule
406-
407409 filterValue , ok := filter .(Filter )
408410 if ! ok {
409411 return errors .New ("invalid filter type" )
410412 }
413+
414+ lines := make ([]* CasbinRule , 0 , 64 )
411415 if err := a .filterQuery (a .engine .NewSession (), filterValue ).Find (& lines ); err != nil {
412416 return err
413417 }
@@ -425,26 +429,29 @@ func (a *Adapter) IsFiltered() bool {
425429}
426430
427431func (a * Adapter ) filterQuery (session * xorm.Session , filter Filter ) * xorm.Session {
428- if len (filter .PType ) > 0 {
429- session = session .In ("p_type" , filter .PType )
430- }
431- if len (filter .V0 ) > 0 {
432- session = session .In ("v0" , filter .V0 )
433- }
434- if len (filter .V1 ) > 0 {
435- session = session .In ("v1" , filter .V1 )
436- }
437- if len (filter .V2 ) > 0 {
438- session = session .In ("v2" , filter .V2 )
439- }
440- if len (filter .V3 ) > 0 {
441- session = session .In ("v3" , filter .V3 )
442- }
443- if len (filter .V4 ) > 0 {
444- session = session .In ("v4" , filter .V4 )
445- }
446- if len (filter .V5 ) > 0 {
447- session = session .In ("v5" , filter .V5 )
432+ filterValue := [7 ]struct {
433+ col string
434+ val []string
435+ }{
436+ {"p_type" , filter .PType },
437+ {"v0" , filter .V0 },
438+ {"v1" , filter .V1 },
439+ {"v2" , filter .V2 },
440+ {"v3" , filter .V3 },
441+ {"v4" , filter .V4 },
442+ {"v5" , filter .V5 },
443+ }
444+
445+ for idx := range filterValue {
446+ switch len (filterValue [idx ].val ) {
447+ case 0 :
448+ continue
449+ case 1 :
450+ session .And (filterValue [idx ].col + " = ?" , filterValue [idx ].val [0 ])
451+ default :
452+ session .In (filterValue [idx ].col , filterValue [idx ].val )
453+ }
448454 }
455+
449456 return session
450457}
0 commit comments