From 47bdfcb518abf90951c3b82992f19644864b0a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A5=AD?= Date: Thu, 9 May 2019 11:43:32 +0800 Subject: [PATCH] add db2 support (#5) * support db2 --- .../java/org/casbin/adapter/DB2Adapter.java | 55 +++++++++++++++++++ .../CasbinAutoConfiguration.java | 8 ++- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/casbin/adapter/DB2Adapter.java diff --git a/src/main/java/org/casbin/adapter/DB2Adapter.java b/src/main/java/org/casbin/adapter/DB2Adapter.java new file mode 100644 index 0000000..28572a6 --- /dev/null +++ b/src/main/java/org/casbin/adapter/DB2Adapter.java @@ -0,0 +1,55 @@ +package org.casbin.adapter; + +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * @author liuyunsh@cn.ibm.com + * @date 2019年4月23日 + * @version V1.0 + * + */ +public class DB2Adapter extends JdbcAdapter{ + + private static final String CHECK_TABLE_SQL = "select 1 from syscat.tables where tabname = upper('CASBIN_RULE')"; + + private final static String DROP_TABLE_SQL = "DROP TABLE CASBIN_RULE"; + + private static final String INIT_TABLE_SQL = "CREATE TABLE CASBIN_RULE (" + + " PTYPE VARCHAR(255) NOT NULL ," + + " V0 VARCHAR(255) DEFAULT NULL ," + + " V1 VARCHAR(255) DEFAULT NULL ," + + " V2 VARCHAR(255) DEFAULT NULL ," + + " V3 VARCHAR(255) DEFAULT NULL ," + + " V4 VARCHAR(255) DEFAULT NULL ," + + " V5 VARCHAR(255) DEFAULT NULL" + + ")"; + + /** + * @param jdbcTemplate + * @param autoCreateTable + */ + public DB2Adapter(JdbcTemplate jdbcTemplate, boolean autoCreateTable) { + super(jdbcTemplate, autoCreateTable); + } + + @Override + protected void initTable() { + try { + jdbcTemplate.queryForObject(CHECK_TABLE_SQL, Integer.class); + } catch (EmptyResultDataAccessException e) { + super.initTable(); + } + } + + @Override + protected String getInitTableSql() { + return INIT_TABLE_SQL; + } + + @Override + protected String getDropTableSql() { + return DROP_TABLE_SQL; + } + +} diff --git a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java index e8e65d1..f7e6746 100644 --- a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java +++ b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java @@ -1,6 +1,8 @@ package org.casbin.spring.boot.autoconfigure; -import lombok.extern.slf4j.Slf4j; +import javax.sql.DataSource; + +import org.casbin.adapter.DB2Adapter; import org.casbin.adapter.JdbcAdapter; import org.casbin.adapter.OracleAdapter; import org.casbin.exception.CasbinAdapterException; @@ -28,7 +30,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; -import javax.sql.DataSource; +import lombok.extern.slf4j.Slf4j; /** * @author fangzhengjin @@ -80,6 +82,8 @@ public Adapter autoConfigJdbcAdapter(JdbcTemplate jdbcTemplate, CasbinProperties return new JdbcAdapter(jdbcTemplate, autoCreateTable); case "oracle": return new OracleAdapter(jdbcTemplate, autoCreateTable); + case "db2": + return new DB2Adapter(jdbcTemplate, autoCreateTable); default: throw new CasbinAdapterException("Can't find " + databaseName + " jdbc adapter"); }