mirror of
https://gitee.com/jmix/cuba.git
synced 2024-11-30 18:27:56 +08:00
MySQL support
This commit is contained in:
parent
d47109a1b0
commit
90adb4c3f1
5
build.properties
Normal file
5
build.properties
Normal file
@ -0,0 +1,5 @@
|
||||
db.driver=org.hsqldb.jdbcDriver
|
||||
db.name=cubadb
|
||||
db.url=jdbc:hsqldb:hsql://localhost/${db.name}
|
||||
db.user=sa
|
||||
db.password=
|
@ -6,9 +6,10 @@
|
||||
|
||||
<import file="${root.dir}/build-inc-prj.xml"/>
|
||||
<property file="${root.dir}/build.properties"/>
|
||||
<property file="${project.dir}/build.properties"/>
|
||||
|
||||
<property name="sql.dir" location="sql"/>
|
||||
<property name="db.name" value="cubadb"/>
|
||||
<property name="sql.delimiter" value=";"/>
|
||||
|
||||
<path id="run-cp">
|
||||
<fileset refid="server-lib-fs"/>
|
||||
@ -26,7 +27,7 @@
|
||||
<sql classpathref="run-cp"
|
||||
driver="org.hsqldb.jdbcDriver"
|
||||
url="jdbc:hsqldb:hsql://localhost/${db.name}"
|
||||
userid="sa" password="" onerror="continue">
|
||||
userid="${db.user}" password="${db.password}" onerror="continue">
|
||||
shutdown
|
||||
</sql>
|
||||
</target>
|
||||
@ -39,10 +40,10 @@
|
||||
</target>
|
||||
|
||||
<target name="create-db">
|
||||
<sql classpathref="run-cp" src="${sql.dir}/create-db.sql"
|
||||
driver="org.hsqldb.jdbcDriver"
|
||||
url="jdbc:hsqldb:hsql://localhost/${db.name}"
|
||||
userid="sa" password=""/>
|
||||
<sql classpathref="run-cp" src="${sql.dir}/create-db.sql" delimiter="${sql.delimiter}"
|
||||
driver="${db.driver}"
|
||||
url="${db.url}"
|
||||
userid="${db.user}" password="${db.password}"/>
|
||||
</target>
|
||||
|
||||
</project>
|
223
db/init/mysql/create-db.sql
Normal file
223
db/init/mysql/create-db.sql
Normal file
@ -0,0 +1,223 @@
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SYS_SERVER (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
NAME varchar(255),
|
||||
ADDRESS varchar(255),
|
||||
IS_RUNNING smallint,
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SYS_CONFIG (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
NAME varchar(255),
|
||||
VALUE varchar(500),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SYS_CONFIG add constraint SYS_CONFIG_UNIQ_NAME unique (NAME)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_USER (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
LOGIN varchar(20),
|
||||
PASSWORD varchar(32),
|
||||
NAME varchar(255),
|
||||
AD_USER varchar(100),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_USER add constraint SEC_USER_UNIQ_LOGIN unique (LOGIN, DELETE_TS)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_ROLE (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
NAME varchar(255),
|
||||
IS_SUPER smallint,
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_ROLE add constraint SEC_ROLE_UNIQ_NAME unique (NAME, DELETE_TS)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_GROUP (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
NAME varchar(255),
|
||||
PARENT_ID binary(16),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_GROUP add constraint SEC_GROUP_PARENT foreign key (PARENT_ID) references SEC_GROUP(ID)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_GROUP_HIERARCHY (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
GROUP_ID binary(16),
|
||||
PARENT_ID binary(16),
|
||||
LEVEL integer,
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_GROUP_HIERARCHY add constraint SEC_GROUP_HIERARCHY_GROUP foreign key (GROUP_ID) references SEC_GROUP(ID)^
|
||||
|
||||
alter table SEC_GROUP_HIERARCHY add constraint SEC_GROUP_HIERARCHY_PARENT foreign key (PARENT_ID) references SEC_GROUP(ID)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_PROFILE (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
NAME varchar(255),
|
||||
IS_DEFAULT smallint,
|
||||
USER_ID binary(16),
|
||||
GROUP_ID binary(16),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_USER foreign key (USER_ID) references SEC_USER(ID)^
|
||||
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_GROUP foreign key (GROUP_ID) references SEC_GROUP(ID)^
|
||||
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_UNIQ_NAME unique (USER_ID, NAME, DELETE_TS)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_PROFILE_ROLE (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
PROFILE_ID binary(16),
|
||||
ROLE_ID binary(16),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_ROLE_PROFILE foreign key (PROFILE_ID) references SEC_PROFILE(ID)^
|
||||
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_ROLE_ROLE foreign key (ROLE_ID) references SEC_ROLE(ID)^
|
||||
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_UNIQ_ROLE unique (PROFILE_ID, ROLE_ID, DELETE_TS)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_PERMISSION (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
TYPE integer,
|
||||
TARGET varchar(100),
|
||||
VALUE integer,
|
||||
ROLE_ID binary(16),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_PERMISSION add constraint SEC_PERMISSION_ROLE foreign key (ROLE_ID) references SEC_ROLE(ID)^
|
||||
|
||||
alter table SEC_PERMISSION add constraint SEC_PERMISSION_UNIQUE unique (ROLE_ID, TYPE, TARGET, DELETE_TS)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create table SEC_CONSTRAINT (
|
||||
ID binary(16),
|
||||
CREATE_TS datetime,
|
||||
CREATED_BY varchar(20),
|
||||
VERSION integer,
|
||||
UPDATE_TS datetime,
|
||||
UPDATED_BY varchar(20),
|
||||
DELETE_TS datetime,
|
||||
DELETED_BY varchar(20),
|
||||
ENTITY_NAME varchar(50),
|
||||
WHERE_CLAUSE varchar(500),
|
||||
GROUP_ID binary(16),
|
||||
primary key (ID)
|
||||
)^
|
||||
|
||||
alter table SEC_CONSTRAINT add constraint SEC_CONSTRAINT_GROUP foreign key (GROUP_ID) references SEC_GROUP(ID)^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
create function from_id(uuid binary(16)) returns char(36)
|
||||
begin
|
||||
return concat(hex(left(uuid,4)),'-', hex(mid(uuid,5,2)),'-', hex(mid(uuid,7,2)),'-',hex(mid(uuid,9,2)),'-',hex(right(uuid,6)));
|
||||
end^
|
||||
|
||||
create function to_id(str varchar(36)) returns binary(16)
|
||||
begin
|
||||
return concat(unhex(left(str,8)),unhex(mid(str,10,4)),unhex(mid(str,15,4)),unhex(mid(str,20,4)),unhex(right(str,12)));
|
||||
end^
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
insert into SEC_USER (ID, CREATE_TS, VERSION, LOGIN, PASSWORD, NAME)
|
||||
values (to_id('60885987-1b61-4247-94c7-dff348347f93'), current_timestamp, 0, 'admin', '21232f297a57a5a743894a0e4a801fc3', 'Administrator')^
|
||||
|
||||
insert into SEC_GROUP (ID, CREATE_TS, VERSION, NAME, PARENT_ID)
|
||||
values (to_id('0fa2b1a5-1d68-4d69-9fbd-dff348347f93'), current_timestamp, 0, 'Company', null)^
|
||||
|
||||
insert into SEC_PROFILE (ID, CREATE_TS, VERSION, NAME, IS_DEFAULT, USER_ID, GROUP_ID)
|
||||
values (to_id('bf83541f-f610-46f4-a268-dff348347f93'), current_timestamp, 0, 'Default', 1, to_id('60885987-1b61-4247-94c7-dff348347f93'), to_id('0fa2b1a5-1d68-4d69-9fbd-dff348347f93'))^
|
||||
|
||||
--insert into SEC_PROFILE (ID, CREATE_TS, VERSION, NAME, IS_DEFAULT, USER_ID, GROUP_ID)
|
||||
--values (to_id('cc1e0bc4-1062-4218-a09f-dff348347f93'), current_timestamp, 0, 'Test', 0, to_id('60885987-1b61-4247-94c7-dff348347f93'), to_id('0fa2b1a5-1d68-4d69-9fbd-dff348347f93'))^
|
||||
|
||||
insert into SEC_ROLE (ID, CREATE_TS, VERSION, NAME, IS_SUPER)
|
||||
values (to_id('0c018061-b26f-4de2-a5be-dff348347f93'), current_timestamp, 0, 'Administrators', 1)^
|
||||
|
||||
insert into SEC_PROFILE_ROLE (ID, CREATE_TS, VERSION, PROFILE_ID, ROLE_ID)
|
||||
values (to_id('c838be0a-96d0-4ef4-a7c0-dff348347f93'), current_timestamp, 0, to_id('bf83541f-f610-46f4-a268-dff348347f93'), to_id('0c018061-b26f-4de2-a5be-dff348347f93'))^
|
@ -85,7 +85,7 @@ create table SEC_GROUP (
|
||||
primary key (ID)
|
||||
);
|
||||
|
||||
alter table SEC_GROUP add constraint SEC_GROUP_PARENT foreign key (PARENT_ID) references SEC_GROUP;
|
||||
alter table SEC_GROUP add constraint SEC_GROUP_PARENT foreign key (PARENT_ID) references SEC_GROUP(ID);
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@ -99,9 +99,9 @@ create table SEC_GROUP_HIERARCHY (
|
||||
primary key (ID)
|
||||
);
|
||||
|
||||
alter table SEC_GROUP_HIERARCHY add constraint SEC_GROUP_HIERARCHY_GROUP foreign key (GROUP_ID) references SEC_GROUP;
|
||||
alter table SEC_GROUP_HIERARCHY add constraint SEC_GROUP_HIERARCHY_GROUP foreign key (GROUP_ID) references SEC_GROUP(ID);
|
||||
|
||||
alter table SEC_GROUP_HIERARCHY add constraint SEC_GROUP_HIERARCHY_PARENT foreign key (PARENT_ID) references SEC_GROUP;
|
||||
alter table SEC_GROUP_HIERARCHY add constraint SEC_GROUP_HIERARCHY_PARENT foreign key (PARENT_ID) references SEC_GROUP(ID);
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@ -121,9 +121,9 @@ create table SEC_PROFILE (
|
||||
primary key (ID)
|
||||
);
|
||||
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_USER foreign key (USER_ID) references SEC_USER;
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_USER foreign key (USER_ID) references SEC_USER(ID);
|
||||
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_GROUP foreign key (GROUP_ID) references SEC_GROUP;
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_GROUP foreign key (GROUP_ID) references SEC_GROUP(ID);
|
||||
|
||||
alter table SEC_PROFILE add constraint SEC_PROFILE_UNIQ_NAME unique (USER_ID, NAME, DELETE_TS);
|
||||
|
||||
@ -143,9 +143,9 @@ create table SEC_PROFILE_ROLE (
|
||||
primary key (ID)
|
||||
);
|
||||
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_ROLE_PROFILE foreign key (PROFILE_ID) references SEC_PROFILE;
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_ROLE_PROFILE foreign key (PROFILE_ID) references SEC_PROFILE(ID);
|
||||
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_ROLE_ROLE foreign key (ROLE_ID) references SEC_ROLE;
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_ROLE_ROLE foreign key (ROLE_ID) references SEC_ROLE(ID);
|
||||
|
||||
alter table SEC_PROFILE_ROLE add constraint SEC_PROFILE_UNIQ_ROLE unique (PROFILE_ID, ROLE_ID, DELETE_TS);
|
||||
|
||||
@ -167,7 +167,7 @@ create table SEC_PERMISSION (
|
||||
primary key (ID)
|
||||
);
|
||||
|
||||
alter table SEC_PERMISSION add constraint SEC_PERMISSION_ROLE foreign key (ROLE_ID) references SEC_ROLE;
|
||||
alter table SEC_PERMISSION add constraint SEC_PERMISSION_ROLE foreign key (ROLE_ID) references SEC_ROLE(ID);
|
||||
|
||||
alter table SEC_PERMISSION add constraint SEC_PERMISSION_UNIQUE unique (ROLE_ID, TYPE, TARGET, DELETE_TS);
|
||||
|
||||
@ -188,7 +188,7 @@ create table SEC_CONSTRAINT (
|
||||
primary key (ID)
|
||||
);
|
||||
|
||||
alter table SEC_CONSTRAINT add constraint SEC_CONSTRAINT_GROUP foreign key (GROUP_ID) references SEC_GROUP;
|
||||
alter table SEC_CONSTRAINT add constraint SEC_CONSTRAINT_GROUP foreign key (GROUP_ID) references SEC_GROUP(ID);
|
||||
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
<property name="openjpa.jdbc.DBDictionary" value="com.haulmont.cuba.core.sys.persistence.CubaHSQLDictionary(SimulateLocking=true)"/>
|
||||
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>
|
||||
<property name="openjpa.jdbc.MappingDefaults"
|
||||
value="FieldStrategies='java.util.UUID=com.haulmont.cuba.core.sys.persistence.UuidValueHandler'"/>
|
||||
value="FieldStrategies='java.util.UUID=com.haulmont.cuba.core.sys.persistence.UuidStringValueHandler'"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
@ -21,124 +21,11 @@ import com.haulmont.cuba.core.PersistenceProvider;
|
||||
|
||||
public class CubaHSQLDictionary extends HSQLDictionary
|
||||
{
|
||||
private static final String DELETE_TS_COL = "DELETE_TS";
|
||||
|
||||
public SQLBuffer toTraditionalJoin(Join join) {
|
||||
ForeignKey fk = join.getForeignKey();
|
||||
if (fk == null)
|
||||
return null;
|
||||
|
||||
boolean inverse = join.isForeignKeyInversed();
|
||||
Column[] from = (inverse) ? fk.getPrimaryKeyColumns()
|
||||
: fk.getColumns();
|
||||
Column[] to = (inverse) ? fk.getColumns()
|
||||
: fk.getPrimaryKeyColumns();
|
||||
|
||||
// do column joins
|
||||
SQLBuffer buf = new SQLBuffer(this);
|
||||
int count = 0;
|
||||
for (int i = 0; i < from.length; i++, count++) {
|
||||
if (count > 0)
|
||||
buf.append(" AND ");
|
||||
buf.append(join.getAlias1()).append(".").append(from[i]);
|
||||
buf.append(" = ");
|
||||
buf.append(join.getAlias2()).append(".").append(to[i]);
|
||||
|
||||
// KK: support deferred delete for collections
|
||||
if (inverse
|
||||
&& to[i].getTable().containsColumn(DELETE_TS_COL)
|
||||
&& PersistenceProvider.getEntityManager().isDeleteDeferred())
|
||||
{
|
||||
buf.append(" AND ");
|
||||
buf.append(join.getAlias2()).append(".").append(DELETE_TS_COL).append(" IS NULL");
|
||||
}
|
||||
}
|
||||
|
||||
// do constant joins
|
||||
Column[] constCols = fk.getConstantColumns();
|
||||
for (int i = 0; i < constCols.length; i++, count++) {
|
||||
if (count > 0)
|
||||
buf.append(" AND ");
|
||||
if (inverse)
|
||||
buf.appendValue(fk.getConstant(constCols[i]), constCols[i]);
|
||||
else
|
||||
buf.append(join.getAlias1()).append(".").
|
||||
append(constCols[i]);
|
||||
buf.append(" = ");
|
||||
|
||||
if (inverse)
|
||||
buf.append(join.getAlias2()).append(".").
|
||||
append(constCols[i]);
|
||||
else
|
||||
buf.appendValue(fk.getConstant(constCols[i]), constCols[i]);
|
||||
}
|
||||
|
||||
Column[] constColsPK = fk.getConstantPrimaryKeyColumns();
|
||||
for (int i = 0; i < constColsPK.length; i++, count++) {
|
||||
if (count > 0)
|
||||
buf.append(" AND ");
|
||||
if (inverse)
|
||||
buf.append(join.getAlias1()).append(".").
|
||||
append(constColsPK[i]);
|
||||
else
|
||||
buf.appendValue(fk.getPrimaryKeyConstant(constColsPK[i]),
|
||||
constColsPK[i]);
|
||||
buf.append(" = ");
|
||||
|
||||
if (inverse)
|
||||
buf.appendValue(fk.getPrimaryKeyConstant(constColsPK[i]),
|
||||
constColsPK[i]);
|
||||
else
|
||||
buf.append(join.getAlias2()).append(".").
|
||||
append(constColsPK[i]);
|
||||
}
|
||||
return buf;
|
||||
return DBDictionaryUtils.toTraditionalJoin(this, join);
|
||||
}
|
||||
|
||||
protected SQLBuffer getWhere(Select sel, boolean forUpdate) {
|
||||
Joins joins = sel.getJoins();
|
||||
if (sel.getJoinSyntax() == SYNTAX_SQL92
|
||||
|| joins == null || joins.isEmpty())
|
||||
{
|
||||
SQLBuffer buf = sel.getWhere();
|
||||
if (!PersistenceProvider.getEntityManager().isDeleteDeferred())
|
||||
return buf;
|
||||
|
||||
Map<Table, String> tables = new HashMap<Table, String>();
|
||||
Collection columns;
|
||||
if (buf != null) {
|
||||
columns = buf.getColumns();
|
||||
}
|
||||
else {
|
||||
columns = sel.getSelects();
|
||||
}
|
||||
for (Object item : columns) {
|
||||
if (item instanceof Column) {
|
||||
Column col = (Column) item;
|
||||
for (String s : (Collection<String>) sel.getTableAliases()) {
|
||||
int i = s.indexOf(' ');
|
||||
String tableName = s.substring(0, i);
|
||||
if (col.getTable().getName().equals(tableName)) {
|
||||
if (col.getTable().containsColumn(DELETE_TS_COL))
|
||||
tables.put(col.getTable(), s.substring(i + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String alias : tables.values()) {
|
||||
sb.append(alias).append(".").append(DELETE_TS_COL).append(" IS NULL");
|
||||
}
|
||||
sel.where(sb.toString());
|
||||
return sel.getWhere();
|
||||
}
|
||||
|
||||
SQLBuffer where = new SQLBuffer(this);
|
||||
if (sel.getWhere() != null)
|
||||
where.append(sel.getWhere());
|
||||
if (joins != null)
|
||||
sel.append(where, joins);
|
||||
return where;
|
||||
return DBDictionaryUtils.getWhere(this, sel, forUpdate);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 21.01.2009 17:33:38
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.sys.persistence;
|
||||
|
||||
import org.apache.openjpa.jdbc.sql.MySQLDictionary;
|
||||
import org.apache.openjpa.jdbc.sql.SQLBuffer;
|
||||
import org.apache.openjpa.jdbc.sql.Join;
|
||||
import org.apache.openjpa.jdbc.sql.Select;
|
||||
|
||||
public class CubaMySQLDictionary extends MySQLDictionary
|
||||
{
|
||||
public SQLBuffer toTraditionalJoin(Join join) {
|
||||
return DBDictionaryUtils.toTraditionalJoin(this, join);
|
||||
}
|
||||
|
||||
protected SQLBuffer getWhere(Select sel, boolean forUpdate) {
|
||||
return DBDictionaryUtils.getWhere(this, sel, forUpdate);
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 21.01.2009 17:35:20
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.sys.persistence;
|
||||
|
||||
import org.apache.openjpa.jdbc.sql.*;
|
||||
import org.apache.openjpa.jdbc.schema.ForeignKey;
|
||||
import org.apache.openjpa.jdbc.schema.Column;
|
||||
import org.apache.openjpa.jdbc.schema.Table;
|
||||
import com.haulmont.cuba.core.PersistenceProvider;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collection;
|
||||
|
||||
public class DBDictionaryUtils
|
||||
{
|
||||
private static final String DELETE_TS_COL = "DELETE_TS";
|
||||
|
||||
public static SQLBuffer toTraditionalJoin(DBDictionary dbDictionary, Join join) {
|
||||
ForeignKey fk = join.getForeignKey();
|
||||
if (fk == null)
|
||||
return null;
|
||||
|
||||
boolean inverse = join.isForeignKeyInversed();
|
||||
Column[] from = (inverse) ? fk.getPrimaryKeyColumns()
|
||||
: fk.getColumns();
|
||||
Column[] to = (inverse) ? fk.getColumns()
|
||||
: fk.getPrimaryKeyColumns();
|
||||
|
||||
// do column joins
|
||||
SQLBuffer buf = new SQLBuffer(dbDictionary);
|
||||
int count = 0;
|
||||
for (int i = 0; i < from.length; i++, count++) {
|
||||
if (count > 0)
|
||||
buf.append(" AND ");
|
||||
buf.append(join.getAlias1()).append(".").append(from[i]);
|
||||
buf.append(" = ");
|
||||
buf.append(join.getAlias2()).append(".").append(to[i]);
|
||||
|
||||
// KK: support deferred delete for collections
|
||||
if (inverse
|
||||
&& to[i].getTable().containsColumn(DELETE_TS_COL)
|
||||
&& PersistenceProvider.getEntityManager().isDeleteDeferred())
|
||||
{
|
||||
buf.append(" AND ");
|
||||
buf.append(join.getAlias2()).append(".").append(DELETE_TS_COL).append(" IS NULL");
|
||||
}
|
||||
}
|
||||
|
||||
// do constant joins
|
||||
Column[] constCols = fk.getConstantColumns();
|
||||
for (int i = 0; i < constCols.length; i++, count++) {
|
||||
if (count > 0)
|
||||
buf.append(" AND ");
|
||||
if (inverse)
|
||||
buf.appendValue(fk.getConstant(constCols[i]), constCols[i]);
|
||||
else
|
||||
buf.append(join.getAlias1()).append(".").
|
||||
append(constCols[i]);
|
||||
buf.append(" = ");
|
||||
|
||||
if (inverse)
|
||||
buf.append(join.getAlias2()).append(".").
|
||||
append(constCols[i]);
|
||||
else
|
||||
buf.appendValue(fk.getConstant(constCols[i]), constCols[i]);
|
||||
}
|
||||
|
||||
Column[] constColsPK = fk.getConstantPrimaryKeyColumns();
|
||||
for (int i = 0; i < constColsPK.length; i++, count++) {
|
||||
if (count > 0)
|
||||
buf.append(" AND ");
|
||||
if (inverse)
|
||||
buf.append(join.getAlias1()).append(".").
|
||||
append(constColsPK[i]);
|
||||
else
|
||||
buf.appendValue(fk.getPrimaryKeyConstant(constColsPK[i]),
|
||||
constColsPK[i]);
|
||||
buf.append(" = ");
|
||||
|
||||
if (inverse)
|
||||
buf.appendValue(fk.getPrimaryKeyConstant(constColsPK[i]),
|
||||
constColsPK[i]);
|
||||
else
|
||||
buf.append(join.getAlias2()).append(".").
|
||||
append(constColsPK[i]);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static SQLBuffer getWhere(DBDictionary dbDictionary, Select sel, boolean forUpdate) {
|
||||
Joins joins = sel.getJoins();
|
||||
if (sel.getJoinSyntax() == JoinSyntaxes.SYNTAX_SQL92
|
||||
|| joins == null || joins.isEmpty())
|
||||
{
|
||||
SQLBuffer buf = sel.getWhere();
|
||||
if (!PersistenceProvider.getEntityManager().isDeleteDeferred())
|
||||
return buf;
|
||||
|
||||
Map<Table, String> tables = new HashMap<Table, String>();
|
||||
Collection columns;
|
||||
if (buf != null) {
|
||||
columns = buf.getColumns();
|
||||
}
|
||||
else {
|
||||
columns = sel.getSelects();
|
||||
}
|
||||
for (Object item : columns) {
|
||||
if (item instanceof Column) {
|
||||
Column col = (Column) item;
|
||||
for (String s : (Collection<String>) sel.getTableAliases()) {
|
||||
int i = s.indexOf(' ');
|
||||
String tableName = s.substring(0, i);
|
||||
if (col.getTable().getName().equals(tableName)) {
|
||||
if (col.getTable().containsColumn(DELETE_TS_COL))
|
||||
tables.put(col.getTable(), s.substring(i + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String alias : tables.values()) {
|
||||
sb.append(alias).append(".").append(DELETE_TS_COL).append(" IS NULL");
|
||||
}
|
||||
sel.where(sb.toString());
|
||||
return sel.getWhere();
|
||||
}
|
||||
|
||||
SQLBuffer where = new SQLBuffer(dbDictionary);
|
||||
if (sel.getWhere() != null)
|
||||
where.append(sel.getWhere());
|
||||
if (joins != null)
|
||||
sel.append(where, joins);
|
||||
return where;
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 21.01.2009 14:55:02
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.sys.persistence;
|
||||
|
||||
import org.apache.openjpa.jdbc.kernel.JDBCStore;
|
||||
import org.apache.openjpa.jdbc.meta.ValueMapping;
|
||||
import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
|
||||
import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;
|
||||
import org.apache.openjpa.jdbc.schema.Column;
|
||||
import org.apache.openjpa.jdbc.schema.ColumnIO;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UuidBinaryValueHandler extends AbstractValueHandler
|
||||
{
|
||||
private static final long serialVersionUID = 7373689934203363322L;
|
||||
|
||||
public Column[] map(ValueMapping vm, String name, ColumnIO io, boolean adapt) {
|
||||
Column col = new Column();
|
||||
col.setName(name);
|
||||
col.setType(Types.BINARY);
|
||||
col.setJavaType(JavaSQLTypes.ARRAY);
|
||||
col.setSize(16);
|
||||
return new Column[]{col};
|
||||
}
|
||||
|
||||
public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) {
|
||||
byte[] bytes = new byte[16];
|
||||
long l = ((UUID) val).getMostSignificantBits();
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
byte b = (byte) (Long.rotateRight(l, i * 8) & 255);
|
||||
bytes[7 - i] = b;
|
||||
}
|
||||
l = ((UUID) val).getLeastSignificantBits();
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
byte b = (byte) (Long.rotateRight(l, i * 8) & 255);
|
||||
bytes[15 - i] = b;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public Object toObjectValue(ValueMapping vm, Object val) {
|
||||
if (val == null)
|
||||
return null;
|
||||
|
||||
byte[] bytes = (byte[]) val;
|
||||
|
||||
long mostBits = 0;
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
long b = Long.rotateLeft(((long) bytes[i]) & 255, 8 * (7 - i));
|
||||
mostBits = mostBits + b;
|
||||
}
|
||||
|
||||
long leastBits = 0;
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
long b = Long.rotateLeft(((long) bytes[8 + i]) & 255, 8 * (7 - i));
|
||||
leastBits = leastBits + b;
|
||||
}
|
||||
return new UUID(mostBits, leastBits);
|
||||
}
|
||||
}
|
@ -18,8 +18,10 @@ import org.apache.openjpa.jdbc.kernel.JDBCStore;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class UuidValueHandler extends AbstractValueHandler
|
||||
public class UuidStringValueHandler extends AbstractValueHandler
|
||||
{
|
||||
private static final long serialVersionUID = -8302367450468711877L;
|
||||
|
||||
public Column[] map(ValueMapping vm, String name, ColumnIO io, boolean adapt) {
|
||||
Column col = new Column();
|
||||
col.setName(name);
|
||||
@ -27,8 +29,9 @@ public class UuidValueHandler extends AbstractValueHandler
|
||||
col.setSize(-1);
|
||||
return new Column[]{col};
|
||||
}
|
||||
|
||||
public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) {
|
||||
return ((UUID) val).toString();
|
||||
return val.toString();
|
||||
}
|
||||
|
||||
public Object toObjectValue(ValueMapping vm, Object val) {
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 21.01.2009 15:33:49
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.sys.persistence;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
|
||||
public class UuidBinaryValueHandlerTest extends TestCase
|
||||
{
|
||||
private UuidBinaryValueHandler handler = new UuidBinaryValueHandler();
|
||||
|
||||
public void test() {
|
||||
UUID uuid = UUID.fromString("60885987-1b61-4247-94c7-dff348347f93");
|
||||
|
||||
byte[] bytes = (byte[]) handler.toDataStoreValue(null, uuid, null);
|
||||
UUID val = (UUID) handler.toObjectValue(null, bytes);
|
||||
|
||||
assertEquals(uuid, val);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user