Skip to content

Commit

Permalink
add db2 support (#5)
Browse files Browse the repository at this point in the history
* support db2
  • Loading branch information
doctormacky authored and fangzhengjin committed May 9, 2019
1 parent 6b4d226 commit 47bdfcb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/main/java/org/casbin/adapter/DB2Adapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.casbin.adapter;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
* @author [email protected]
* @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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 47bdfcb

Please sign in to comment.