Search in sources :

Example 1 with SqlAction

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();
}
Also used : AbstractSqlAction(info.xiancloud.dao.core.action.AbstractSqlAction) XianConnection(info.xiancloud.dao.core.connection.XianConnection) AbstractSqlAction(info.xiancloud.dao.core.action.AbstractSqlAction) SqlAction(info.xiancloud.dao.core.action.SqlAction)

Example 2 with SqlAction

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);
    });
}
Also used : XianConnection(info.xiancloud.dao.core.connection.XianConnection) TableMetaCache(info.xiancloud.dao.core.utils.TableMetaCache) TransactionFactory(info.xiancloud.dao.core.transaction.TransactionFactory) Unit(info.xiancloud.core.Unit) UnitRequest(info.xiancloud.core.message.UnitRequest) StringUtil(info.xiancloud.core.util.StringUtil) AbstractSqlAction(info.xiancloud.dao.core.action.AbstractSqlAction) Completable(io.reactivex.Completable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnitResponse(info.xiancloud.core.message.UnitResponse) SqlAction(info.xiancloud.dao.core.action.SqlAction) Handler(info.xiancloud.core.Handler) Single(io.reactivex.Single) ISingleTableAction(info.xiancloud.dao.core.action.ISingleTableAction) Input(info.xiancloud.core.Input) ISelect(info.xiancloud.dao.core.action.select.ISelect) Flowable(io.reactivex.Flowable) PoolFactory(info.xiancloud.dao.core.pool.PoolFactory) Map(java.util.Map) LOG(info.xiancloud.core.util.LOG) XianTransaction(info.xiancloud.dao.core.transaction.XianTransaction) ExceptionWithUnitResponse(info.xiancloud.core.message.ExceptionWithUnitResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnitResponse(info.xiancloud.core.message.UnitResponse) ExceptionWithUnitResponse(info.xiancloud.core.message.ExceptionWithUnitResponse) ISingleTableAction(info.xiancloud.dao.core.action.ISingleTableAction) XianTransaction(info.xiancloud.dao.core.transaction.XianTransaction) ExceptionWithUnitResponse(info.xiancloud.core.message.ExceptionWithUnitResponse) AbstractSqlAction(info.xiancloud.dao.core.action.AbstractSqlAction) SqlAction(info.xiancloud.dao.core.action.SqlAction)

Example 3 with SqlAction

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" };
        }
    } };
}
Also used : Table(info.xiancloud.dao.core.model.ddl.Table) ArrayList(java.util.ArrayList) DeleteAction(info.xiancloud.dao.core.action.delete.DeleteAction) SqlAction(info.xiancloud.dao.core.action.SqlAction)

Example 4 with SqlAction

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" };
        }
    } };
}
Also used : Table(info.xiancloud.dao.core.model.ddl.Table) UpdateAction(info.xiancloud.dao.core.action.update.UpdateAction) ArrayList(java.util.ArrayList) Map(java.util.Map) SqlAction(info.xiancloud.dao.core.action.SqlAction)

Aggregations

SqlAction (info.xiancloud.dao.core.action.SqlAction)4 AbstractSqlAction (info.xiancloud.dao.core.action.AbstractSqlAction)2 XianConnection (info.xiancloud.dao.core.connection.XianConnection)2 Table (info.xiancloud.dao.core.model.ddl.Table)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Handler (info.xiancloud.core.Handler)1 Input (info.xiancloud.core.Input)1 Unit (info.xiancloud.core.Unit)1 ExceptionWithUnitResponse (info.xiancloud.core.message.ExceptionWithUnitResponse)1 UnitRequest (info.xiancloud.core.message.UnitRequest)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 LOG (info.xiancloud.core.util.LOG)1 StringUtil (info.xiancloud.core.util.StringUtil)1 ISingleTableAction (info.xiancloud.dao.core.action.ISingleTableAction)1 DeleteAction (info.xiancloud.dao.core.action.delete.DeleteAction)1 ISelect (info.xiancloud.dao.core.action.select.ISelect)1 UpdateAction (info.xiancloud.dao.core.action.update.UpdateAction)1 PoolFactory (info.xiancloud.dao.core.pool.PoolFactory)1 TransactionFactory (info.xiancloud.dao.core.transaction.TransactionFactory)1