/*
 * Copyright (C) 2004-2006  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope;

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

import com.exedio.dsmf.Driver;

final class MigrationStep // TODO make public when migration has matured
{
	final int version;
	final String comment;
	final Body body;
	
	MigrationStep(final int version, final String comment, final Body body) // TODO make public when migration has matured
	{
		this.version = version;
		this.comment = comment;
		this.body = body;
		
		if(version<0)
			throw new IllegalArgumentException("version must not be negative");
		if(comment==null)
			throw new NullPointerException("comment must not be null");
		if(body==null)
			throw new NullPointerException("body must not be null");
	}
	
	static abstract class Body // TODO make public when migration has matured
	{
		private final Object lock = new Object();
		private Driver driver;
		private ArrayList<String> result;
		
		final List<String> getPatch(final Driver driver)
		{
			assert driver!=null;
			
			synchronized(lock)
			{
				assert this.driver==null;
				assert this.result==null;
				
				try
				{
					this.driver = driver;
					this.result = new ArrayList<String>();
					execute();
					return Collections.unmodifiableList(result);
				}
				finally
				{
					this.driver = null;
					this.result = null;
				}
			}
		}
		
		public final String protectName(final String name)
		{
			return driver.protectName(name);
		}
		
		public final void sql(final String sql)
		{
			if(sql==null)
				throw new NullPointerException("sql must not be null");
			
			result.add(sql);
		}
		
		public final void renameColumn(final String tableName, final String oldColumnName, final String newColumnName, final String columnType)
		{
			sql(driver.renameColumn(protectName(tableName), protectName(oldColumnName), protectName(newColumnName), columnType));
		}
		
		public final void createColumn(final String tableName, final String columnName, final String columnType)
		{
			sql(driver.createColumn(protectName(tableName), protectName(columnName), columnType));
		}
		
		public final void modifyColumn(final String tableName, final String columnName, final String newColumnType)
		{
			sql(driver.modifyColumn(protectName(tableName), protectName(columnName), newColumnType));
		}

		public final void dropPrimaryKeyConstraint(final String tableName, final String constraintName)
		{
			sql(driver.dropPrimaryKeyConstraint(protectName(tableName), protectName(constraintName)));
		}
		
		public final void dropForeignKeyConstraint(final String tableName, final String constraintName)
		{
			sql(driver.dropForeignKeyConstraint(protectName(tableName), protectName(constraintName)));
		}
		
		public final void dropUniqueConstraint(final String tableName, final String constraintName)
		{
			sql(driver.dropUniqueConstraint(protectName(tableName), protectName(constraintName)));
		}
		
		public abstract void execute();
	}
	
	@Override
	public String toString()
	{
		return "MS" + version + ':' + comment;
	}
}

@@ -2158,20 +2158,16 @@
  protect
-Name
 (final S
@@ -2174,24 +2174,24 @@
 tring name)%0A
+
 %09%09%7B%0A%09%09%09retur
@@ -2547,36 +2547,32 @@
 meColumn(protect
-Name
 (tableName), pro
@@ -2575,20 +2575,16 @@
  protect
-Name
 (oldColu
@@ -2599,20 +2599,16 @@
  protect
-Name
 (newColu
@@ -2774,36 +2774,32 @@
 teColumn(protect
-Name
 (tableName), pro
@@ -2794,36 +2794,32 @@
 leName), protect
-Name
 (columnName), co
@@ -2977,36 +2977,32 @@
 fyColumn(protect
-Name
 (tableName), pro
@@ -3005,20 +3005,16 @@
  protect
-Name
 (columnN
@@ -3181,36 +3181,32 @@
 nstraint(protect
-Name
 (tableName), pro
@@ -3201,36 +3201,32 @@
 leName), protect
-Name
 (constraintName)
@@ -3376,36 +3376,32 @@
 nstraint(protect
-Name
 (tableName), pro
@@ -3396,36 +3396,32 @@
 leName), protect
-Name
 (constraintName)
@@ -3571,20 +3571,16 @@
 (protect
-Name
 (tableNa
@@ -3591,20 +3591,16 @@
  protect
-Name
 (constra

rename protectName to protect