package org.seamframework.sql;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Create<X> implements Statement {
	
	private final Table<X> table;
	private final List<Column<X, ?>> columns = new ArrayList<Column<X,?>>();
	private final List<String> definitions = new ArrayList<String>();
	
	public Create(Table<X> table) {
		this.table = table;
	}
	
	public void add(Column<X, ?> column, String definition) {
		columns.add(column);
		definitions.add(definition);
	}
	
	@Override @SuppressWarnings("unchecked")
	public List<Parameter<?>> getParameters() {
		return Collections.EMPTY_LIST;
	}

	@Override
	public String toSql() {
		StringBuilder builder = new StringBuilder();
		builder.append("create table ");
		builder.append( table.getQualifiedName() );
		builder.append(" (");
		for (int i=0; i<columns.size(); i++) {
			builder.append( columns.get(i).getName() )
				.append(' ')
				.append( definitions.get(i) );
			if ( i<columns.size()-1 ) builder.append(", ");
		}
		builder.append(')');
		return builder.toString();
	}
	
}
