001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012package org.nuxeo.ecm.core.storage.sql.jdbc.db; 013 014import java.io.Serializable; 015import java.util.Collections; 016import java.util.LinkedList; 017import java.util.List; 018import java.util.Set; 019 020import org.apache.commons.lang.StringUtils; 021 022/** 023 * An {@code UPDATE} statement. 024 */ 025public class Update implements Serializable { 026 027 private static final long serialVersionUID = 1L; 028 029 protected final Table table; 030 031 protected String newValues; 032 033 protected String[] from; 034 035 protected String where; 036 037 public Update(Table table) { 038 this.table = table; 039 } 040 041 public Table getTable() { 042 return table; 043 } 044 045 public void setNewValues(String newValues) { 046 this.newValues = newValues; 047 } 048 049 /** Alternative to {@link #setNewValues} */ 050 public void setUpdatedColumns(List<Column> columns) { 051 setUpdatedColumns(columns, Collections.<String> emptySet()); 052 } 053 054 /** 055 * Alternative to {@link #setNewValues} 056 * 057 * @param columns the columns 058 * @param deltas which of the columns are delta updates 059 */ 060 public void setUpdatedColumns(List<Column> columns, Set<String> deltas) { 061 List<String> updatedColumns = new LinkedList<String>(); 062 for (Column column : columns) { 063 if (column.isIdentity()) { 064 // identity column is never inserted 065 continue; 066 } 067 String col = column.getQuotedName(); 068 String fvs = column.getFreeVariableSetter(); 069 String update; 070 if (deltas.contains(column.getKey())) { 071 update = col + " = " + col + " + " + fvs; 072 } else { 073 update = col + " = " + fvs; 074 } 075 updatedColumns.add(update); 076 } 077 newValues = StringUtils.join(updatedColumns, ", "); 078 } 079 080 /** 081 * Sets additional table names with which to join for this update. 082 */ 083 public void setFrom(String... from) { 084 this.from = from; 085 } 086 087 public void setWhere(String where) { 088 if (where == null || where.length() == 0) { 089 throw new IllegalArgumentException("unexpected empty WHERE"); 090 } 091 this.where = where; 092 } 093 094 public String getStatement() { 095 StringBuilder buf = new StringBuilder(128); 096 buf.append("UPDATE "); 097 buf.append(table.getQuotedName()); 098 buf.append(" SET "); 099 buf.append(newValues); 100 if (from != null) { 101 buf.append(" FROM "); 102 if (table.getDialect().doesUpdateFromRepeatSelf()) { 103 buf.append(table.getQuotedName()); 104 buf.append(", "); 105 } 106 buf.append(StringUtils.join(from, ", ")); 107 } 108 if (where != null) { 109 buf.append(" WHERE "); 110 buf.append(where); 111 } else { 112 throw new IllegalArgumentException("unexpected empty WHERE"); 113 } 114 return buf.toString(); 115 } 116}