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 031class ProviderUtil { 032 033 private static final Object[] NULL_ARGS = new Object[0]; 034 035 public static String getSqlString(Map params) { 036 return (String) params.get(FlexConsts.SQL); 037 } 038 039 public static void setSqlArgs(Map params, Object[] args) { 040 params.put(FlexConsts.SQL_ARGS, args); 041 } 042 043 public static String getTableName(Map params) { 044 Object tableNameObj = params.get(FlexConsts.TABLE_NAME); 045 return tableNameObj != null ? tableNameObj.toString().trim() : null; 046 } 047 048 public static String[] getPrimaryKeys(Map params) { 049 String primaryKey = (String) params.get(FlexConsts.PRIMARY_KEY); 050 if (StringUtil.isBlank(primaryKey)) { 051 throw FlexExceptions.wrap("primaryKey can not be null or blank."); 052 } 053 String[] primaryKeys = primaryKey.split(","); 054 for (int i = 0; i < primaryKeys.length; i++) { 055 primaryKeys[i] = primaryKeys[i].trim(); 056 } 057 return primaryKeys; 058 } 059 060 public static Object[] getPrimaryValues(Map params) { 061 Object primaryValue = params.get(FlexConsts.PRIMARY_VALUE); 062 if (primaryValue == null) { 063 return NULL_ARGS; 064 } 065 if (primaryValue.getClass().isArray()) { 066 return (Object[]) primaryValue; 067 } else if (primaryValue instanceof Collection) { 068 return ((Collection<?>) primaryValue).toArray(); 069 } else { 070 return new Object[]{primaryValue}; 071 } 072 } 073 074 public static QueryWrapper getQueryWrapper(Map params) { 075 return (QueryWrapper) params.get(FlexConsts.QUERY); 076 } 077 078 public static Row getRow(Map params) { 079 return (Row) params.get(FlexConsts.ROW); 080 } 081 082 public static List<Row> getRows(Map params) { 083 return (List<Row>) params.get(FlexConsts.ROWS); 084 } 085 086 public static TableInfo getTableInfo(ProviderContext context) { 087 return TableInfoFactory.ofMapperClass(context.getMapperType()); 088 } 089 090 public static Object getEntity(Map params) { 091 return params.get(FlexConsts.ENTITY); 092 } 093 094 095 public static List<Object> getEntities(Map params) { 096 return (List<Object>) params.get(FlexConsts.ENTITIES); 097 } 098 099 public static boolean isIgnoreNulls(Map params) { 100 return params.containsKey(FlexConsts.IGNORE_NULLS) && (boolean) params.get(FlexConsts.IGNORE_NULLS); 101 } 102 103 104}