Skip to content

Commit

Permalink
fix: make the id field backward compatible (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: tangyang9464 <[email protected]>
  • Loading branch information
tangyang9464 authored Apr 7, 2022
1 parent 846c3e1 commit 8cae426
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ For Maven:
<dependency>
<groupId>org.casbin</groupId>
<artifactId>jdbc-adapter</artifactId>
<version>2.0.1</version>
<version>LATEST</version>
</dependency>
```

**Notice:**

Since version 2.0.2, jdbc-adapter adds an ``id`` field to the database table structure by default.

If you want to upgrade to 2.0.2 - 2.2.0, you have to add the ``id`` field manually. It is recommended to add the ``id`` field. If you don't want to add the ``id`` field, please use 2.2.1+.

## Simple Example

```java
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
<dependency>
<groupId>org.casbin</groupId>
<artifactId>jcasbin</artifactId>
<version>1.22.2</version>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
Expand Down
38 changes: 14 additions & 24 deletions src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,22 @@ protected void migrate() throws SQLException {

protected void loadPolicyLine(CasbinRule line, Model model) {
String lineText = line.ptype;
if (!line.v0.equals("")) {
if (!"".equals(line.v0)) {
lineText += ", " + line.v0;
}
if (!line.v1.equals("")) {
if (!"".equals(line.v1)) {
lineText += ", " + line.v1;
}
if (!line.v2.equals("")) {
if (!"".equals(line.v2)) {
lineText += ", " + line.v2;
}
if (!line.v3.equals("")) {
if (!"".equals(line.v3)) {
lineText += ", " + line.v3;
}
if (!line.v4.equals("")) {
if (!"".equals(line.v4)) {
lineText += ", " + line.v4;
}
if (!line.v5.equals("")) {
if (!"".equals(line.v5)) {
lineText += ", " + line.v5;
}

Expand All @@ -184,27 +184,17 @@ public void loadPolicy(Model model) {
retry(ctx);
}
try (Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule")) {
ResultSet rSet = stmt.executeQuery("SELECT ptype,v0,v1,v2,v3,v4,v5 FROM casbin_rule")) {
ResultSetMetaData rData = rSet.getMetaData();
while (rSet.next()) {
CasbinRule line = new CasbinRule();
for (int i = 1; i <= rData.getColumnCount(); i++) {
if (i == 2) {
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 3) {
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 4) {
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 5) {
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 6) {
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 7) {
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 8) {
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
}
}
line.ptype = rSet.getObject(1) == null ? "" : (String) rSet.getObject(1);
line.v0 = rSet.getObject(2) == null ? "" : (String) rSet.getObject(2);
line.v1 = rSet.getObject(3) == null ? "" : (String) rSet.getObject(3);
line.v2 = rSet.getObject(4) == null ? "" : (String) rSet.getObject(4);
line.v3 = rSet.getObject(5) == null ? "" : (String) rSet.getObject(5);
line.v4 = rSet.getObject(6) == null ? "" : (String) rSet.getObject(6);
line.v5 = rSet.getObject(7) == null ? "" : (String) rSet.getObject(7);
loadPolicyLine(line, model);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/casbin/adapter/AdapterCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ public JDBCAdapter createViaDataSource() throws Exception {
return new JDBCAdapter(dataSource);
}
}

0 comments on commit 8cae426

Please sign in to comment.