Skip to content

Commit

Permalink
feat: support customized table name (#52)
Browse files Browse the repository at this point in the history
* support customized table name when using jdbc

fix #51

Signed-off-by: Macky <[email protected]>
  • Loading branch information
doctormacky authored Nov 26, 2021
1 parent 14b7f6b commit c5e289d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ casbin:
#The jdbc adapter will actively look for the data source information you configured in spring.datasource
#Default use jdbc, and use the built-in h2 database for memory storage
storeType: jdbc
#Customized policy table name when use jdbc, casbin_rule as default.
tableName: casbin_rule
#Data source initialization policy [create (automatically create data table, no longer initialized if created), never (always do not initialize)]
initializeSchema: create
#Local model configuration file address, the default reading location: classpath: casbin/model.conf
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ casbin:
#jdbc适配器将主动寻找您在spring.datasource配置的数据源信息
#默认使用jdbc,并使用内置h2数据库进行内存存储
storeType: jdbc
#当使用jdbc时,定制化数据库表名,默认表名是casbin_rule
tableName: casbin_rule
#数据源初始化策略[create(自动创建数据表,如已创建则不再进行初始化),never(始终不进行初始化)]
initializeSchema: create
#本地模型配置文件地址,约定默认读取位置:classpath:casbin/model.conf
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/org/casbin/adapter/DB2Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
public class DB2Adapter extends JdbcAdapter {

private static final String CHECK_TABLE_SQL = "select 1 from syscat.tables where tabname = upper('CASBIN_RULE')";
private static final String CHECK_TABLE_SQL = "select 1 from syscat.tables where tabname = upper('casbin_rule')";

private static final String DROP_TABLE_SQL = "DROP TABLE CASBIN_RULE";
private static final String DROP_TABLE_SQL = "DROP TABLE casbin_rule";

private static final String INIT_TABLE_SQL = "CREATE TABLE CASBIN_RULE (" +
private static final String INIT_TABLE_SQL = "CREATE TABLE casbin_rule (" +
" ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) ,"+
" PTYPE VARCHAR(255) NOT NULL ," +
" V0 VARCHAR(255) DEFAULT NULL ," +
Expand All @@ -34,24 +34,32 @@ public class DB2Adapter extends JdbcAdapter {
public DB2Adapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) {
super(jdbcTemplate, casbinExceptionProperties, autoCreateTable);
}

public DB2Adapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) {
super(jdbcTemplate, casbinExceptionProperties, tableName, autoCreateTable);
}

@Override
protected void initTable() {
try {
jdbcTemplate.queryForObject(CHECK_TABLE_SQL, Integer.class);
jdbcTemplate.queryForObject(getCheckTableSql(), Integer.class);
} catch (EmptyResultDataAccessException e) {
super.initTable();
}
}

protected String getCheckTableSql() {
return renderActualSql(CHECK_TABLE_SQL);
}

@Override
protected String getInitTableSql() {
return INIT_TABLE_SQL;
return renderActualSql(INIT_TABLE_SQL);
}

@Override
protected String getDropTableSql() {
return DROP_TABLE_SQL;
return renderActualSql(DROP_TABLE_SQL);
}

}
67 changes: 45 additions & 22 deletions src/main/java/org/casbin/adapter/JdbcAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.casbin.adapter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.casbin.exception.CasbinAdapterException;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.FilteredAdapter;
Expand All @@ -11,11 +19,6 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author shy
* @version V1.1
Expand All @@ -26,9 +29,13 @@
*/
public class JdbcAdapter implements FilteredAdapter {

private final static Logger logger = LoggerFactory.getLogger(JdbcAdapter.class);
private static final Logger logger = LoggerFactory.getLogger(JdbcAdapter.class);

public static final String DEFAULT_TABLE_NAME = "casbin_rule";

private String tableName;

private final static String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS casbin_rule (" +
private static final String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS casbin_rule (" +
" id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "+
" ptype varchar(255) NOT NULL," +
" v0 varchar(255) DEFAULT NULL," +
Expand All @@ -38,19 +45,24 @@ public class JdbcAdapter implements FilteredAdapter {
" v4 varchar(255) DEFAULT NULL," +
" v5 varchar(255) DEFAULT NULL" +
")";
private final static String DROP_TABLE_SQL = "DROP TABLE IF EXISTS casbin_rule";
private final static String DELETE_TABLE_CONTENT_SQL = "DELETE FROM casbin_rule";
private final static String LOAD_POLICY_SQL = "SELECT * FROM casbin_rule";
private final static String INSERT_POLICY_SQL = "INSERT INTO casbin_rule(ptype, v0, v1, v2, v3, v4, v5) VALUES(?, ?, ?, ?, ?, ?, ?)";
private final static String DELETE_POLICY_SQL = "DELETE FROM casbin_rule WHERE ptype = ? ";
private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS casbin_rule";
private static final String DELETE_TABLE_CONTENT_SQL = "DELETE FROM casbin_rule";
private static final String LOAD_POLICY_SQL = "SELECT * FROM casbin_rule";
private static final String INSERT_POLICY_SQL = "INSERT INTO casbin_rule(ptype, v0, v1, v2, v3, v4, v5) VALUES(?, ?, ?, ?, ?, ?, ?)";
private static final String DELETE_POLICY_SQL = "DELETE FROM casbin_rule WHERE ptype = ? ";

protected JdbcTemplate jdbcTemplate;
protected CasbinExceptionProperties casbinExceptionProperties;

private volatile boolean isFiltered = true;

public JdbcAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) {
this(jdbcTemplate,casbinExceptionProperties,DEFAULT_TABLE_NAME,autoCreateTable);
}

public JdbcAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) {
this.jdbcTemplate = jdbcTemplate;
this.tableName = tableName;
this.casbinExceptionProperties = casbinExceptionProperties;
if (autoCreateTable) {
initTable();
Expand All @@ -65,24 +77,35 @@ public static class Filter {
public String[] p;
public String[] g;
}



protected String renderActualSql(String sql) {
return sql.replace(DEFAULT_TABLE_NAME, tableName);
}

protected String getInitTableSql() {
return INIT_TABLE_SQL;
return renderActualSql(INIT_TABLE_SQL);
}

protected String getDropTableSql() {
return DROP_TABLE_SQL;
return renderActualSql(DROP_TABLE_SQL);
}

protected String getLoadPolicySql() {
return LOAD_POLICY_SQL;
return renderActualSql(LOAD_POLICY_SQL);
}

protected String getDeleteTableContentSql() {
return DELETE_TABLE_CONTENT_SQL;
return renderActualSql(DELETE_TABLE_CONTENT_SQL);
}


protected String getInsertPolicySql() {
return renderActualSql(INSERT_POLICY_SQL);
}

protected String getDeletePolicySql() {
return renderActualSql(DELETE_POLICY_SQL);
}

/**
* Initialize the table structure
*/
Expand Down Expand Up @@ -145,7 +168,7 @@ public void savePolicy(Model model) {
deleteTableContent();
List<CasbinRule> casbinRules = CasbinRule.transformToCasbinRule(model);
int[] rows = jdbcTemplate.batchUpdate(
INSERT_POLICY_SQL,
getInsertPolicySql(),
new BatchPreparedStatementSetter() {

@Override
Expand Down Expand Up @@ -190,7 +213,7 @@ public void addPolicy(String sec, String ptype, List<String> rule) {
for (int i = 0; i < 6 - rule.size(); i++) {
rules.add(null);
}
int rows = jdbcTemplate.update(INSERT_POLICY_SQL, rules.toArray());
int rows = jdbcTemplate.update(getInsertPolicySql(), rules.toArray());
if (rows != 1) {
throw new CasbinAdapterException(String.format("Add policy error, add %d rows, expect %d rows", rows, 1));
}
Expand Down Expand Up @@ -229,7 +252,7 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
}
List<String> params = new ArrayList<>(Arrays.asList(fieldValues));
params.add(0, ptype);
String delSql = DELETE_POLICY_SQL;
String delSql = getDeletePolicySql();
int columnIndex = fieldIndex;
for (int i = 0; i < fieldValues.length; i++) {
delSql = String.format("%s%s%s%s", delSql, " AND v", columnIndex, " = ? ");
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/org/casbin/adapter/OracleAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class OracleAdapter extends JdbcAdapter {

private final static String INIT_TABLE_SQL = "CREATE TABLE CASBIN_RULE (" +
private static final String INIT_TABLE_SQL = "CREATE TABLE casbin_rule (" +
" ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "+
" PTYPE VARCHAR2(255) NOT NULL ," +
" V0 VARCHAR2(255) DEFAULT NULL ," +
Expand All @@ -23,28 +23,36 @@ public class OracleAdapter extends JdbcAdapter {
" V4 VARCHAR2(255) DEFAULT NULL ," +
" V5 VARCHAR2(255) DEFAULT NULL" +
")";
private final static String DROP_TABLE_SQL = "DROP TABLE CASBIN_RULE";
private final static String CHECK_TABLE_EXIST = "SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = UPPER('CASBIN_RULE')";
private static final String DROP_TABLE_SQL = "DROP TABLE casbin_rule";
private static final String CHECK_TABLE_EXIST = "SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = UPPER('casbin_rule')";

public OracleAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) {
super(jdbcTemplate, casbinExceptionProperties, autoCreateTable);
}

public OracleAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) {
super(jdbcTemplate, casbinExceptionProperties, tableName, autoCreateTable);
}

@Override
protected void initTable() {
Integer count = jdbcTemplate.queryForObject(CHECK_TABLE_EXIST, Integer.class);
Integer count = jdbcTemplate.queryForObject(getCheckTableExistSql(), Integer.class);
if (count != null && count == 0) {
super.initTable();
}
}

protected String getCheckTableExistSql() {
return renderActualSql(CHECK_TABLE_EXIST);
}

@Override
protected String getInitTableSql() {
return INIT_TABLE_SQL;
return renderActualSql(INIT_TABLE_SQL);
}

@Override
protected String getDropTableSql() {
return DROP_TABLE_SQL;
return renderActualSql(DROP_TABLE_SQL);
}
}
8 changes: 6 additions & 2 deletions src/main/java/org/casbin/adapter/PostgreSQLAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class PostgreSQLAdapter extends JdbcAdapter {

private final static String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS CASBIN_RULE (" +
private static final String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS casbin_rule (" +
" id SERIAL PRIMARY KEY, "+
" ptype varchar(255) NOT NULL," +
" v0 varchar(255) DEFAULT NULL," +
Expand All @@ -27,9 +27,13 @@ public class PostgreSQLAdapter extends JdbcAdapter {
public PostgreSQLAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) {
super(jdbcTemplate, casbinExceptionProperties, autoCreateTable);
}

public PostgreSQLAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) {
super(jdbcTemplate, casbinExceptionProperties, tableName, autoCreateTable);
}

@Override
protected String getInitTableSql() {
return INIT_TABLE_SQL;
return renderActualSql(INIT_TABLE_SQL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@ConditionalOnExpression("${casbin.enableCasbin:true}")
public class CasbinAutoConfiguration {

private final static Logger logger = LoggerFactory.getLogger(CasbinAutoConfiguration.class);
private static final Logger logger = LoggerFactory.getLogger(CasbinAutoConfiguration.class);

/**
* Automatic configuration file storage adapter
Expand Down Expand Up @@ -89,17 +89,18 @@ public Adapter autoConfigJdbcAdapter(
String databaseName = getDatabaseName(jdbcTemplateToUse.getDataSource());
CasbinDataSourceInitializationMode initializeSchema = properties.getInitializeSchema();
boolean autoCreateTable = initializeSchema == CasbinDataSourceInitializationMode.CREATE;
String tableName = properties.getTableName();
logger.info("Casbin current use database product: {}", databaseName);
switch (databaseName) {
case "mysql":
case "h2":
return new JdbcAdapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable);
return new JdbcAdapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable);
case "postgresql":
return new PostgreSQLAdapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable);
return new PostgreSQLAdapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable);
case "oracle":
return new OracleAdapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable);
return new OracleAdapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable);
case "db2":
return new DB2Adapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable);
return new DB2Adapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable);
default:
throw new CasbinAdapterException("Can't find " + databaseName + " jdbc adapter");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
@ConfigurationProperties("casbin")
public class CasbinProperties {
/**
* Default table name when storage strategy is JDBC
*/
private String tableName = "casbin_rule";
/**
* Enable Casbin
*/
Expand Down Expand Up @@ -157,5 +161,13 @@ public boolean isUseDefaultModelIfModelNotSetting() {
public void setUseDefaultModelIfModelNotSetting(boolean useDefaultModelIfModelNotSetting) {
this.useDefaultModelIfModelNotSetting = useDefaultModelIfModelNotSetting;
}

public String getTableName() {
return tableName;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}
}

2 comments on commit c5e289d

@senningsoft
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么数据库表数据会重复,如果项目重新启动,然后再增加(AddPolicies)就可以相同的

@hsluoyz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@senningsoft plz create a new github issue

Please sign in to comment.