forked from Blank-Xu/sql-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
711 lines (561 loc) · 16.6 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// Copyright 2020 by Blank-Xu. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqladapter
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
)
const (
// defaultTableName if tableName == "", the Adapter will use this default table name.
defaultTableName = "casbin_rule"
// maxParamLength .
maxParamLength = 7
)
// CasbinRule defines the casbin rule model.
// It used for save or load policy lines from connected database.
type CasbinRule struct {
PType string
V0 string
V1 string
V2 string
V3 string
V4 string
V5 string
}
// Adapter defines the database adapter for Casbin.
// It can load policy lines from connected database or save policy lines.
type Adapter struct {
db *sql.DB
ctx context.Context
driverName string
tableName string
isFiltered bool
sqlPlaceHolder string
sqlCreateTable string
sqlTruncateTable string
sqlIsTableExist string
sqlInsertRow string
sqlUpdateRow string
sqlDeleteAll string
sqlDeleteRow string
sqlDeleteByArgs string
sqlSelectAll string
sqlSelectWhere string
}
// Filter defines the filtering rules for a FilteredAdapter's policy.
// Empty values are ignored, but all others must match the filter.
type Filter struct {
PType []string
V0 []string
V1 []string
V2 []string
V3 []string
V4 []string
V5 []string
}
// NewAdapter the constructor for Adapter.
// db should connected to database and controlled by user.
// If tableName == "", the Adapter will automatically create a table named "casbin_rule".
func NewAdapter(db *sql.DB, driverName, tableName string) (*Adapter, error) {
return NewAdapterContext(context.Background(), db, driverName, tableName)
}
// NewAdapterContext the constructor for Adapter.
// db should connected to database and controlled by user.
// If tableName == "", the Adapter will automatically create a table named "casbin_rule".
func NewAdapterContext(ctx context.Context, db *sql.DB, driverName, tableName string) (*Adapter, error) {
if db == nil {
return nil, errors.New("db is nil")
}
switch driverName {
case "":
return nil, errors.New("need driverName param")
case "oci8", "ora", "goracle":
return nil, errors.New("sqladapter: please checkout 'oracle' branch")
}
// check db connecting
err := db.PingContext(ctx)
if err != nil {
return nil, err
}
if tableName == "" {
tableName = defaultTableName
}
adapter := Adapter{
db: db,
ctx: ctx,
driverName: driverName,
tableName: tableName,
}
// generate different databases sql
adapter.genSQL()
if !adapter.isTableExist() {
if err = adapter.createTable(); err != nil {
return nil, err
}
}
return &adapter, nil
}
// genSQL generate sql based on db driver name.
func (p *Adapter) genSQL() {
p.sqlPlaceHolder = sqlPlaceHolder
p.sqlCreateTable = fmt.Sprintf(sqlCreateTable, p.tableName)
p.sqlTruncateTable = fmt.Sprintf(sqlTruncateTable, p.tableName)
p.sqlIsTableExist = fmt.Sprintf(sqlIsTableExist, p.tableName)
p.sqlInsertRow = fmt.Sprintf(sqlInsertRow, p.tableName)
p.sqlUpdateRow = fmt.Sprintf(sqlUpdateRow, p.tableName)
p.sqlDeleteAll = fmt.Sprintf(sqlDeleteAll, p.tableName)
p.sqlDeleteRow = fmt.Sprintf(sqlDeleteRow, p.tableName)
p.sqlDeleteByArgs = fmt.Sprintf(sqlDeleteByArgs, p.tableName)
p.sqlSelectAll = fmt.Sprintf(sqlSelectAll, p.tableName)
p.sqlSelectWhere = fmt.Sprintf(sqlSelectWhere, p.tableName)
switch p.driverName {
case "postgres", "pgx", "pq-timeouts", "cloudsqlpostgres":
p.sqlPlaceHolder = sqlPlaceHolderPostgres
p.sqlCreateTable = fmt.Sprintf(sqlCreateTablePostgres, p.tableName)
p.sqlInsertRow = fmt.Sprintf(sqlInsertRowPostgres, p.tableName)
p.sqlUpdateRow = fmt.Sprintf(sqlUpdateRowPostgres, p.tableName)
p.sqlDeleteRow = fmt.Sprintf(sqlDeleteRowPostgres, p.tableName)
case "mysql":
p.sqlCreateTable = fmt.Sprintf(sqlCreateTableMysql, p.tableName)
case "sqlite3":
p.sqlCreateTable = fmt.Sprintf(sqlCreateTableSqlite3, p.tableName)
p.sqlTruncateTable = fmt.Sprintf(sqlTruncateTableSqlite3, p.tableName)
case "sqlserver":
p.sqlPlaceHolder = sqlPlaceHolderSqlserver
p.sqlCreateTable = fmt.Sprintf(sqlCreateTableSqlserver, p.tableName)
p.sqlInsertRow = fmt.Sprintf(sqlInsertRowSqlserver, p.tableName)
p.sqlUpdateRow = fmt.Sprintf(sqlUpdateRowSqlserver, p.tableName)
p.sqlDeleteRow = fmt.Sprintf(sqlDeleteRowSqlserver, p.tableName)
}
}
func (p *Adapter) sqlRebind(query string) string {
if p.sqlPlaceHolder == sqlPlaceHolder {
return query
}
var idx, num int
result := make([]byte, 0, len(query)+10)
for {
idx = strings.Index(query, sqlPlaceHolder)
if idx == -1 {
break
}
num++
result = append(result, query[:idx]...)
result = append(result, p.sqlPlaceHolder...)
result = strconv.AppendInt(result, int64(num), 10)
query = query[idx+1:]
}
return string(append(result, query...))
}
// createTable create a not exists table.
func (p *Adapter) createTable() error {
_, err := p.db.ExecContext(p.ctx, p.sqlCreateTable)
return err
}
// truncateTable clear the table.
func (p *Adapter) truncateTable() error {
_, err := p.db.ExecContext(p.ctx, p.sqlTruncateTable)
return err
}
// deleteAll clear the table.
func (p *Adapter) deleteAll() error {
_, err := p.db.ExecContext(p.ctx, p.sqlDeleteAll)
return err
}
// isTableExist check the table exists.
func (p *Adapter) isTableExist() bool {
_, err := p.db.ExecContext(p.ctx, p.sqlIsTableExist)
return err == nil
}
// deleteRows delete eligible data.
func (p *Adapter) deleteRows(query string, args ...interface{}) error {
query = p.sqlRebind(query)
_, err := p.db.ExecContext(p.ctx, query, args...)
return err
}
// truncateAndInsertRows clear table and insert new rows.
func (p *Adapter) truncateAndInsertRows(rules [][]interface{}) error {
if err := p.truncateTable(); err != nil {
return err
}
return p.execTxSqlRows(p.sqlInsertRow, rules)
}
// deleteAllAndInsertRows clear table and insert new rows.
func (p *Adapter) deleteAllAndInsertRows(rules [][]interface{}) error {
if err := p.deleteAll(); err != nil {
return err
}
return p.execTxSqlRows(p.sqlInsertRow, rules)
}
// execTxSqlRows exec sql rows.
func (p *Adapter) execTxSqlRows(query string, rules [][]interface{}) (err error) {
tx, err := p.db.BeginTx(p.ctx, nil)
if err != nil {
return
}
var action string
stmt, err := tx.PrepareContext(p.ctx, query)
if err != nil {
action = "prepare context"
goto ROLLBACK
}
for _, rule := range rules {
if _, err = stmt.ExecContext(p.ctx, rule...); err != nil {
action = "stmt exec context"
goto ROLLBACK
}
}
if err = stmt.Close(); err != nil {
action = "stmt close"
goto ROLLBACK
}
if err = tx.Commit(); err != nil {
action = "commit"
goto ROLLBACK
}
return
ROLLBACK:
if err1 := tx.Rollback(); err1 != nil {
err = fmt.Errorf("%s err: %v, rollback err: %v", action, err, err1)
}
return
}
// queryFunc define func for query
var queryFunc = func(ctx context.Context, db *sql.DB, query string, args ...interface{}) ([]*CasbinRule, error) {
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
lines := make([]*CasbinRule, 0, 64)
for rows.Next() {
var rule CasbinRule
err = rows.Scan(&rule.PType, &rule.V0, &rule.V1, &rule.V2, &rule.V3, &rule.V4, &rule.V5)
if err != nil {
return nil, err
}
lines = append(lines, &rule)
}
return lines, nil
}
// selectRows select eligible data by args from the table.
func (p *Adapter) selectRows(query string, args ...interface{}) ([]*CasbinRule, error) {
if len(args) == 0 {
return queryFunc(p.ctx, p.db, query)
}
query = p.sqlRebind(query)
return queryFunc(p.ctx, p.db, query, args...)
}
// selectWhereIn select eligible data by filter from the table.
func (p *Adapter) selectWhereIn(filter *Filter) (lines []*CasbinRule, err error) {
var (
sqlBuf bytes.Buffer
buf bytes.Buffer
)
sqlBuf.Grow(64)
sqlBuf.WriteString(p.sqlSelectWhere)
args := make([]string, 0, 4)
for _, col := range [maxParamLength]struct {
name string
arg []string
}{
{"p_type", filter.PType},
{"v0", filter.V0},
{"v1", filter.V1},
{"v2", filter.V2},
{"v3", filter.V3},
{"v4", filter.V4},
{"v5", filter.V5},
} {
l := len(col.arg)
if l == 0 {
continue
}
switch sqlBuf.Bytes()[sqlBuf.Len()-1] {
case '?', ')':
sqlBuf.WriteString(" AND ")
}
sqlBuf.WriteString(col.name)
if l == 1 {
sqlBuf.WriteString("=?")
args = append(args, col.arg[0])
} else {
buf.Grow(l * 2)
for i := 0; i < l; i++ {
buf.WriteString("?,")
}
buf.Truncate(buf.Len() - 1)
sqlBuf.WriteString(" IN (")
sqlBuf.Write(buf.Bytes())
sqlBuf.WriteByte(')')
args = append(args, col.arg...)
buf.Reset()
}
}
params := make([]interface{}, len(args))
for idx := range args {
params[idx] = args[idx]
}
return p.selectRows(sqlBuf.String(), params...)
}
// LoadPolicy load all policy rules from the storage.
func (p *Adapter) LoadPolicy(model model.Model) error {
lines, err := p.selectRows(p.sqlSelectAll)
if err != nil {
return err
}
for _, line := range lines {
p.loadPolicyLine(line, model)
}
return nil
}
// SavePolicy save policy rules to the storage.
func (p *Adapter) SavePolicy(model model.Model) error {
args := make([][]interface{}, 0, 32)
for ptype, ast := range model["p"] {
for _, rule := range ast.Policy {
arg := p.genArgs(ptype, rule)
args = append(args, arg)
}
}
for ptype, ast := range model["g"] {
for _, rule := range ast.Policy {
arg := p.genArgs(ptype, rule)
args = append(args, arg)
}
}
// return p.truncateAndInsertRows(args)
return p.deleteAllAndInsertRows(args)
}
// AddPolicy add one policy rule to the storage.
func (p *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
args := p.genArgs(ptype, rule)
_, err := p.db.ExecContext(p.ctx, p.sqlInsertRow, args...)
return err
}
// AddPolicies add multiple policy rules to the storage.
func (p *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
args := make([][]interface{}, 0, 8)
for _, rule := range rules {
arg := p.genArgs(ptype, rule)
args = append(args, arg)
}
return p.execTxSqlRows(p.sqlInsertRow, args)
}
// RemovePolicy remove policy rules from the storage.
func (p *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
var sqlBuf bytes.Buffer
sqlBuf.Grow(64)
sqlBuf.WriteString(p.sqlDeleteByArgs)
args := make([]interface{}, 0, 4)
args = append(args, ptype)
for idx, arg := range rule {
if arg != "" {
sqlBuf.WriteString(" AND v")
sqlBuf.WriteString(strconv.Itoa(idx))
sqlBuf.WriteString("=?")
args = append(args, arg)
}
}
return p.deleteRows(sqlBuf.String(), args...)
}
// RemoveFilteredPolicy remove policy rules that match the filter from the storage.
func (p *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
var sqlBuf bytes.Buffer
sqlBuf.Grow(64)
sqlBuf.WriteString(p.sqlDeleteByArgs)
args := make([]interface{}, 0, 4)
args = append(args, ptype)
var value string
l := fieldIndex + len(fieldValues)
for idx := 0; idx < 6; idx++ {
if fieldIndex <= idx && idx < l {
value = fieldValues[idx-fieldIndex]
if value != "" {
sqlBuf.WriteString(" AND v")
sqlBuf.WriteString(strconv.Itoa(idx))
sqlBuf.WriteString("=?")
args = append(args, value)
}
}
}
return p.deleteRows(sqlBuf.String(), args...)
}
func (p *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) (err error) {
args := make([][]interface{}, 0, 8)
for _, rule := range rules {
arg := p.genArgs(ptype, rule)
args = append(args, arg)
}
return p.execTxSqlRows(p.sqlDeleteRow, args)
}
// LoadFilteredPolicy load policy rules that match the filter.
// filterPtr must be a pointer.
func (p *Adapter) LoadFilteredPolicy(model model.Model, filterPtr interface{}) error {
if filterPtr == nil {
return p.LoadPolicy(model)
}
filter, ok := filterPtr.(*Filter)
if !ok {
return errors.New("invalid filter type")
}
lines, err := p.selectWhereIn(filter)
if err != nil {
return err
}
for _, line := range lines {
p.loadPolicyLine(line, model)
}
p.isFiltered = true
return nil
}
// IsFiltered returns true if the loaded policy rules has been filtered.
func (p *Adapter) IsFiltered() bool {
return p.isFiltered
}
// UpdatePolicy update a policy rule from storage.
// This is part of the Auto-Save feature.
func (p *Adapter) UpdatePolicy(sec, ptype string, oldRule, newPolicy []string) error {
oldArg := p.genArgs(ptype, oldRule)
newArg := p.genArgs(ptype, newPolicy)
_, err := p.db.ExecContext(p.ctx, p.sqlUpdateRow, append(newArg, oldArg...)...)
return err
}
// UpdatePolicies updates policy rules to storage.
func (p *Adapter) UpdatePolicies(sec, ptype string, oldRules, newRules [][]string) (err error) {
if len(oldRules) != len(newRules) {
return errors.New("old rules size not equal to new rules size")
}
args := make([][]interface{}, 0, 16)
for idx := range oldRules {
oldArg := p.genArgs(ptype, oldRules[idx])
newArg := p.genArgs(ptype, newRules[idx])
args = append(args, append(newArg, oldArg...))
}
return p.execTxSqlRows(p.sqlUpdateRow, args)
}
// UpdateFilteredPolicies deletes old rules and adds new rules.
func (p *Adapter) UpdateFilteredPolicies(sec, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) (oldPolicies [][]string, err error) {
var value string
var whereBuf bytes.Buffer
whereBuf.Grow(32)
l := fieldIndex + len(fieldValues)
whereArgs := make([]interface{}, 0, 4)
whereArgs = append(whereArgs, ptype)
for idx := 0; idx < 6; idx++ {
if fieldIndex <= idx && idx < l {
value = fieldValues[idx-fieldIndex]
if value != "" {
whereBuf.WriteString(" AND v")
whereBuf.WriteString(strconv.Itoa(idx))
whereBuf.WriteString("=?")
whereArgs = append(whereArgs, value)
}
}
}
var selectBuf bytes.Buffer
selectBuf.Grow(64)
selectBuf.WriteString(p.sqlSelectWhere)
selectBuf.WriteString("p_type=?")
selectBuf.Write(whereBuf.Bytes())
var oldRows []*CasbinRule
value = p.sqlRebind(selectBuf.String())
oldRows, err = p.selectRows(value, whereArgs...)
if err != nil {
return
}
var deleteBuf bytes.Buffer
deleteBuf.Grow(64)
deleteBuf.WriteString(p.sqlDeleteByArgs)
deleteBuf.Write(whereBuf.Bytes())
var tx *sql.Tx
tx, err = p.db.BeginTx(p.ctx, nil)
if err != nil {
return
}
var (
stmt *sql.Stmt
action string
)
value = p.sqlRebind(deleteBuf.String())
if _, err = tx.ExecContext(p.ctx, value, whereArgs...); err != nil {
action = "delete old policies"
goto ROLLBACK
}
stmt, err = tx.PrepareContext(p.ctx, p.sqlInsertRow)
if err != nil {
action = "prepare context"
goto ROLLBACK
}
for _, policy := range newPolicies {
arg := p.genArgs(ptype, policy)
if _, err = stmt.ExecContext(p.ctx, arg...); err != nil {
action = "stmt exec context"
goto ROLLBACK
}
}
if err = stmt.Close(); err != nil {
action = "stmt close"
goto ROLLBACK
}
if err = tx.Commit(); err != nil {
action = "commit"
goto ROLLBACK
}
oldPolicies = make([][]string, 0, len(oldRows))
for _, rule := range oldRows {
oldPolicies = append(oldPolicies, []string{rule.PType, rule.V0, rule.V1, rule.V2, rule.V3, rule.V4, rule.V5})
}
return
ROLLBACK:
if err1 := tx.Rollback(); err1 != nil {
err = fmt.Errorf("%s err: %v, rollback err: %v", action, err, err1)
}
return
}
// loadPolicyLine load a policy line to model.
func (Adapter) loadPolicyLine(line *CasbinRule, model model.Model) {
if line == nil {
return
}
var lineBuf bytes.Buffer
lineBuf.Grow(64)
lineBuf.WriteString(line.PType)
args := [6]string{line.V0, line.V1, line.V2, line.V3, line.V4, line.V5}
for _, arg := range args {
if arg != "" {
lineBuf.WriteByte(',')
lineBuf.WriteString(arg)
}
}
persist.LoadPolicyLine(lineBuf.String(), model)
}
// genArgs generate args from ptype and rule.
func (Adapter) genArgs(ptype string, rule []string) []interface{} {
args := make([]interface{}, maxParamLength)
args[0] = ptype
for idx := range rule {
args[idx+1] = rule[idx]
}
for idx := len(rule) + 1; idx < maxParamLength; idx++ {
args[idx] = ""
}
return args
}