online.sanen.unabo.sql.pipe.BatchOperationPileline Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unabo Show documentation
Show all versions of unabo Show documentation
Unabo, the Java™ progressive ORM framework
The newest version!
package online.sanen.unabo.sql.pipe;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.mhdt.toolkit.Reflect;
import online.sanen.unabo.api.Handel;
import online.sanen.unabo.api.exception.QueryException;
import online.sanen.unabo.api.structure.ChannelContext;
import online.sanen.unabo.api.structure.enums.ProductType;
import online.sanen.unabo.api.structure.enums.QueryType;
import online.sanen.unabo.template.SqlTemplate;
import online.sanen.unabo.template.jpa.JPA;
import online.sanen.unabo.template.jpa.JPA.PrimaryKey;
/**
*
* @author LazyToShow
* Date: 2017/11/29
* Time: 11:51
*/
public class BatchOperationPileline implements SimplePileline, Handel {
/**
* The original return value of batch operations is an int array, and now it is
* omitted to return 1 or -1 to represent whether an exception has occurred
*/
@Override
public Object handel(ChannelContext context, Object product) {
try {
if ((context.getEntities() == null || context.getEntities().isEmpty()) && (context.getEntityMaps() == null || context.getEntityMaps().isEmpty()))
return new NullPointerException("Batch operation data source is null");
QueryType type = context.getQueryType();
SqlTemplate template = (SqlTemplate) context.getTemplate();
// Batch delete does not exist, this is to unify the interface to do the
// adaptation,
// delete the way to use in function, separate processing
if (type.equals(QueryType.delete)) {
PrimaryKey primaryKey = context.getPrimaryKey();
return batchRemove(template, context.getEntities(), context.getSql(), primaryKey);
}
// if update or insert should add where condition by primary key
if (type.equals(QueryType.update)) {
try {
appendPrimaryCondition(context.getSql(), context.getPrimaryKey());
} catch (NullPointerException e) {
e.printStackTrace();
}
}
batchUpdate(template, context);
return 1;
} catch (Exception e) {
throw new QueryException(e);
}
}
private void batchUpdate(SqlTemplate template, ChannelContext context) throws SQLException,IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
if (context.getEntityMaps() != null&& (context.productType() == ProductType.SQLITE || context.productType() == ProductType.MYSQL)&& context.getQueryType().equals(QueryType.insert)) {
storedProcedure(context, template);
} else {
template.batchUpdate(context.getSql().toString(), commonBatch(context, template));
}
}
private LinkedList
© 2015 - 2024 Weber Informatics LLC | Privacy Policy