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