11<?php
2+
23namespace CasbinAdapter \Database ;
34
45use Casbin \Exceptions \CasbinException ;
56use Casbin \Persist \Adapter as AdapterContract ;
67use TechOne \Database \Manager ;
78use Casbin \Persist \AdapterHelper ;
9+
810/**
9- * DatabaseAdapter
11+ * DatabaseAdapter.
12+ *
10131114 */
1215class Adapter implements AdapterContract
@@ -21,7 +24,7 @@ class Adapter implements AdapterContract
2124
2225 public function __construct (array $ config )
2326 {
24- $ this ->config = $ config ;
27+ $ this ->config = $ config ;
2528 $ this ->connection = (new Manager ($ config ))->getConnection ();
2629 $ this ->initTable ();
2730 }
@@ -33,7 +36,48 @@ public static function newAdapter(array $config)
3336
3437 public function initTable ()
3538 {
36- $ sql = <<<EOT
39+ if ('pgsql ' == $ this ->config ['type ' ]) {
40+ $ sql = <<<EOT
41+ CREATE TABLE IF NOT EXISTS $ this ->casbinRuleTableName (
42+ id bigserial NOT NULL,
43+ ptype varchar(255) NOT NULL,
44+ v0 varchar(255) DEFAULT NULL,
45+ v1 varchar(255) DEFAULT NULL,
46+ v2 varchar(255) DEFAULT NULL,
47+ v3 varchar(255) DEFAULT NULL,
48+ v4 varchar(255) DEFAULT NULL,
49+ v5 varchar(255) DEFAULT NULL,
50+ PRIMARY KEY (id)
51+ );
52+ EOT ;
53+ } elseif ('sqlite ' == $ this ->config ['type ' ]) {
54+ $ sql = <<<EOT
55+ CREATE TABLE IF NOT EXISTS ` $ this ->casbinRuleTableName ` (
56+ `id` INTEGER PRIMARY KEY AUTOINCREMENT ,
57+ `ptype` varchar(255) NOT NULL,
58+ `v0` varchar(255) DEFAULT NULL,
59+ `v1` varchar(255) DEFAULT NULL,
60+ `v2` varchar(255) DEFAULT NULL,
61+ `v3` varchar(255) DEFAULT NULL,
62+ `v4` varchar(255) DEFAULT NULL,
63+ `v5` varchar(255) DEFAULT NULL
64+ );
65+ EOT ;
66+ } elseif ('sqlsrv ' == $ this ->config ['type ' ]) {
67+ $ sql = <<<EOT
68+ CREATE TABLE IF NOT EXISTS ` $ this ->casbinRuleTableName ` (
69+ id int IDENTITY(1,1),
70+ ptype varchar(255) NOT NULL,
71+ v0 varchar(255) DEFAULT NULL,
72+ v1 varchar(255) DEFAULT NULL,
73+ v2 varchar(255) DEFAULT NULL,
74+ v3 varchar(255) DEFAULT NULL,
75+ v4 varchar(255) DEFAULT NULL,
76+ v5 varchar(255) DEFAULT NULL
77+ );
78+ EOT ;
79+ } else {
80+ $ sql = <<<EOT
3781CREATE TABLE IF NOT EXISTS ` $ this ->casbinRuleTableName ` (
3882 `id` int(11) NOT NULL AUTO_INCREMENT,
3983 `ptype` varchar(255) NOT NULL,
@@ -46,29 +90,29 @@ public function initTable()
4690 PRIMARY KEY (`id`)
4791);
4892EOT ;
93+ }
4994 $ this ->connection ->execute ($ sql , []);
5095 }
5196
5297 public function savePolicyLine ($ ptype , array $ rule )
5398 {
54-
55- $ col ['`ptype` ' ] = $ ptype ;
99+ $ col ['ptype ' ] = $ ptype ;
56100 foreach ($ rule as $ key => $ value ) {
57- $ col ['`v ' . strval ($ key ) . ' ` ' ] = $ value ;
101+ $ col ['v ' . strval ($ key ). ' ' ] = $ value ;
58102 }
59103
60104 $ colStr = implode (', ' , array_keys ($ col ));
61105
62106 $ name = rtrim (str_repeat ('?, ' , count ($ col )), ', ' );
63107
64- $ sql = 'INSERT INTO ` ' . $ this ->casbinRuleTableName . ' `( ' . $ colStr . ') VALUES ( ' . $ name . ') ' ;
108+ $ sql = 'INSERT INTO ' . $ this ->casbinRuleTableName . ' ( ' . $ colStr. ') VALUES ( ' . $ name. ') ' ;
65109
66110 $ this ->connection ->execute ($ sql , array_values ($ col ));
67111 }
68112
69113 public function loadPolicy ($ model )
70114 {
71- $ rows = $ this ->connection ->query ('SELECT * FROM ` ' . $ this ->casbinRuleTableName . ' ` ' );
115+ $ rows = $ this ->connection ->query ('SELECT * FROM ' . $ this ->casbinRuleTableName . ' ' );
72116
73117 foreach ($ rows as $ row ) {
74118 $ line = implode (', ' , array_slice (array_values ($ row ), 1 ));
@@ -89,6 +133,7 @@ public function savePolicy($model)
89133 $ this ->savePolicyLine ($ ptype , $ rule );
90134 }
91135 }
136+
92137 return true ;
93138 }
94139
@@ -100,22 +145,22 @@ public function addPolicy($sec, $ptype, $rule)
100145 public function removePolicy ($ sec , $ ptype , $ rule )
101146 {
102147 $ where ['ptype ' ] = $ ptype ;
103- $ condition [] = 'ptype = :ptype ' ;
148+ $ condition [] = 'ptype = :ptype ' ;
104149 foreach ($ rule as $ key => $ value ) {
105- $ where ['v ' . strval ($ key )] = $ value ;
106- $ condition [] = 'v ' . strval ($ key ) . ' = : ' . 'v ' . strval ($ key );
150+ $ where ['v ' . strval ($ key )] = $ value ;
151+ $ condition [] = 'v ' . strval ($ key ). ' = : ' . 'v ' . strval ($ key );
107152 }
108153 if (empty ($ condition )) {
109154 return ;
110155 }
111156
112- $ sql = 'DELETE FROM ` ' . $ this ->casbinRuleTableName . ' ` WHERE ' . implode (' AND ' , $ condition );
157+ $ sql = 'DELETE FROM ' . $ this ->casbinRuleTableName . ' WHERE '. implode (' AND ' , $ condition );
113158
114159 return $ this ->connection ->execute ($ sql , $ where );
115160 }
116161
117162 public function removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues )
118163 {
119- throw new CasbinException (" not implemented " );
164+ throw new CasbinException (' not implemented ' );
120165 }
121166}
0 commit comments