24
24
import org .casbin .jcasbin .persist .Adapter ;
25
25
import org .casbin .jcasbin .persist .BatchAdapter ;
26
26
import org .casbin .jcasbin .persist .Helper ;
27
+ import org .casbin .jcasbin .persist .UpdatableAdapter ;
27
28
28
29
import javax .sql .DataSource ;
29
30
import java .sql .*;
@@ -49,7 +50,7 @@ public String[] toStringArray() {
49
50
* JDBCAdapter is the JDBC adapter for jCasbin.
50
51
* It can load policy from JDBC supported database or save policy to it.
51
52
*/
52
- abstract class JDBCBaseAdapter implements Adapter , BatchAdapter {
53
+ abstract class JDBCBaseAdapter implements Adapter , BatchAdapter , UpdatableAdapter {
53
54
protected static final String DEFAULT_TABLE_NAME = "casbin_rule" ;
54
55
protected static final boolean DEFAULT_REMOVE_POLICY_FAILED = false ;
55
56
protected static final boolean DEFAULT_AUTO_CREATE_TABLE = true ;
@@ -480,6 +481,47 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
480
481
});
481
482
}
482
483
484
+ /**
485
+ * updatePolicy updates a policy rule from the current policy.
486
+ */
487
+ @ Override
488
+ public void updatePolicy (String sec , String ptype , List <String > oldRule , List <String > newRule ) {
489
+ if (CollectionUtils .isEmpty (oldRule ) || CollectionUtils .isEmpty (newRule )) {
490
+ return ;
491
+ }
492
+
493
+ String sql = renderActualSql ("INSERT INTO casbin_rule (ptype,v0,v1,v2,v3,v4,v5) VALUES(?,?,?,?,?,?,?)" );
494
+
495
+
496
+ Failsafe .with (retryPolicy ).run (ctx -> {
497
+ if (ctx .isRetry ()) {
498
+ retry (ctx );
499
+ }
500
+ conn .setAutoCommit (false );
501
+ removePolicy (sec , ptype , oldRule );
502
+ try (PreparedStatement ps = conn .prepareStatement (sql )) {
503
+ CasbinRule line = this .savePolicyLine (ptype , newRule );
504
+
505
+ ps .setString (1 , line .ptype );
506
+ ps .setString (2 , line .v0 );
507
+ ps .setString (3 , line .v1 );
508
+ ps .setString (4 , line .v2 );
509
+ ps .setString (5 , line .v3 );
510
+ ps .setString (6 , line .v4 );
511
+ ps .setString (7 , line .v5 );
512
+ ps .executeUpdate ();
513
+ conn .commit ();
514
+ } catch (SQLException e ) {
515
+ conn .rollback ();
516
+
517
+ e .printStackTrace ();
518
+ throw e ;
519
+ } finally {
520
+ conn .setAutoCommit (true );
521
+ }
522
+ });
523
+ }
524
+
483
525
/**
484
526
* Close the Connection.
485
527
*/
0 commit comments