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.update; 017 018import com.mybatisflex.core.query.QueryColumn; 019import com.mybatisflex.core.query.QueryCondition; 020import com.mybatisflex.core.query.QueryWrapper; 021import com.mybatisflex.core.util.LambdaGetter; 022import com.mybatisflex.core.util.LambdaUtil; 023import com.mybatisflex.core.util.UpdateEntity; 024import org.apache.ibatis.javassist.util.proxy.ProxyObject; 025 026import java.io.Serializable; 027import java.util.Map; 028 029public interface UpdateWrapper extends Serializable { 030 031 default Map<String, Object> getUpdates() { 032 ModifyAttrsRecordHandler handler = (ModifyAttrsRecordHandler) ((ProxyObject) this).getHandler(); 033 return handler.getUpdates(); 034 } 035 036 037 default UpdateWrapper set(String property, Object value) { 038 if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { 039 setRaw(property, value); 040 } else { 041 getUpdates().put(property, value); 042 } 043 return this; 044 } 045 046 047 default <T> UpdateWrapper set(LambdaGetter<T> getter, Object value) { 048 if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { 049 setRaw(getter, value); 050 } else { 051 getUpdates().put(LambdaUtil.getFieldName(getter), value); 052 } 053 054 return this; 055 } 056 057 058 default <T> UpdateWrapper set(QueryColumn queryColumn, Object value) { 059 if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { 060 setRaw(queryColumn, value); 061 } else { 062 getUpdates().put(queryColumn.getName(), value); 063 } 064 return this; 065 } 066 067 default UpdateWrapper setRaw(String property, Object value) { 068 getUpdates().put(property, new RawValue(value)); 069 return this; 070 } 071 072 073 default <T> UpdateWrapper setRaw(LambdaGetter<T> getter, Object value) { 074 getUpdates().put(LambdaUtil.getFieldName(getter), new RawValue(value)); 075 return this; 076 } 077 078 default <T> UpdateWrapper setRaw(QueryColumn queryColumn, Object value) { 079 getUpdates().put(queryColumn.getName(), new RawValue(value)); 080 return this; 081 } 082 083 084 static UpdateWrapper of(Object entity) { 085 if (entity instanceof UpdateWrapper) { 086 return (UpdateWrapper) entity; 087 } else { 088 return (UpdateWrapper) UpdateEntity.ofNotNull(entity); 089 } 090 } 091 092}