All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.venky.swf.sql.Update Maven / Gradle / Ivy

The newest version!
package com.venky.swf.sql;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import com.venky.swf.db.model.Model;
import com.venky.swf.db.model.reflection.ModelReflector;
import com.venky.swf.db.table.BindVariable;

public class Update extends DataManupulationStatement{
	private String table = null;
	private Map values = new HashMap();
	private Map unBoundedValues = new HashMap();
	private SortedSet keys = new TreeSet();
	
	public  Update(ModelReflector ref){
		this(ref.getPool(),ref.getTableName());
	}
	
	public Update(String pool,String table){
		super(pool);
		this.table = table;
	}
	public Update setUnBounded(String name,String value){
		assert !values.containsKey(name);
		unBoundedValues.put(name, value);
		keys.add(name);
		return this;
	}
	public Update set(String name,BindVariable value){
		assert !unBoundedValues.containsKey(name);
		values.put(name, value);
		keys.add(name);
		return this;
	}
	public Update set(Map values){
		for (String key : values.keySet()){
			set(key,values.get(key));
		}
		return this;
	}


	@Override
	protected void finalizeParameterizedSQL() {
		StringBuilder builder = getQuery();
		builder.append("UPDATE ").append(table);
		builder.append(" SET ");
		Iterator ki = keys.iterator();
		while (ki.hasNext()){
			String key = ki.next();
			if (unBoundedValues.containsKey(key)){
				builder.append(key).append(" = ").append(unBoundedValues.get(key));
			}else {
				builder.append(key).append(" = ? " );
				getValues().add(values.get(key));
			}
			if (ki.hasNext()){
				builder.append(",");
			}
		}
		Expression where = getWhereExpression();
		if (where != null){
			builder.append(" WHERE ");
			builder.append(where.getParameterizedSQL());
			getValues().addAll(where.getValues());
		}
	}

	private Expression whereExpression ;
	public Update where(Expression expression){
		this.whereExpression = expression;
		return this;
	}
	
	public Expression getWhereExpression(){
		return whereExpression;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy