001/**
002 * Copyright 2015 Tampere University of Technology, Pori Department
003 * 
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 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
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 core.tut.pori.dao.clause;
017
018import org.apache.commons.lang3.StringUtils;
019
020/**
021 * This class defines a single SQL update clause.
022 * 
023 * Passing null equals to creating an update clause with SQL NULL.
024 *
025 */
026public class UpdateClause implements SQLClause{
027  private String _column = null;
028  private Object[] _value = null;
029  private SQLType[] _type = null;
030  
031  /**
032   * 
033   * @param column
034   * @param value a single object or null, passing an array will create undefined behavior
035   * @param type
036   */
037  public UpdateClause(String column, Object value, SQLType type){
038    _column = column;
039    _value = new Object[]{value};
040    _type = new SQLType[]{type};
041  }
042  
043  /**
044   * 
045   * @param column
046   * @param value a single object or null, passing an array will have undefined behavior
047   * @param type the correct type of the value parameter
048   * @return new update clause or null if the given value was null, empty or blank
049   */
050  public static UpdateClause getIfNotBlankValue(String column, Object value, SQLType type){
051    if(value == null){
052      return null;
053    }else if(type == SQLType.STRING && StringUtils.isBlank((CharSequence) value)){
054      return null;
055    }else{
056      return new UpdateClause(column, value, type);
057    }
058  }
059  
060  @Override
061  public void toSQLString(StringBuilder sql){
062    sql.append(_column);
063    sql.append("=?");
064  }
065
066  @Override
067  public SQLType[] getValueTypes() {
068    return _type;
069  }
070
071  @Override
072  public Object[] getValues() {
073    return _value;
074  }
075}