Search in sources :

Example 1 with UpdateAction

use of info.xiancloud.dao.core.action.update.UpdateAction 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)1 UpdateAction (info.xiancloud.dao.core.action.update.UpdateAction)1 Table (info.xiancloud.dao.core.model.ddl.Table)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1