diff --git a/pom.xml b/pom.xml index 506a132..ea9ae09 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,11 @@ org.casbin mybatis-adapter - 1.0-SNAPSHOT + 1.0.0 + + Mybatis Adapter for JCasbin + Load policy from Mybatis or save policy to it + https://github.com/jcasbin/mybatis-adapter 2020 @@ -51,36 +55,96 @@ UTF-8 - - - - org.eluder.coveralls - coveralls-maven-plugin - 4.3.0 - - - org.jacoco - jacoco-maven-plugin - 0.7.6.201602180812 - - - prepare-agent - - prepare-agent - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - + + + default + + true + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + package + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + package + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + true + + + + verify + + sign + + + + + + org.eluder.coveralls + coveralls-maven-plugin + 4.3.0 + + + org.jacoco + jacoco-maven-plugin + 0.7.6.201602180812 + + + prepare-agent + + prepare-agent + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots/ + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + @@ -105,6 +169,12 @@ mybatis 3.4.6 + + com.microsoft.sqlserver + mssql-jdbc + 8.2.2.jre8 + test + diff --git a/src/main/java/org/casbin/adapter/CasbinRuleDao.java b/src/main/java/org/casbin/adapter/CasbinRuleDao.java index 3a0c1a9..e24ef97 100644 --- a/src/main/java/org/casbin/adapter/CasbinRuleDao.java +++ b/src/main/java/org/casbin/adapter/CasbinRuleDao.java @@ -12,7 +12,15 @@ public interface CasbinRuleDao { List loadAll(); @Update("CREATE DATABASE IF NOT EXISTS ${databaseName}") - void createDatabase(@Param("databaseName") String databaseName); + void createMysqlDatabase(@Param("databaseName") String databaseName); + + @Update("IF NOT EXISTS (" + + "SELECT * FROM sysdatabases WHERE name = 'casbin') CREATE DATABASE casbin ON PRIMARY " + + "( NAME = N'casbin', FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\DATA\\casbinDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) " + + "LOG ON\n" + + "( NAME = N'casbin_log', FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\DATA\\casbinDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) " + + "COLLATE Chinese_PRC_CI_AS") + void createSqlServerDatabase(@Param("databaseName") String databaseName); @Update("CREATE TABLE IF NOT EXISTS ${tableName} " + "(ptype VARCHAR(100) not NULL, " + @@ -24,6 +32,17 @@ public interface CasbinRuleDao { " v5 VARCHAR(100))") void createMysqlTable(@Param("tableName") String tableName); + @Update("if not exists (select * from sysobjects where id = object_id('${tableName}')) " + + "create table ${tableName} (" + + " ptype VARCHAR(100) not NULL, " + + " v0 VARCHAR(100), " + + " v1 VARCHAR(100), " + + " v2 VARCHAR(100), " + + " v3 VARCHAR(100), " + + " v4 VARCHAR(100), " + + " v5 VARCHAR(100) " + + ")") + void createSqlServerTable(@Param("tableName") String tableName); @Update("declare " + "nCount NUMBER;" + @@ -49,8 +68,6 @@ public interface CasbinRuleDao { @Update("DROP TABLE IF EXISTS ${tableName}") void dropMysqlTable(@Param("tableName") String tableName); - - @Update("declare " + "nCount NUMBER;" + "v_sql LONG;" + @@ -64,7 +81,8 @@ public interface CasbinRuleDao { "end;") void dropOracleTable(@Param("tableName") String tableName); - + @Update("if exists (select * from sysobjects where id = object_id('${tableName}') drop table ${tableName}") + void dropSqlServerTable(@Param("tableName") String tableName); @Insert("INSERT INTO casbin_rule (ptype,v0,v1,v2,v3,v4,v5) VALUES (#{ptype},#{v0},#{v1},#{v2},#{v3},#{v4},#{v5})") void insertData(CasbinRule line); diff --git a/src/main/java/org/casbin/adapter/MybatisAdapter.java b/src/main/java/org/casbin/adapter/MybatisAdapter.java index 3451f57..a2184a2 100644 --- a/src/main/java/org/casbin/adapter/MybatisAdapter.java +++ b/src/main/java/org/casbin/adapter/MybatisAdapter.java @@ -104,7 +104,16 @@ private String getUrl(String url){ private void createDatabase(){ SqlSession sqlSession = factory.openSession(true); CasbinRuleDao casbinRuleDao = sqlSession.getMapper(CasbinRuleDao.class); - casbinRuleDao.createDatabase("casbin"); + + switch (driver) { + case "com.mysql.cj.jdbc.Driver": + casbinRuleDao.createMysqlDatabase("casbin"); + break; + case "com.microsoft.sqlserver.jdbc.SQLServerDriver": + casbinRuleDao.createSqlServerDatabase("casbin"); + break; + } + sqlSession.close(); } @@ -119,6 +128,9 @@ private void createTable(){ case "com.mysql.cj.jdbc.Driver": casbinRuleDao.createMysqlTable("casbin_rule"); break; + case "com.microsoft.sqlserver.jdbc.SQLServerDriver": + casbinRuleDao.createSqlServerTable("casbin_rule"); + break; } sqlSession.close(); @@ -136,6 +148,9 @@ private void dropTable(){ case "com.mysql.cj.jdbc.Driver": casbinRuleDao.dropMysqlTable("casbin_rule"); break; + case "com.microsoft.sqlserver.jdbc.SQLServerDriver": + casbinRuleDao.dropSqlServerTable("casbin_rule"); + break; } sqlSession.close();