use of info.xiancloud.dao.core.action.SqlAction in project xian by happyyangyuan.
the class DaoUnit method logSql.
/**
* 打印sql语句,它不会将sql执行,只是打印sql语句。
* 仅供内部测试使用
*
* @param daoUnitClass unit class
* @param map parameter map
*/
public static void logSql(Class daoUnitClass, Map<String, Object> map) {
XianConnection connection = PoolFactory.getPool().getMasterDatasource().getConnection().blockingGet();
DaoUnit daoUnit;
try {
daoUnit = (DaoUnit) daoUnitClass.newInstance();
for (SqlAction action : daoUnit.getActions()) {
((AbstractSqlAction) action).setConnection(connection);
((AbstractSqlAction) action).setMap(map);
action.logSql(map);
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
PoolFactory.getPool().destroyPoolIfNot();
}
use of info.xiancloud.dao.core.action.SqlAction in project xian by happyyangyuan.
the class DaoUnit method execute.
/**
* @param request the request object.
* @param handler the unit response consumer, this handler must be executed asynchronously.
*/
@Override
public final void execute(UnitRequest request, Handler<UnitResponse> handler) {
final SqlAction[] sqlActions = getActions();
for (SqlAction sqlAction : sqlActions) {
if (sqlAction instanceof ISingleTableAction) {
TableMetaCache.makeSureCache(((ISingleTableAction) sqlAction).getTableName());
}
}
bareError(request);
final boolean readOnly = readOnly(sqlActions, request) || request.getContext().isReadyOnly();
final AtomicBoolean transactional = new AtomicBoolean(false);
final UnitResponse[] tempUnitResponse = new UnitResponse[] { null };
final XianTransaction[] tempTransaction = new XianTransaction[] { null };
TransactionFactory.getTransaction(request.getContext().getMsgId(), readOnly).flatMap(transaction -> {
tempTransaction[0] = transaction;
transactional.set(isTransactional(sqlActions, transaction));
if (transactional.get()) {
// business layer has begun the transaction, here we reentrant it.
return transaction.begin().toSingle(() -> transaction);
} else {
// if transaction is not begun by business layer, here we do not begin the transaction
return Single.just(transaction);
}
}).flatMap(transaction -> Flowable.fromArray(sqlActions).concatMapSingle(action -> action.execute(this, request.getArgMap(), transaction.getConnection(), request.getContext().getMsgId())).reduce(UnitResponse.succeededSingleton(), (unitResponse, unitResponse2) -> {
if (unitResponse2.succeeded()) {
return unitResponse2;
} else {
throw new ExceptionWithUnitResponse(unitResponse2);
}
}).flatMapCompletable(unitResponse -> {
tempUnitResponse[0] = unitResponse;
if (transactional.get()) {
return transaction.commit();
} else {
return Completable.complete();
}
}).toSingle(() -> tempUnitResponse[0]).onErrorResumeNext(error -> {
ExceptionWithUnitResponse exceptionWithUnitResponse;
if (error instanceof ExceptionWithUnitResponse) {
exceptionWithUnitResponse = (ExceptionWithUnitResponse) error;
} else {
LOG.error(error);
exceptionWithUnitResponse = new ExceptionWithUnitResponse(UnitResponse.createException(error));
}
if (transactional.get()) {
// if transaction is begun then rollback here
return transaction.rollback().andThen(Single.just(exceptionWithUnitResponse.getUnitResponse()));
} else {
// if no transaction is begun, no need transaction rollback.
return Single.just(exceptionWithUnitResponse.getUnitResponse());
}
})).doFinally(() -> tempTransaction[0].close().subscribe()).subscribe(unitResponse -> {
unitResponse.getContext().setMsgId(request.getContext().getMsgId());
handler.handle(unitResponse);
}, error -> {
LOG.error(error);
UnitResponse errorResponse = UnitResponse.createException(error);
errorResponse.getContext().setMsgId(request.getContext().getMsgId());
handler.handle(errorResponse);
});
}
use of info.xiancloud.dao.core.action.SqlAction in project xian by happyyangyuan.
the class BaseDeleteDB method getActions.
@Override
public SqlAction[] getActions() {
return new SqlAction[] { new DeleteAction() {
@Override
public String tableName() {
Object tableName = getMap().get("$tableName");
if (tableName instanceof String) {
Table table = TableHeader.getTable(getMap().get("$tableName").toString());
if (table.getType().equals(Table.Type.view)) {
throw new RuntimeException(String.format("视图:%s,不允许操作 BaseDeleteDB", table.getName()));
}
return table.getName();
}
return "";
}
@Override
protected String[] searchConditions() {
List<String> whereList = new ArrayList<>();
Object tableName = getMap().get("$tableName");
if (tableName instanceof String) {
Map<String, Class<?>> columns = TableHeader.getTableColumnTypeMap(tableName.toString(), getSqlDriver());
for (Entry<String, Class<?>> entry : columns.entrySet()) {
String key = entry.getKey();
Object value = getMap().get(StringUtil.underlineToCamel(key));
if (value != null) {
String[] array = fmtObjectToStrArray(value);
if (array != null) {
whereList.add(String.format("%s in {%s}", key, StringUtil.underlineToCamel(key)));
} else {
whereList.add(String.format("%s = {%s}", key, StringUtil.underlineToCamel(key)));
}
}
}
}
// 这里不允许执行整表删除功能
if (whereList.size() > 0) {
return whereList.toArray(new String[] {});
}
return new String[] { "1 < 0" };
}
} };
}
use of info.xiancloud.dao.core.action.SqlAction in project xian by happyyangyuan.
the class BaseUpdateDB method getActions.
@Override
public SqlAction[] getActions() {
return new SqlAction[] { new UpdateAction() {
private Table table;
private Table getTable(Map map) {
if (table != null) {
return table;
}
Object tableName = map.get("$tableName");
if (tableName instanceof String) {
table = TableHeader.getTable(map.get("$tableName").toString());
if (table.getType().equals(Table.Type.view)) {
throw new RuntimeException(String.format("视图:%s,不允许操作 BaseUpdateDB", table.getName()));
}
return table;
}
return null;
}
@Override
public String tableName() {
Table table = getTable(getMap());
return table == null ? "" : table.getName();
}
@Override
public String[] unique() {
Table table = getTable(getMap());
return table == null ? new String[] {} : table.getUnique();
}
@Override
protected String[] searchConditions() {
List<String> whereList = new ArrayList<>();
// $tableName is table alias
Object tableName = getMap().get("$tableName");
if (tableName instanceof String) {
Map<String, Class<?>> columns = TableHeader.getTableColumnTypeMap(tableName.toString(), getSqlDriver());
for (Entry<String, Class<?>> entry : columns.entrySet()) {
String key = entry.getKey();
if (getMap().containsKey(StringUtil.underlineToCamel(key))) {
whereList.add(String.format("%s = {%s}", key, StringUtil.underlineToCamel(key)));
}
}
}
// 这里不允许执行整表修改功能
if (whereList.size() > 0) {
return whereList.toArray(new String[] {});
}
return new String[] { "1 < 0" };
}
} };
}
Aggregations