Skip to content

Commit

Permalink
Added support for SQL Server 2012.
Browse files Browse the repository at this point in the history
Signed-off-by: tldyl <756560020@qq.com>
  • Loading branch information
tldyl committed Sep 29, 2020
1 parent e4d3abf commit 1cc59e8
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 36 deletions.
132 changes: 101 additions & 31 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

<groupId>org.casbin</groupId>
<artifactId>mybatis-adapter</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>

<name>Mybatis Adapter for JCasbin</name>
<description>Load policy from Mybatis or save policy to it</description>
<url>https://github.com/jcasbin/mybatis-adapter</url>
<inceptionYear>2020</inceptionYear>

<issueManagement>
Expand Down Expand Up @@ -51,36 +55,96 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>

<dependencies>
<dependency>
Expand All @@ -105,6 +169,12 @@
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
26 changes: 22 additions & 4 deletions src/main/java/org/casbin/adapter/CasbinRuleDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ public interface CasbinRuleDao {
List<CasbinRule> 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, " +
Expand All @@ -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;" +
Expand All @@ -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;" +
Expand All @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/casbin/adapter/MybatisAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 1cc59e8

Please sign in to comment.