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 setSqlArgs(Map params, Object[] args) { 044 params.put(FlexConsts.SQL_ARGS, args); 045 } 046 047 public static String getSchemaName(Map params) { 048 Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME); 049 return schemaNameObj != null ? schemaNameObj.toString().trim() : null; 050 } 051 052 public static String getTableName(Map params) { 053 Object tableNameObj = params.get(FlexConsts.TABLE_NAME); 054 return tableNameObj != null ? tableNameObj.toString().trim() : null; 055 } 056 057 public static String[] getPrimaryKeys(Map params) { 058 String primaryKey = (String) params.get(FlexConsts.PRIMARY_KEY); 059 if (StringUtil.isBlank(primaryKey)) { 060 throw FlexExceptions.wrap(LocalizedFormats.OBJECT_NULL_OR_BLANK, "primaryKey"); 061 } 062 String[] primaryKeys = primaryKey.split(","); 063 for (int i = 0; i < primaryKeys.length; i++) { 064 primaryKeys[i] = primaryKeys[i].trim(); 065 } 066 return primaryKeys; 067 } 068 069 public static Object[] getPrimaryValues(Map params) { 070 Object primaryValue = params.get(FlexConsts.PRIMARY_VALUE); 071 if (primaryValue == null) { 072 return FlexConsts.EMPTY_ARRAY; 073 } 074 if (primaryValue.getClass().isArray()) { 075 return (Object[]) primaryValue; 076 } else if (primaryValue instanceof Collection) { 077 return ((Collection<?>) primaryValue).toArray(); 078 } else { 079 return new Object[]{primaryValue}; 080 } 081 } 082 083 public static QueryWrapper getQueryWrapper(Map params) { 084 Object queryWrapper = params.get(FlexConsts.QUERY); 085 FlexAssert.notNull(queryWrapper,"queryWrapper"); 086 return (QueryWrapper) queryWrapper; 087 } 088 089 public static Row getRow(Map params) { 090 return (Row) params.get(FlexConsts.ROW); 091 } 092 093 public static List<Row> getRows(Map params) { 094 return (List<Row>) params.get(FlexConsts.ROWS); 095 } 096 097 public static TableInfo getTableInfo(ProviderContext context) { 098 return TableInfoFactory.ofMapperClass(context.getMapperType()); 099 } 100 101 public static Object getEntity(Map params) { 102 return params.get(FlexConsts.ENTITY); 103 } 104 105 public static String getFieldName(Map params) { 106 return (String) params.get(FlexConsts.FIELD_NAME); 107 } 108 109 public static Object getValue(Map params) { 110 return params.get(FlexConsts.VALUE); 111 } 112 113 public static List<Object> getEntities(Map params) { 114 return (List<Object>) params.get(FlexConsts.ENTITIES); 115 } 116 117 public static boolean isIgnoreNulls(Map params) { 118 return params.containsKey(FlexConsts.IGNORE_NULLS) && (boolean) params.get(FlexConsts.IGNORE_NULLS); 119 } 120 121 122}