use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class DalClientFactory method getClient.
public static DalClient getClient(String logicDbName) {
if (logicDbName == null)
throw new NullPointerException("Database Set name can not be null");
DalClient dalClient = null;
DalConfigure config = getDalConfigure();
// Verify if it is existed
config.getDatabaseSet(logicDbName);
String className = config.getFactory().getProperty(CUSTOM_DAL_CLIENT_CLASS);
if (StringUtils.isEmpty(className)) {
className = DalPropertiesManager.getInstance().getDalPropertiesLocator().getCustomerClientClassName();
}
if (StringUtils.isEmpty(className)) {
dalClient = new DalDirectClient(config, logicDbName);
} else {
try {
dalClient = (DalClient) Class.forName(className).newInstance();
((DalDirectClient) dalClient).init(config, logicDbName);
} catch (Throwable t) {
throw new DalRuntimeException(CREATE_CUSTOMER_CLIENT_ERROR, t);
}
}
return dalClient;
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class DalClassScanner method getClasses.
@Override
public List<Class<?>> getClasses(String packageName, boolean recursive) {
long startTime = System.currentTimeMillis();
// fileCount.set(0);
// jarCount.set(0);
List<Class<?>> classList = new Vector<>();
if (null == packageName || packageName.trim().isEmpty()) {
throw new IllegalArgumentException("packageName should not be null or empty");
}
packageName = packageName.trim();
String packagePath = packageName.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
try {
Enumeration<URL> urls = classLoader.getResources(packagePath);
List<URL> urlList = new ArrayList<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url != null) {
urlList.add(url);
}
}
findClasses(classList, packageName, urlList, recursive);
} catch (IOException e) {
throw new DalRuntimeException(e);
}
long endTime = System.currentTimeMillis();
LOGGER.info("=========================================================================");
LOGGER.info(String.format("Time cost: %d ms", endTime - startTime));
LOGGER.info(String.format("%d classes found", classList.size()));
// LOGGER.info(String.format("%d files scanned", fileCount.get()));
// LOGGER.info(String.format("%d jars scanned", jarCount.get()));
LOGGER.info("=========================================================================");
return classList;
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class AbstractCombinedInsertTask 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();
Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
List<String> finalInsertableColumns = buildValidColumnsForInsert(unqualifiedColumns);
String insertColumns = combineColumns(finalInsertableColumns, COLUMN_SEPARATOR);
List<Map<String, Object>> identityFields = new ArrayList<>();
int startIndex = 1;
for (Integer index : daoPojos.keySet()) {
Map<String, ?> pojo = daoPojos.get(index);
removeUnqualifiedColumns(pojo, unqualifiedColumns);
Map<String, Object> identityField = getIdentityField(pojo);
if (identityField != null) {
identityFields.add(identityField);
}
int paramCount = addParameters(startIndex, parameters, pojo, finalInsertableColumns);
startIndex += paramCount;
values.append(String.format("(%s),", combine("?", paramCount, ",")));
}
// Put identityFields and pojos count into context
if (taskContext instanceof DefaultTaskContext) {
((DefaultTaskContext) taskContext).setIdentityFields(identityFields);
((DefaultTaskContext) taskContext).setPojosCount(daoPojos.size());
}
String tableName = getRawTableName(hints);
if (taskContext instanceof DalContextConfigure) {
((DalContextConfigure) taskContext).addTables(tableName);
((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
}
String sql = String.format(getSqlTpl(), quote(tableName), insertColumns, 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 AbstractBatchInsertTask method execute.
@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
int i = 0;
Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
for (Integer index : daoPojos.keySet()) {
Map<String, ?> pojo = daoPojos.get(index);
removeUnqualifiedColumns(pojo, unqualifiedColumns);
StatementParameters parameters = new StatementParameters();
addParameters(parameters, pojo);
parametersList[i++] = parameters;
}
String tableName = getRawTableName(hints);
if (taskContext instanceof DalContextConfigure) {
((DalContextConfigure) taskContext).addTables(tableName);
((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
}
String batchInsertSql = buildBatchInsertSql(hints, unqualifiedColumns, tableName);
int[] result;
if (client instanceof DalContextClient)
result = ((DalContextClient) client).batchUpdate(batchInsertSql, parametersList, hints, taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
return result;
}
Aggregations