001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.provider;
017
018import com.mybatisflex.core.FlexConsts;
019import com.mybatisflex.core.exception.FlexAssert;
020import com.mybatisflex.core.exception.FlexExceptions;
021import com.mybatisflex.core.exception.locale.LocalizedFormats;
022import com.mybatisflex.core.query.QueryWrapper;
023import com.mybatisflex.core.row.Row;
024import com.mybatisflex.core.table.TableInfo;
025import com.mybatisflex.core.table.TableInfoFactory;
026import com.mybatisflex.core.util.StringUtil;
027import org.apache.ibatis.builder.annotation.ProviderContext;
028
029import java.util.Collection;
030import java.util.List;
031import java.util.Map;
032
033@SuppressWarnings({"rawtypes", "unchecked"})
034class ProviderUtil {
035
036    private ProviderUtil() {
037    }
038
039    public static String getSqlString(Map params) {
040        return (String) params.get(FlexConsts.SQL);
041    }
042
043    public static void flatten(Map params) {
044        Object[] o = (Object[]) params.get(FlexConsts.SQL_ARGS);
045        Object map;
046        if (o != null && o.length == 1 && (map = o[0]) instanceof Map) {
047            params.putAll((Map) map);
048            params.put(FlexConsts.RAW_ARGS, Boolean.TRUE);
049        }
050    }
051
052    public static void setSqlArgs(Map params, Object[] args) {
053        params.put(FlexConsts.SQL_ARGS, args);
054    }
055
056    public static String getSchemaName(Map params) {
057        Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME);
058        return schemaNameObj != null ? schemaNameObj.toString().trim() : null;
059    }
060
061    public static String getTableName(Map params) {
062        Object tableNameObj = params.get(FlexConsts.TABLE_NAME);
063        return tableNameObj != null ? tableNameObj.toString().trim() : null;
064    }
065
066    public static String[] getPrimaryKeys(Map params) {
067        String primaryKey = (String) params.get(FlexConsts.PRIMARY_KEY);
068        if (StringUtil.isBlank(primaryKey)) {
069            throw FlexExceptions.wrap(LocalizedFormats.OBJECT_NULL_OR_BLANK, "primaryKey");
070        }
071        String[] primaryKeys = primaryKey.split(",");
072        for (int i = 0; i < primaryKeys.length; i++) {
073            primaryKeys[i] = primaryKeys[i].trim();
074        }
075        return primaryKeys;
076    }
077
078    public static Object[] getPrimaryValues(Map params) {
079        Object primaryValue = params.get(FlexConsts.PRIMARY_VALUE);
080        if (primaryValue == null) {
081            return FlexConsts.EMPTY_ARRAY;
082        }
083        if (primaryValue.getClass().isArray()) {
084            return (Object[]) primaryValue;
085        } else if (primaryValue instanceof Collection) {
086            return ((Collection<?>) primaryValue).toArray();
087        } else {
088            return new Object[]{primaryValue};
089        }
090    }
091
092    public static QueryWrapper getQueryWrapper(Map params) {
093        Object queryWrapper = params.get(FlexConsts.QUERY);
094        FlexAssert.notNull(queryWrapper, "queryWrapper");
095        return (QueryWrapper) queryWrapper;
096    }
097
098    public static Row getRow(Map params) {
099        return (Row) params.get(FlexConsts.ROW);
100    }
101
102    public static List<Row> getRows(Map params) {
103        return (List<Row>) params.get(FlexConsts.ROWS);
104    }
105
106    public static TableInfo getTableInfo(ProviderContext context) {
107        return TableInfoFactory.ofMapperClass(context.getMapperType());
108    }
109
110    public static Object getEntity(Map params) {
111        return params.get(FlexConsts.ENTITY);
112    }
113
114    public static String getFieldName(Map params) {
115        return (String) params.get(FlexConsts.FIELD_NAME);
116    }
117
118    public static Object getValue(Map params) {
119        return params.get(FlexConsts.VALUE);
120    }
121
122    public static List<Object> getEntities(Map params) {
123        return (List<Object>) params.get(FlexConsts.ENTITIES);
124    }
125
126    public static boolean isIgnoreNulls(Map params) {
127        return params.containsKey(FlexConsts.IGNORE_NULLS) && (boolean) params.get(FlexConsts.IGNORE_NULLS);
128    }
129
130
131}