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.FlexExceptions; 020import com.mybatisflex.core.query.QueryWrapper; 021import com.mybatisflex.core.row.Row; 022import com.mybatisflex.core.table.TableInfo; 023import com.mybatisflex.core.table.TableInfoFactory; 024import com.mybatisflex.core.util.StringUtil; 025import org.apache.ibatis.builder.annotation.ProviderContext; 026 027import java.util.Collection; 028import java.util.List; 029import java.util.Map; 030 031@SuppressWarnings({"rawtypes", "unchecked"}) 032class ProviderUtil { 033 034 private ProviderUtil() { 035 } 036 037 public static String getSqlString(Map params) { 038 return (String) params.get(FlexConsts.SQL); 039 } 040 041 public static void setSqlArgs(Map params, Object[] args) { 042 params.put(FlexConsts.SQL_ARGS, args); 043 } 044 045 public static String getSchemaName(Map params) { 046 Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME); 047 return schemaNameObj != null ? schemaNameObj.toString().trim() : null; 048 } 049 050 public static String getTableName(Map params) { 051 Object tableNameObj = params.get(FlexConsts.TABLE_NAME); 052 return tableNameObj != null ? tableNameObj.toString().trim() : null; 053 } 054 055 public static String[] getPrimaryKeys(Map params) { 056 String primaryKey = (String) params.get(FlexConsts.PRIMARY_KEY); 057 if (StringUtil.isBlank(primaryKey)) { 058 throw FlexExceptions.wrap("primaryKey can not be null or blank."); 059 } 060 String[] primaryKeys = primaryKey.split(","); 061 for (int i = 0; i < primaryKeys.length; i++) { 062 primaryKeys[i] = primaryKeys[i].trim(); 063 } 064 return primaryKeys; 065 } 066 067 public static Object[] getPrimaryValues(Map params) { 068 Object primaryValue = params.get(FlexConsts.PRIMARY_VALUE); 069 if (primaryValue == null) { 070 return FlexConsts.EMPTY_ARRAY; 071 } 072 if (primaryValue.getClass().isArray()) { 073 return (Object[]) primaryValue; 074 } else if (primaryValue instanceof Collection) { 075 return ((Collection<?>) primaryValue).toArray(); 076 } else { 077 return new Object[]{primaryValue}; 078 } 079 } 080 081 public static QueryWrapper getQueryWrapper(Map params) { 082 Object queryWrapper = params.get(FlexConsts.QUERY); 083 if (queryWrapper == null) { 084 throw new IllegalArgumentException("queryWrapper can not be null."); 085 } 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}