use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class AbstractSingleInsertTask method execute.
@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
List<Map<String, ?>> pojoList = new ArrayList<Map<String, ?>>();
List<T> rawPojos = new ArrayList<>();
pojoList.add(fields);
rawPojos.add(rawPojo);
Set<String> unqualifiedColumns = filterUnqualifiedColumns(hints, pojoList, rawPojos);
removeUnqualifiedColumns(fields, unqualifiedColumns);
// Put identityFields into context
List<Map<String, Object>> identityFields = new ArrayList<>();
Map<String, Object> identityField = getIdentityField(fields);
if (identityField != null) {
identityFields.add(identityField);
}
if (taskContext instanceof DefaultTaskContext) {
((DefaultTaskContext) taskContext).setIdentityFields(identityFields);
((DefaultTaskContext) taskContext).setPojosCount(1);
}
String tableName = getRawTableName(hints, fields);
if (taskContext instanceof DalContextConfigure)
((DalContextConfigure) taskContext).addTables(tableName);
/*
* In case fields is empty, the final sql will be like "insert into tableName () values()".
* We do not report error or simply return 0, but just let DB decide what to do.
* For MS Sql server, sql like this is illegal, but for mysql, this works however
*/
String insertSql = buildInsertSql(hints, fields, tableName);
StatementParameters parameters = new StatementParameters();
addParameters(parameters, fields);
if (client instanceof DalContextClient)
return ((DalContextClient) client).update(insertSql, parameters, hints, taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class BatchUpdateTask method execute.
@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
List<T> rawPojos = taskContext.getRawPojos();
boolean isUpdatableEntity = taskContext.isUpdatableEntity();
Map<String, Boolean> pojoFieldStatus = taskContext.getPojoFieldStatus();
StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
int i = 0;
String[] updateColumnNames = pojoFieldStatus.keySet().toArray(new String[pojoFieldStatus.size()]);
for (Integer index : daoPojos.keySet()) {
Map<String, ?> pojo = daoPojos.get(index);
StatementParameters parameters = new StatementParameters();
if (isUpdatableEntity && !hints.isUpdateUnchangedField())
addParameters(parameters, pojo, updateColumnNames, ((UpdatableEntity) rawPojos.get(index)).getUpdatedColumns());
else
addParameters(parameters, pojo, updateColumnNames);
addParameters(parameters, pojo, parser.getPrimaryKeyNames());
addVersion(parameters, pojo);
parametersList[i++] = parameters;
}
String tableName = getRawTableName(hints);
if (taskContext instanceof DalContextConfigure) {
((DalContextConfigure) taskContext).addTables(tableName);
((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
}
String batchUpdateSql = buildBatchUpdateSql(quote(tableName), pojoFieldStatus);
int[] result;
if (client instanceof DalContextClient)
result = ((DalContextClient) client).batchUpdate(batchUpdateSql, parametersList, hints, taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
return result;
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class CombinedDeleteTask method execute.
@Override
public Integer execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
StatementParameters parameters = new StatementParameters();
StringBuilder values = new StringBuilder();
List<String> pkColumns = clonePkColumns();
int startIndex = 1;
for (Integer index : daoPojos.keySet()) {
Map<String, ?> daoPojo = daoPojos.get(index);
int paramCount = addParameters(startIndex, parameters, daoPojo, pkColumns);
startIndex += paramCount;
values.append(String.format(IN_TEMPLATE, combine("?", paramCount, ",")));
}
String tableName = getRawTableName(hints);
if (taskContext instanceof DalContextConfigure) {
((DalContextConfigure) taskContext).addTables(tableName);
((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
}
String sql = String.format(TMPL_COMBINED_SQL_DELETE, quote(tableName), combineColumns(pkColumns, COLUMN_SEPARATOR), values.substring(0, values.length() - 2) + ")");
if (client instanceof DalContextClient)
return ((DalContextClient) client).update(sql, parameters, hints, taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class BatchDeleteTask method execute.
@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
List<String> pkNames = Arrays.asList(parser.getPrimaryKeyNames());
int i = 0;
for (Integer index : daoPojos.keySet()) {
StatementParameters parameters = new StatementParameters();
addParameters(1, parameters, daoPojos.get(index), pkNames);
parametersList[i++] = parameters;
}
String tableName = getRawTableName(hints);
if (taskContext instanceof DalContextConfigure)
((DalContextConfigure) taskContext).addTables(tableName);
String deleteSql = buildDeleteSql(quote(tableName));
int[] result;
if (client instanceof DalContextClient)
result = ((DalContextClient) client).batchUpdate(deleteSql, parametersList, hints, taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
return result;
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class IdGeneratorFactoryManager method createFactory.
private IIdGeneratorFactory createFactory(String className) {
IIdGeneratorFactory factory = null;
Class clazz = null;
try {
clazz = Class.forName(className);
} catch (Throwable t) {
throw new DalRuntimeException("Failed to load class: " + className, t);
}
try {
factory = (IIdGeneratorFactory) clazz.newInstance();
} catch (Throwable t1) {
try {
factory = (IIdGeneratorFactory) clazz.getMethod("getInstance").invoke(null);
} catch (Throwable t2) {
String msg = String.format("Failed to create factory: %s. Cause 1: %s; cause 2: %s", className, t1.getMessage(), t2.getMessage());
throw new DalRuntimeException(msg);
}
}
if (null == factory) {
throw new DalRuntimeException(String.format("The created factory '%s' is null", className));
}
return factory;
}
Aggregations