Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: '0'
- name: Set up JDK 1.8
uses: actions/setup-java@v1
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 1.8
java-version: '17'
distribution: 'temurin'
server-id: ossrh
server-username: OSSRH_JIRA_USERNAME
server-password: OSSRH_JIRA_PASSWORD
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
gpg-passphrase: GPG_PASSPHRASE

- name: Build with Maven
run: mvn clean test cobertura:cobertura
run: mvn clean test

- name: Codecov
uses: codecov/codecov-action@v1
Expand Down
20 changes: 4 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check />
<release>17</release>
</configuration>
</plugin>
</plugins>
Expand Down Expand Up @@ -188,7 +176,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
<version>7.0.10.Final</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
Expand All @@ -199,7 +187,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
<version>8.0.33</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/org/casbin/adapter/HibernateAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ private void createDatabase() {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
if (this.databaseProductName.contains("MySQL") || this.databaseProductName.contains("MariaDB")) {
session.createSQLQuery("CREATE DATABASE IF NOT EXISTS casbin").executeUpdate();
session.createSQLQuery("USE casbin").executeUpdate();
session.createNativeQuery("CREATE DATABASE IF NOT EXISTS casbin").executeUpdate();
session.createNativeQuery("USE casbin").executeUpdate();
} else if (this.databaseProductName.contains("SQLServer")) {
session.createSQLQuery("IF NOT EXISTS (" +
session.createNativeQuery("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").executeUpdate();
session.createSQLQuery("USE casbin").executeUpdate();
session.createNativeQuery("USE casbin").executeUpdate();
}
tx.commit();
session.close();
Expand All @@ -79,7 +79,7 @@ private void createTable() {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
if (this.databaseProductName.contains("MySQL") || this.databaseProductName.contains("MariaDB")) {
session.createSQLQuery("CREATE TABLE IF NOT EXISTS casbin_rule (" +
session.createNativeQuery("CREATE TABLE IF NOT EXISTS casbin_rule (" +
"id INT not NULL primary key," +
"ptype VARCHAR(100) not NULL," +
"v0 VARCHAR(100)," +
Expand All @@ -89,7 +89,7 @@ private void createTable() {
"v4 VARCHAR(100)," +
"v5 VARCHAR(100))").executeUpdate();
} else if (this.databaseProductName.contains("Oracle")) {
session.createSQLQuery("declare " +
session.createNativeQuery("declare " +
"nCount NUMBER;" +
"v_sql LONG;" +
"begin " +
Expand All @@ -110,7 +110,7 @@ private void createTable() {
"END IF;" +
"end;").executeUpdate();
} else if (this.databaseProductName.contains("SQLServer")) {
session.createSQLQuery("if not exists (select * from sysobjects where id = object_id('casbin_rule')) " +
session.createNativeQuery("if not exists (select * from sysobjects where id = object_id('casbin_rule')) " +
"create table casbin_rule (" +
" id int, " +
" ptype VARCHAR(100) , " +
Expand All @@ -131,9 +131,9 @@ private void dropTable() {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
if (this.databaseProductName.contains("MySQL") || this.databaseProductName.contains("MariaDB")) {
session.createSQLQuery("DROP TABLE IF EXISTS casbin_rule").executeUpdate();
session.createNativeQuery("DROP TABLE IF EXISTS casbin_rule").executeUpdate();
} else if (this.databaseProductName.contains("Oracle")) {
session.createSQLQuery("declare " +
session.createNativeQuery("declare " +
"nCount NUMBER;" +
"v_sql LONG;" +
"begin " +
Expand All @@ -145,7 +145,7 @@ private void dropTable() {
"END IF;" +
"end;").executeUpdate();
} else if (this.databaseProductName.contains("SQLServer")) {
session.createSQLQuery("if exists (select * from sysobjects where id = object_id('casbin_rule') drop table casbin_rule").executeUpdate();
session.createNativeQuery("if exists (select * from sysobjects where id = object_id('casbin_rule') drop table casbin_rule").executeUpdate();
}
tx.commit();
session.close();
Expand All @@ -164,9 +164,7 @@ private SessionFactory initSessionFactory() throws SQLException {
properties.setProperty("hibernate.connection.url", this.url);
properties.setProperty("hibernate.connection.username", this.username);
properties.setProperty("hibernate.connection.password", this.password);
if (this.driver.contains("mysql")) {
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
} else if (this.driver.contains("oracle")) {
if (this.driver.contains("oracle")) {
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9iDialect");
} else if (this.driver.contains("sqlserver")) {
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");
Expand All @@ -183,7 +181,7 @@ private SessionFactory initSessionFactory() throws SQLException {
public void loadPolicy(Model model) {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
List<CasbinRule> casbinRules = session.createSQLQuery("SELECT * FROM casbin_rule").addEntity(CasbinRule.class).list();
List<CasbinRule> casbinRules = session.createNativeQuery("SELECT * FROM casbin_rule").addEntity(CasbinRule.class).list();
for (CasbinRule line : casbinRules) {
loadPolicyLine(line, model);
}
Expand Down Expand Up @@ -254,15 +252,15 @@ private void insertData(CasbinRule line, Session session) {
line.getV3(),
line.getV4(),
line.getV5());
session.createSQLQuery(sql).executeUpdate();
session.createNativeQuery(sql).executeUpdate();
}

private void deleteData(Session session, String ptype, List<String> rules) {
StringBuilder sql = new StringBuilder("DELETE FROM casbin_rule WHERE ptype = '" + ptype + "'");
for (int i=0;i<rules.size();i++) {
sql.append(" AND v").append(i).append(" = '").append(rules.get(i)).append("'");
}
session.createSQLQuery(sql.toString()).executeUpdate();
session.createNativeQuery(sql.toString()).executeUpdate();
}

private CasbinRule savePolicyLine(String ptype, List<String> rule, int id) {
Expand Down Expand Up @@ -323,7 +321,7 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
private void reset() {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
List<CasbinRule> casbinRules = session.createSQLQuery("SELECT * FROM casbin_rule").addEntity(CasbinRule.class).list();
List<CasbinRule> casbinRules = session.createNativeQuery("SELECT * FROM casbin_rule").addEntity(CasbinRule.class).list();
tx.commit();
session.close();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/casbin/test/HibernateAdapterTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.casbin.test;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.casbin.adapter.HibernateAdapter;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.persist.Adapter;
Expand Down