1
1
package org .casbin .adapter ;
2
2
3
3
import org .apache .commons .collections .CollectionUtils ;
4
+ import org .casbin .constants .AdapterConstants ;
4
5
import org .casbin .jcasbin .model .Assertion ;
5
6
import org .casbin .jcasbin .model .Model ;
6
7
import org .casbin .jcasbin .persist .Adapter ;
@@ -59,10 +60,10 @@ private void open() throws SQLException {
59
60
private void createDatabase () {
60
61
Session session = factory .openSession ();
61
62
Transaction tx = session .beginTransaction ();
62
- if (this .databaseProductName .contains (" MySQL" )) {
63
+ if (this .databaseProductName .contains (AdapterConstants . MySQL . name ()) || this . databaseProductName . contains ( AdapterConstants . MariaDB . name () )) {
63
64
session .createSQLQuery ("CREATE DATABASE IF NOT EXISTS casbin" ).executeUpdate ();
64
65
session .createSQLQuery ("USE casbin" ).executeUpdate ();
65
- } else if (this .databaseProductName .contains (" SQLServer" )) {
66
+ } else if (this .databaseProductName .contains (AdapterConstants . SQLServer . name () )) {
66
67
session .createSQLQuery ("IF NOT EXISTS (" +
67
68
"SELECT * FROM sysdatabases WHERE name = 'casbin') CREATE DATABASE casbin ON PRIMARY " +
68
69
"( NAME = N'casbin', FILENAME = N'C:\\ Program Files\\ Microsoft SQL Server\\ MSSQL.1\\ MSSQL\\ DATA\\ casbinDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) " +
@@ -78,7 +79,7 @@ private void createDatabase() {
78
79
private void createTable () {
79
80
Session session = factory .openSession ();
80
81
Transaction tx = session .beginTransaction ();
81
- if (this .databaseProductName .contains (" MySQL" )) {
82
+ if (this .databaseProductName .contains (AdapterConstants . MySQL . name ()) || this . databaseProductName . contains ( AdapterConstants . MariaDB . name () )) {
82
83
session .createSQLQuery ("CREATE TABLE IF NOT EXISTS casbin_rule (" +
83
84
"id INT not NULL primary key," +
84
85
"ptype VARCHAR(100) not NULL," +
@@ -88,7 +89,7 @@ private void createTable() {
88
89
"v3 VARCHAR(100)," +
89
90
"v4 VARCHAR(100)," +
90
91
"v5 VARCHAR(100))" ).executeUpdate ();
91
- } else if (this .databaseProductName .contains (" Oracle" )) {
92
+ } else if (this .databaseProductName .contains (AdapterConstants . Oracle . name () )) {
92
93
session .createSQLQuery ("declare " +
93
94
"nCount NUMBER;" +
94
95
"v_sql LONG;" +
@@ -109,7 +110,7 @@ private void createTable() {
109
110
"execute immediate v_sql;" +
110
111
"END IF;" +
111
112
"end;" ).executeUpdate ();
112
- } else if (this .databaseProductName .contains (" SQLServer" )) {
113
+ } else if (this .databaseProductName .contains (AdapterConstants . SQLServer . name () )) {
113
114
session .createSQLQuery ("if not exists (select * from sysobjects where id = object_id('casbin_rule')) " +
114
115
"create table casbin_rule (" +
115
116
" id int, " +
@@ -130,9 +131,9 @@ private void createTable() {
130
131
private void dropTable () {
131
132
Session session = factory .openSession ();
132
133
Transaction tx = session .beginTransaction ();
133
- if (this .databaseProductName .contains (" MySQL" )) {
134
+ if (this .databaseProductName .contains (AdapterConstants . MySQL . name ()) || this . databaseProductName . contains ( AdapterConstants . MariaDB . name () )) {
134
135
session .createSQLQuery ("DROP TABLE IF EXISTS casbin_rule" ).executeUpdate ();
135
- } else if (this .databaseProductName .contains (" Oracle" )) {
136
+ } else if (this .databaseProductName .contains (AdapterConstants . Oracle . name () )) {
136
137
session .createSQLQuery ("declare " +
137
138
"nCount NUMBER;" +
138
139
"v_sql LONG;" +
@@ -144,7 +145,7 @@ private void dropTable() {
144
145
"execute immediate v_sql;" +
145
146
"END IF;" +
146
147
"end;" ).executeUpdate ();
147
- } else if (this .databaseProductName .contains (" SQLServer" )) {
148
+ } else if (this .databaseProductName .contains (AdapterConstants . SQLServer . name () )) {
148
149
session .createSQLQuery ("if exists (select * from sysobjects where id = object_id('casbin_rule') drop table casbin_rule" ).executeUpdate ();
149
150
}
150
151
tx .commit ();
0 commit comments