Search in sources :

Example 1 with Table

use of info.xiancloud.dao.core.model.ddl.Table 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 2 with Table

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