mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-05 04:38:10 +08:00
PL-1708 Generate INSERT SQL statements for selected entity in System Information window
#PL-1708
This commit is contained in:
parent
db3554b0b9
commit
1a07459f04
@ -7,6 +7,7 @@ package com.haulmont.cuba.core.app.script;
|
||||
|
||||
import com.haulmont.bali.util.Preconditions;
|
||||
import com.haulmont.cuba.core.entity.Entity;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@ -18,12 +19,14 @@ public class ScriptGenerationServiceBean implements ScriptGenerationService {
|
||||
@Override
|
||||
public String generateInsertScript(Entity entity) {
|
||||
Preconditions.checkNotNullArgument(entity);
|
||||
return new ScriptGeneratorImpl(entity.getClass()).generateInsertScript(entity);
|
||||
SqlScriptGenerator generator = AppBeans.getPrototype(SqlScriptGenerator.NAME, entity.getClass());
|
||||
return generator.generateInsertScript(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateUpdateScript(Entity entity) {
|
||||
Preconditions.checkNotNullArgument(entity);
|
||||
return new ScriptGeneratorImpl(entity.getClass()).generateUpdateScript(entity);
|
||||
SqlScriptGenerator generator = AppBeans.getPrototype(SqlScriptGenerator.NAME, entity.getClass());
|
||||
return generator.generateUpdateScript(entity);
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,18 @@ package com.haulmont.cuba.core.app.script;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.haulmont.chile.core.datatypes.impl.EnumClass;
|
||||
import com.haulmont.chile.core.model.Instance;
|
||||
import com.haulmont.cuba.core.Persistence;
|
||||
import com.haulmont.cuba.core.entity.Entity;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import com.haulmont.cuba.core.sys.persistence.DbTypeConverter;
|
||||
import com.haulmont.cuba.core.global.Metadata;
|
||||
import com.haulmont.cuba.core.sys.persistence.PostgresUUID;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import javax.annotation.ManagedBean;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.Table;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -31,9 +33,12 @@ import static java.lang.String.format;
|
||||
* @author degtyarjov
|
||||
* @version $Id$
|
||||
*/
|
||||
public class ScriptGeneratorImpl {
|
||||
protected final SimpleDateFormat dateFormat = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss''");
|
||||
@ManagedBean(SqlScriptGenerator.NAME)
|
||||
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
public class SqlScriptGenerator {
|
||||
public static final String NAME = "cuba_SqlScriptGenerator";
|
||||
|
||||
protected SimpleDateFormat dateFormat = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss''");
|
||||
protected String insertTemplate = "insert into %s \n(%s) \nvalues (%s);";
|
||||
protected String updateTemplate = "update %s \nset %s \nwhere id=%s;";
|
||||
protected Class clazz;
|
||||
@ -41,14 +46,21 @@ public class ScriptGeneratorImpl {
|
||||
protected List<String> fieldNames = new ArrayList<>();
|
||||
protected String tableName;
|
||||
|
||||
@Inject
|
||||
protected Metadata metadata;
|
||||
|
||||
public ScriptGeneratorImpl(Class<? extends Entity> clazz) {
|
||||
@Inject
|
||||
protected Persistence persistence;
|
||||
|
||||
public SqlScriptGenerator(Class<? extends Entity> clazz) {
|
||||
this.clazz = clazz;
|
||||
Table tableAnnotation = (Table) clazz.getAnnotation(Table.class);
|
||||
Preconditions.checkNotNull(tableAnnotation,
|
||||
format("Could not generate script for class [%s], because it's not linked with DB table using @Table annotation", clazz));
|
||||
}
|
||||
|
||||
this.tableName = tableAnnotation.name();
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
this.tableName = metadata.getTools().getDatabaseTable(metadata.getClass(clazz));
|
||||
Preconditions.checkNotNull(tableName,
|
||||
format("Could not generate script for class [%s], because it's not linked with DB table using @Table annotation", clazz));
|
||||
collectMetadata(clazz);
|
||||
}
|
||||
|
||||
@ -99,7 +111,7 @@ public class ScriptGeneratorImpl {
|
||||
value = ((EnumClass) value).getId();
|
||||
}
|
||||
|
||||
value = getDbTypeConverter().getSqlObject(value);
|
||||
value = persistence.getDbTypeConverter().getSqlObject(value);
|
||||
|
||||
if (value == null) {
|
||||
valueStr = null;
|
||||
@ -118,10 +130,6 @@ public class ScriptGeneratorImpl {
|
||||
}
|
||||
}
|
||||
|
||||
protected DbTypeConverter getDbTypeConverter() {
|
||||
return AppBeans.get(Persistence.class).getDbTypeConverter();
|
||||
}
|
||||
|
||||
protected String convertList(List<String> strings) {
|
||||
String string = strings.toString();
|
||||
return string.substring(1, string.length() - 1);
|
@ -5,11 +5,11 @@
|
||||
|
||||
package com.haulmont.cuba.core.app.script;
|
||||
|
||||
import com.haulmont.cuba.core.sys.persistence.DbTypeConverter;
|
||||
import com.haulmont.cuba.core.sys.persistence.PostgresTypeConverter;
|
||||
import com.haulmont.cuba.core.CubaTestCase;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import com.haulmont.cuba.security.entity.User;
|
||||
import com.haulmont.cuba.security.entity.UserSessionEntity;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
@ -18,20 +18,14 @@ import java.util.UUID;
|
||||
* @author degtyarjov
|
||||
* @version $Id$
|
||||
*/
|
||||
public class ScriptGeneratorTest {
|
||||
@Test
|
||||
public class SqlScriptGeneratorTest extends CubaTestCase {
|
||||
public void testInsert() throws Exception {
|
||||
ScriptGeneratorImpl scriptGenerator = new ScriptGeneratorImpl(User.class) {
|
||||
@Override
|
||||
protected DbTypeConverter getDbTypeConverter() {
|
||||
return new PostgresTypeConverter();
|
||||
}
|
||||
};
|
||||
SqlScriptGenerator sqlScriptGenerator = AppBeans.getPrototype(SqlScriptGenerator.NAME, User.class);
|
||||
User entity = new User();
|
||||
entity.setId(UUID.fromString("bda126a4-da26-bc72-087c-079d9e25ce5c"));
|
||||
entity.setCreateTs(new Date(1));
|
||||
|
||||
String script = scriptGenerator.generateInsertScript(entity);
|
||||
String script = sqlScriptGenerator.generateInsertScript(entity);
|
||||
System.out.println(script);
|
||||
|
||||
// Assert.assertEquals("insert into SEC_USER \n" +
|
||||
@ -40,23 +34,29 @@ public class ScriptGeneratorTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() throws Exception {
|
||||
ScriptGeneratorImpl scriptGenerator = new ScriptGeneratorImpl(User.class) {
|
||||
@Override
|
||||
protected DbTypeConverter getDbTypeConverter() {
|
||||
return new PostgresTypeConverter();
|
||||
}
|
||||
};
|
||||
SqlScriptGenerator sqlScriptGenerator = AppBeans.getPrototype(SqlScriptGenerator.NAME, User.class);
|
||||
User entity = new User();
|
||||
entity.setId(UUID.fromString("bda126a4-da26-bc72-087c-079d9e25ce5c"));
|
||||
entity.setCreateTs(new Date(1));
|
||||
|
||||
String script = scriptGenerator.generateUpdateScript(entity);
|
||||
String script = sqlScriptGenerator.generateUpdateScript(entity);
|
||||
System.out.println(script);
|
||||
|
||||
// Assert.assertEquals("update SEC_USER \n" +
|
||||
// "set CREATE_TS='1970-01-01 04:00:00', CREATED_BY=null, VERSION=null, UPDATE_TS=null, UPDATED_BY=null, DELETE_TS=null, DELETED_BY=null, LOGIN=null, LOGIN_LC=null, PASSWORD=null, NAME=null, FIRST_NAME=null, LAST_NAME=null, MIDDLE_NAME=null, POSITION_=null, EMAIL=null, LANGUAGE_=null, ACTIVE=true, CHANGE_PASSWORD_AT_LOGON=false, GROUP_ID=null, IP_MASK=null \n" +
|
||||
// "where id='bda126a4-da26-bc72-087c-079d9e25ce5c';", script);
|
||||
}
|
||||
|
||||
public void testFail() throws Exception {
|
||||
try {
|
||||
SqlScriptGenerator sqlScriptGenerator = AppBeans.getPrototype(SqlScriptGenerator.NAME, UserSessionEntity.class);
|
||||
UserSessionEntity userSessionEntity = new UserSessionEntity();
|
||||
String script = sqlScriptGenerator.generateInsertScript(userSessionEntity);
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.fail("The test should be finished by exception");
|
||||
}
|
||||
}
|
@ -98,7 +98,9 @@ public class PersistenceHelper {
|
||||
/**
|
||||
* @param entityClass entity class
|
||||
* @return table name as defined in {@link Table} annotation, or <code>null</code> if there is no such annotation
|
||||
* @deprecated please use com.haulmont.cuba.core.global.MetadataTools#getDatabaseTable instead
|
||||
*/
|
||||
@Deprecated
|
||||
@Nullable
|
||||
public static String getTableName(Class<?> entityClass) {
|
||||
Table annotation = entityClass.getAnnotation(Table.class);
|
||||
|
Loading…
Reference in New Issue
Block a user